import { describe, expect, it } from 'vitest';

import {
  createAgentSignalSelfIterationPrompt,
  createAgentSignalSelfIterationSystemRole,
  createAgentSignalSelfIterationToolSystemRole,
} from './runtime';

describe('agent signal self-iteration runtime prompt', () => {
  /**
   * @example
   * Review mode renders the stable nightly review XML envelope.
   */
  it('renders review mode prompt in stable XML sections', () => {
    expect(
      createAgentSignalSelfIterationPrompt({
        agentId: 'agent-1',
        context: {
          selfReviewSignals: [
            {
              evidenceRefs: [{ id: 'doc-1', type: 'agent_document' }],
              kind: 'skill_document_with_tool_failure',
            },
          ],
          topics: [{ id: 'topic-1', summary: 'Skill update failed after a tool call.' }],
        },
        mode: 'review',
        sourceId: 'source-1',
        userId: 'user-1',
        window: {
          end: '2026-05-09T16:00:00.000Z',
          start: '2026-05-08T16:00:00.000Z',
        },
      }),
    ).toMatchSnapshot();
  });

  /**
   * @example
   * Reflection mode keeps bounded context visible without nightly review policy XML.
   */
  it('renders reflection mode prompt with explicit mode and context JSON', () => {
    expect(
      createAgentSignalSelfIterationPrompt({
        agentId: 'agent-1',
        context: { selfFeedbackCandidates: [{ messageId: 'm-1' }] },
        mode: 'reflection',
        sourceId: 'source-2',
        userId: 'user-1',
        window: {
          end: '2026-05-09T16:00:00.000Z',
          start: '2026-05-09T15:00:00.000Z',
        },
      }),
    ).toMatchSnapshot();
  });

  /**
   * @example
   * System roles and tool roles stay generated by the prompt package.
   */
  it('renders system and tool role text', () => {
    expect(createAgentSignalSelfIterationSystemRole()).toContain(
      'You are the Agent Signal self-iteration agent.',
    );
    expect(createAgentSignalSelfIterationToolSystemRole('review')).toContain(
      'Use resource read tools before writes',
    );
    expect(createAgentSignalSelfIterationToolSystemRole('reflection')).toContain(
      'recordSelfFeedbackIntent',
    );
  });
});
