Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/plugin-runtime/src/capability-gateway.coverage.test.ts
T
2026-07-08 16:15:37 +02:00

159 lines
6.2 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { CapabilityDeniedError, createCapabilityGateway } from "./capability-gateway.js";
import type { PluginManifest } from "./manifest.js";
/**
* Coverage for capability-gateway branches not hit by capability-gateway.test.ts:
* - an available host binding (log/audit) declared + granted but with no
* host-fn wired (the typeof-fn guard, async + sync),
* - invokeSync grant-gated paths (not_granted_for_tenant, grant happy path),
* - payload-descriptor shapes for object / array / null arguments,
* - non-Error throws surfacing errorCode "unknown".
*/
function fixture(capabilities: PluginManifest["capabilities"] = []): PluginManifest {
return {
schemaVersion: 1,
id: "de.hivegrid.test",
version: "1.0.0",
name: "Test",
entry: "plugin.wasm",
wasmSha256: "0".repeat(64),
signature: "0".repeat(128),
signedBy: "0".repeat(64),
allowedHosts: [],
capabilities,
hooks: [{ name: "x", export: "x" }],
};
}
describe("createCapabilityGateway — additional branches", () => {
test("async invoke denies an available, granted capability with no host-fn wired", async () => {
const calls: Array<{ capability: string; outcome: string; errorCode?: string }> = [];
const gateway = createCapabilityGateway({
// "log" IS host-binding-available, so the deny falls through to the
// typeof-fn guard rather than the binding-availability guard.
manifest: fixture([{ name: "log" }]),
approvedGrants: [{ capabilityName: "log", scopeJson: null }],
hostFns: {},
onCall: (info) => calls.push(info),
});
await expect(gateway.invoke("log")).rejects.toMatchObject({
reason: "no_host_implementation",
});
expect(calls).toEqual([
expect.objectContaining({ capability: "log", outcome: "denied", errorCode: "no_host_implementation" }),
]);
});
test("invokeSync denies an available, granted capability with no host-fn wired", () => {
const gateway = createCapabilityGateway({
manifest: fixture([{ name: "audit" }]),
approvedGrants: [{ capabilityName: "audit", scopeJson: null }],
hostFns: {},
});
expect(() => gateway.invokeSync("audit")).toThrow(CapabilityDeniedError);
expect(() => gateway.invokeSync("audit")).toThrow(/no_host_implementation/u);
});
test("invokeSync denies a declared capability without a tenant grant", () => {
const calls: Array<{ capability: string; outcome: string; errorCode?: string }> = [];
const gateway = createCapabilityGateway({
manifest: fixture([{ name: "log" }, { name: "audit" }]),
approvedGrants: [{ capabilityName: "log", scopeJson: null }],
hostFns: { log: () => "ok", audit: () => "should not run" },
onCall: (info) => calls.push(info),
});
expect(() => gateway.invokeSync("audit")).toThrow(CapabilityDeniedError);
expect(calls).toEqual([
expect.objectContaining({ capability: "audit", outcome: "denied", errorCode: "not_granted_for_tenant" }),
]);
});
test("invokeSync runs the scope check for a granted, unscoped capability and records the call", () => {
const calls: Array<Record<string, unknown>> = [];
const gateway = createCapabilityGateway({
manifest: fixture([{ name: "log" }]),
approvedGrants: [{ capabilityName: "log", scopeJson: null }],
hostFns: { log: (payload: unknown) => ({ echoed: payload }) },
onCall: (info) => calls.push(info),
});
// Object argument exercises the object-shaped payload descriptor.
const result = gateway.invokeSync<{ echoed: unknown }>("log", { action: "ping", retries: 2 });
expect(result).toEqual({ echoed: { action: "ping", retries: 2 } });
expect(gateway.fuelCharged()).toBe(100);
expect(calls[0]?.outcome).toBe("success");
expect(calls[0]?.payloadHash).toMatch(/^[a-f0-9]{64}$/u);
// Raw argument values must never leak into the audit record.
expect(JSON.stringify(calls)).not.toContain("ping");
});
test("payload descriptor distinguishes object, array and null argument shapes", async () => {
const calls: Array<{ payloadHash?: string }> = [];
const gateway = createCapabilityGateway({
manifest: fixture([{ name: "log" }]),
hostFns: { log: () => "ok" },
onCall: (info) => calls.push(info),
});
await gateway.invoke("log", { a: 1, b: 2 });
await gateway.invoke("log", { b: 2, a: 1 }); // key order must not matter
await gateway.invoke("log", [1, 2, 3]);
await gateway.invoke("log", null);
expect(calls).toHaveLength(4);
// Object key ordering is normalised → identical hash.
expect(calls[1]?.payloadHash).toBe(calls[0]?.payloadHash);
// Object vs array vs null produce distinct descriptors.
expect(calls[2]?.payloadHash).not.toBe(calls[0]?.payloadHash);
expect(calls[3]?.payloadHash).not.toBe(calls[0]?.payloadHash);
for (const call of calls) {
expect(call.payloadHash).toMatch(/^[a-f0-9]{64}$/u);
}
});
test("async host-fn throwing a non-Error reports errorCode 'unknown'", async () => {
const calls: Array<{ outcome: string; errorCode?: string }> = [];
const gateway = createCapabilityGateway({
manifest: fixture([{ name: "log" }]),
hostFns: {
log: () => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw "raw string failure";
},
},
onCall: (info) => calls.push(info),
});
await expect(gateway.invoke("log")).rejects.toBe("raw string failure");
expect(calls).toEqual([expect.objectContaining({ outcome: "error", errorCode: "unknown" })]);
});
test("sync host-fn throwing a non-Error reports errorCode 'unknown'", () => {
const calls: Array<{ outcome: string; errorCode?: string }> = [];
const gateway = createCapabilityGateway({
manifest: fixture([{ name: "log" }]),
hostFns: {
log: () => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw 42;
},
},
onCall: (info) => calls.push(info),
});
let thrown: unknown;
try {
gateway.invokeSync("log");
} catch (err) {
thrown = err;
}
expect(thrown).toBe(42);
expect(calls).toEqual([expect.objectContaining({ outcome: "error", errorCode: "unknown" })]);
});
});