forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-codegen.test.ts
More file actions
162 lines (151 loc) · 6.32 KB
/
Copy pathjava-codegen.test.ts
File metadata and controls
162 lines (151 loc) · 6.32 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
import { describe, expect, it } from "vitest";
import { renderRpcTypes, renderRpcWrappers } from "../../java/scripts/codegen/java.ts";
function rpcSource(files: Map<string, string>, name: string): string {
const source = files.get(
`sdk/src/generated/java/com/github/copilot/generated/rpc/${name}.java`
);
expect(source).toBeDefined();
return source!;
}
describe("java RPC codegen visibility", () => {
const schema = {
server: {
publicOnly: {
get: {
rpcMethod: "publicOnly.get",
params: { $ref: "#/definitions/PublicOnlyRequest" },
result: { $ref: "#/definitions/PublicOnlyResult" },
},
},
mixed: {
visible: {
rpcMethod: "mixed.visible",
params: null,
result: { $ref: "#/definitions/SharedResult" },
},
hidden: {
rpcMethod: "mixed.hidden",
visibility: "internal",
params: { $ref: "#/definitions/InternalOnlyRequest" },
result: { $ref: "#/definitions/InternalOnlyResult" },
},
},
internalOnly: {
hidden: {
rpcMethod: "internalOnly.hidden",
visibility: "internal",
params: { $ref: "#/definitions/InternalOnlyRequest" },
result: { $ref: "#/definitions/InternalOnlyResult" },
},
},
},
definitions: {
PublicOnlyRequest: {
type: "object",
properties: { value: { type: "string" } },
},
PublicOnlyResult: {
type: "object",
properties: { shared: { $ref: "#/definitions/SharedResult" } },
},
SharedResult: {
type: "object",
properties: { ok: { type: "boolean" } },
},
InternalOnlyRequest: {
type: "object",
properties: { secret: { type: "string" } },
},
InternalOnlyResult: {
type: "object",
properties: { secret: { type: "string" } },
},
},
} satisfies Parameters<typeof renderRpcTypes>[0];
it("keeps internal-only RPC types package-private", async () => {
const files = await renderRpcTypes(schema, {});
expect(rpcSource(files, "PublicOnlyGetParams")).toContain(
"public record PublicOnlyGetParams("
);
expect(rpcSource(files, "SharedResult")).toContain("public record SharedResult(");
expect(rpcSource(files, "MixedHiddenParams")).toContain("record MixedHiddenParams(");
expect(rpcSource(files, "MixedHiddenParams")).not.toContain(
"public record MixedHiddenParams("
);
expect(rpcSource(files, "InternalOnlyResult")).toContain("record InternalOnlyResult(");
expect(rpcSource(files, "InternalOnlyResult")).not.toContain(
"public record InternalOnlyResult("
);
});
it("does not expose internal methods or namespaces through public Java members", async () => {
const files = await renderRpcWrappers(schema);
expect(rpcSource(files, "ServerRpc")).toContain(
"public final ServerPublicOnlyApi publicOnly;"
);
expect(rpcSource(files, "ServerRpc")).toContain("public final ServerMixedApi mixed;");
expect(rpcSource(files, "ServerRpc")).toContain(
"final ServerInternalOnlyApi internalOnly;"
);
expect(rpcSource(files, "ServerRpc")).not.toContain(
"public final ServerInternalOnlyApi internalOnly;"
);
expect(rpcSource(files, "ServerMixedApi")).toContain("public final class ServerMixedApi");
expect(rpcSource(files, "ServerMixedApi")).toContain(
"public CompletableFuture<MixedVisibleResult> visible()"
);
expect(rpcSource(files, "ServerMixedApi")).toContain(
"CompletableFuture<InternalOnlyResult> hidden(MixedHiddenParams params)"
);
expect(rpcSource(files, "ServerMixedApi")).not.toContain(
"public CompletableFuture<InternalOnlyResult> hidden"
);
expect(rpcSource(files, "ServerInternalOnlyApi")).toContain(
"final class ServerInternalOnlyApi"
);
expect(rpcSource(files, "ServerInternalOnlyApi")).not.toContain(
"public final class ServerInternalOnlyApi"
);
});
it("reuses a shared result definition for internal methods", async () => {
const sharedResultSchema = {
server: {
globalState: {
load: {
rpcMethod: "globalState.load",
visibility: "internal",
params: null,
result: { $ref: "#/definitions/GlobalStateLoadResult" },
},
loadForConfigDir: {
rpcMethod: "globalState.loadForConfigDir",
visibility: "internal",
params: null,
result: { $ref: "#/definitions/GlobalStateLoadResult" },
},
},
},
definitions: {
GlobalStateLoadResult: {
type: "object",
properties: { staff: { type: "boolean" } },
},
},
} satisfies Parameters<typeof renderRpcTypes>[0];
const types = await renderRpcTypes(sharedResultSchema, {});
const wrappers = await renderRpcWrappers(sharedResultSchema);
expect(rpcSource(wrappers, "ServerGlobalStateApi")).toContain(
"CompletableFuture<GlobalStateLoadResult> loadForConfigDir()"
);
expect(rpcSource(wrappers, "ServerGlobalStateApi")).toContain(
"CompletableFuture<GlobalStateLoadResult> load()"
);
expect(rpcSource(types, "GlobalStateLoadResult")).not.toContain(
"public record GlobalStateLoadResult("
);
expect(
types.has(
"sdk/src/generated/java/com/github/copilot/generated/rpc/GlobalStateLoadForConfigDirResult.java"
)
).toBe(false);
});
});