classroom-agent-mode.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { describe, test, expect } from 'vitest';
  2. /**
  3. * Unit test for #353 fix: verify Stage object has correct agent fields
  4. * based on agentMode.
  5. *
  6. * This doesn't call any LLM — it directly tests the conditional logic
  7. * that was changed in classroom-generation.ts.
  8. */
  9. import { getDefaultAgents } from '@/lib/orchestration/registry/store';
  10. import { AGENT_COLOR_PALETTE, AGENT_DEFAULT_AVATARS } from '@/lib/constants/agent-defaults';
  11. interface DefaultModeFields {
  12. agentIds: string[];
  13. }
  14. interface GenerateModeFields {
  15. generatedAgentConfigs: Array<{
  16. id: string;
  17. name: string;
  18. role: string;
  19. persona: string;
  20. avatar: string;
  21. color: string;
  22. priority: number;
  23. }>;
  24. }
  25. describe('#353: generatedAgentConfigs conditional on agentMode', () => {
  26. // Replicate the Stage construction logic from classroom-generation.ts L322-349
  27. function buildStageAgentFields(
  28. agentMode: 'default' | 'generate',
  29. agents: Array<{ id: string; name: string; role: string; persona?: string }>,
  30. ): DefaultModeFields | GenerateModeFields {
  31. return agentMode === 'generate'
  32. ? {
  33. generatedAgentConfigs: agents.map((a, i) => ({
  34. id: a.id,
  35. name: a.name,
  36. role: a.role,
  37. persona: a.persona || '',
  38. avatar: AGENT_DEFAULT_AVATARS[i % AGENT_DEFAULT_AVATARS.length],
  39. color: AGENT_COLOR_PALETTE[i % AGENT_COLOR_PALETTE.length],
  40. priority: a.role === 'teacher' ? 10 : a.role === 'assistant' ? 7 : 5,
  41. })),
  42. }
  43. : {
  44. agentIds: agents.map((a) => a.id),
  45. };
  46. }
  47. test('default mode should set agentIds, NOT generatedAgentConfigs', () => {
  48. const agents = getDefaultAgents();
  49. const fields = buildStageAgentFields('default', agents);
  50. // Should have agentIds
  51. expect(fields).toHaveProperty('agentIds');
  52. expect((fields as DefaultModeFields).agentIds).toEqual([
  53. 'default-1',
  54. 'default-2',
  55. 'default-3',
  56. 'default-4',
  57. 'default-5',
  58. 'default-6',
  59. ]);
  60. // Should NOT have generatedAgentConfigs
  61. expect(fields).not.toHaveProperty('generatedAgentConfigs');
  62. });
  63. test('generate mode should set generatedAgentConfigs, NOT agentIds', () => {
  64. const agents = [
  65. { id: 'gen-server-0', name: 'Prof. Li', role: 'teacher', persona: 'An expert' },
  66. { id: 'gen-server-1', name: 'Assistant', role: 'assistant', persona: 'Helpful' },
  67. { id: 'gen-server-2', name: 'Student', role: 'student', persona: 'Curious' },
  68. ];
  69. const fields = buildStageAgentFields('generate', agents);
  70. // Should have generatedAgentConfigs
  71. expect(fields).toHaveProperty('generatedAgentConfigs');
  72. expect((fields as GenerateModeFields).generatedAgentConfigs).toHaveLength(3);
  73. expect((fields as GenerateModeFields).generatedAgentConfigs[0].id).toBe('gen-server-0');
  74. // Should NOT have agentIds
  75. expect(fields).not.toHaveProperty('agentIds');
  76. });
  77. test('generate mode with LLM fallback should behave like default mode', () => {
  78. // Simulates: agentMode was 'generate', LLM failed, fell back to defaults
  79. // After our fix, agentMode is reset to 'default' in the catch block
  80. let agentMode: 'default' | 'generate' = 'generate';
  81. let agents;
  82. try {
  83. throw new Error('Simulated LLM failure');
  84. } catch {
  85. agents = getDefaultAgents();
  86. agentMode = 'default'; // ← This is our fix
  87. }
  88. const fields = buildStageAgentFields(agentMode, agents);
  89. // Should behave exactly like default mode
  90. expect(fields).toHaveProperty('agentIds');
  91. expect(fields).not.toHaveProperty('generatedAgentConfigs');
  92. expect((fields as DefaultModeFields).agentIds).toEqual([
  93. 'default-1',
  94. 'default-2',
  95. 'default-3',
  96. 'default-4',
  97. 'default-5',
  98. 'default-6',
  99. ]);
  100. });
  101. });