M-01: Compose OperationDefinitionSchema from OperationSpecSchema via Type.Intersect M-02: Extract from_openapi to subpath export, remove from main entry M-05: Fix mapError fragile includes() matching — use startsWith(code+':') or exact M-06: Replace any casts with MCPClientLike/MCPToolResult interfaces in from_mcp M-07: Add injectable fetch to HTTPServiceConfig for from_openapi M-08: Add OpenAPIServiceRegistry with lifecycle methods (add, remove, registerAll) L-01: Validate subscription handler type at registration and runtime L-02: Strengthen isResponseEnvelope with source-specific field validation L-03: Add logger.warn on FromSchema fallback to Type.Unknown L-04: Noted as intentional (SSE GET body handling) L-05: Add registerAll to MCPClientLoader and OpenAPIServiceRegistry
512 lines
19 KiB
TypeScript
512 lines
19 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { OperationRegistry } from "../src/registry.js";
|
|
import { OperationType, type IOperationDefinition, type OperationContext, type OperationSpec, type OperationHandler } from "../src/index.js";
|
|
import { isResponseEnvelope, localEnvelope, type ResponseEnvelope } from "../src/response-envelope.js";
|
|
import { CallError, InfrastructureErrorCode } from "../src/error.js";
|
|
import * as Type from "@alkdev/typebox";
|
|
import { Value } from "@alkdev/typebox/value";
|
|
|
|
function makeOperation(overrides: Partial<IOperationDefinition> = {}): IOperationDefinition {
|
|
return {
|
|
name: "testOp",
|
|
namespace: "test",
|
|
version: "1.0.0",
|
|
type: OperationType.QUERY,
|
|
description: "A test operation",
|
|
inputSchema: Type.Object({ value: Type.String() }),
|
|
outputSchema: Type.Object({ result: Type.String() }),
|
|
accessControl: { requiredScopes: [] },
|
|
handler: async (input: any) => ({ result: `processed: ${input.value}` }),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function makeSpec(overrides: Partial<OperationSpec> = {}): OperationSpec {
|
|
return {
|
|
name: "testOp",
|
|
namespace: "test",
|
|
version: "1.0.0",
|
|
type: OperationType.QUERY,
|
|
description: "A test operation",
|
|
inputSchema: Type.Object({ value: Type.String() }),
|
|
outputSchema: Type.Object({ result: Type.String() }),
|
|
accessControl: { requiredScopes: [] },
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
const testHandler: OperationHandler = async (input: any) => ({ result: `processed: ${input.value}` });
|
|
|
|
describe("OperationRegistry", () => {
|
|
it("registers and retrieves an operation", () => {
|
|
const registry = new OperationRegistry();
|
|
const op = makeOperation();
|
|
registry.register(op);
|
|
const retrieved = registry.get("test.testOp")!;
|
|
expect(retrieved).toStrictEqual(op);
|
|
expect(retrieved.name).toBe("testOp");
|
|
expect(retrieved.handler).toBeDefined();
|
|
});
|
|
|
|
it("retrieves by namespace and name", () => {
|
|
const registry = new OperationRegistry();
|
|
const op = makeOperation();
|
|
registry.register(op);
|
|
const retrieved = registry.getByName("test", "testOp")!;
|
|
expect(retrieved).toStrictEqual(op);
|
|
expect(retrieved.name).toBe("testOp");
|
|
});
|
|
|
|
it("returns undefined for missing operations", () => {
|
|
const registry = new OperationRegistry();
|
|
expect(registry.get("nonexistent.op")).toBeUndefined();
|
|
});
|
|
|
|
it("lists all registered operations", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation());
|
|
registry.register(makeOperation({ name: "op2" }));
|
|
expect(registry.list()).toHaveLength(2);
|
|
});
|
|
|
|
it("registerAll registers multiple operations", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.registerAll([makeOperation(), makeOperation({ name: "op2" })]);
|
|
expect(registry.list()).toHaveLength(2);
|
|
});
|
|
|
|
it("extracts spec without handler", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation());
|
|
const spec = registry.getSpec("test.testOp")!;
|
|
expect(spec).toBeDefined();
|
|
expect((spec as any).handler).toBeUndefined();
|
|
expect(spec.name).toBe("testOp");
|
|
});
|
|
|
|
it("getAllSpecs returns all specs", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation());
|
|
registry.register(makeOperation({ name: "op2" }));
|
|
expect(registry.getAllSpecs()).toHaveLength(2);
|
|
});
|
|
|
|
it("executes an operation and returns ResponseEnvelope", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation());
|
|
const envelope = await registry.execute("test.testOp", { value: "hello" }, {} as OperationContext);
|
|
expect(isResponseEnvelope(envelope)).toBe(true);
|
|
expect(envelope.meta.source).toBe("local");
|
|
expect((envelope.meta as any).operationId).toBe("test.testOp");
|
|
expect(envelope.data).toEqual({ result: "processed: hello" });
|
|
});
|
|
|
|
it("wraps raw handler result in localEnvelope", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation());
|
|
const envelope = await registry.execute("test.testOp", { value: "hello" }, {} as OperationContext);
|
|
expect(envelope.meta.source).toBe("local");
|
|
expect(envelope.data).toEqual({ result: "processed: hello" });
|
|
expect(typeof (envelope.meta as any).timestamp).toBe("number");
|
|
});
|
|
|
|
it("passes through pre-built ResponseEnvelope from handler", async () => {
|
|
const registry = new OperationRegistry();
|
|
const preBuilt = localEnvelope({ custom: "data" }, "custom.op");
|
|
registry.register(makeOperation({
|
|
handler: async () => preBuilt,
|
|
}));
|
|
const envelope = await registry.execute("test.testOp", { value: "x" }, {} as OperationContext);
|
|
expect(envelope).toBe(preBuilt);
|
|
});
|
|
|
|
it("normalizes output with Value.Cast when outputSchema is not Unknown", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
outputSchema: Type.Object({ result: Type.String(), count: Type.Number() }),
|
|
handler: async () => ({ result: "ok" }),
|
|
}));
|
|
const envelope = await registry.execute("test.testOp", { value: "x" }, {} as OperationContext);
|
|
expect((envelope.data as any).result).toBe("ok");
|
|
expect((envelope.data as any).count).toBe(0);
|
|
});
|
|
|
|
it("does not normalize when outputSchema is Unknown", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
outputSchema: Type.Unknown(),
|
|
handler: async () => ({ anything: "goes", extra: 42 }),
|
|
}));
|
|
const envelope = await registry.execute("test.testOp", { value: "x" }, {} as OperationContext);
|
|
expect(envelope.data).toEqual({ anything: "goes", extra: 42 });
|
|
});
|
|
|
|
it("normalizes mismatched output via Value.Cast and returns envelope", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
handler: async () => ({ unexpected: "field" }),
|
|
}));
|
|
const envelope = await registry.execute("test.testOp", { value: "x" }, {} as OperationContext);
|
|
expect(isResponseEnvelope(envelope)).toBe(true);
|
|
expect((envelope.data as any).result).toBe("");
|
|
});
|
|
|
|
it("throws on invalid input", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation());
|
|
await expect(
|
|
registry.execute("test.testOp", { wrong: "field" }, {} as OperationContext)
|
|
).rejects.toThrow();
|
|
});
|
|
|
|
it("throws CallError on missing operation", async () => {
|
|
const registry = new OperationRegistry();
|
|
try {
|
|
await registry.execute("missing.op", {}, {} as OperationContext);
|
|
expect.fail("Should have thrown");
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(CallError);
|
|
expect((error as CallError).code).toBe(InfrastructureErrorCode.OPERATION_NOT_FOUND);
|
|
}
|
|
});
|
|
|
|
it("throws CallError on missing handler", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.registerSpec(makeSpec());
|
|
try {
|
|
await registry.execute("test.testOp", { value: "hello" }, {} as OperationContext);
|
|
expect.fail("Should have thrown");
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(CallError);
|
|
expect((error as CallError).code).toBe(InfrastructureErrorCode.OPERATION_NOT_FOUND);
|
|
}
|
|
});
|
|
|
|
it("registerSpec and registerHandler separately", async () => {
|
|
const registry = new OperationRegistry();
|
|
const spec = makeSpec();
|
|
registry.registerSpec(spec);
|
|
registry.registerHandler("test.testOp", testHandler);
|
|
|
|
const retrieved = registry.get("test.testOp")!;
|
|
expect(retrieved.name).toBe("testOp");
|
|
expect(retrieved.handler).toBeDefined();
|
|
|
|
const envelope = await registry.execute("test.testOp", { value: "hello" }, {} as OperationContext);
|
|
expect(envelope.data).toEqual({ result: "processed: hello" });
|
|
expect(envelope.meta.source).toBe("local");
|
|
});
|
|
|
|
it("registerHandler throws for unknown operation", () => {
|
|
const registry = new OperationRegistry();
|
|
expect(() => registry.registerHandler("unknown.op", testHandler)).toThrow("Cannot register handler for unknown operation");
|
|
});
|
|
|
|
it("getHandler returns handler", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation());
|
|
expect(registry.getHandler("test.testOp")).toBeDefined();
|
|
});
|
|
|
|
it("getHandler returns undefined for spec-only registration", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.registerSpec(makeSpec());
|
|
expect(registry.getHandler("test.testOp")).toBeUndefined();
|
|
});
|
|
|
|
it("register with spec-only (no handler)", () => {
|
|
const registry = new OperationRegistry();
|
|
const spec = makeSpec();
|
|
registry.register(spec);
|
|
const retrieved = registry.get("test.testOp")!;
|
|
expect(retrieved.name).toBe("testOp");
|
|
expect(retrieved.handler).toBeUndefined();
|
|
});
|
|
|
|
it("getSpec returns spec without handler after combined register", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation());
|
|
const spec = registry.getSpec("test.testOp")!;
|
|
expect(spec.name).toBe("testOp");
|
|
expect((spec as any).handler).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("OperationRegistry access control", () => {
|
|
it("denies access when requiredScopes are set and no identity provided", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: ["admin"] },
|
|
}));
|
|
|
|
try {
|
|
await registry.execute("test.testOp", { value: "hello" }, {} as OperationContext);
|
|
expect.fail("Should have thrown");
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(CallError);
|
|
expect((error as CallError).code).toBe(InfrastructureErrorCode.ACCESS_DENIED);
|
|
}
|
|
});
|
|
|
|
it("denies access when identity lacks required scopes", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: ["admin", "write"] },
|
|
}));
|
|
|
|
const context: OperationContext = {
|
|
identity: { id: "user1", scopes: ["read"] },
|
|
};
|
|
|
|
try {
|
|
await registry.execute("test.testOp", { value: "hello" }, context);
|
|
expect.fail("Should have thrown");
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(CallError);
|
|
expect((error as CallError).code).toBe(InfrastructureErrorCode.ACCESS_DENIED);
|
|
}
|
|
});
|
|
|
|
it("grants access when identity has all required scopes", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: ["read", "write"] },
|
|
}));
|
|
|
|
const context: OperationContext = {
|
|
identity: { id: "user1", scopes: ["read", "write", "admin"] },
|
|
};
|
|
|
|
const envelope = await registry.execute("test.testOp", { value: "hello" }, context);
|
|
expect(envelope.data).toEqual({ result: "processed: hello" });
|
|
});
|
|
|
|
it("grants access when no scopes are required", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: [] },
|
|
}));
|
|
|
|
const envelope = await registry.execute("test.testOp", { value: "hello" }, {} as OperationContext);
|
|
expect(envelope.data).toEqual({ result: "processed: hello" });
|
|
});
|
|
|
|
it("denies access when requiredScopesAny requires at least one scope and identity has none", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: [], requiredScopesAny: ["admin", "write"] },
|
|
}));
|
|
|
|
const context: OperationContext = {
|
|
identity: { id: "user1", scopes: ["read"] },
|
|
};
|
|
|
|
try {
|
|
await registry.execute("test.testOp", { value: "hello" }, context);
|
|
expect.fail("Should have thrown");
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(CallError);
|
|
expect((error as CallError).code).toBe(InfrastructureErrorCode.ACCESS_DENIED);
|
|
}
|
|
});
|
|
|
|
it("grants access when requiredScopesAny and identity has one matching scope", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: [], requiredScopesAny: ["admin", "write"] },
|
|
}));
|
|
|
|
const context: OperationContext = {
|
|
identity: { id: "user1", scopes: ["write"] },
|
|
};
|
|
|
|
const envelope = await registry.execute("test.testOp", { value: "hello" }, context);
|
|
expect(envelope.data).toEqual({ result: "processed: hello" });
|
|
});
|
|
|
|
it("denies access when resourceType/resourceAction are set and identity has no resources", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: [], resourceType: "project", resourceAction: "read" },
|
|
}));
|
|
|
|
const context: OperationContext = {
|
|
identity: { id: "user1", scopes: [] },
|
|
};
|
|
|
|
try {
|
|
await registry.execute("test.testOp", { value: "hello" }, context);
|
|
expect.fail("Should have thrown");
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(CallError);
|
|
expect((error as CallError).code).toBe(InfrastructureErrorCode.ACCESS_DENIED);
|
|
}
|
|
});
|
|
|
|
it("grants access when resourceType/resourceAction match identity resources", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: [], resourceType: "project", resourceAction: "read" },
|
|
}));
|
|
|
|
const context: OperationContext = {
|
|
identity: { id: "user1", scopes: [], resources: { "project:abc": ["read", "write"] } },
|
|
};
|
|
|
|
const envelope = await registry.execute("test.testOp", { value: "hello" }, context);
|
|
expect(envelope.data).toEqual({ result: "processed: hello" });
|
|
});
|
|
|
|
it("skips access control when context.trusted is true", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: ["admin"] },
|
|
}));
|
|
|
|
const context: OperationContext = {
|
|
identity: { id: "user1", scopes: ["read"] },
|
|
trusted: true,
|
|
};
|
|
|
|
const envelope = await registry.execute("test.testOp", { value: "hello" }, context);
|
|
expect(envelope.data).toEqual({ result: "processed: hello" });
|
|
});
|
|
|
|
it("skips access control when context.trusted is true even without identity", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: ["admin"] },
|
|
}));
|
|
|
|
const context: OperationContext = { trusted: true };
|
|
|
|
const envelope = await registry.execute("test.testOp", { value: "hello" }, context);
|
|
expect(envelope.data).toEqual({ result: "processed: hello" });
|
|
});
|
|
|
|
it("denies access when identity is missing but requiredScopes are set", async () => {
|
|
const registry = new OperationRegistry();
|
|
registry.register(makeOperation({
|
|
accessControl: { requiredScopes: ["read"] },
|
|
}));
|
|
|
|
const context: OperationContext = {};
|
|
|
|
try {
|
|
await registry.execute("test.testOp", { value: "hello" }, context);
|
|
expect.fail("Should have thrown");
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(CallError);
|
|
expect((error as CallError).code).toBe(InfrastructureErrorCode.ACCESS_DENIED);
|
|
expect((error as CallError).message).toContain("identity required");
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("OperationRegistry subscription handler validation", () => {
|
|
it("rejects non-async-generator handler for SUBSCRIPTION via registerHandler", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.registerSpec({
|
|
name: "badSub",
|
|
namespace: "test",
|
|
version: "1.0.0",
|
|
type: OperationType.SUBSCRIPTION,
|
|
description: "bad sub",
|
|
inputSchema: Type.Object({}),
|
|
outputSchema: Type.Unknown(),
|
|
accessControl: { requiredScopes: [] },
|
|
});
|
|
const regularAsyncFn = async () => "not a generator";
|
|
expect(() => registry.registerHandler("test.badSub", regularAsyncFn as any)).toThrow(
|
|
/must be an async generator function/i,
|
|
);
|
|
});
|
|
|
|
it("rejects synchronous function handler for SUBSCRIPTION via registerHandler", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.registerSpec({
|
|
name: "syncSub",
|
|
namespace: "test",
|
|
version: "1.0.0",
|
|
type: OperationType.SUBSCRIPTION,
|
|
description: "sync sub",
|
|
inputSchema: Type.Object({}),
|
|
outputSchema: Type.Unknown(),
|
|
accessControl: { requiredScopes: [] },
|
|
});
|
|
expect(() => registry.registerHandler("test.syncSub", (() => {}) as any)).toThrow(
|
|
/must be an async generator function/i,
|
|
);
|
|
});
|
|
|
|
it("allows async generator function handler for SUBSCRIPTION via registerHandler", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.registerSpec({
|
|
name: "goodSub",
|
|
namespace: "test",
|
|
version: "1.0.0",
|
|
type: OperationType.SUBSCRIPTION,
|
|
description: "good sub",
|
|
inputSchema: Type.Object({}),
|
|
outputSchema: Type.Unknown(),
|
|
accessControl: { requiredScopes: [] },
|
|
});
|
|
async function* goodHandler(_input: unknown, _context: any) {
|
|
yield "event";
|
|
}
|
|
expect(() => registry.registerHandler("test.goodSub", goodHandler as any)).not.toThrow();
|
|
});
|
|
|
|
it("rejects non-async-generator handler for SUBSCRIPTION via register", () => {
|
|
const registry = new OperationRegistry();
|
|
expect(() =>
|
|
registry.register({
|
|
name: "badRegSub",
|
|
namespace: "test",
|
|
version: "1.0.0",
|
|
type: OperationType.SUBSCRIPTION,
|
|
description: "bad sub via register",
|
|
inputSchema: Type.Object({}),
|
|
outputSchema: Type.Unknown(),
|
|
accessControl: { requiredScopes: [] },
|
|
handler: async () => "not a generator" as any,
|
|
}),
|
|
).toThrow(/must be an async generator function/i);
|
|
});
|
|
|
|
it("allows async generator handler for SUBSCRIPTION via register", () => {
|
|
const registry = new OperationRegistry();
|
|
async function* handler(_input: unknown, _context: any) {
|
|
yield "event";
|
|
}
|
|
expect(() =>
|
|
registry.register({
|
|
name: "goodRegSub",
|
|
namespace: "test",
|
|
version: "1.0.0",
|
|
type: OperationType.SUBSCRIPTION,
|
|
description: "good sub via register",
|
|
inputSchema: Type.Object({}),
|
|
outputSchema: Type.Unknown(),
|
|
accessControl: { requiredScopes: [] },
|
|
handler,
|
|
}),
|
|
).not.toThrow();
|
|
});
|
|
|
|
it("allows regular async handler for QUERY via registerHandler", () => {
|
|
const registry = new OperationRegistry();
|
|
registry.registerSpec({
|
|
name: "queryOp",
|
|
namespace: "test",
|
|
version: "1.0.0",
|
|
type: OperationType.QUERY,
|
|
description: "query op",
|
|
inputSchema: Type.Object({}),
|
|
outputSchema: Type.Unknown(),
|
|
accessControl: { requiredScopes: [] },
|
|
});
|
|
const handler = async (_input: unknown, _context: any) => ({ result: "ok" });
|
|
expect(() => registry.registerHandler("test.queryOp", handler as any)).not.toThrow();
|
|
});
|
|
}); |