prompts.test.js 3.44 KB
/**
 * Unit Tests — core/prompts.js
 */
import { describe, it, expect } from 'vitest';

const { Prompts } = require('../../src/core/prompts');

describe('Prompts', () => {
  describe('initial', () => {
    it('should include the requirement text', () => {
      const result = Prompts.initial('Build a website', 'coder, verifier');
      expect(result).toContain('Build a website');
      expect(result).toContain('coder, verifier');
    });

    it('should include JSON dispatch format instructions', () => {
      const result = Prompts.initial('Task', 'agents');
      expect(result).toContain('agent');
      expect(result).toContain('task');
      expect(result).toContain('[CALL:');
    });

    it('should warn against direct code writing', () => {
      const result = Prompts.initial('Task', 'agents');
      expect(result).toContain('禁止直接编写代码');
    });
  });

  describe('followup', () => {
    it('should include execution results', () => {
      const result = Prompts.followup({
        currentResults: [
          { agent: 'coder', status: 'DONE', output: 'Code written' },
          { agent: 'verifier', status: 'PASS', message: 'Verified' },
        ],
        passedValidators: ['verifier'],
        unpassedValidators: [],
      });
      expect(result).toContain('coder');
      expect(result).toContain('Code written');
      expect(result).toContain('Reflection');
      expect(result).toContain('Summary');
    });

    it('should include artifact section when present', () => {
      const result = Prompts.followup({
        currentResults: [
          { agent: 'coder', status: 'DONE', output: 'ok', artifacts: ['main.py', 'utils.py'] },
        ],
        passedValidators: [],
        unpassedValidators: ['verifier'],
      });
      expect(result).toContain('产出文件');
      expect(result).toContain('main.py');
    });

    it('should handle results without output gracefully', () => {
      const result = Prompts.followup({
        currentResults: [{ agent: 'coder', status: 'ERROR' }],
        passedValidators: [],
        unpassedValidators: [],
      });
      expect(result).toContain('coder');
    });
  });

  describe('forceValidation', () => {
    it('should list files and validators', () => {
      const result = Prompts.forceValidation(['main.py', 'test.py'], ['code_verifier']);
      expect(result).toContain('main.py');
      expect(result).toContain('code_verifier');
    });
  });

  describe('forceResume', () => {
    it('should list unpassed validators', () => {
      const result = Prompts.forceResume(['syntax_checker', 'logic_verifier']);
      expect(result).toContain('syntax_checker');
      expect(result).toContain('logic_verifier');
      expect(result).toContain('尚未完成');
    });
  });

  describe('formatCorrection', () => {
    it('should include retry count for validator', () => {
      const result = Prompts.formatCorrection({
        agentType: 'validator', retryCount: 2, maxRetries: 5,
        originalTask: 'Check syntax',
      });
      expect(result).toContain('2/5');
      expect(result).toContain('PASS');
      expect(result).toContain('FAIL');
      expect(result).toContain('Check syntax');
    });

    it('should include DONE/ERROR for executor', () => {
      const result = Prompts.formatCorrection({
        agentType: 'executor', retryCount: 1, maxRetries: 3,
        originalTask: 'Write code',
      });
      expect(result).toContain('DONE');
      expect(result).toContain('ERROR');
    });
  });
});