import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { join, resolve } from "node:path";
import { tmpdir } from "node:os";
// Task 207 — `readPassword()` (memory plugin) falls back to a
// `<platformRoot>/config/.neo4j-password` lookup when neither
// `NEO4J_PASSWORD` nor `PLATFORM_ROOT` is set in env. Before this task,
// the fallback's `../../../..` math from `dist/lib/neo4j.js` landed at
// `platform/plugins/` (one level too high), so the read failed silently
// while the per-spawn `.mcp.json` masked the bug by always injecting
// `PLATFORM_ROOT`. With Task 207 dropping the per-spawn JSON for
// specialists, this fallback is the new load-bearing path; the path
// math is `../../../../..` (five up).
//
// This test verifies the resolved path lands at `<platformRoot>/config/`,
// not `<platformRoot>/plugins/config/`, by walking the same number of
// segments up from a fixture directory shaped like the dist layout.
describe("Task 207 — readPassword fallback path math", () => {
    let tmpRoot = "";
    const originalEnv = { ...process.env };
    beforeEach(() => {
        tmpRoot = mkdtempSync(join(tmpdir(), "task207-neo4j-path-"));
        delete process.env.NEO4J_PASSWORD;
        delete process.env.PLATFORM_ROOT;
    });
    afterEach(() => {
        process.env = { ...originalEnv };
        if (tmpRoot)
            rmSync(tmpRoot, { recursive: true, force: true });
    });
    it("five-segment hop from dist/lib/neo4j.js lands at platform/config/.neo4j-password", () => {
        // Layout mirrors the compiled tree:
        //   <tmpRoot>/platform/config/.neo4j-password
        //   <tmpRoot>/platform/plugins/memory/mcp/dist/lib/neo4j.js
        const platformRoot = join(tmpRoot, "platform");
        mkdirSync(join(platformRoot, "config"), { recursive: true });
        writeFileSync(join(platformRoot, "config", ".neo4j-password"), "secret-from-file\n", "utf-8");
        // Simulate the source-of-truth math: import.meta.dirname is the
        // directory of the compiled neo4j.js. Five segments up is platform/.
        const distLibDir = join(platformRoot, "plugins", "memory", "mcp", "dist", "lib");
        mkdirSync(distLibDir, { recursive: true });
        const resolved = resolve(distLibDir, "../../../../..", "config/.neo4j-password");
        expect(resolved).toBe(join(platformRoot, "config/.neo4j-password"));
        expect(readFileSync(resolved, "utf-8").trim()).toBe("secret-from-file");
    });
    it("four-segment hop lands at platform/plugins/ which has no .neo4j-password — proves the regression case", () => {
        const platformRoot = join(tmpRoot, "platform");
        mkdirSync(join(platformRoot, "config"), { recursive: true });
        writeFileSync(join(platformRoot, "config", ".neo4j-password"), "secret\n", "utf-8");
        const distLibDir = join(platformRoot, "plugins", "memory", "mcp", "dist", "lib");
        mkdirSync(distLibDir, { recursive: true });
        const wrong = resolve(distLibDir, "../../../..", "config/.neo4j-password");
        expect(wrong).toBe(join(platformRoot, "plugins", "config", ".neo4j-password"));
        expect(existsSync(wrong)).toBe(false);
    });
});
//# sourceMappingURL=neo4j-password-path.test.js.map