/**
 * db-path-worktree-symlink.test.ts — #2517
 *
 * Regression test for the db_unavailable loop in worktree/symlink layouts.
 */

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { join, sep } from "node:path";
import { describe, it } from "vitest";

describe("#2517: resolveProjectRootDbPath handles symlink-resolved layout", () => {
	it("standard worktree layout resolves to project root DB path", async () => {
		const { resolveProjectRootDbPath } = await import(
			"../bootstrap/dynamic-tools.js"
		);

		const standardPath = `/home/user/myproject/.sf/worktrees/M001/work`;
		const standardResult = resolveProjectRootDbPath(standardPath);
		assert.strictEqual(
			standardResult,
			join("/home/user/myproject", ".sf", "sf.db"),
		);
	});

	it("symlink-resolved layout resolves to hash-level DB", async () => {
		const { resolveProjectRootDbPath } = await import(
			"../bootstrap/dynamic-tools.js"
		);

		const symlinkPath = `/home/user/myproject/.sf/projects/abc123def/worktrees/M001/work`;
		const symlinkResult = resolveProjectRootDbPath(symlinkPath);
		assert.strictEqual(
			symlinkResult,
			join("/home/user/myproject/.sf/projects/abc123def", "sf.db"),
		);
	});

	it("deep nested path resolves to hash-level DB", async () => {
		const { resolveProjectRootDbPath } = await import(
			"../bootstrap/dynamic-tools.js"
		);

		const deepSymlinkPath = `/home/user/myproject/.sf/projects/deadbeef42/worktrees/M003/sub/dir`;
		const deepResult = resolveProjectRootDbPath(deepSymlinkPath);
		assert.strictEqual(
			deepResult,
			join("/home/user/myproject/.sf/projects/deadbeef42", "sf.db"),
		);
	});

	it("non-worktree path is unchanged", async () => {
		const { resolveProjectRootDbPath } = await import(
			"../bootstrap/dynamic-tools.js"
		);

		const normalPath = `/home/user/myproject`;
		const normalResult = resolveProjectRootDbPath(normalPath);
		assert.strictEqual(
			normalResult,
			join("/home/user/myproject", ".sf", "sf.db"),
		);
	});
});

describe("#2517: ensureDbOpen structured diagnostics", () => {
	it("ensureDbOpen catch block surfaces diagnostic information", () => {
		const dynamicToolsSrc = readFileSync(
			join(import.meta.dirname, "..", "bootstrap", "dynamic-tools.ts"),
			"utf-8",
		);

		assert.ok(
			dynamicToolsSrc.includes("ensureDbOpen failed") &&
				dynamicToolsSrc.includes("logWarning"),
		);
	});
});

describe("#2517: post-unit does NOT artifact-retry on db_unavailable", () => {
	it("post-unit artifact retry path checks DB availability", () => {
		const postUnitSrc = readFileSync(
			join(import.meta.dirname, "..", "auto-post-unit.ts"),
			"utf-8",
		);

		assert.ok(
			postUnitSrc.includes("db_unavailable") ||
				postUnitSrc.includes("isDbAvailable"),
		);
	});

	it("the retry block explicitly guards against !isDbAvailable() before returning 'retry'", () => {
		const postUnitSrc = readFileSync(
			join(import.meta.dirname, "..", "auto-post-unit.ts"),
			"utf-8",
		);

		const dbUnavailableGuard = postUnitSrc.match(
			/!triggerArtifactVerified\s*&&\s*!isDbAvailable\(\)/,
		);
		assert.ok(!!dbUnavailableGuard);
	});
});
