-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.test-d.ts
318 lines (273 loc) · 7.58 KB
/
index.test-d.ts
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { expectType } from "tsd";
import { request } from "@octokit/request";
import {
createAckEvent,
createConfirmationEvent,
createDoneEvent,
createErrorsEvent,
createReferencesEvent,
createTextEvent,
fetchVerificationKeys,
verifyRequest,
verifyRequestByKeyId,
parseRequestBody,
transformPayloadForOpenAICompatibility,
verifyAndParseRequest,
getUserMessage,
getUserConfirmation,
type VerificationPublicKey,
type InteropMessage,
CopilotRequestPayload,
prompt,
PromptResult,
getFunctionCalls,
VerificationKeysCache,
} from "./index.js";
const token = "";
export async function verifyRequestByKeyIdTest(
rawBody: string,
signature: string,
keyId: string,
) {
const result = await verifyRequestByKeyId(rawBody, signature, keyId);
expectType<{
isValid: boolean;
cache: { id: string; keys: VerificationPublicKey[] };
}>(result);
// @ts-expect-error - first 3 arguments are required
verifyRequestByKeyId(rawBody, signature);
// @ts-expect-error - rawBody must be a string
await verifyRequestByKeyId(1, signature, keyId);
// @ts-expect-error - signature must be a string
await verifyRequestByKeyId(rawBody, 1, keyId);
// @ts-expect-error - keyId must be a string
await verifyRequestByKeyId(rawBody, signature, 1);
// accepts a token argument
await verifyRequestByKeyId(rawBody, signature, keyId, { token });
// accepts a request argument
await verifyRequestByKeyId(rawBody, signature, keyId, { request });
// accepts a cache argument
await verifyRequestByKeyId(rawBody, signature, keyId, {
cache: { id: "test", keys: [] },
});
}
export async function verifyRequestTest(
rawBody: string,
signature: string,
key: string,
) {
const result = await verifyRequest(rawBody, signature, key);
expectType<boolean>(result);
// @ts-expect-error - first 3 arguments are required
verifyRequest(rawBody, signature);
// @ts-expect-error - rawBody must be a string
await verifyRequest(1, signature, key);
// @ts-expect-error - signature must be a string
await verifyRequest(rawBody, 1, key);
// @ts-expect-error - key must be a string
await verifyRequest(rawBody, signature, 1);
}
export async function fetchVerificationKeysTest() {
const result = await fetchVerificationKeys();
expectType<{ id: string; keys: VerificationPublicKey[] }>(result);
// accepts a token argument
await fetchVerificationKeys({ token });
// accepts a request argument
await fetchVerificationKeys({ request });
// accepts a cache argument
await fetchVerificationKeys({ cache: { id: "test", keys: [] } });
}
export function createAckEventTest() {
const event = createAckEvent();
expectType<string>(event);
}
export function createTextEventTest() {
const event = createTextEvent("test");
expectType<string>(event);
}
export function createConfirmationEventTest() {
const event = createConfirmationEvent({
id: "test",
title: "test",
message: "test",
});
expectType<string>(event);
}
export function createReferencesEventTest() {
const event = createReferencesEvent([
{
type: "test.story",
id: "test",
data: {
file: "test.js",
start: "1",
end: "42",
content: "function test() {...}",
},
is_implicit: false,
metadata: {
display_name: "Lines 1-42 from test.js",
display_icon: "test-icon",
display_url:
"http://github.com/monalisa/hello-world/blob/main/test.js#L1-L42",
},
},
]);
expectType<string>(event);
}
export function createErrorsEventTest() {
const event = createErrorsEvent([
{
type: "reference",
code: "1",
message: "test reference error",
identifier: "reference-identifier",
},
{
type: "function",
code: "1",
message: "test function error",
identifier: "function-identifier",
},
{
type: "agent",
code: "1",
message: "test agent error",
identifier: "agent-identifier",
},
]);
expectType<string>(event);
}
export function createDoneEventTest() {
const event = createDoneEvent();
expectType<string>(event);
}
export function parseRequestBodyTest(body: string) {
const result = parseRequestBody(body);
expectType<CopilotRequestPayload>(result);
}
export function transformPayloadForOpenAICompatibilityTest(
payload: CopilotRequestPayload,
) {
const result = transformPayloadForOpenAICompatibility(payload);
expectType<{
messages: {
content: string;
role: "system" | "user" | "assistant";
name?: string;
[key: string]: unknown;
}[];
}>(result);
}
export async function verifyAndParseRequestTest(
rawBody: string,
signature: string,
keyId: string,
) {
const result = await verifyAndParseRequest(rawBody, signature, keyId);
expectType<{
isValidRequest: boolean;
payload: CopilotRequestPayload;
cache: { id: string; keys: VerificationPublicKey[] };
}>(result);
}
export function getUserMessageTest(payload: CopilotRequestPayload) {
const result = getUserMessage(payload);
expectType<string>(result);
}
export function getUserConfirmationTest(payload: CopilotRequestPayload) {
const result = getUserConfirmation(payload);
if (result === undefined) {
expectType<undefined>(result);
return;
}
expectType<{
accepted: boolean;
id?: string;
metadata: Record<string, unknown>;
}>(result);
}
export async function promptTest() {
const result = await prompt("What is the capital of France?", {
token: "secret",
});
expectType<string>(result.requestId);
expectType<string>(result.message.content);
// with custom fetch
await prompt("What is the capital of France?", {
token: "secret",
request: {
fetch: () => {},
},
});
// @ts-expect-error - 2nd argument is required
prompt("What is the capital of France?");
// @ts-expect-error - token argument is required
prompt("What is the capital of France?", { model: "" });
}
export async function promptWithToolsTest() {
await prompt("What is the capital of France?", {
token: "secret",
tools: [
{
type: "function",
function: {
name: "",
description: "",
parameters: {},
strict: true,
},
},
],
});
}
export async function promptWithMessageAndMessages() {
await prompt("What about Spain?", {
model: "gpt-4",
token: "secret",
messages: [
{ role: "user", content: "What is the capital of France?" },
{ role: "assistant", content: "The capital of France is Paris." },
],
});
}
export async function promptWithoutMessageButMessages() {
await prompt({
model: "gpt-4",
token: "secret",
messages: [
{ role: "user", content: "What is the capital of France?" },
{ role: "assistant", content: "The capital of France is Paris." },
{ role: "user", content: "What about Spain?" },
],
});
}
export async function otherPromptOptionsTest() {
const result = await prompt("What is the capital of France?", {
token: "secret",
model: "gpt-4",
endpoint: "https://api.githubcopilot.com",
});
}
export async function promptStreamTest() {
const result = await prompt.stream("What is the capital of France?", {
model: "gpt-4",
token: "secret",
});
expectType<string>(result.requestId);
expectType<ReadableStream<Uint8Array>>(result.stream);
}
export async function getFunctionCallsTest(
promptResponsePayload: PromptResult,
) {
const result = getFunctionCalls(promptResponsePayload);
expectType<
{
id: string;
function: {
name: string;
arguments: string;
};
}[]
>(result);
}