import { describe, it, expect } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { REVISION_CYPHER_FRAGMENT_UNWIND } from "../compiled-truth-revision.js";
describe("compiled-truth-revision helper", () => {
    it("emits CREATEs for the :CompiledTruthRevision row and the :HAS_REVISION edge keyed by twin/source/truth", () => {
        // The fragment is concatenated into the writer's SET in a single
        // session.run so both mutations commit atomically (Task 391/436).
        expect(REVISION_CYPHER_FRAGMENT_UNWIND).toContain("UNWIND $revisions AS rev");
        expect(REVISION_CYPHER_FRAGMENT_UNWIND).toContain(":CompiledTruthRevision");
        expect(REVISION_CYPHER_FRAGMENT_UNWIND).toContain(":HAS_REVISION");
        expect(REVISION_CYPHER_FRAGMENT_UNWIND).toContain("revisionId: randomUUID()");
        expect(REVISION_CYPHER_FRAGMENT_UNWIND).toContain("accountId: $accountId");
        expect(REVISION_CYPHER_FRAGMENT_UNWIND).toContain("truth: rev.truth");
        expect(REVISION_CYPHER_FRAGMENT_UNWIND).toContain("twin: rev.twin");
        expect(REVISION_CYPHER_FRAGMENT_UNWIND).toContain("source: rev.source");
        expect(REVISION_CYPHER_FRAGMENT_UNWIND).toContain("writtenAt: datetime()");
    });
    it("memory-update folds the revision fragment into the same Cypher template as the SET (single TX)", () => {
        // Single-TX invariant: the SET that lands the new `compiledTruth` value
        // and the `:CompiledTruthRevision` CREATE must live in the same
        // session.run, otherwise a partial failure can desync revision history
        // from the live property. Verify the updateQuery template literal
        // contains both the SET and the ${revisionClause} interpolation, with
        // no closing backtick between them.
        const src = readFileSync(resolve(import.meta.dirname, "../../tools/memory-update.ts"), "utf-8");
        const templateStart = src.indexOf("const updateQuery = `");
        expect(templateStart).toBeGreaterThan(-1);
        const templateEnd = src.indexOf("`", templateStart + "const updateQuery = `".length);
        expect(templateEnd).toBeGreaterThan(templateStart);
        const template = src.slice(templateStart, templateEnd);
        expect(template).toContain("SET n += $props, n.updatedAt = $updatedAt");
        expect(template).toContain("${revisionClause}");
        // And the revisionClause itself embeds the UNWIND fragment.
        const clauseIdx = src.indexOf("const revisionClause =");
        expect(clauseIdx).toBeGreaterThan(-1);
        const clauseEnd = src.indexOf(";", clauseIdx);
        const clauseBlock = src.slice(clauseIdx, clauseEnd);
        expect(clauseBlock).toContain("${REVISION_CYPHER_FRAGMENT_UNWIND}");
    });
    it("memory-update gates the revision write on the rewriter env (specialist-only audit)", () => {
        // Non-specialist callers never reach the revision branch — the
        // system-managed-key guard refuses the property earlier. Verify
        // the revisionRows accumulator is itself gated on `rewriterCaller`.
        const src = readFileSync(resolve(import.meta.dirname, "../../tools/memory-update.ts"), "utf-8");
        const guardIdx = src.indexOf("if (rewriterCaller) {");
        const pushIdx = src.indexOf("revisionRows.push(");
        expect(guardIdx).toBeGreaterThan(-1);
        expect(pushIdx).toBeGreaterThan(guardIdx);
    });
});
describe("InlineRewriteCandidate + InlineRewriteDeferred exports (Task 595)", () => {
    it("compiled-truth-revision.ts exports InlineRewriteCandidate with required fields", () => {
        const src = readFileSync(resolve(import.meta.dirname, "../compiled-truth-revision.ts"), "utf-8");
        expect(src).toContain("export interface InlineRewriteCandidate");
        expect(src).toContain("elementId: string");
        expect(src).toContain("label: string");
        expect(src).toContain("name: string");
        expect(src).toContain("edgeCount: number");
    });
    it("compiled-truth-revision.ts exports InlineRewriteDeferred with reason and compiledTruthUpdatedAt", () => {
        const src = readFileSync(resolve(import.meta.dirname, "../compiled-truth-revision.ts"), "utf-8");
        expect(src).toContain("export interface InlineRewriteDeferred");
        expect(src).toContain('reason: "cooldown"');
        expect(src).toContain("compiledTruthUpdatedAt: string");
    });
});
//# sourceMappingURL=compiled-truth-revision.test.js.map