65 lines
2.7 KiB
TypeScript
65 lines
2.7 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import {
|
|
hydrateRuleVersionStableIds,
|
|
loadPublishedRuleVersionSnapshot,
|
|
savePublishedRuleVersionSnapshot
|
|
} from "./rule-version-snapshot.js";
|
|
import type { RuleSetVersion } from "./types.js";
|
|
|
|
const originalDir = process.env.SEAL_USE_RULE_VERSION_SNAPSHOTS_DIR;
|
|
let testDir = "";
|
|
|
|
afterEach(() => {
|
|
if (originalDir === undefined) delete process.env.SEAL_USE_RULE_VERSION_SNAPSHOTS_DIR;
|
|
else process.env.SEAL_USE_RULE_VERSION_SNAPSHOTS_DIR = originalDir;
|
|
if (testDir) rmSync(testDir, { recursive: true, force: true });
|
|
testDir = "";
|
|
});
|
|
|
|
describe("published rule version identity snapshots", () => {
|
|
test("persists draft IDs and hydrates API snapshots that only expose code", () => {
|
|
testDir = mkdtempSync(join(tmpdir(), "seal-use-rule-snapshot-"));
|
|
process.env.SEAL_USE_RULE_VERSION_SNAPSHOTS_DIR = testDir;
|
|
const version = {
|
|
id: "published-1",
|
|
versionNumber: 1,
|
|
rules: [
|
|
{ code: "rule-0", description: "A", scope: "ALL", strictness: "MUST_FOLLOW" },
|
|
{ code: "rule-1", description: "B", scope: "ALL", strictness: "SHOULD_FOLLOW" }
|
|
]
|
|
} as RuleSetVersion;
|
|
savePublishedRuleVersionSnapshot("corp/a", version, [
|
|
{ id: "draft-a", code: 0, description: "A", scope: "ALL", strictness: "MUST_FOLLOW" },
|
|
{ id: "draft-b", code: 1, description: "B", scope: "ALL", strictness: "SHOULD_FOLLOW" }
|
|
]);
|
|
expect(loadPublishedRuleVersionSnapshot("corp/a", version)?.rules.map((rule) => rule.stableId)).toEqual(["draft-a", "draft-b"]);
|
|
const hydrated = hydrateRuleVersionStableIds("corp/a", version);
|
|
expect(hydrated.rules.map((rule) => rule.stableId)).toEqual(["draft-a", "draft-b"]);
|
|
});
|
|
|
|
test("prunes snapshots to at most 20 files via LRU", () => {
|
|
testDir = mkdtempSync(join(tmpdir(), "seal-use-rule-snapshot-lru-"));
|
|
process.env.SEAL_USE_RULE_VERSION_SNAPSHOTS_DIR = testDir;
|
|
|
|
for (let i = 1; i <= 25; i++) {
|
|
const version = {
|
|
id: `published-${i}`,
|
|
versionNumber: i,
|
|
rules: []
|
|
} as RuleSetVersion;
|
|
savePublishedRuleVersionSnapshot("corp-test", version, []);
|
|
}
|
|
|
|
const { readdirSync } = require("node:fs");
|
|
const remaining = readdirSync(testDir).filter((f: string) => f.endsWith(".json"));
|
|
expect(remaining.length).toBe(20);
|
|
// Oldest version 1 should have been pruned, latest version 25 must exist
|
|
expect(loadPublishedRuleVersionSnapshot("corp-test", { versionNumber: 1 } as any)).toBeNull();
|
|
expect(loadPublishedRuleVersionSnapshot("corp-test", { versionNumber: 25 } as any)).not.toBeNull();
|
|
});
|
|
});
|
|
|