forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypescript-codegen.test.ts
More file actions
271 lines (254 loc) · 8.73 KB
/
Copy pathtypescript-codegen.test.ts
File metadata and controls
271 lines (254 loc) · 8.73 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import type { JSONSchema7 } from "json-schema";
import { compile } from "json-schema-to-typescript";
import { describe, expect, it } from "vitest";
import {
assertNoPublicInternalReferences,
filterPublicSessionEventVariants,
normalizeSchemaForTypeScript,
} from "../../scripts/codegen/typescript.ts";
import type { DefinitionCollections } from "../../scripts/codegen/utils.ts";
describe("typescript schema codegen", () => {
it("emits JSDoc comments for described enum values", async () => {
const schema: JSONSchema7 = {
title: "SyntheticOptions",
type: "object",
additionalProperties: false,
properties: {
namedMode: {
title: "SyntheticMode",
type: "string",
enum: ["alpha", "beta"],
description: "Synthetic mode.",
"x-enumDescriptions": {
alpha: "Use alpha mode.",
},
},
inlineMode: {
type: "string",
enum: ["direct", "indirect"],
description: "Inline mode.",
"x-enumDescriptions": {
direct: "Use a direct value.",
},
},
},
required: ["namedMode", "inlineMode"],
};
const code = await compile(normalizeSchemaForTypeScript(schema), "SyntheticOptions", {
bannerComment: "",
style: { semi: true, singleQuote: false },
additionalProperties: false,
});
expect(code).toContain(
'export type SyntheticMode = /** Use alpha mode. */ "alpha" | "beta";'
);
expect(code).toContain('inlineMode: /** Use a direct value. */ "direct" | "indirect";');
});
});
describe("filterPublicSessionEventVariants", () => {
const makeCollections = (defs: Record<string, JSONSchema7>): DefinitionCollections => ({
definitions: defs,
$defs: {},
});
it("keeps public union arms", () => {
const defs = {
PublicEvent: { type: "object" as const, properties: { type: { const: "pub" } } },
};
const variants: JSONSchema7[] = [{ $ref: "#/definitions/PublicEvent" }];
const { publicVariants, excludedDefinitionNames } = filterPublicSessionEventVariants(
variants,
makeCollections(defs)
);
expect(publicVariants).toHaveLength(1);
expect(excludedDefinitionNames.size).toBe(0);
});
it("excludes arms whose arm object is marked visibility:internal", () => {
const defs = {
InternalEvent: {
type: "object" as const,
visibility: "internal",
properties: { type: { const: "internal.evt" } },
} as JSONSchema7 & { visibility: string },
};
const variants: JSONSchema7[] = [
{ $ref: "#/definitions/InternalEvent", visibility: "internal" } as JSONSchema7 & {
visibility: string;
},
];
const { publicVariants, excludedDefinitionNames } = filterPublicSessionEventVariants(
variants,
makeCollections(defs)
);
expect(publicVariants).toHaveLength(0);
expect(excludedDefinitionNames.has("InternalEvent")).toBe(true);
});
it("excludes arms whose resolved definition is marked visibility:internal", () => {
const defs = {
InternalEvent: {
type: "object" as const,
visibility: "internal",
properties: { type: { const: "internal.evt" } },
} as JSONSchema7 & { visibility: string },
};
// arm object itself is NOT marked, but the resolved definition is
const variants: JSONSchema7[] = [{ $ref: "#/definitions/InternalEvent" }];
const { publicVariants, excludedDefinitionNames } = filterPublicSessionEventVariants(
variants,
makeCollections(defs)
);
expect(publicVariants).toHaveLength(0);
expect(excludedDefinitionNames.has("InternalEvent")).toBe(true);
});
it("excludes arms whose internal data sub-property is the only internal marker (legacy pattern)", () => {
// Event types that carry a `data: InternalData` field — the `data` property is what is
// internal, not the event wrapper type itself.
const defs = {
InternalData: {
type: "object" as const,
visibility: "internal",
} as JSONSchema7 & { visibility: string },
WrapperEvent: {
type: "object" as const,
properties: {
type: { const: "wrapper.evt" },
data: { $ref: "#/definitions/InternalData" },
},
},
};
const variants: JSONSchema7[] = [{ $ref: "#/definitions/WrapperEvent" }];
const { publicVariants, excludedDefinitionNames } = filterPublicSessionEventVariants(
variants,
makeCollections(defs)
);
expect(publicVariants).toHaveLength(0);
expect(excludedDefinitionNames.has("WrapperEvent")).toBe(true);
expect(excludedDefinitionNames.has("InternalData")).toBe(true);
});
});
describe("assertNoPublicInternalReferences", () => {
it("passes when all declarations are public and do not reference internal types", () => {
const ts = `
export interface Foo {
bar: string;
}
export type Bar = "a" | "b";
`;
expect(() => assertNoPublicInternalReferences(ts, new Set(["Hidden"]))).not.toThrow();
});
it("passes when the only reference is from an @internal-tagged declaration", () => {
const ts = `
/** @internal */
export interface Hidden {
x: number;
}
/** @internal */
export interface AlsoInternal {
h: Hidden;
}
export interface Public {
y: string;
}
`;
expect(() => assertNoPublicInternalReferences(ts, new Set(["Hidden"]))).not.toThrow();
});
it("passes when the reference is inside an @internal-tagged member of a public type", () => {
const ts = `
/** @internal */
export interface Hidden {
x: number;
}
export interface Public {
/**
* Some field.
* @internal
*/
secret?: Hidden;
visible: string;
}
`;
expect(() => assertNoPublicInternalReferences(ts, new Set(["Hidden"]))).not.toThrow();
});
it("throws when a public declaration references an internal type directly", () => {
const ts = `
/** @internal */
export interface Hidden {
x: number;
}
export type Event = PublicEvent | Hidden;
`;
expect(() => assertNoPublicInternalReferences(ts, new Set(["Hidden"]))).toThrow(
/Event \(public\) references internal type Hidden/
);
});
it("throws when a public interface member references an internal type", () => {
const ts = `
/** @internal */
export interface Hidden {
x: number;
}
export interface Public {
value: Hidden;
}
`;
expect(() => assertNoPublicInternalReferences(ts, new Set(["Hidden"]))).toThrow(
/Public \(public\) references internal type Hidden/
);
});
it("does not count JSDoc comment text as a code reference", () => {
// The auto-generated JSDoc says 'via the definition "Hidden"' but that is not a
// real TypeScript type reference — it must not trigger the validator.
const ts = `
/** @internal */
export interface Hidden {
x: number;
}
export interface Preceding {
y: string;
}
/**
* This interface was referenced by something.
* via the definition "Hidden".
*/
export interface Following {
z: string;
}
`;
expect(() => assertNoPublicInternalReferences(ts, new Set(["Hidden"]))).not.toThrow();
});
it("does not count inline object-shaped @internal members as public references", () => {
const ts = `
/** @internal */
export interface Hidden {
x: number;
}
export interface Public {
/**
* Some field.
* @internal
*/
secret?: {
[k: string]: Hidden | undefined;
};
visible: string;
}
`;
expect(() => assertNoPublicInternalReferences(ts, new Set(["Hidden"]))).not.toThrow();
});
it("does not count function body references as public type references", () => {
const ts = `
/** @internal */
export interface Hidden {
x: number;
}
/** @internal */
export function doInternal(connection: unknown): void {
connection.onRequest("x", async (params: Hidden) => { return params; });
}
export function doPublic(connection: unknown): void {
connection.onRequest("x", async (params: Hidden) => { return params; });
}
`;
// function body references are stripped — only signature matters
expect(() => assertNoPublicInternalReferences(ts, new Set(["Hidden"]))).not.toThrow();
});
});