38 tests: recording provider double answers with the sibling services' real response shapes; each suite asserts the exact request wire shape (path, method, tenant/actor/idempotency headers, camelCase vs snake_case body) so a regression to the pre-rewrite invented shapes fails loudly. Includes the fail-closed policy cases (missing decision => deny, CL0UD down => typed error, never implicit allow) and the production factory guard for all four clients. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
171 lines
5.9 KiB
TypeScript
171 lines
5.9 KiB
TypeScript
/**
|
|
* CONTRACT test for src/clients/cl0ud-client.ts against CL0UD's REAL wire
|
|
* contract (sax3l/cl0ud main — policy evaluate + capability discovery,
|
|
* verified 2026-08-31):
|
|
*
|
|
* POST /api/policy/evaluate — camelCase body, `{ data, meta }` envelope,
|
|
* decision ∈ {allow, deny} + requiresApproval flag.
|
|
* GET /api/capabilities — capability-registry discovery.
|
|
*
|
|
* The pre-rewrite client POSTed snake_case to /v1/cl0ud/policies/evaluate —
|
|
* a path that never existed on CL0UD. Policy stays FAIL-CLOSED: missing or
|
|
* malformed decision reads as deny.
|
|
*/
|
|
import { describe, it, expect, afterEach } from "vitest";
|
|
import { createCl0udClient, type EvaluatePolicyInput } from "@/src/clients/cl0ud-client";
|
|
import { SiaxError } from "@/src/platform/errors";
|
|
import { startContractServer, type ContractServer } from "./contract-test-server";
|
|
|
|
let server: ContractServer | undefined;
|
|
afterEach(async () => {
|
|
await server?.close();
|
|
server = undefined;
|
|
});
|
|
|
|
function input(overrides: Partial<EvaluatePolicyInput> = {}): EvaluatePolicyInput {
|
|
return {
|
|
tenant_id: "t_test",
|
|
project_id: "p_test",
|
|
actor_id: "svc_0pt",
|
|
capability_id: "0pt.write_external_record",
|
|
resource_type: "external_record",
|
|
resource_id: "rec_1",
|
|
risk_level: "L4",
|
|
correlation_id: "corr-1",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("Cl0udClient wire contract — policy evaluate", () => {
|
|
it("POSTs the REAL camelCase body to /api/policy/evaluate and maps the {data} envelope", async () => {
|
|
server = await startContractServer([
|
|
{
|
|
method: "POST",
|
|
path: "/api/policy/evaluate",
|
|
status: 200,
|
|
respond: () => ({
|
|
data: {
|
|
decision: "allow",
|
|
requiresApproval: false,
|
|
auditRequired: true,
|
|
riskLevel: "L4",
|
|
decisionId: "pdec_r3al",
|
|
reason: "rbac_ok",
|
|
},
|
|
meta: {},
|
|
}),
|
|
},
|
|
]);
|
|
const client = createCl0udClient(server.baseUrl);
|
|
const decision = await client.evaluatePolicy(input());
|
|
|
|
const req = server.requests[0];
|
|
expect(req.path).toBe("/api/policy/evaluate");
|
|
const body = req.body as Record<string, unknown>;
|
|
expect(body.capability).toBe("0pt.write_external_record");
|
|
expect(body.tenantId).toBe("t_test");
|
|
expect(body.projectId).toBe("p_test");
|
|
expect(body.actor).toEqual({ kind: "operator", role: "member" });
|
|
expect(body.surface).toBe("api");
|
|
expect(body.resource).toEqual({ type: "external_record", id: "rec_1" });
|
|
expect(body.correlationId).toBe("corr-1");
|
|
// Invented pre-rewrite fields must NOT be on the wire.
|
|
expect(body.capability_id).toBeUndefined();
|
|
expect(body.tenant_id).toBeUndefined();
|
|
expect(body.risk_level).toBeUndefined();
|
|
|
|
expect(decision).toEqual({
|
|
decision: "allow",
|
|
decision_id: "pdec_r3al",
|
|
reason: "rbac_ok",
|
|
deny_code: undefined,
|
|
});
|
|
});
|
|
|
|
it("maps allow + requiresApproval=true onto 0pt's approval_required (gates the caller)", async () => {
|
|
server = await startContractServer([
|
|
{
|
|
method: "POST",
|
|
path: "/api/policy/evaluate",
|
|
status: 200,
|
|
respond: () => ({ data: { decision: "allow", requiresApproval: true, decisionId: "pdec_a" }, meta: {} }),
|
|
},
|
|
]);
|
|
const client = createCl0udClient(server.baseUrl);
|
|
const decision = await client.evaluatePolicy(input());
|
|
expect(decision.decision).toBe("approval_required");
|
|
});
|
|
|
|
it("maps deny with denyCode", async () => {
|
|
server = await startContractServer([
|
|
{
|
|
method: "POST",
|
|
path: "/api/policy/evaluate",
|
|
status: 200,
|
|
respond: () => ({
|
|
data: { decision: "deny", requiresApproval: false, decisionId: "pdec_d", denyCode: "rbac_forbidden" },
|
|
meta: {},
|
|
}),
|
|
},
|
|
]);
|
|
const client = createCl0udClient(server.baseUrl);
|
|
const decision = await client.evaluatePolicy(input());
|
|
expect(decision.decision).toBe("deny");
|
|
expect(decision.deny_code).toBe("rbac_forbidden");
|
|
});
|
|
|
|
it("FAILS CLOSED: 200 with a body missing `decision` is a deny, never an allow", async () => {
|
|
server = await startContractServer([
|
|
{ method: "POST", path: "/api/policy/evaluate", status: 200, respond: () => ({ data: {}, meta: {} }) },
|
|
]);
|
|
const client = createCl0udClient(server.baseUrl);
|
|
const decision = await client.evaluatePolicy(input());
|
|
expect(decision.decision).toBe("deny");
|
|
});
|
|
|
|
it("FAILS CLOSED: CL0UD unreachable → SiaxError, never an implicit allow (§52)", async () => {
|
|
server = await startContractServer([
|
|
{ method: "POST", path: "/api/policy/evaluate", status: 503, respond: () => ({ error: { code: "DOWN" } }) },
|
|
]);
|
|
const client = createCl0udClient(server.baseUrl);
|
|
try {
|
|
await client.evaluatePolicy(input());
|
|
expect.unreachable("must throw");
|
|
} catch (err) {
|
|
expect(err).toBeInstanceOf(SiaxError);
|
|
expect((err as SiaxError).code).toBe("DEPENDENCY_UNAVAILABLE");
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("Cl0udClient wire contract — capability discovery", () => {
|
|
it("GETs /api/capabilities with filters and returns the data array", async () => {
|
|
server = await startContractServer([
|
|
{
|
|
method: "GET",
|
|
path: "/api/capabilities",
|
|
status: 200,
|
|
respond: () => ({
|
|
data: [
|
|
{
|
|
name: "0pt.search.query",
|
|
version: "1.0.0",
|
|
owner: "0pt",
|
|
status: "active",
|
|
requiredEntitlement: "0pt.search",
|
|
surfaces: ["api"],
|
|
},
|
|
],
|
|
meta: { total: 1 },
|
|
}),
|
|
},
|
|
]);
|
|
const client = createCl0udClient(server.baseUrl);
|
|
const caps = await client.listCapabilities({ owner: "0pt", status: "active" });
|
|
expect(server.requests[0].query).toEqual({ owner: "0pt", status: "active" });
|
|
expect(caps).toHaveLength(1);
|
|
expect(caps[0].name).toBe("0pt.search.query");
|
|
expect(caps[0].requiredEntitlement).toBe("0pt.search");
|
|
});
|
|
});
|