56 lines
1.7 KiB
JavaScript
56 lines
1.7 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,
|
|
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(""), {});
|