Merge registry-envelope-integration into main (resolve conflicts with call-envelope-integration)
This commit is contained in:
@@ -31,6 +31,7 @@ describe("PendingRequestMap", () => {
|
||||
}, 10);
|
||||
|
||||
const result = await callPromise;
|
||||
expect(isResponseEnvelope(result)).toBe(true);
|
||||
expect(result.meta.source).toBe("local");
|
||||
expect(result.data).toEqual({ result: "world" });
|
||||
});
|
||||
@@ -59,6 +60,11 @@ describe("PendingRequestMap", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("respond() throws when called with non-envelope value", () => {
|
||||
const map = new PendingRequestMap();
|
||||
expect(() => map.respond("req-1", { result: "world" } as any)).toThrow("ResponseEnvelope");
|
||||
});
|
||||
|
||||
it("call() rejects when emitError() is called", async () => {
|
||||
const map = new PendingRequestMap();
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { describe, it, expect } from "vitest";
|
||||
import { OperationRegistry, OperationType, buildEnv, type IOperationDefinition, type OperationContext } from "../src/index.js";
|
||||
import * as Type from "@alkdev/typebox";
|
||||
import { PendingRequestMap } from "../src/call.js";
|
||||
import { localEnvelope, isResponseEnvelope, type ResponseEnvelope } from "../src/response-envelope.js";
|
||||
|
||||
function makeOperation(name: string, handler?: any): IOperationDefinition {
|
||||
return {
|
||||
@@ -33,7 +34,9 @@ describe("buildEnv", () => {
|
||||
expect(typeof env.test.writeFile).toBe("function");
|
||||
|
||||
const result = await env.test.readFile({ value: "test" });
|
||||
expect(result).toEqual({ result: "test" });
|
||||
expect(isResponseEnvelope(result)).toBe(true);
|
||||
expect(result.meta.source).toBe("local");
|
||||
expect(result.data).toEqual({ result: "test" });
|
||||
});
|
||||
|
||||
it("filters out SUBSCRIPTION operations", () => {
|
||||
@@ -76,8 +79,8 @@ describe("buildEnv", () => {
|
||||
registry.register(makeOperation("readFile"));
|
||||
|
||||
const callMap = {
|
||||
call: async (opId: string, input: unknown, opts?: any) => {
|
||||
return { result: `routed: ${opId}` };
|
||||
call: async (opId: string, input: unknown, opts?: any): Promise<ResponseEnvelope> => {
|
||||
return localEnvelope({ result: `routed: ${opId}` }, opId);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -88,6 +91,7 @@ describe("buildEnv", () => {
|
||||
});
|
||||
|
||||
const result = await env.test.readFile({ value: "test" });
|
||||
expect(result).toEqual({ result: "routed: test.readFile" });
|
||||
expect(isResponseEnvelope(result)).toBe(true);
|
||||
expect(result.data).toEqual({ result: "routed: test.readFile" });
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
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 * as Type from "@alkdev/typebox";
|
||||
import { Value } from "@alkdev/typebox/value";
|
||||
|
||||
@@ -89,11 +90,64 @@ describe("OperationRegistry", () => {
|
||||
expect(registry.getAllSpecs()).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("executes an operation and validates input", async () => {
|
||||
it("executes an operation and returns ResponseEnvelope", async () => {
|
||||
const registry = new OperationRegistry();
|
||||
registry.register(makeOperation());
|
||||
const result = await registry.execute("test.testOp", { value: "hello" }, {} as OperationContext);
|
||||
expect(result).toEqual({ result: "processed: hello" });
|
||||
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 () => {
|
||||
@@ -119,15 +173,6 @@ describe("OperationRegistry", () => {
|
||||
).rejects.toThrow("No handler registered");
|
||||
});
|
||||
|
||||
it("warns on output mismatch but returns result", async () => {
|
||||
const registry = new OperationRegistry();
|
||||
registry.register(makeOperation({
|
||||
handler: async () => ({ unexpected: "field" }),
|
||||
}));
|
||||
const result = await registry.execute("test.testOp", { value: "x" }, {} as OperationContext);
|
||||
expect(result).toEqual({ unexpected: "field" });
|
||||
});
|
||||
|
||||
it("registerSpec and registerHandler separately", async () => {
|
||||
const registry = new OperationRegistry();
|
||||
const spec = makeSpec();
|
||||
@@ -138,8 +183,9 @@ describe("OperationRegistry", () => {
|
||||
expect(retrieved.name).toBe("testOp");
|
||||
expect(retrieved.handler).toBeDefined();
|
||||
|
||||
const result = await registry.execute("test.testOp", { value: "hello" }, {} as OperationContext);
|
||||
expect(result).toEqual({ result: "processed: hello" });
|
||||
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", () => {
|
||||
|
||||
Reference in New Issue
Block a user