Skip to content

Commit 5121f30

Browse files
samzongsteipete
authored andcommitted
fix(gateway): lazy compile protocol validators
Signed-off-by: samzong <[email protected]>
1 parent 28f59a9 commit 5121f30

2 files changed

Lines changed: 302 additions & 167 deletions

File tree

‎src/gateway/protocol/index.test.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import type { ErrorObject } from "ajv";
1+
import AjvPkg, { type ErrorObject } from "ajv";
22
import { describe, expect, it } from "vitest";
33
import { TALK_TEST_PROVIDER_ID } from "../../test-utils/talk-test-provider.js";
4+
import * as protocol from "./index.js";
45
import {
56
formatValidationErrors,
67
validateChatEvent,
8+
validateCommandsListParams,
9+
validateConnectParams,
710
validateModelsListParams,
811
validateNodeEventResult,
912
validateNodePairRequestParams,
@@ -35,6 +38,71 @@ const makeError = (overrides: Partial<ErrorObject>): ErrorObject => ({
3538
...overrides,
3639
});
3740

41+
type CompileMethod = (schema: unknown, meta?: boolean) => unknown;
42+
type ProtocolValidator = (value: unknown) => boolean;
43+
44+
describe("lazy protocol validators", () => {
45+
it("compiles on first use and reuses the compiled validator", () => {
46+
const ajvPrototype = (AjvPkg as unknown as { prototype: { compile: CompileMethod } }).prototype;
47+
const originalCompile = ajvPrototype.compile;
48+
let compileCalls = 0;
49+
50+
ajvPrototype.compile = function (this: unknown, schema: unknown, meta?: boolean) {
51+
compileCalls += 1;
52+
return originalCompile.call(this, schema, meta);
53+
};
54+
55+
try {
56+
expect(compileCalls).toBe(0);
57+
expect(validateCommandsListParams({})).toBe(true);
58+
expect(compileCalls).toBe(1);
59+
expect(validateCommandsListParams({ includeArgs: true })).toBe(true);
60+
expect(compileCalls).toBe(1);
61+
} finally {
62+
ajvPrototype.compile = originalCompile;
63+
}
64+
});
65+
66+
it("keeps validation errors readable on the exported validator", () => {
67+
expect(validateConnectParams({})).toBe(false);
68+
expect(formatValidationErrors(validateConnectParams.errors)).toContain("must have required");
69+
70+
expect(
71+
validateConnectParams({
72+
minProtocol: 1,
73+
maxProtocol: 1,
74+
client: {
75+
id: "test",
76+
version: "1.0.0",
77+
platform: "test",
78+
mode: "test",
79+
},
80+
}),
81+
).toBe(true);
82+
expect(validateConnectParams.errors).toBeNull();
83+
});
84+
85+
it("can still compile every exported protocol validator", () => {
86+
const failures: string[] = [];
87+
const validators: Array<[string, ProtocolValidator]> = [];
88+
for (const [name, value] of Object.entries(protocol)) {
89+
if (name.startsWith("validate") && typeof value === "function") {
90+
validators.push([name, value as ProtocolValidator]);
91+
}
92+
}
93+
94+
expect(validators.length).toBeGreaterThan(150);
95+
for (const [name, validate] of validators) {
96+
try {
97+
validate(undefined);
98+
} catch (err) {
99+
failures.push(`${name}: ${err instanceof Error ? err.message : String(err)}`);
100+
}
101+
}
102+
expect(failures).toEqual([]);
103+
});
104+
});
105+
38106
describe("formatValidationErrors", () => {
39107
it("returns unknown validation error when missing errors", () => {
40108
expect(formatValidationErrors(undefined)).toBe("unknown validation error");

0 commit comments

Comments
 (0)