-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathclient-api-codegen.test.ts
More file actions
177 lines (163 loc) · 7.08 KB
/
Copy pathclient-api-codegen.test.ts
File metadata and controls
177 lines (163 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { describe, expect, it } from "vitest";
import {
emitClientGlobalApiRegistration as emitTypeScriptClientGlobalApiRegistration,
emitClientSessionApiRegistration as emitTypeScriptClientSessionApiRegistration,
} from "../../scripts/codegen/typescript.ts";
import {
emitClientGlobalApiRegistration as emitGoClientGlobalApiRegistration,
emitClientSessionApiRegistration as emitGoClientSessionApiRegistration,
} from "../../scripts/codegen/go.ts";
import {
emitClientGlobalApiRegistration as emitPythonClientGlobalApiRegistration,
emitClientSessionApiRegistration as emitPythonClientSessionApiRegistration,
} from "../../scripts/codegen/python.ts";
const clientSessionSchema: Record<string, unknown> = {
mixed: {
visible: {
rpcMethod: "mixed.visible",
params: {
type: "object",
title: "VisibleRequest",
properties: {
sessionId: { type: "string" },
},
required: ["sessionId"],
},
result: {
type: "object",
title: "VisibleResult",
properties: {},
},
},
secret: {
rpcMethod: "mixed.secret",
visibility: "internal",
params: {
$ref: "#/definitions/InternalRequest",
},
result: {
$ref: "#/definitions/InternalResult",
},
},
},
internalOnly: {
hidden: {
rpcMethod: "internalOnly.hidden",
visibility: "internal",
params: {
$ref: "#/definitions/InternalRequest",
},
result: {
$ref: "#/definitions/InternalResult",
},
},
},
};
const allInternalClientSessionSchema: Record<string, unknown> = {
internalOnly: clientSessionSchema.internalOnly,
};
function expectOnlyPublicClientSessionHandlers(code: string): void {
expect(code).toContain("mixed.visible");
expect(code).not.toContain("mixed.secret");
expect(code).not.toContain("internalOnly.hidden");
expect(code).not.toContain("InternalRequest");
expect(code).not.toContain("InternalResult");
}
describe("client-session API codegen", () => {
it("excludes internal methods from TypeScript handlers", () => {
const code = emitTypeScriptClientSessionApiRegistration(clientSessionSchema).join("\n");
const allInternalCode = emitTypeScriptClientSessionApiRegistration(
allInternalClientSessionSchema
).join("\n");
expectOnlyPublicClientSessionHandlers(code);
expect(code).not.toContain("InternalOnlyHandler");
expect(allInternalCode).toContain("export interface ClientSessionApiHandlers {");
expect(allInternalCode).toContain("export function registerClientSessionApiHandlers(");
expect(allInternalCode).not.toContain("InternalOnlyHandler");
});
describe("client-global API codegen", () => {
it("keeps internal methods out of global registration", () => {
const code = emitTypeScriptClientGlobalApiRegistration(clientSessionSchema).join("\n");
expectOnlyPublicClientSessionHandlers(code);
expect(code).not.toContain("InternalOnlyHandler");
});
});
describe("client-global host API codegen", () => {
const schema = {
host: {
shutdown: {
rpcMethod: "host.shutdown",
visibility: "internal",
params: { $ref: "#/definitions/HostEmptyResult" },
result: { $ref: "#/definitions/HostEmptyResult" },
},
exited: {
rpcMethod: "host.exited",
params: { $ref: "#/definitions/HostExitedNotification" },
result: { type: "null" },
notification: true,
},
},
};
it("uses named request references for Go child and exit handlers", () => {
const lines: string[] = [];
emitGoClientGlobalApiRegistration(lines, schema, (name) => name, new Map());
const code = lines.join("\n");
expect(code).toContain('"host.exited"');
expect(code).toContain("request *HostEmptyResult");
expect(code).toContain("request *HostExitedNotification");
expect(code).not.toContain("HostShutdownRequest");
});
it("uses named Python exit references and dictionary child shutdown payloads", () => {
const lines: string[] = [];
emitPythonClientGlobalApiRegistration(lines, schema, (name) =>
name === "HostEmptyResult" ? "dict" : name
);
const code = lines.join("\n");
expect(code).toContain("request = HostExitedNotification.from_dict(params)");
expect(code).toContain('"host.exited"');
expect(code).not.toContain("HostExitedRequest");
expect(code).toContain("request = dict(params)");
expect(code).toContain("async def shutdown(self, params: dict) -> dict:");
expect(code).not.toContain("dict.from_dict");
expect(code).not.toContain("HostShutdownResult");
expect(code).not.toContain("HostShutdownRequest");
});
});
it("excludes internal methods from Go handlers", () => {
const lines: string[] = [];
emitGoClientSessionApiRegistration(lines, clientSessionSchema, (name) => name, new Map());
const code = lines.join("\n");
const allInternalLines: string[] = [];
emitGoClientSessionApiRegistration(
allInternalLines,
allInternalClientSessionSchema,
(name) => name,
new Map()
);
const allInternalCode = allInternalLines.join("\n");
expectOnlyPublicClientSessionHandlers(code);
expect(code).not.toContain("InternalOnlyHandler");
expect(allInternalCode).toContain("type ClientSessionAPIHandlers struct {");
expect(allInternalCode).toContain("func RegisterClientSessionAPIHandlers(");
expect(allInternalCode).not.toContain("InternalOnlyHandler");
expect(allInternalCode).not.toContain("clientSessionHandlerError");
});
it("excludes internal methods from Python handlers", () => {
const lines: string[] = [];
emitPythonClientSessionApiRegistration(lines, clientSessionSchema, (name) => name);
const code = lines.join("\n");
const allInternalLines: string[] = [];
emitPythonClientSessionApiRegistration(
allInternalLines,
allInternalClientSessionSchema,
(name) => name
);
const allInternalCode = allInternalLines.join("\n");
expectOnlyPublicClientSessionHandlers(code);
expect(code).not.toContain("InternalOnlyHandler");
expect(allInternalCode).toContain("class ClientSessionApiHandlers:");
expect(allInternalCode).toContain("def register_client_session_api_handlers(");
expect(allInternalCode).not.toContain("InternalOnlyHandler");
});
});