-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcustom-provider-e2e.test.ts
More file actions
376 lines (359 loc) · 16.5 KB
/
Copy pathcustom-provider-e2e.test.ts
File metadata and controls
376 lines (359 loc) · 16.5 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// QA: end-to-end custom-provider lifecycle against a REAL fake upstream.
// Boots the app, registers a provider pointing at a local OpenAI-compatible
// server, and proves: validation, catalog surfacing, key hygiene, a real
// model call leaving QM and hitting the endpoint, edit-without-key, delete.
import "./support/auto-fake-sprites.ts";
import assert from "node:assert/strict";
import { createServer } from "node:http";
import type { AddressInfo } from "node:net";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import { createInsecureTestServer } from "../src/api/server.ts";
import { buildApp } from "../src/wiring.ts";
import { testConfig } from "./support/test-config.ts";
import { createPiHarness, oneShot } from "../src/harness/pi-harness.ts";
import type { HarnessTurnInput } from "../src/harness/harness.ts";
import { resolveModel, modelSupportedByHarness, modelServiceable } from "../src/model/pi-models.ts";
import { setCustomProviders } from "../src/model/custom-providers.ts";
import { createCustomProviderStore } from "../src/model/custom-provider-store.ts";
import { createMemoryMap } from "../src/persistence/durable-map.ts";
import type { Api, Model } from "@earendil-works/pi-ai";
const ADMIN = { "content-type": "application/json", "x-admin-actor": "admin-alice@default-org" };
test("QA: full custom-provider lifecycle against a live fake upstream", async () => {
// --- fake OpenAI-compatible upstream ---
const seen: Array<{ path: string; auth: string | undefined; model?: string; body?: unknown }> = [];
const upstream = createServer((req, res) => {
let body = "";
req.on("data", (c) => (body += c));
req.on("end", () => {
const record = { path: req.url ?? "", auth: req.headers.authorization as string | undefined } as (typeof seen)[0];
if (req.url?.endsWith("/models")) {
seen.push(record);
if (record.auth !== "Bearer sk-qa-good") {
res.writeHead(401, { "content-type": "application/json" });
return res.end(JSON.stringify({ error: { message: "bad key" } }));
}
res.writeHead(200, { "content-type": "application/json" });
return res.end(JSON.stringify({ data: [{ id: "qa-chat" }] }));
}
if (req.url?.endsWith("/chat/completions")) {
record.body = JSON.parse(body);
record.model = (record.body as { model?: string }).model;
seen.push(record);
res.writeHead(200, { "content-type": "text/event-stream" });
const chunk = (delta: object, finish: string | null) =>
`data: ${JSON.stringify({ id: "cmpl-qa", object: "chat.completion.chunk", model: "qa-chat", choices: [{ index: 0, delta, finish_reason: finish }], usage: finish ? { prompt_tokens: 5, completion_tokens: 3, total_tokens: 8 } : undefined })}\n\n`;
res.write(chunk({ role: "assistant", content: "QA UPSTREAM REPLY" }, null));
res.write(chunk({}, "stop"));
res.write("data: [DONE]\n\n");
return res.end();
}
seen.push(record);
res.writeHead(404);
res.end();
});
});
await new Promise<void>((r) => upstream.listen(0, "127.0.0.1", r));
const upstreamUrl = `http://127.0.0.1:${(upstream.address() as AddressInfo).port}/v1`;
const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "qa-custom-")) }));
const server = createInsecureTestServer(built.app, {
config: built.config,
modelCredentials: built.modelCredentials,
customProviders: built.customProviders,
refreshCustomProviders: built.refreshCustomProviders,
admin: built.admin,
auditLog: built.auditLog,
harnessId: "pi",
});
server.listen(0);
const base = `http://localhost:${(server.address() as AddressInfo).port}`;
const api = (path: string, init?: RequestInit) => fetch(`${base}${path}`, { headers: ADMIN, ...init });
try {
// 1. empty list
let r = await api("/v1/admin/custom-providers");
assert.equal(r.status, 200);
assert.deepEqual(((await r.json()) as { providers: unknown[] }).providers, []);
// 2. guardrails
r = await api("/v1/admin/custom-providers/openai", {
method: "PUT",
body: JSON.stringify({
name: "X",
protocol: "openai",
baseUrl: upstreamUrl,
apiKey: "sk-qa-good",
models: [{ id: "m" }],
}),
});
assert.equal(r.status, 400, "reserved slug refused");
r = await api("/v1/admin/custom-providers/qa", {
method: "PUT",
body: JSON.stringify({
name: "QA",
protocol: "openai",
baseUrl: "ftp://nope",
apiKey: "sk-qa-good",
models: [{ id: "m" }],
}),
});
assert.equal(r.status, 400, "non-http url refused");
r = await api("/v1/admin/custom-providers/qa", {
method: "PUT",
body: JSON.stringify({ name: "QA", protocol: "openai", baseUrl: upstreamUrl, apiKey: "sk-qa-good", models: [] }),
});
assert.equal(r.status, 400, "no models refused");
r = await api("/v1/admin/custom-providers/qa", {
method: "PUT",
body: JSON.stringify({
name: "QA",
protocol: "openai",
baseUrl: upstreamUrl,
apiKey: "sk-wrong",
models: [{ id: "qa-chat" }],
}),
});
assert.equal(r.status, 400, "bad key rejected by REAL upstream 401");
assert.equal(((await r.json()) as { error: string }).error, "invalid_api_key");
// 3. register for real — validation hits the live upstream
r = await api("/v1/admin/custom-providers/qa", {
method: "PUT",
body: JSON.stringify({
name: "QA Provider",
protocol: "openai",
baseUrl: upstreamUrl,
apiKey: "sk-qa-good",
models: [
{
id: "qa-chat",
name: "QA Chat",
contextWindow: 64000,
maxTokens: 4096,
modalities: ["text", "image"],
input: 1.5,
output: 6,
},
],
}),
});
assert.equal(r.status, 200);
assert.ok(
seen.some((s) => s.path.endsWith("/models") && s.auth === "Bearer sk-qa-good"),
"validation actually reached the upstream",
);
// 4. list: keyConfigured true, key NEVER present anywhere in the payload
r = await api("/v1/admin/custom-providers");
const listing = JSON.stringify(await r.json());
assert.ok(listing.includes('"hasKey":true'));
assert.ok(!listing.includes("sk-qa-good"), "key never readable");
// 5. model resolves like a built-in and is catalog-visible
const model = resolveModel("qa-chat");
assert.ok(model, "custom model resolves");
assert.equal(model!.provider, "qa");
assert.equal((model as { baseUrl?: string }).baseUrl, upstreamUrl);
assert.equal(modelSupportedByHarness("qa-chat", "pi"), true);
assert.equal(modelSupportedByHarness("qa-chat", "opencode"), true);
assert.equal(modelSupportedByHarness("qa-chat", "codex"), false);
assert.equal(modelServiceable("qa-chat", { anthropic: false, openai: false, openrouter: false }), true);
// 6. REAL model call through QM's pi path → fake upstream answers
const reply = await oneShot(
"qa",
model as unknown as Model<Api>,
{ qa: "sk-qa-good" },
"you are terse",
"say anything",
);
assert.equal(reply, "QA UPSTREAM REPLY");
const call = seen.find((s) => s.path.endsWith("/chat/completions"));
assert.ok(call, "completion request reached the upstream");
assert.equal(call!.model, "qa-chat");
assert.equal(call!.auth, "Bearer sk-qa-good", "stored key was sent to the custom endpoint");
const imageHarness = createPiHarness({
modelId: "qa-chat",
resolveProviderKeys: async () => ({ qa: "sk-qa-good" }),
captureRequests: false,
turnWallClockMs: 5_000,
});
const imageData = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";
const imageTurn = {
session: { id: "qa-image" } as HarnessTurnInput["session"],
input: "describe this image",
images: [{ mimeType: "image/png", dataBase64: imageData }],
model: "qa-chat",
systemPrompt: "be concise",
history: [],
tools: {} as HarnessTurnInput["tools"],
scopeLabel: "org:test" as HarnessTurnInput["scopeLabel"],
orgScopeId: "org:test" as HarnessTurnInput["orgScopeId"],
emit: async (entry) =>
({ ...entry, seq: 1, sessionId: "qa-image", createdAt: Date.now() }) as Awaited<
ReturnType<HarnessTurnInput["emit"]>
>,
recordModelCall: () => {},
} satisfies HarnessTurnInput;
const imageReply = await imageHarness.turns.runTurn(imageTurn);
assert.equal(imageReply.reply, "QA UPSTREAM REPLY");
const imageCall = seen.filter((item) => item.path.endsWith("/chat/completions")).at(-1);
const imagePayload = JSON.stringify(imageCall?.body);
assert.ok(imagePayload.includes(`data:image/png;base64,${imageData}`));
assert.doesNotMatch(imagePayload, /image omitted: model does not support images/);
// 7. edit WITHOUT key keeps the stored key
r = await api("/v1/admin/custom-providers/qa", {
method: "PUT",
body: JSON.stringify({
name: "QA Provider v2",
protocol: "openai",
baseUrl: upstreamUrl,
models: [{ id: "qa-chat", modalities: ["text", "image"], input: 1.5, output: 6 }],
}),
});
assert.equal(r.status, 200);
r = await api("/v1/admin/custom-providers");
assert.ok(JSON.stringify(await r.json()).includes('"hasKey":true'), "key survives keyless edit");
// 8. delete: models leave the registry
r = await api("/v1/admin/custom-providers/qa", { method: "DELETE" });
assert.equal(r.status, 200);
assert.equal(resolveModel("qa-chat"), undefined, "model gone after delete");
r = await api("/v1/admin/custom-providers/qa", { method: "DELETE" });
assert.equal(r.status, 404, "second delete 404s");
// 9. non-admin cannot touch any of it
r = await fetch(`${base}/v1/admin/custom-providers`, { headers: { "content-type": "application/json" } });
assert.notEqual(r.status, 200, "unauthenticated read refused");
} finally {
server.close();
upstream.close();
}
});
test("QA: anthropic-protocol custom provider serves a real turn (correct wire shape + headers)", async () => {
const seen: Array<{ path: string; apiKeyHeader?: string; version?: string; model?: string }> = [];
const upstream = createServer((req, res) => {
let body = "";
req.on("data", (c) => (body += c));
req.on("end", () => {
const record = {
path: req.url ?? "",
apiKeyHeader: req.headers["x-api-key"] as string | undefined,
version: req.headers["anthropic-version"] as string | undefined,
} as (typeof seen)[0];
if (req.url?.endsWith("/v1/models")) {
seen.push(record);
res.writeHead(record.apiKeyHeader === "sk-ant-qa" ? 200 : 401, { "content-type": "application/json" });
return res.end(JSON.stringify({ data: [] }));
}
if (req.url?.endsWith("/v1/messages")) {
record.model = (JSON.parse(body) as { model?: string }).model;
seen.push(record);
res.writeHead(200, { "content-type": "text/event-stream" });
res.write(
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_qa", type: "message", role: "assistant", content: [], model: "claude-compat", stop_reason: null, usage: { input_tokens: 5, output_tokens: 0 } } })}\n\n`,
);
res.write(
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
);
res.write(
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ANTHROPIC QA REPLY" } })}\n\n`,
);
res.write(`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`);
res.write(
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } })}\n\n`,
);
res.write(`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`);
return res.end();
}
seen.push(record);
res.writeHead(404);
res.end();
});
});
await new Promise<void>((r) => upstream.listen(0, "127.0.0.1", r));
const upstreamUrl = `http://127.0.0.1:${(upstream.address() as AddressInfo).port}`;
const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "qa-ant-")) }));
const server = createInsecureTestServer(built.app, {
config: built.config,
modelCredentials: built.modelCredentials,
customProviders: built.customProviders,
refreshCustomProviders: built.refreshCustomProviders,
admin: built.admin,
auditLog: built.auditLog,
harnessId: "pi",
});
server.listen(0);
const base = `http://localhost:${(server.address() as AddressInfo).port}`;
try {
const r = await fetch(`${base}/v1/admin/custom-providers/antcompat`, {
method: "PUT",
headers: ADMIN,
body: JSON.stringify({
name: "Ant Compat",
protocol: "anthropic",
baseUrl: upstreamUrl,
apiKey: "sk-ant-qa",
models: [{ id: "claude-compat", name: "Claude Compat" }],
}),
});
assert.equal(r.status, 200, "anthropic-protocol registration validates against /v1/models with x-api-key");
const model = resolveModel("claude-compat");
assert.ok(model);
assert.equal((model as { api?: string }).api, "anthropic-messages");
const reply = await oneShot("qa-ant", model as unknown as Model<Api>, { antcompat: "sk-ant-qa" }, "terse", "go");
assert.equal(reply, "ANTHROPIC QA REPLY");
const call = seen.find((s) => s.path.endsWith("/v1/messages"));
assert.ok(call, "messages request reached the anthropic-compatible upstream");
assert.equal(call!.model, "claude-compat");
assert.equal(call!.apiKeyHeader, "sk-ant-qa", "anthropic wire auth uses x-api-key");
} finally {
server.close();
upstream.close();
}
});
test("QA: registrations survive a restart (shared durable backing + same secret)", async () => {
// In production the backing map is the Postgres artifact store (same as
// model credentials); a restart is a new store instance over the same
// rows with the same CONNECTOR_SECRET_KEY. Simulate exactly that.
const backing = createMemoryMap<never>() as Parameters<typeof createCustomProviderStore>[0]["backing"];
const secret = "restart-secret-restart-secret-restart-secret";
const first = createCustomProviderStore({ backing, keyMaterial: secret });
await first.upsert(
{
id: "survivor",
name: "Survivor",
protocol: "openai",
baseUrl: "https://gw.example.com/v1",
models: [{ id: "survivor-model" }],
},
"sk-live-key",
"admin-alice@default-org",
);
// "restart": brand-new store instance over the same backing
const second = createCustomProviderStore({ backing, keyMaterial: secret });
const enabled = await second.enabled();
assert.equal(enabled[0]?.id, "survivor", "spec survives the restart");
assert.equal(await second.resolveKey("survivor"), "sk-live-key", "key decrypts after restart with the same secret");
// and the hydration path wires it into the runtime registry
setCustomProviders(enabled);
assert.ok(resolveModel("survivor-model"), "hydrated model resolves");
setCustomProviders([]);
});
test("QA: a corrupt stored key degrades that provider only — admin surface stays intact", async () => {
const backing = createMemoryMap<never>() as Parameters<typeof createCustomProviderStore>[0]["backing"];
const writer = createCustomProviderStore({ backing, keyMaterial: "first-secret-first-secret-first-secret-1" });
await writer.upsert(
{
id: "corrupted",
name: "Corrupted",
protocol: "openai",
baseUrl: "https://gw.example.com/v1",
models: [{ id: "corrupted-model" }],
},
"sk-will-be-unreadable",
"admin-alice@default-org",
);
// reboot with a DIFFERENT secret: the stored key is undecryptable
const reader = createCustomProviderStore({ backing, keyMaterial: "other-secret-other-secret-other-secret-2" });
await assert.rejects(reader.resolveKey("corrupted"), "decryption fails with the wrong secret");
const statuses = await reader.statuses();
assert.equal(statuses[0]?.id, "corrupted");
assert.equal(statuses[0]?.hasKey, true, "admin surface (no secrets) unaffected");
const enabled = await reader.enabled();
assert.equal(enabled[0]?.id, "corrupted", "spec listing unaffected — only the key is lost");
});