// tests/validators/next-session-prompt-validator.test.mjs
// Unit + CLI integration tests for lib/validators/next-session-prompt-validator.mjs.
// Covers Bug 3 contract: producer-mismatch detection + state-anchored staleness +
// 24h soft-warning + missing-frontmatter downgrade.

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { execFileSync } from 'node:child_process';

import {
  validateNextSessionPromptContent,
  validateNextSessionPromptObject,
  validateNextSessionPromptConsistency,
} from '../../lib/validators/next-session-prompt-validator.mjs';

function frontmatter(producedBy, producedAt, extra = '') {
  return `---\nproduced_by: ${producedBy}\nproduced_at: ${producedAt}\n${extra}---\n\n# A1 — example\n\nbody\n`;
}

test('validateNextSessionPromptContent — both consistent producers (valid)', () => {
  const text = frontmatter('ultraexecute-local', '2026-05-04T16:00:00.000Z');
  const r = validateNextSessionPromptContent(text);
  assert.equal(r.valid, true, JSON.stringify(r.errors));
  assert.equal(r.parsed.produced_by, 'ultraexecute-local');
});

test('validateNextSessionPromptObject — missing produced_by is invalid', () => {
  const r = validateNextSessionPromptObject({ produced_at: '2026-05-04T16:00:00Z' });
  assert.equal(r.valid, false);
  assert.ok(r.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_MISSING_FIELD' && /produced_by/.test(e.message)));
});

test('validateNextSessionPromptObject — missing produced_at is invalid', () => {
  const r = validateNextSessionPromptObject({ produced_by: 'ultraexecute-local' });
  assert.equal(r.valid, false);
  assert.ok(r.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_MISSING_FIELD' && /produced_at/.test(e.message)));
});

test('validateNextSessionPromptObject — invalid produced_at timestamp rejected', () => {
  const r = validateNextSessionPromptObject({ produced_by: 'x', produced_at: 'not-a-date' });
  assert.equal(r.valid, false);
  assert.ok(r.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_INVALID_TIMESTAMP'));
});

test('validateNextSessionPromptContent — no frontmatter downgrades to warning (valid)', () => {
  const r = validateNextSessionPromptContent('# Plain markdown, no frontmatter\n\ntext\n');
  assert.equal(r.valid, true);
  assert.ok(r.warnings.find(w => w.code === 'NEXT_SESSION_PROMPT_NO_FRONTMATTER'));
});

test('validateNextSessionPromptConsistency — producer mismatch with both fresh fails', () => {
  const a = { path: '/a', parsed: { produced_by: 'ultraexecute-local', produced_at: '2026-05-04T16:00:00.000Z' } };
  const b = { path: '/b', parsed: { produced_by: 'graceful-handoff', produced_at: '2026-05-04T16:05:00.000Z' } };
  const state = { updated_at: '2026-05-04T15:00:00.000Z' };
  const r = validateNextSessionPromptConsistency(a, b, { state, now: Date.parse('2026-05-04T16:30:00.000Z') });
  assert.equal(r.valid, false);
  assert.ok(r.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_PRODUCER_MISMATCH'));
});

test('validateNextSessionPromptConsistency — state-anchored stale candidate ignored', () => {
  const a = { path: '/a', parsed: { produced_by: 'graceful-handoff', produced_at: '2026-05-03T10:00:00.000Z' } };
  const b = { path: '/b', parsed: { produced_by: 'ultraexecute-local', produced_at: '2026-05-04T16:05:00.000Z' } };
  const state = { updated_at: '2026-05-04T16:00:00.000Z' };
  const r = validateNextSessionPromptConsistency(a, b, { state, now: Date.parse('2026-05-04T16:30:00.000Z') });
  assert.equal(r.valid, true, JSON.stringify(r.errors));
  assert.ok(r.warnings.find(w => w.code === 'NEXT_SESSION_PROMPT_STALE_IGNORED'));
});

test('validateNextSessionPromptConsistency — 24h wall-clock drift emits soft warning', () => {
  const a = { path: '/a', parsed: { produced_by: 'ultraexecute-local', produced_at: '2026-05-01T16:00:00.000Z' } };
  const b = { path: '/b', parsed: { produced_by: 'ultraexecute-local', produced_at: '2026-05-01T16:00:00.000Z' } };
  const r = validateNextSessionPromptConsistency(a, b, { now: Date.parse('2026-05-04T16:30:00.000Z') });
  assert.equal(r.valid, true);
  assert.ok(r.warnings.find(w => w.code === 'NEXT_SESSION_PROMPT_WALL_CLOCK_DRIFT'));
});

test('validateNextSessionPromptConsistency — same producer, both fresh, no errors', () => {
  const a = { path: '/a', parsed: { produced_by: 'ultraexecute-local', produced_at: '2026-05-04T16:00:00.000Z' } };
  const b = { path: '/b', parsed: { produced_by: 'ultraexecute-local', produced_at: '2026-05-04T16:01:00.000Z' } };
  const r = validateNextSessionPromptConsistency(a, b, { now: Date.parse('2026-05-04T16:30:00.000Z') });
  assert.equal(r.valid, true);
  assert.deepEqual(r.errors, []);
  // No 24h warning: produced_at is well within 24h of `now`.
  assert.deepEqual(r.warnings.filter(w => w.code === 'NEXT_SESSION_PROMPT_WALL_CLOCK_DRIFT'), []);
});

test('CLI shim — single-file mode returns JSON for valid file', () => {
  const dir = mkdtempSync(join(tmpdir(), 'nspv-cli-'));
  try {
    const file = join(dir, 'NEXT-SESSION-PROMPT.local.md');
    writeFileSync(file, frontmatter('ultraexecute-local', '2026-05-04T16:00:00.000Z'));
    const out = execFileSync(process.execPath, [
      'lib/validators/next-session-prompt-validator.mjs',
      '--json',
      file,
    ], { encoding: 'utf-8' });
    const parsed = JSON.parse(out);
    assert.equal(parsed.valid, true);
  } finally {
    rmSync(dir, { recursive: true, force: true });
  }
});

test('CLI shim — consistency mode flags producer mismatch', () => {
  const dir = mkdtempSync(join(tmpdir(), 'nspv-cli-'));
  try {
    const a = join(dir, 'a.md');
    const b = join(dir, 'b.md');
    writeFileSync(a, frontmatter('ultraexecute-local', '2026-05-04T16:00:00.000Z'));
    writeFileSync(b, frontmatter('graceful-handoff', '2026-05-04T16:01:00.000Z'));
    let exitCode = 0;
    let out = '';
    try {
      out = execFileSync(process.execPath, [
        'lib/validators/next-session-prompt-validator.mjs',
        '--json',
        '--consistency',
        a,
        b,
      ], { encoding: 'utf-8' });
    } catch (e) {
      exitCode = e.status;
      out = e.stdout ? e.stdout.toString() : '';
    }
    assert.notEqual(exitCode, 0);
    const parsed = JSON.parse(out);
    assert.equal(parsed.valid, false);
    assert.ok(parsed.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_PRODUCER_MISMATCH'));
  } finally {
    rmSync(dir, { recursive: true, force: true });
  }
});
