Files
pikasTech-HWLAB/internal/cloud/json-rpc.test.mjs
T
2026-05-21 17:57:51 +00:00

105 lines
3.6 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import { AUDIT_FIELD_NAMES } from "../audit/index.mjs";
import { ERROR_CODES, validateResponse } from "../protocol/index.mjs";
import { handleJsonRpcRequest } from "./json-rpc.mjs";
test("unknown JSON-RPC method returns an error envelope with audit fields", async () => {
const response = await handleJsonRpcRequest({
jsonrpc: "2.0",
id: "req_01J00000000000000000000000",
method: "hardware.operation.cancel",
params: {
operationId: "op_01J00000000000000000000000"
},
meta: {
traceId: "trc_01J00000000000000000000000",
actorId: "usr_01J00000000000000000000000",
serviceId: "hwlab-cloud-web",
environment: "dev"
}
});
validateResponse(response);
assert.equal(response.error.code, ERROR_CODES.methodNotFound);
assert.equal(Object.hasOwn(response, "result"), false);
const audit = response.error.data.audit;
for (const field of AUDIT_FIELD_NAMES) {
assert.ok(Object.hasOwn(audit, field), `expected audit.${field}`);
}
assert.equal(audit.requestId, "req_01J00000000000000000000000");
assert.equal(audit.actor.id, "usr_01J00000000000000000000000");
assert.equal(audit.operation, "hardware.operation.cancel");
assert.equal(audit.target.type, "rpc_method");
assert.equal(audit.result, "rejected");
});
test("hardware.operation.request is a non-dispatching skeleton with visible audit fields", async () => {
const response = await handleJsonRpcRequest({
jsonrpc: "2.0",
id: "req_01J00000000000000000000001",
method: "hardware.operation.request",
params: {
projectId: "prj_01J00000000000000000000000"
},
meta: {
traceId: "trc_01J00000000000000000000001",
actorId: "usr_01J00000000000000000000000",
serviceId: "hwlab-cloud-web",
environment: "dev"
}
});
validateResponse(response);
assert.equal(response.result.accepted, false);
assert.equal(response.result.status, "skeleton");
for (const field of AUDIT_FIELD_NAMES) {
assert.ok(Object.hasOwn(response.result.audit, field), `expected audit.${field}`);
}
assert.equal(response.result.audit.operation, "hardware.operation.request");
assert.equal(response.result.audit.target.id, "prj_01J00000000000000000000000");
});
test("system.health includes the redacted DEV DB env contract", async () => {
const originalUrl = process.env.HWLAB_CLOUD_DB_URL;
const originalSslMode = process.env.HWLAB_CLOUD_DB_SSL_MODE;
delete process.env.HWLAB_CLOUD_DB_URL;
delete process.env.HWLAB_CLOUD_DB_SSL_MODE;
try {
const response = await handleJsonRpcRequest({
jsonrpc: "2.0",
id: "req_01J00000000000000000000002",
method: "system.health",
params: {},
meta: {
traceId: "trc_01J00000000000000000000002",
serviceId: "hwlab-cloud-web",
environment: "dev"
}
});
validateResponse(response);
assert.equal(response.result.db.status, "blocked");
assert.equal(response.result.db.connected, false);
assert.ok(response.result.db.missingEnv.includes("HWLAB_CLOUD_DB_URL"));
assert.equal(response.result.db.secretRefs[0].secretName, "hwlab-cloud-api-dev-db");
assert.equal(response.result.db.secretRefs[0].secretKey, "database-url");
assert.equal(response.result.db.secretRefs[0].redacted, true);
} finally {
if (originalUrl === undefined) {
delete process.env.HWLAB_CLOUD_DB_URL;
} else {
process.env.HWLAB_CLOUD_DB_URL = originalUrl;
}
if (originalSslMode === undefined) {
delete process.env.HWLAB_CLOUD_DB_SSL_MODE;
} else {
process.env.HWLAB_CLOUD_DB_SSL_MODE = originalSslMode;
}
}
});