import { test, expect } from "vitest";
import { runDriftCheck } from "../live-schema-source.js";
// ---------------------------------------------------------------------------
// (e) Drift comparison emits ONE summary line per non-empty kind.
//
// Task 167: prior shape was one log line per drifting token, which buried
// real signals under ~100 lines per refresh once the drift sets grew.
// The summary shape compresses the spam to ≤3 lines per refresh while
// still naming the first 10 tokens; remainder count appears as `+<N>`.
//
// Per-token lines remain reachable behind MEMORY_MCP_VERBOSE_DRIFT=1.
// ---------------------------------------------------------------------------
function withoutVerbose(fn) {
    const prev = process.env.MEMORY_MCP_VERBOSE_DRIFT;
    delete process.env.MEMORY_MCP_VERBOSE_DRIFT;
    try {
        return fn();
    }
    finally {
        if (prev !== undefined)
            process.env.MEMORY_MCP_VERBOSE_DRIFT = prev;
    }
}
function withVerbose(fn) {
    const prev = process.env.MEMORY_MCP_VERBOSE_DRIFT;
    process.env.MEMORY_MCP_VERBOSE_DRIFT = "1";
    try {
        return fn();
    }
    finally {
        if (prev === undefined)
            delete process.env.MEMORY_MCP_VERBOSE_DRIFT;
        else
            process.env.MEMORY_MCP_VERBOSE_DRIFT = prev;
    }
}
test("(e1) one non-empty kind, ≤10 tokens → one summary line, no `+` suffix", () => {
    withoutVerbose(() => {
        const lines = [];
        const drifts = runDriftCheck({
            live: new Set(["Person", "Surprise"]),
            declared: new Set(["Person"]),
            markdown: new Set(["Person"]),
            emit: (l) => lines.push(l),
        });
        // Return-value shape preserved (regression boundary).
        expect(drifts).toEqual([
            { kind: "live-not-declared", token: "Surprise" },
        ]);
        expect(lines).toEqual([
            "[schema-validator] drift kind=live-not-declared count=1 tokens=Surprise",
        ]);
    });
});
test("(e2) one non-empty kind, >10 tokens → one summary line with `+<remainder>` suffix", () => {
    withoutVerbose(() => {
        // 12 markdown-orphan tokens A..L → first 10 listed, `+2` on last.
        const orphans = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"];
        const lines = [];
        runDriftCheck({
            live: new Set([]),
            declared: new Set([]),
            markdown: new Set(orphans),
            emit: (l) => lines.push(l),
        });
        expect(lines).toEqual([
            "[schema-validator] drift kind=markdown-orphan count=12 tokens=A,B,C,D,E,F,G,H,I,J+2",
        ]);
    });
});
test("(e3) three non-empty kinds → three summary lines in fixed order", () => {
    withoutVerbose(() => {
        const lines = [];
        runDriftCheck({
            live: new Set(["Person", "Surprise"]),
            declared: new Set(["Person", "LocalBusiness"]),
            markdown: new Set(["Person", "OldEntity"]),
            emit: (l) => lines.push(l),
        });
        // Fixed iteration order: declared-not-live, live-not-declared, markdown-orphan.
        expect(lines).toEqual([
            "[schema-validator] drift kind=declared-not-live count=1 tokens=LocalBusiness",
            "[schema-validator] drift kind=live-not-declared count=1 tokens=Surprise",
            "[schema-validator] drift kind=markdown-orphan count=1 tokens=OldEntity",
        ]);
    });
});
test("(e4) all kinds empty → zero lines", () => {
    withoutVerbose(() => {
        const lines = [];
        runDriftCheck({
            live: new Set(["Person", "LocalBusiness"]),
            declared: new Set(["Person", "LocalBusiness"]),
            markdown: new Set(["Person"]),
            emit: (l) => lines.push(l),
        });
        expect(lines.length).toBe(0);
    });
});
test("(e5) verbose env → summary then per-token lines, in that order", () => {
    withVerbose(() => {
        const lines = [];
        runDriftCheck({
            live: new Set(["Person", "Surprise", "Mystery"]),
            declared: new Set(["Person"]),
            markdown: new Set(["Person"]),
            emit: (l) => lines.push(l),
        });
        // Two live-not-declared tokens (Mystery, Surprise — alphabetical).
        // Summary first, then one per-token line per drifting label.
        expect(lines).toEqual([
            "[schema-validator] drift kind=live-not-declared count=2 tokens=Mystery,Surprise",
            "[schema-validator] drift kind=live-not-declared token=Mystery",
            "[schema-validator] drift kind=live-not-declared token=Surprise",
        ]);
    });
});
test("(e6) declared-not-live alone → bootstrap label rendered as summary", () => {
    // Fresh-install scenario — single declared bootstrap label, no nodes yet.
    withoutVerbose(() => {
        const lines = [];
        const drifts = runDriftCheck({
            live: new Set(["AdminUser"]),
            declared: new Set(["AdminUser", "LocalBusiness"]),
            markdown: new Set([]),
            emit: (l) => lines.push(l),
        });
        expect(drifts).toEqual([
            { kind: "declared-not-live", token: "LocalBusiness" },
        ]);
        expect(lines).toEqual([
            "[schema-validator] drift kind=declared-not-live count=1 tokens=LocalBusiness",
        ]);
    });
});
test("(e7) markdown-orphan ignored when label is in live OR declared", () => {
    withoutVerbose(() => {
        const lines = [];
        // OldEntity is in live, so it's live-not-declared, not a markdown-orphan.
        const drifts = runDriftCheck({
            live: new Set(["OldEntity"]),
            declared: new Set([]),
            markdown: new Set(["OldEntity"]),
            emit: (l) => lines.push(l),
        });
        expect(drifts).toEqual([
            { kind: "live-not-declared", token: "OldEntity" },
        ]);
        expect(lines).toEqual([
            "[schema-validator] drift kind=live-not-declared count=1 tokens=OldEntity",
        ]);
    });
});
//# sourceMappingURL=live-schema-source.test.js.map