// tests/lib/cleanup.test.mjs
// Unit tests for lib/util/cleanup.mjs (Bug 4).

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, existsSync, unlinkSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { cleanupProject } from '../../lib/util/cleanup.mjs';

function buildProject(dir, status) {
  mkdirSync(dir, { recursive: true });
  const stateObj = {
    schema_version: 1,
    project: dir,
    next_session_brief_path: join(dir, 'brief.md'),
    next_session_label: 'Cleanup test fixture',
    status,
    updated_at: '2026-05-04T16:00:00.000Z',
  };
  writeFileSync(join(dir, '.session-state.local.json'), JSON.stringify(stateObj, null, 2));
  writeFileSync(join(dir, 'NEXT-SESSION-PROMPT.local.md'),
    `---\nproduced_by: ultraexecute-local\nproduced_at: 2026-05-04T16:00:00.000Z\n---\n\n# Done\n`);
  return dir;
}

test('cleanupProject — dry-run on completed project lists candidates without deleting', () => {
  const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
  try {
    const dir = buildProject(join(root, 'project-a'), 'completed');
    const r = cleanupProject(dir, { dryRun: true });
    assert.equal(r.valid, true, JSON.stringify(r.errors));
    assert.equal(r.parsed.wouldDelete.length, 2);
    assert.deepEqual(r.parsed.deleted, []);
    // Files MUST still exist.
    assert.equal(existsSync(join(dir, '.session-state.local.json')), true);
    assert.equal(existsSync(join(dir, 'NEXT-SESSION-PROMPT.local.md')), true);
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test('cleanupProject — confirm-mode deletes both candidate files', () => {
  const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
  try {
    const dir = buildProject(join(root, 'project-b'), 'completed');
    const r = cleanupProject(dir, { dryRun: false, confirm: true });
    assert.equal(r.valid, true, JSON.stringify(r.errors));
    assert.equal(r.parsed.deleted.length, 2);
    assert.equal(existsSync(join(dir, '.session-state.local.json')), false);
    assert.equal(existsSync(join(dir, 'NEXT-SESSION-PROMPT.local.md')), false);
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test('cleanupProject — idempotent re-run after partial cleanup succeeds with deleted: []', () => {
  const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
  try {
    const dir = buildProject(join(root, 'project-c'), 'completed');
    // First confirm-mode deletes the prompt file BUT we still have the state file.
    // Manually remove the prompt file FIRST so the state file (still completed) is
    // the only candidate left after first cleanup.
    unlinkSync(join(dir, 'NEXT-SESSION-PROMPT.local.md'));
    const first = cleanupProject(dir, { dryRun: false, confirm: true });
    assert.equal(first.valid, true);
    assert.equal(first.parsed.deleted.length, 1, 'first cleanup deletes only the state file (prompt was pre-removed)');
    // Second invocation must fail — no state file → CLEANUP_NO_STATE_FILE.
    // This is the documented "fully cleaned" terminal state and is NOT an error
    // for the operator (they can ignore CLEANUP_NO_STATE_FILE), but the function
    // signals it deterministically.
    const second = cleanupProject(dir, { dryRun: false, confirm: true });
    assert.equal(second.valid, false);
    assert.ok(second.errors.find(e => e.code === 'CLEANUP_NO_STATE_FILE'));
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test('cleanupProject — refuses on status: in_progress (CLEANUP_NOT_COMPLETED)', () => {
  const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
  try {
    const dir = buildProject(join(root, 'project-d'), 'in_progress');
    const r = cleanupProject(dir, { dryRun: false, confirm: true });
    assert.equal(r.valid, false);
    assert.ok(r.errors.find(e => e.code === 'CLEANUP_NOT_COMPLETED'));
    // Files MUST still exist (refusal must not partially clean).
    assert.equal(existsSync(join(dir, '.session-state.local.json')), true);
    assert.equal(existsSync(join(dir, 'NEXT-SESSION-PROMPT.local.md')), true);
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test('cleanupProject — refuses dryRun: false without confirm: true (CLEANUP_REQUIRES_CONFIRM)', () => {
  const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
  try {
    const dir = buildProject(join(root, 'project-e'), 'completed');
    const r = cleanupProject(dir, { dryRun: false }); // no confirm
    assert.equal(r.valid, false);
    assert.ok(r.errors.find(e => e.code === 'CLEANUP_REQUIRES_CONFIRM'));
    assert.equal(existsSync(join(dir, '.session-state.local.json')), true);
    assert.equal(existsSync(join(dir, 'NEXT-SESSION-PROMPT.local.md')), true);
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test('cleanupProject — defaults to dry-run when opts is omitted', () => {
  const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
  try {
    const dir = buildProject(join(root, 'project-f'), 'completed');
    const r = cleanupProject(dir);
    assert.equal(r.valid, true);
    assert.deepEqual(r.parsed.deleted, []);
    assert.equal(r.parsed.wouldDelete.length, 2);
    assert.equal(existsSync(join(dir, '.session-state.local.json')), true);
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test('cleanupProject — missing state file returns CLEANUP_NO_STATE_FILE', () => {
  const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
  try {
    const dir = join(root, 'project-empty');
    mkdirSync(dir, { recursive: true });
    const r = cleanupProject(dir);
    assert.equal(r.valid, false);
    assert.ok(r.errors.find(e => e.code === 'CLEANUP_NO_STATE_FILE'));
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});
