import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "vitest";

import {
	clearPathCache,
	relMilestoneFile,
	resolveMilestoneFile,
	resolveMilestonePath,
} from "../paths.js";

test("resolveMilestonePath_when_unique_id_has_bare_disk_dir_uses_canonical_dir", () => {
	const root = mkdtempSync(join(tmpdir(), "sf-paths-unique-"));
	mkdirSync(join(root, ".sf", "milestones", "M001"), { recursive: true });
	writeFileSync(
		join(root, ".sf", "milestones", "M001", "M001-CONTEXT.md"),
		"# Context\n",
	);
	clearPathCache();

	assert.equal(
		resolveMilestonePath(root, "M001-6377a4"),
		join(root, ".sf", "milestones", "M001"),
	);
	assert.equal(
		resolveMilestoneFile(root, "M001-6377a4", "CONTEXT"),
		join(root, ".sf", "milestones", "M001", "M001-CONTEXT.md"),
	);
});

test("relMilestoneFile_when_unique_id_resolves_bare_dir_uses_bare_file_id", () => {
	const root = mkdtempSync(join(tmpdir(), "sf-paths-unique-rel-"));
	mkdirSync(join(root, ".sf", "milestones", "M001"), { recursive: true });
	clearPathCache();

	assert.equal(
		relMilestoneFile(root, "M001-6377a4", "ROADMAP"),
		".sf/milestones/M001/M001-ROADMAP.md",
	);
});
