"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_test_1 = __importDefault(require("node:test"));
const strict_1 = __importDefault(require("node:assert/strict"));
const node_fs_1 = require("node:fs");
const node_os_1 = require("node:os");
const node_path_1 = require("node:path");
const index_js_1 = require("../index.js");
function makeFixture() {
    const root = (0, node_fs_1.mkdtempSync)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "acct-enum-"));
    // (a) non-UUID dir → excluded.
    (0, node_fs_1.mkdirSync)((0, node_path_1.join)(root, "not-a-uuid"));
    (0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, "not-a-uuid", "account.json"), "{}");
    // (b) UUID dir, no account.json → excluded.
    (0, node_fs_1.mkdirSync)((0, node_path_1.join)(root, "11111111-2222-3333-4444-555555555555"));
    // (c) UUID dir, unparseable account.json → excluded (over-prune doctrine).
    (0, node_fs_1.mkdirSync)((0, node_path_1.join)(root, "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"));
    (0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", "account.json"), "{ broken json");
    // (d) UUID dir, parseable account.json → included.
    (0, node_fs_1.mkdirSync)((0, node_path_1.join)(root, "12345678-9abc-def0-1234-56789abcdef0"));
    (0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, "12345678-9abc-def0-1234-56789abcdef0", "account.json"), JSON.stringify({ id: "12345678-9abc-def0-1234-56789abcdef0" }));
    return root;
}
(0, node_test_1.default)("enumerateValidAccountIds returns only UUID dirs with parseable account.json", () => {
    (0, index_js_1._resetEnumerationCache)();
    const root = makeFixture();
    try {
        const result = (0, index_js_1.enumerateValidAccountIds)(root);
        strict_1.default.deepEqual(result, ["12345678-9abc-def0-1234-56789abcdef0"]);
    }
    finally {
        (0, node_fs_1.rmSync)(root, { recursive: true });
    }
});
(0, node_test_1.default)("enumerateValidAccountIds returns [] when accountsDir does not exist", () => {
    (0, index_js_1._resetEnumerationCache)();
    const ghost = (0, node_path_1.join)((0, node_os_1.tmpdir)(), "acct-enum-does-not-exist-" + Date.now());
    strict_1.default.deepEqual((0, index_js_1.enumerateValidAccountIds)(ghost), []);
});
(0, node_test_1.default)("enumerateValidAccountIds caches per accountsDir; reset re-reads", () => {
    (0, index_js_1._resetEnumerationCache)();
    const root = makeFixture();
    try {
        const first = (0, index_js_1.enumerateValidAccountIds)(root);
        // Add a second valid dir AFTER first call; cached call must NOT see it.
        const newId = "99999999-aaaa-bbbb-cccc-dddddddddddd";
        (0, node_fs_1.mkdirSync)((0, node_path_1.join)(root, newId));
        (0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, newId, "account.json"), "{}");
        const cached = (0, index_js_1.enumerateValidAccountIds)(root);
        strict_1.default.deepEqual(cached, first, "cache should hide the newly-added dir");
        (0, index_js_1._resetEnumerationCache)();
        const fresh = (0, index_js_1.enumerateValidAccountIds)(root);
        strict_1.default.equal(fresh.length, 2);
        strict_1.default.ok(fresh.includes(newId));
    }
    finally {
        (0, node_fs_1.rmSync)(root, { recursive: true });
    }
});
(0, node_test_1.default)("getAccountsDirFromEnv throws when MAXY_PLATFORM_ROOT is unset", () => {
    const saved = process.env.MAXY_PLATFORM_ROOT;
    delete process.env.MAXY_PLATFORM_ROOT;
    try {
        strict_1.default.throws(() => (0, index_js_1.getAccountsDirFromEnv)(), /MAXY_PLATFORM_ROOT not set/);
    }
    finally {
        if (saved !== undefined)
            process.env.MAXY_PLATFORM_ROOT = saved;
    }
});
(0, node_test_1.default)("getAccountsDirFromEnv resolves to ${root}/../data/accounts when set", () => {
    const saved = process.env.MAXY_PLATFORM_ROOT;
    process.env.MAXY_PLATFORM_ROOT = "/tmp/fake-root/platform";
    try {
        const result = (0, index_js_1.getAccountsDirFromEnv)();
        strict_1.default.equal(result, (0, node_path_1.resolve)("/tmp/fake-root/platform", "..", "data/accounts"));
    }
    finally {
        if (saved !== undefined)
            process.env.MAXY_PLATFORM_ROOT = saved;
        else
            delete process.env.MAXY_PLATFORM_ROOT;
    }
});
//# sourceMappingURL=enumerate.test.js.map