75 lines
2.3 KiB
JavaScript
75 lines
2.3 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import ts from "typescript";
|
|
|
|
const testDir = path.dirname(fileURLToPath(import.meta.url));
|
|
const sourcePath = path.resolve(
|
|
testDir,
|
|
"../src/features/admin-scripts/model.ts",
|
|
);
|
|
const source = readFileSync(sourcePath, "utf8");
|
|
const compiled = ts.transpileModule(source, {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
|
|
});
|
|
|
|
const moduleExports = {};
|
|
new Function("exports", compiled.outputText)(moduleExports);
|
|
|
|
const {
|
|
buildAdminScriptVersionRows,
|
|
buildAdminScriptAuditRows,
|
|
defaultScriptVersionDraft,
|
|
buildCreateScriptVersionPayload,
|
|
parseJsonObject,
|
|
} = moduleExports;
|
|
|
|
const rows = buildAdminScriptVersionRows([
|
|
{
|
|
id: "11111111-1111-1111-1111-111111111101",
|
|
target_site_id: "22222222-2222-2222-2222-222222222222",
|
|
version: 1,
|
|
status: "ACTIVE",
|
|
diff: { summary: "seed" },
|
|
publishing_yaml_hash: "abc",
|
|
transform_script_hash: "def",
|
|
rollback_target_version_id: null,
|
|
created_by: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
|
|
created_at: "2025-01-01T00:00:00Z",
|
|
activated_at: "2025-01-01T01:00:00Z",
|
|
},
|
|
]);
|
|
|
|
assert.equal(rows.length, 1);
|
|
assert.equal(rows[0].isActive, true);
|
|
assert.equal(rows[0].diff, '{"summary":"seed"}');
|
|
|
|
const payload = buildCreateScriptVersionPayload(defaultScriptVersionDraft());
|
|
assert.equal(payload.activate, false);
|
|
assert.ok(typeof payload.publishing_yaml === "string");
|
|
assert.equal(payload.rollback_target_version_id, null);
|
|
|
|
assert.deepEqual(parseJsonObject('{"a":1}'), { a: 1 });
|
|
assert.equal(parseJsonObject("[]"), null);
|
|
assert.deepEqual(parseJsonObject(""), {});
|
|
|
|
const auditRows = buildAdminScriptAuditRows([
|
|
{
|
|
id: "evt-1",
|
|
target_site_id: "site-1",
|
|
version_id: "ver-1",
|
|
event_type: "SCRIPT_CONFIG_VERSION_ROLLBACK",
|
|
actor_user_id: "user-1",
|
|
payload: { action: "rollback" },
|
|
created_at: "2026-05-21T12:00:00Z",
|
|
version: 2,
|
|
diff: { summary: "rollback event" },
|
|
rollback_target_version_id: "ver-0",
|
|
},
|
|
]);
|
|
assert.equal(auditRows.length, 1);
|
|
assert.equal(auditRows[0].eventType, "SCRIPT_CONFIG_VERSION_ROLLBACK");
|
|
assert.equal(auditRows[0].rollbackTargetVersionId, "ver-0");
|