import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { test } from 'vitest';

test("copyPlanningArtifacts skips when source and destination .sf resolve to the same path", () => {
	const srcPath = join(import.meta.dirname, "..", "auto-worktree.ts");
	const src = readFileSync(srcPath, "utf-8");

	const fnIdx = src.indexOf("function copyPlanningArtifacts");
	assert.ok(fnIdx !== -1, "copyPlanningArtifacts function exists");

	const fnBody = src.slice(fnIdx, fnIdx + 2400);

	const guardIdx = fnBody.indexOf("if (isSamePath(srcSf, dstSf)) return;");
	const copyIdx = fnBody.indexOf(
		'safeCopyRecursive(join(srcSf, "milestones")',
	);

	assert.ok(
		guardIdx !== -1,
		"copyPlanningArtifacts should guard same-path .sf copies",
	);
	assert.ok(
		copyIdx !== -1,
		"copyPlanningArtifacts should still copy milestones when paths differ",
	);
	assert.ok(
		guardIdx < copyIdx,
		"same-path guard should run before any copy attempt",
	);
});
