import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { mkdtempSync, writeFileSync, readFileSync, existsSync, rmSync, mkdirSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { inferVerdictFromCodexOutput } from '../reviewers/codex.js';
const CODEX_FOOTER = '\n\n---\n_Reviewed with [OpenAI Codex](https://openai.com/codex)_';
describe('inferVerdictFromCodexOutput', () => {
    it('returns BLOCK when P0 is present alone', () => {
        const text = `Release-blocking issue.\n\n- [P0] Data loss bug — src/db.ts:5\n  Fix this.${CODEX_FOOTER}`;
        expect(inferVerdictFromCodexOutput(text)).toBe('BLOCK');
    });
    it('returns BLOCK when P0 present alongside lower levels', () => {
        const text = `Issues found.\n\n- [P0] Critical\n- [P2] Minor${CODEX_FOOTER}`;
        expect(inferVerdictFromCodexOutput(text)).toBe('BLOCK');
    });
    it('returns BLOCK when P1 is present', () => {
        const text = `Critical issue found.\n\n- [P1] Broken auth — src/auth.ts:12\n  Fix this.${CODEX_FOOTER}`;
        expect(inferVerdictFromCodexOutput(text)).toBe('BLOCK');
    });
    it('returns BLOCK when P1 present alongside P2/P3', () => {
        const text = `Issues found.\n\n- [P1] Security issue\n- [P2] Minor issue\n- [P3] Nit${CODEX_FOOTER}`;
        expect(inferVerdictFromCodexOutput(text)).toBe('BLOCK');
    });
    it('returns NEEDS WORK when only P2 is present', () => {
        const text = `Issues found.\n\n- [P2] Missing validation — src/api.ts:45\n  Add this.${CODEX_FOOTER}`;
        expect(inferVerdictFromCodexOutput(text)).toBe('NEEDS WORK');
    });
    it('returns NEEDS WORK when only P3 is present', () => {
        const text = `Minor issue.\n\n- [P3] Rename variable — src/util.ts:10${CODEX_FOOTER}`;
        expect(inferVerdictFromCodexOutput(text)).toBe('NEEDS WORK');
    });
    it('returns NEEDS WORK when P2 and P3 present, no P1', () => {
        const text = `Issues.\n\n- [P2] Fix bug\n- [P3] Nit${CODEX_FOOTER}`;
        expect(inferVerdictFromCodexOutput(text)).toBe('NEEDS WORK');
    });
    it('returns APPROVE when no priority markers present', () => {
        const text = `The changes look correct and complete.${CODEX_FOOTER}`;
        expect(inferVerdictFromCodexOutput(text)).toBe('APPROVE');
    });
    it('does not double-append when VERDICT already present', () => {
        // The caller guards against re-appending — verify inference is case-insensitive
        expect(inferVerdictFromCodexOutput('[p1] issue')).toBe('BLOCK');
        expect(inferVerdictFromCodexOutput('[P2] issue')).toBe('NEEDS WORK');
    });
    it('is case-insensitive for [p0]/[p1]/[p2]/[p3]', () => {
        expect(inferVerdictFromCodexOutput('[p1] issue')).toBe('BLOCK');
        expect(inferVerdictFromCodexOutput('[P2] issue')).toBe('NEEDS WORK');
    });
    it('infers correctly from real Codex output shape (motivation-form PR #90)', () => {
        const realOutput = `The added guidance contains copy-paste survey templates that default to form mode.

Full review comments:

- [P2] Keep survey examples in survey mode — /tmp/repo/agent-guide.mdx:431-433
  Add \`type: survey\` near the top of each template.

- [P3] Move detached media-url row back into table — /tmp/repo/agent-guide.mdx:686-686
  This row renders as a stray paragraph.

---
_Reviewed with [OpenAI Codex](https://openai.com/codex)_`;
        expect(inferVerdictFromCodexOutput(realOutput)).toBe('NEEDS WORK');
    });
});
describe('.codex/instructions cleanup after review', () => {
    let repoDir;
    beforeEach(() => {
        repoDir = mkdtempSync(join(tmpdir(), 'crosscheck-test-'));
        mkdirSync(join(repoDir, '.codex'), { recursive: true });
    });
    afterEach(() => {
        rmSync(repoDir, { force: true, recursive: true });
    });
    it('deletes .codex/instructions when it did not exist before the review', () => {
        const instructionsPath = join(repoDir, '.codex', 'instructions');
        // Simulate what runCodexReview does: write the file
        writeFileSync(instructionsPath, 'crosscheck review instructions');
        // Simulate cleanup (originalInstructions was undefined)
        rmSync(instructionsPath, { force: true });
        expect(existsSync(instructionsPath)).toBe(false);
    });
    it('restores original .codex/instructions content after review', () => {
        const instructionsPath = join(repoDir, '.codex', 'instructions');
        const original = 'user-defined codex instructions';
        writeFileSync(instructionsPath, original);
        // Simulate: crosscheck overwrites, then restores
        writeFileSync(instructionsPath, 'crosscheck review instructions');
        writeFileSync(instructionsPath, original);
        expect(readFileSync(instructionsPath, 'utf8')).toBe(original);
    });
});
//# sourceMappingURL=codex.test.js.map