import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { resolveCapabilitiesHere } from "../tools/capabilities-here.js";
function writeFile(path, body) {
    mkdirSync(join(path, ".."), { recursive: true });
    writeFileSync(path, body);
}
function seedTree(root, spec) {
    const accountConfigPath = join(root, "accounts", spec.accountId, "account.json");
    const brandConfigPath = join(root, "platform", "config", "brand.json");
    const platformRoot = join(root, "platform");
    const premiumPluginsRoot = join(root, "premium-plugins");
    const claudeConfigDir = join(root, "claude-config");
    writeFile(accountConfigPath, JSON.stringify({
        accountId: spec.accountId,
        enabledPlugins: spec.enabledPlugins ?? [],
        tier: spec.tier,
        adminModel: spec.adminModel,
        publicModel: spec.publicModel,
    }));
    writeFile(brandConfigPath, JSON.stringify({
        productName: spec.productName,
        shipsPremiumBundles: spec.shipsPremiumBundles ?? [],
    }));
    const specialistsDir = join(platformRoot, "templates", "specialists", "agents");
    mkdirSync(specialistsDir, { recursive: true });
    for (const name of spec.platformSpecialists) {
        writeFileSync(join(specialistsDir, `${name}.md`), `---\nname: ${name}\n---\n`);
    }
    for (const [bundle, names] of Object.entries(spec.premiumSpecialists ?? {})) {
        const dir = join(premiumPluginsRoot, bundle, "agents");
        mkdirSync(dir, { recursive: true });
        for (const name of names) {
            writeFileSync(join(dir, `${name}.md`), `---\nname: ${name}\n---\n`);
        }
    }
    const agentsDir = join(claudeConfigDir, "agents");
    mkdirSync(agentsDir, { recursive: true });
    for (const name of spec.registeredAgents ?? []) {
        writeFileSync(join(agentsDir, `${name}.md`), `---\nname: ${name}\n---\n`);
    }
    return {
        input: {
            accountId: spec.accountId,
            accountConfigPath,
            brandConfigPath,
            platformRoot,
            premiumPluginsRoot,
            claudeConfigDir,
        },
    };
}
describe("resolveCapabilitiesHere", () => {
    let root;
    beforeEach(() => { root = mkdtempSync(join(tmpdir(), "cap-here-")); });
    afterEach(() => { rmSync(root, { recursive: true, force: true }); });
    it("returns Maxy install set with no premium bundles", async () => {
        const { input } = seedTree(root, {
            accountId: "acct-maxy",
            enabledPlugins: ["business-assistant", "sales", "teaching"],
            tier: "pro",
            adminModel: "claude-opus-4-7",
            publicModel: "claude-haiku-4-5",
            productName: "Maxy",
            shipsPremiumBundles: [],
            platformSpecialists: ["personal-assistant", "project-manager", "librarian"],
            registeredAgents: ["plugin:admin:librarian", "plugin:admin:project-manager"],
        });
        const result = await resolveCapabilitiesHere(input, "2026-05-31T07:00:00Z");
        expect(result.accountId).toBe("acct-maxy");
        expect(result.tier).toBe("pro");
        expect(result.enabledPlugins).toEqual(["business-assistant", "sales", "teaching"]);
        expect(result.installedPlatformSpecialists).toEqual(["librarian", "personal-assistant", "project-manager"]);
        expect(result.installedPremiumSpecialists).toEqual([]);
        expect(result.registeredAgentTypes).toEqual(["plugin:admin:librarian", "plugin:admin:project-manager"]);
        expect(result.brand.productName).toBe("Maxy");
        expect(result.brand.shipsPremiumBundles).toEqual([]);
        expect(result.capturedAt).toBe("2026-05-31T07:00:00Z");
    });
    it("returns Real Agent install set including premium specialists from each shipped bundle", async () => {
        const { input } = seedTree(root, {
            accountId: "acct-ra",
            enabledPlugins: ["loop", "property-data", "estate-sales"],
            tier: "agency",
            productName: "Real Agent",
            shipsPremiumBundles: ["real-agent"],
            platformSpecialists: ["coding-assistant", "database-operator"],
            premiumSpecialists: {
                "real-agent": ["negotiator", "valuer", "compliance", "listing-curator"],
            },
            registeredAgents: ["plugin:real-agent:negotiator"],
        });
        const result = await resolveCapabilitiesHere(input, "2026-05-31T07:00:00Z");
        expect(result.brand.productName).toBe("Real Agent");
        expect(result.brand.shipsPremiumBundles).toEqual(["real-agent"]);
        expect(result.installedPlatformSpecialists).toEqual(["coding-assistant", "database-operator"]);
        expect(result.installedPremiumSpecialists).toEqual([
            { bundle: "real-agent", name: "compliance" },
            { bundle: "real-agent", name: "listing-curator" },
            { bundle: "real-agent", name: "negotiator" },
            { bundle: "real-agent", name: "valuer" },
        ]);
        expect(result.registeredAgentTypes).toEqual(["plugin:real-agent:negotiator"]);
    });
    it("handles missing fields and missing directories gracefully", async () => {
        const accountConfigPath = join(root, "account.json");
        const brandConfigPath = join(root, "brand.json");
        writeFileSync(accountConfigPath, JSON.stringify({ accountId: "x" }));
        writeFileSync(brandConfigPath, JSON.stringify({}));
        const result = await resolveCapabilitiesHere({
            accountId: "x",
            accountConfigPath,
            brandConfigPath,
            platformRoot: join(root, "absent-platform"),
            premiumPluginsRoot: join(root, "absent-premium"),
            claudeConfigDir: join(root, "absent-claude"),
        }, "2026-05-31T07:00:00Z");
        expect(result.tier).toBeNull();
        expect(result.adminModel).toBeNull();
        expect(result.publicModel).toBeNull();
        expect(result.enabledPlugins).toEqual([]);
        expect(result.installedPlatformSpecialists).toEqual([]);
        expect(result.installedPremiumSpecialists).toEqual([]);
        expect(result.registeredAgentTypes).toEqual([]);
        expect(result.brand.productName).toBeNull();
    });
});
//# sourceMappingURL=capabilities-here.test.js.map