/**
 * Tests for Refinement Prompt Generation
 *
 * Story 2.3: Refinement Loop (Regeneration)
 */

import { describe, it, expect } from 'vitest';
import { generateRefinementPrompt } from './prompt-engine';
import type { ChatMessage } from '../db';

describe('PromptEngine - Refinement Prompt Generation', () => {
  const mockChatHistory: ChatMessage[] = [
    {
      id: 1,
      role: 'user',
      content: 'I struggled with understanding async/await in JavaScript',
      timestamp: Date.now() - 10000,
    },
    {
      id: 2,
      role: 'assistant',
      content: 'That\'s a common challenge. Can you tell me more about what specifically confused you?',
      timestamp: Date.now() - 8000,
    },
    {
      id: 3,
      role: 'user',
      content: 'The order of execution and when promises resolve',
      timestamp: Date.now() - 5000,
    },
  ];

  const originalDraft = `# Understanding Async/Await

Learning async/await was challenging because I struggled with understanding when promises resolve and the order of execution.

**Tags:** [JavaScript, async-await, learning]`;

  describe('generateRefinementPrompt', () => {
    it('should include original draft in prompt', () => {
      const prompt = generateRefinementPrompt(
        originalDraft,
        'Make it shorter',
        mockChatHistory
      );

      expect(prompt).toContain('ORIGINAL DRAFT:');
      expect(prompt).toContain(originalDraft);
    });

    it('should include user feedback in prompt', () => {
      const feedback = 'Make it shorter';
      const prompt = generateRefinementPrompt(
        originalDraft,
        feedback,
        mockChatHistory
      );

      expect(prompt).toContain('USER FEEDBACK:');
      expect(prompt).toContain(`"${feedback}"`);
    });

    it('should include chat history for context', () => {
      const prompt = generateRefinementPrompt(
        originalDraft,
        'Make it shorter',
        mockChatHistory
      );

      expect(prompt).toContain('Original Chat History:');
      expect(prompt).toContain(mockChatHistory[0].content);
    });

    it('should include requirements for maintaining voice and not hallucinating', () => {
      const prompt = generateRefinementPrompt(
        originalDraft,
        'Make it shorter',
        mockChatHistory
      );

      expect(prompt).toContain('maintaining their authentic voice');
      expect(prompt).toContain('Do NOT introduce new ideas or hallucinate facts');
      expect(prompt).toContain('Keep what worked in the original');
    });

    it('should handle vague feedback gracefully', () => {
      const prompt = generateRefinementPrompt(
        originalDraft,
        'make it better',
        mockChatHistory
      );

      expect(prompt).toContain('If feedback is vague, make a reasonable best guess');
    });

    it('should specify markdown output format', () => {
      const prompt = generateRefinementPrompt(
        originalDraft,
        'Make it shorter',
        mockChatHistory
      );

      expect(prompt).toContain('OUTPUT FORMAT:');
      expect(prompt).toContain('```markdown');
    });

    it('should include intent when provided', () => {
      const prompt = generateRefinementPrompt(
        originalDraft,
        'Make it shorter',
        mockChatHistory,
        'venting'
      );

      expect(prompt).toContain('User Intent: venting');
    });

    it('should handle unknown intent', () => {
      const prompt = generateRefinementPrompt(
        originalDraft,
        'Make it shorter',
        mockChatHistory,
        undefined
      );

      expect(prompt).toContain('User Intent: unknown');
    });
  });
});
