import assert from "node:assert/strict"; import { test } from "node:test"; import { ConfigVersionError } from "./errors.js"; import { readGoldenConfigDocument } from "./fixtures.js"; import { type ConfigMigration, configMigrations, liftToCurrentVersion, } from "./migrations.js"; import { readConfigDocument } from "./read.js"; import { CURRENT_SCHEMA_VERSION } from "./version.js"; test("v1 lifts to v2 by dropping each anchor and every anchor-only filter", () => { const source = readGoldenConfigDocument() as Record; const lifted = liftToCurrentVersion(source, 1, configMigrations, 2); assert.equal(lifted.schemaVersion, 2); const filters = lifted.filters as Record[]; assert.deepEqual( filters.map((filter) => filter.name), ["Invoices to Facturen"], ); for (const filter of filters) { assert.equal("anchor" in filter, false); } }); test("v2 lifts to v3 by turning each filter's clauses into a rule", () => { const lifted = liftToCurrentVersion( { schemaVersion: 2, filters: [ { name: "GitHub", matchOperator: "Or", literalClauses: [ { field: "FromDomain", value: "github.com" }, { field: "HasWords", value: "pull request" }, ], }, ], }, 2, configMigrations, 3, ); assert.deepEqual(lifted, { schemaVersion: 3, filters: [ { name: "GitHub", rule: { version: 1, test: { test: "anyof", tests: [ { test: "address", match: "Is", part: "RegistrableDomain", key: "github.com", }, { test: "hasWords", words: ["pull", "request"] }, ], }, }, priority: 0, stop: false, }, ], }); }); test("a document already at the current version passes through untouched", () => { const source = readGoldenConfigDocument() as Record; assert.equal(liftToCurrentVersion(source, CURRENT_SCHEMA_VERSION), source); }); test("the chain lifts one version at a time", () => { const migrations: ConfigMigration[] = [ { from: 1, to: 2, migrate: (document) => ({ ...document, schemaVersion: 2, added: "two" }), }, { from: 2, to: 3, migrate: (document) => ({ ...document, schemaVersion: 3, added: "three", }), }, ]; const lifted = liftToCurrentVersion({ schemaVersion: 1 }, 1, migrations, 3); assert.deepEqual(lifted, { schemaVersion: 3, added: "three" }); }); test("a gap in the chain fails instead of leaving the document at its old version", () => { const migrations: ConfigMigration[] = [ { from: 2, to: 3, migrate: (document) => document }, ]; assert.throws( () => liftToCurrentVersion({ schemaVersion: 1 }, 1, migrations, 3), (error: unknown) => { assert.ok(error instanceof ConfigVersionError); assert.equal(error.documentVersion, 1); assert.equal(error.supportedVersion, 3); return true; }, ); }); test("a document already at the current version never enters the chain", () => { let ran = 0; const migrations: ConfigMigration[] = [ { from: 1, to: 2, migrate: (document) => { ran += 1; return document; }, }, ]; const current = liftToCurrentVersion( readGoldenConfigDocument() as Record, 1, ); const parsed = readConfigDocument(current, migrations); assert.equal(ran, 0); assert.equal(parsed.schemaVersion, CURRENT_SCHEMA_VERSION); });