Files
pikasTech-HWLAB/internal/db/schema.test.mjs
T
2026-05-21 18:33:01 +00:00

82 lines
2.7 KiB
JavaScript

import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import path from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { TABLES, assertProtocolRecord, assertProtocolRecords } from "../protocol/index.mjs";
import {
CLOUD_CORE_MIGRATIONS,
FROZEN_CLOUD_CORE_TABLES,
assertFrozenCloudCoreTables
} from "./schema.mjs";
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
test("cloud core schema keeps the frozen L0 table names", () => {
assert.deepEqual(FROZEN_CLOUD_CORE_TABLES, TABLES);
assertFrozenCloudCoreTables();
assert.equal(CLOUD_CORE_MIGRATIONS[0].connectsToDatabase, false);
});
test("initial migration skeleton declares every frozen table", async () => {
const sql = await readFile(path.join(repoRoot, "internal/db/migrations/0001_cloud_core_skeleton.sql"), "utf8");
for (const table of TABLES) {
assert.match(sql, new RegExp(`CREATE TABLE IF NOT EXISTS ${table}\\b`), `missing ${table}`);
}
assert.match(sql, /\brequest_id\b/);
assert.match(sql, /\bactor\b/);
assert.match(sql, /\bsource\b/);
assert.match(sql, /\boperation\b/);
assert.match(sql, /\btarget\b/);
assert.match(sql, /\bresult\b/);
assert.match(sql, /\btimestamp\b/);
});
test("protocol record guards catch schema drift before runtime writes", () => {
assertProtocolRecord("gatewaySession", {
gatewaySessionId: "gws_01J00000000000000000000000",
projectId: "prj_01J00000000000000000000000",
serviceId: "hwlab-gateway-simu",
gatewayId: "gtw_01J00000000000000000000000",
endpoint: "http://127.0.0.1:7101",
status: "connected",
environment: "dev",
startedAt: "2026-05-21T00:00:00.000Z",
lastSeenAt: "2026-05-21T00:00:00.000Z"
});
assertProtocolRecords("boxCapability", [
{
capabilityId: "cap_01J00000000000000000000000",
resourceId: "res_01J00000000000000000000000",
projectId: "prj_01J00000000000000000000000",
name: "shell.exec",
direction: "bidirectional",
valueType: "object",
mutatesState: true,
createdAt: "2026-05-21T00:00:00.000Z",
updatedAt: "2026-05-21T00:00:00.000Z"
}
]);
assert.throws(
() =>
assertProtocolRecord("evidenceRecord", {
evidenceId: "evd_01J00000000000000000000000",
projectId: "prj_01J00000000000000000000000",
operationId: "op_01J00000000000000000000000",
kind: "trace",
uri: "memory://hwlab/evidence/evd_01J00000000000000000000000",
sha256: "0".repeat(64),
serviceId: "hwlab-cloud-api",
environment: "dev",
createdAt: "2026-05-21T00:00:00.000Z",
evidenceType: "trace"
}),
/evidenceRecord.evidenceType is not part of the frozen protocol schema/
);
});