/**
 * commands-todo-db.test.mjs — TODO triage backlog DB binding coverage.
 *
 * Purpose: prove TODO triage backlog promotion writes to the requested project
 * SQLite DB instead of reusing a previously-open DB from another project.
 */
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, test } from "vitest";
import { triageTodoDump } from "../commands-todo.js";
import { closeDatabase, listBacklogItems, openDatabase } from "../sf-db.js";

const tmpRoots = [];

afterEach(() => {
	closeDatabase();
	for (const root of tmpRoots.splice(0)) {
		rmSync(root, { recursive: true, force: true });
	}
});

function makeProject() {
	const root = mkdtempSync(join(tmpdir(), "sf-todo-db-"));
	mkdirSync(join(root, ".sf"), { recursive: true });
	tmpRoots.push(root);
	return root;
}

function backlogTitles(project) {
	closeDatabase();
	openDatabase(join(project, ".sf", "sf.db"));
	return listBacklogItems().map((item) => item.title);
}

test("triageTodoDump_with_backlog_when_db_already_open_writes_requested_project_db", async () => {
	const first = makeProject();
	const second = makeProject();
	openDatabase(join(first, ".sf", "sf.db"));
	writeFileSync(join(second, "TODO.md"), "# TODO\n\nadd project-local task\n");
	const response = JSON.stringify({
		summary: "triaged",
		eval_candidates: [],
		implementation_tasks: ["add project-local task"],
		memory_requirements: [],
		harness_suggestions: [],
		docs_or_tests: [],
		unclear_notes: [],
	});

	await triageTodoDump(second, async () => response, {
		clear: false,
		backlog: true,
		date: new Date("2026-05-07T00:00:00.000Z"),
	});

	assert.deepEqual(backlogTitles(first), []);
	assert.deepEqual(backlogTitles(second), ["add project-local task"]);
});
