# test.build/core/persistence/graph.persistence.test.ts

> 47 lines of code and 0 definitions.

Tree: Codebase Testing
Language: typescript
Canonical: https://banes-lab.com/anatomy/testing#file-testing-test-build-core-persistence-graph-persistence-test-ts
Source text: https://banes-lab.com/source/testing/test.build/core/persistence/graph.persistence.test.ts.txt

Listed in [test.build/core/persistence](https://banes-lab.com/api/source/testing/test.build/core/persistence.md), after [test.build/core/persistence/folder.persistence.test.ts](https://banes-lab.com/source/testing/test.build/core/persistence/folder.persistence.test.ts.md) and before [test.build/core/persistence/icons.persistence.test.ts](https://banes-lab.com/source/testing/test.build/core/persistence/icons.persistence.test.ts.md).

## Contained in

- [test.build/core/persistence](https://banes-lab.com/anatomy/testing/folder-testing-test-build-core-persistence.md)

## Source

```typescript
import { afterEach, describe, expect, it } from "vitest";
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs";
import { writeGraphChunks, writeGraphReport } from "@banes-lab/build-scripts/core/persistence/graph.persistence.ts";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { writeCanonicalText } from "@govlab/canonical-write";

const scratch: string[] = [];

const folder = function folder(): string {
    const dir = mkdtempSync(join(tmpdir(), "graph-"));
    scratch.push(dir);
    return dir;
};

afterEach(() => {
    for (const dir of scratch.splice(0)) {
        rmSync(dir, { force: true, recursive: true });
    }
});

describe("writeGraphReport", () => {
    it("writes the report as indented JSON, creating its folder", () => {
        const file = join(folder(), "reports", "graph.json");
        const report = {
            ambiguous: [],
            dangling: [],
            duplicates: [],
            fields: [],
            graph: { edges: [], nodes: [] },
            populations: [],
            tooltipGaps: [],
            uncovered: [],
            undeclared: [],
            unresolved: [],
        };
        writeGraphReport(report, file);
        expect(JSON.parse(readFileSync(file, "utf8"))).toStrictEqual(report);
    });
});

describe("writeGraphChunks", () => {
    it("writes the loader and one module per chunk, and removes a chunk no longer written", async () => {
        const dir = folder();
        await writeCanonicalText(join(dir, "graph.stale.generated.ts"), "export {};\n");
        const loader = join(dir, "graph.generated.ts");
        await writeGraphChunks(new Map([["chapter", {}]]), loader);
        expect(readdirSync(dir).toSorted()).toStrictEqual(["graph.chapter.generated.ts", "graph.generated.ts"]);
        expect(readFileSync(loader, "utf8")).toContain("graph.chapter.generated");
        expect(existsSync(join(dir, "graph.stale.generated.ts"))).toBe(false);
    });
});
```
