Repository navigation
Expand file tree
/
Copy pathpython-codegen.test.ts
More file actions
70 lines (67 loc) · 2.76 KB
/
Copy pathpython-codegen.test.ts
File metadata and controls
70 lines (67 loc) · 2.76 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
import { describe, expect, it } from "vitest";
import { emitMethod, generatePythonSessionEventsCode } from "../../scripts/codegen/python.ts";
it("deserializes empty host disposal results as dictionaries", () => {
const lines: string[] = [];
emitMethod(
lines,
"dispose",
{
rpcMethod: "host.dispose",
params: {
type: "object",
properties: { hostId: { type: "string" } },
required: ["hostId"],
},
result: { type: "object", properties: {} },
},
false,
(name) => (name === "HostDisposeResult" ? "dict" : name)
);
const code = lines.join("\n");
expect(code).toContain('return dict(await self._client.request("host.dispose"');
expect(code).not.toContain("dict.from_dict");
});
describe("Python root event payload unions", () => {
it.each(["anyOf", "oneOf"] as const)("preserves referenced %s payload variants", (keyword) => {
const code = generatePythonSessionEventsCode({
definitions: {
Payload: {
[keyword]: ["status", "startup", "server_error", "incremental"].map((kind) => ({
type: "object",
properties: { kind: { const: kind }, value: { type: "string" } },
required: ["kind", "value"],
})),
},
SessionEvent: {
anyOf: [
{
type: "object",
properties: {
type: { const: "sample.union" },
data: { $ref: "#/definitions/Payload" },
},
required: ["type", "data"],
},
{
type: "object",
properties: {
type: { const: "sample.empty" },
data: { type: "object", properties: {} },
},
required: ["type", "data"],
},
],
},
},
});
expect(code).toContain("class SampleUnionData:");
expect(code).toContain("kind: SampleUnionDataKind");
expect(code).toContain("value: str");
for (const kind of ["status", "startup", "server_error", "incremental"]) {
expect(code).toContain(`= "${kind}"`);
}
expect(code).toContain('result["value"] = from_str(self.value)');
expect(code).toContain("data = SampleUnionData.from_dict(data_obj)");
expect(code).toContain("return SampleEmptyData()");
});
});