import { describe, it, expect } from 'vitest';
import { MISSING_SIGNAL_INSTRUCTION, addInstructionToPrompt } from './instructions.js';

describe('instructions', () => {
  describe('MISSING_SIGNAL_INSTRUCTION', () => {
    it('should contain key guidance about signal.json creation', () => {
      expect(MISSING_SIGNAL_INSTRUCTION).toContain('signal.json');
      expect(MISSING_SIGNAL_INSTRUCTION).toContain('.cw/output/signal.json');
      expect(MISSING_SIGNAL_INSTRUCTION).toContain('"status": "done"');
      expect(MISSING_SIGNAL_INSTRUCTION).toContain('"status": "questions"');
      expect(MISSING_SIGNAL_INSTRUCTION).toContain('"status": "error"');
    });

    it('should be a clear instruction for missing signal recovery', () => {
      expect(MISSING_SIGNAL_INSTRUCTION).toContain('IMPORTANT');
      expect(MISSING_SIGNAL_INSTRUCTION).toContain('previous execution completed');
      expect(MISSING_SIGNAL_INSTRUCTION).toContain('did not generate');
    });
  });

  describe('addInstructionToPrompt', () => {
    it('should add instruction to the beginning of the prompt', () => {
      const originalPrompt = 'Please help me with this task';
      const instruction = 'First, create a file called test.txt';

      const result = addInstructionToPrompt(originalPrompt, instruction);

      expect(result).toBe(`First, create a file called test.txt\n\nPlease help me with this task`);
    });

    it('should trim the instruction', () => {
      const originalPrompt = 'Please help me';
      const instruction = '   Important: Do this first   ';

      const result = addInstructionToPrompt(originalPrompt, instruction);

      expect(result).toBe(`Important: Do this first\n\nPlease help me`);
    });

    it('should handle empty original prompt', () => {
      const originalPrompt = '';
      const instruction = 'Create a signal.json file';

      const result = addInstructionToPrompt(originalPrompt, instruction);

      expect(result).toBe(`Create a signal.json file\n\n`);
    });

    it('should handle missing signal instruction with real prompt', () => {
      const originalPrompt = 'Fix the bug in the authentication system';

      const result = addInstructionToPrompt(originalPrompt, MISSING_SIGNAL_INSTRUCTION);

      expect(result).toContain('IMPORTANT: Your previous execution completed');
      expect(result).toContain('Fix the bug in the authentication system');
      expect(result.indexOf('IMPORTANT')).toBeLessThan(result.indexOf('Fix the bug'));
    });
  });
});