@app-vitals/agent-admin 1.40.0 → 1.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/openapi.platform.json +584 -0
- package/package.json +2 -2
- package/prisma/client/internal/class.ts +3 -3
- package/prisma/client/internal/prismaNamespace.ts +1 -0
- package/prisma/client/internal/prismaNamespaceBrowser.ts +1 -0
- package/prisma/client/models/AgentEnv.ts +36 -1
- package/prisma/migrations/20261114000000_add_agent_env_enabled/migration.sql +3 -0
- package/prisma/schema/schema.prisma +1 -0
- package/src/admin-boot.ts +18 -0
- package/src/admin-spec.ts +18 -0
- package/src/agent-cron-jobs.integration.test.ts +77 -0
- package/src/agent-cron-jobs.ts +66 -10
- package/src/agent-deletion.ts +2 -2
- package/src/agent-envs.integration.test.ts +108 -0
- package/src/agent-envs.ts +95 -19
- package/src/agent-plugins.integration.test.ts +52 -0
- package/src/agent-plugins.ts +51 -4
- package/src/agent-template-items.ts +27 -0
- package/src/agent-template-items.unit.test.ts +15 -0
- package/src/agent-templates-api.integration.test.ts +428 -0
- package/src/agent-templates-api.smoke.test.ts +135 -0
- package/src/agent-templates-api.ts +293 -0
- package/src/agent-templates.ts +432 -0
- package/src/agent-tools.integration.test.ts +55 -0
- package/src/agent-tools.ts +35 -4
- package/src/agents-api.smoke.test.ts +1 -0
- package/src/agents.ts +1 -1
- package/src/api-auth.ts +3 -0
- package/src/openapi-schemas.ts +84 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/agent-templates-api.integration.test.ts
|
|
3
|
+
* ATP-1.3: template CRUD authz, validation and env crypto against real
|
|
4
|
+
* Postgres, driven through the /templates app.
|
|
5
|
+
*
|
|
6
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
10
|
+
import { sign } from "hono/jwt";
|
|
11
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
12
|
+
import { AgentTemplateService } from "./agent-templates.ts";
|
|
13
|
+
import { createAgentTemplatesApp } from "./agent-templates-api.ts";
|
|
14
|
+
import { parseAdminApiKeys } from "./api-auth.ts";
|
|
15
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
16
|
+
import { SpecialtyTypes } from "./specialty-types.ts";
|
|
17
|
+
import { makeStrictTokenCrypto } from "./token-crypto.ts";
|
|
18
|
+
|
|
19
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
20
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
21
|
+
|
|
22
|
+
const KEY = "ab".repeat(32);
|
|
23
|
+
const SECRET = "test-session-secret-32-chars-long!!";
|
|
24
|
+
|
|
25
|
+
describeOrSkip("/templates (integration)", () => {
|
|
26
|
+
let prisma: PrismaClient;
|
|
27
|
+
let app: ReturnType<typeof createAgentTemplatesApp>;
|
|
28
|
+
let accountA: string;
|
|
29
|
+
let accountB: string;
|
|
30
|
+
const crypto = makeStrictTokenCrypto(KEY);
|
|
31
|
+
if (!crypto) throw new Error("test crypto");
|
|
32
|
+
|
|
33
|
+
const admin = { Authorization: "Bearer tok-admin" };
|
|
34
|
+
|
|
35
|
+
async function cookie(email: string, isAdmin = false) {
|
|
36
|
+
const token = await sign(
|
|
37
|
+
{
|
|
38
|
+
userId: email,
|
|
39
|
+
email,
|
|
40
|
+
isAdmin,
|
|
41
|
+
exp: Math.floor(Date.now() / 1000) + 600,
|
|
42
|
+
},
|
|
43
|
+
SECRET,
|
|
44
|
+
"HS256",
|
|
45
|
+
);
|
|
46
|
+
return { Cookie: `admin_session=${token}` };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function call(
|
|
50
|
+
method: string,
|
|
51
|
+
path: string,
|
|
52
|
+
headers: Record<string, string>,
|
|
53
|
+
body?: unknown,
|
|
54
|
+
) {
|
|
55
|
+
return app.request(path, {
|
|
56
|
+
method,
|
|
57
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
58
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
beforeEach(async () => {
|
|
63
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
64
|
+
await prisma.agentTemplateItem.deleteMany();
|
|
65
|
+
await prisma.agentTemplate.deleteMany();
|
|
66
|
+
await prisma.agent.deleteMany();
|
|
67
|
+
await prisma.accountInvite.deleteMany();
|
|
68
|
+
await prisma.accountMember.deleteMany();
|
|
69
|
+
await prisma.account.deleteMany();
|
|
70
|
+
accountA = (
|
|
71
|
+
await prisma.account.create({ data: { name: "A", maxAgents: 5 } })
|
|
72
|
+
).id;
|
|
73
|
+
accountB = (
|
|
74
|
+
await prisma.account.create({ data: { name: "B", maxAgents: 5 } })
|
|
75
|
+
).id;
|
|
76
|
+
const accountByEmail: Record<string, string> = {
|
|
77
|
+
"a@example.com": accountA,
|
|
78
|
+
"b@example.com": accountB,
|
|
79
|
+
};
|
|
80
|
+
app = createAgentTemplatesApp({
|
|
81
|
+
templateService: new AgentTemplateService(
|
|
82
|
+
prisma,
|
|
83
|
+
crypto,
|
|
84
|
+
new SpecialtyTypes({ defaultTypeName: "specialist" }),
|
|
85
|
+
),
|
|
86
|
+
callerScopeResolver: async (email, isAdmin) =>
|
|
87
|
+
isAdmin
|
|
88
|
+
? { kind: "all" }
|
|
89
|
+
: {
|
|
90
|
+
kind: "scoped",
|
|
91
|
+
accountId: accountByEmail[email] ?? null,
|
|
92
|
+
agentIds: [],
|
|
93
|
+
},
|
|
94
|
+
sessionSecret: SECRET,
|
|
95
|
+
adminApiKeys: parseAdminApiKeys("root:tok-admin:*,bot:tok-agent:agent-1"),
|
|
96
|
+
agentTokenService: { validate: async () => null },
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
afterEach(async () => {
|
|
101
|
+
await prisma.$disconnect();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("validation", () => {
|
|
105
|
+
it("422 for an unknown specialty", async () => {
|
|
106
|
+
const res = await call("PUT", "/templates", admin, {
|
|
107
|
+
specialty: "nope",
|
|
108
|
+
items: [],
|
|
109
|
+
});
|
|
110
|
+
expect(res.status).toBe(422);
|
|
111
|
+
expect(await prisma.agentTemplate.count()).toBe(0);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it.each([
|
|
115
|
+
"SLACK_BOT_TOKEN",
|
|
116
|
+
"SLACK_APP_TOKEN",
|
|
117
|
+
"GH_TOKEN",
|
|
118
|
+
"ANTHROPIC_API_KEY",
|
|
119
|
+
"CLAUDE_CODE_OAUTH_TOKEN",
|
|
120
|
+
"ANTHROPIC_AUTH_TOKEN",
|
|
121
|
+
"SHIPWRIGHT_TASK_STORE_URL",
|
|
122
|
+
"SHIPWRIGHT_TASK_STORE_TOKEN",
|
|
123
|
+
"gh_token",
|
|
124
|
+
])("422 for denylisted env key %s", async (key) => {
|
|
125
|
+
const res = await call("PUT", "/templates", admin, {
|
|
126
|
+
items: [{ kind: "env", key, payload: { value: "x" } }],
|
|
127
|
+
});
|
|
128
|
+
expect(res.status).toBe(422);
|
|
129
|
+
expect(await prisma.agentTemplate.count()).toBe(0);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("422 for a cron item without a name key", async () => {
|
|
133
|
+
const res = await call("PUT", "/templates", admin, {
|
|
134
|
+
items: [
|
|
135
|
+
{
|
|
136
|
+
kind: "cron",
|
|
137
|
+
key: " ",
|
|
138
|
+
payload: { schedule: "0 9 * * *", prompt: "brief" },
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
});
|
|
142
|
+
expect(res.status).toBe(422);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("422 for a cron item missing schedule or prompt", async () => {
|
|
146
|
+
const res = await call("PUT", "/templates", admin, {
|
|
147
|
+
items: [{ kind: "cron", key: "brief", payload: { prompt: "x" } }],
|
|
148
|
+
});
|
|
149
|
+
expect(res.status).toBe(422);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("422 for duplicate keys within a template, accepted across kinds", async () => {
|
|
153
|
+
const cron = {
|
|
154
|
+
kind: "cron",
|
|
155
|
+
key: "brief",
|
|
156
|
+
payload: { schedule: "0 9 * * *", prompt: "brief" },
|
|
157
|
+
};
|
|
158
|
+
const dup = await call("PUT", "/templates", admin, {
|
|
159
|
+
items: [cron, cron],
|
|
160
|
+
});
|
|
161
|
+
expect(dup.status).toBe(422);
|
|
162
|
+
expect(await prisma.agentTemplate.count()).toBe(0);
|
|
163
|
+
|
|
164
|
+
const ok = await call("PUT", "/templates", admin, {
|
|
165
|
+
items: [cron, { kind: "tool", key: "brief" }],
|
|
166
|
+
});
|
|
167
|
+
expect(ok.status).toBe(201);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("400 for an unknown item kind", async () => {
|
|
171
|
+
const res = await call("PUT", "/templates", admin, {
|
|
172
|
+
items: [{ kind: "bogus", key: "x" }],
|
|
173
|
+
});
|
|
174
|
+
expect(res.status).toBe(400);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe("crypto and masking", () => {
|
|
179
|
+
it("encrypts env values at rest and masks secrets on GET", async () => {
|
|
180
|
+
const put = await call("PUT", "/templates", admin, {
|
|
181
|
+
specialty: "specialist",
|
|
182
|
+
items: [
|
|
183
|
+
{
|
|
184
|
+
kind: "env",
|
|
185
|
+
key: "LINEAR_API_KEY",
|
|
186
|
+
payload: { value: "lin_secret", secret: true },
|
|
187
|
+
},
|
|
188
|
+
{ kind: "env", key: "REGION", payload: { value: "us-east-1" } },
|
|
189
|
+
],
|
|
190
|
+
});
|
|
191
|
+
expect(put.status).toBe(201);
|
|
192
|
+
const { id } = (await put.json()) as { id: string };
|
|
193
|
+
|
|
194
|
+
const rows = await prisma.agentTemplateItem.findMany({
|
|
195
|
+
where: { templateId: id },
|
|
196
|
+
orderBy: { key: "asc" },
|
|
197
|
+
});
|
|
198
|
+
const stored = Object.fromEntries(
|
|
199
|
+
rows.map((r) => [r.key, r.payload as Record<string, unknown>]),
|
|
200
|
+
);
|
|
201
|
+
expect(stored.LINEAR_API_KEY?.value).not.toBe("lin_secret");
|
|
202
|
+
expect(crypto.decrypt(stored.LINEAR_API_KEY?.value as string)).toBe(
|
|
203
|
+
"lin_secret",
|
|
204
|
+
);
|
|
205
|
+
expect(stored.REGION?.value).not.toBe("us-east-1");
|
|
206
|
+
expect(JSON.stringify(rows)).not.toContain("lin_secret");
|
|
207
|
+
|
|
208
|
+
const get = await call("GET", `/templates/${id}`, admin);
|
|
209
|
+
const body = (await get.json()) as {
|
|
210
|
+
items: { key: string; payload: Record<string, unknown> }[];
|
|
211
|
+
};
|
|
212
|
+
const byKey = Object.fromEntries(body.items.map((i) => [i.key, i]));
|
|
213
|
+
expect(byKey.LINEAR_API_KEY?.payload).toEqual({
|
|
214
|
+
value: "***",
|
|
215
|
+
secret: true,
|
|
216
|
+
});
|
|
217
|
+
expect(byKey.REGION?.payload.value).toBe("us-east-1");
|
|
218
|
+
|
|
219
|
+
const list = await call("GET", "/templates", admin);
|
|
220
|
+
expect(JSON.stringify(await list.json())).not.toContain("lin_secret");
|
|
221
|
+
const put2 = await call("PUT", "/templates", admin, {
|
|
222
|
+
specialty: "specialist",
|
|
223
|
+
items: [
|
|
224
|
+
{
|
|
225
|
+
kind: "env",
|
|
226
|
+
key: "LINEAR_API_KEY",
|
|
227
|
+
payload: { value: "lin_secret_2", secret: true },
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
});
|
|
231
|
+
expect(JSON.stringify(await put2.json())).not.toContain("lin_secret_2");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("keeps the stored secret when the mask is sent back", async () => {
|
|
235
|
+
const put = await call("PUT", "/templates", admin, {
|
|
236
|
+
items: [
|
|
237
|
+
{
|
|
238
|
+
kind: "env",
|
|
239
|
+
key: "K",
|
|
240
|
+
payload: { value: "keep-me", secret: true },
|
|
241
|
+
},
|
|
242
|
+
],
|
|
243
|
+
});
|
|
244
|
+
const { id } = (await put.json()) as { id: string };
|
|
245
|
+
const again = await call("PUT", "/templates", admin, {
|
|
246
|
+
items: [
|
|
247
|
+
{ kind: "env", key: "K", payload: { value: "***", secret: true } },
|
|
248
|
+
],
|
|
249
|
+
});
|
|
250
|
+
expect(again.status).toBe(200);
|
|
251
|
+
const row = await prisma.agentTemplateItem.findFirstOrThrow({
|
|
252
|
+
where: { templateId: id, key: "K" },
|
|
253
|
+
});
|
|
254
|
+
expect(crypto.decrypt((row.payload as { value: string }).value)).toBe(
|
|
255
|
+
"keep-me",
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const bad = await call("PUT", "/templates", admin, {
|
|
259
|
+
items: [
|
|
260
|
+
{ kind: "env", key: "NEW", payload: { value: "***", secret: true } },
|
|
261
|
+
],
|
|
262
|
+
});
|
|
263
|
+
expect(bad.status).toBe(422);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
describe("CRUD", () => {
|
|
268
|
+
it("create then replace one scope, list with filters, item ops, delete", async () => {
|
|
269
|
+
const first = await call("PUT", "/templates", admin, {
|
|
270
|
+
accountId: accountA,
|
|
271
|
+
specialty: "specialist",
|
|
272
|
+
items: [{ kind: "tool", key: "Bash" }],
|
|
273
|
+
});
|
|
274
|
+
expect(first.status).toBe(201);
|
|
275
|
+
const { id } = (await first.json()) as { id: string };
|
|
276
|
+
|
|
277
|
+
const second = await call("PUT", "/templates", admin, {
|
|
278
|
+
accountId: accountA,
|
|
279
|
+
specialty: "specialist",
|
|
280
|
+
items: [
|
|
281
|
+
{ kind: "plugin", key: "shipwright@app-vitals/shipwright" },
|
|
282
|
+
{
|
|
283
|
+
kind: "cron",
|
|
284
|
+
key: "brief",
|
|
285
|
+
payload: { schedule: "0 9 * * *", prompt: "go" },
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
});
|
|
289
|
+
expect(second.status).toBe(200);
|
|
290
|
+
const replaced = (await second.json()) as {
|
|
291
|
+
id: string;
|
|
292
|
+
items: { kind: string; key: string }[];
|
|
293
|
+
};
|
|
294
|
+
expect(replaced.id).toBe(id);
|
|
295
|
+
expect(replaced.items.map((i) => i.kind)).toEqual(["cron", "plugin"]);
|
|
296
|
+
expect(await prisma.agentTemplate.count()).toBe(1);
|
|
297
|
+
|
|
298
|
+
await call("PUT", "/templates", admin, { items: [] }); // deployment-wide
|
|
299
|
+
const all = (await (await call("GET", "/templates", admin)).json()) as {
|
|
300
|
+
templates: unknown[];
|
|
301
|
+
};
|
|
302
|
+
expect(all.templates).toHaveLength(2);
|
|
303
|
+
const filtered = (await (
|
|
304
|
+
await call("GET", `/templates?accountId=${accountA}`, admin)
|
|
305
|
+
).json()) as { templates: unknown[] };
|
|
306
|
+
expect(filtered.templates).toHaveLength(1);
|
|
307
|
+
|
|
308
|
+
const added = await call("PUT", `/templates/${id}/items`, admin, {
|
|
309
|
+
kind: "env",
|
|
310
|
+
key: "REGION",
|
|
311
|
+
payload: { value: "eu" },
|
|
312
|
+
});
|
|
313
|
+
expect(added.status).toBe(200);
|
|
314
|
+
expect(
|
|
315
|
+
await prisma.agentTemplateItem.count({ where: { templateId: id } }),
|
|
316
|
+
).toBe(3);
|
|
317
|
+
|
|
318
|
+
const del = await call(
|
|
319
|
+
"DELETE",
|
|
320
|
+
`/templates/${id}/items?kind=plugin&key=${encodeURIComponent("shipwright@app-vitals/shipwright")}`,
|
|
321
|
+
admin,
|
|
322
|
+
);
|
|
323
|
+
expect(del.status).toBe(204);
|
|
324
|
+
const missing = await call(
|
|
325
|
+
"DELETE",
|
|
326
|
+
`/templates/${id}/items?kind=plugin&key=nope`,
|
|
327
|
+
admin,
|
|
328
|
+
);
|
|
329
|
+
expect(missing.status).toBe(404);
|
|
330
|
+
|
|
331
|
+
expect((await call("DELETE", `/templates/${id}`, admin)).status).toBe(
|
|
332
|
+
204,
|
|
333
|
+
);
|
|
334
|
+
expect(
|
|
335
|
+
await prisma.agentTemplateItem.count({ where: { templateId: id } }),
|
|
336
|
+
).toBe(0);
|
|
337
|
+
expect((await call("GET", `/templates/${id}`, admin)).status).toBe(404);
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
describe("authz", () => {
|
|
342
|
+
it("account caller defaults to and manages only its own account", async () => {
|
|
343
|
+
const h = await cookie("a@example.com");
|
|
344
|
+
const put = await call("PUT", "/templates", h, {
|
|
345
|
+
items: [{ kind: "tool", key: "Bash" }],
|
|
346
|
+
});
|
|
347
|
+
expect(put.status).toBe(201);
|
|
348
|
+
const t = (await put.json()) as { id: string; accountId: string };
|
|
349
|
+
expect(t.accountId).toBe(accountA);
|
|
350
|
+
|
|
351
|
+
expect((await call("GET", `/templates/${t.id}`, h)).status).toBe(200);
|
|
352
|
+
const list = (await (await call("GET", "/templates", h)).json()) as {
|
|
353
|
+
templates: unknown[];
|
|
354
|
+
};
|
|
355
|
+
expect(list.templates).toHaveLength(1);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it("cross-account and deployment-wide access is refused", async () => {
|
|
359
|
+
const aTemplate = (await (
|
|
360
|
+
await call("PUT", "/templates", admin, {
|
|
361
|
+
accountId: accountA,
|
|
362
|
+
items: [],
|
|
363
|
+
})
|
|
364
|
+
).json()) as { id: string };
|
|
365
|
+
const global = (await (
|
|
366
|
+
await call("PUT", "/templates", admin, { items: [] })
|
|
367
|
+
).json()) as { id: string };
|
|
368
|
+
const b = await cookie("b@example.com");
|
|
369
|
+
|
|
370
|
+
// Reading/changing another account's or a global template: 404 (no leak).
|
|
371
|
+
for (const id of [aTemplate.id, global.id]) {
|
|
372
|
+
expect((await call("GET", `/templates/${id}`, b)).status).toBe(404);
|
|
373
|
+
expect((await call("DELETE", `/templates/${id}`, b)).status).toBe(404);
|
|
374
|
+
expect(
|
|
375
|
+
(
|
|
376
|
+
await call("PUT", `/templates/${id}/items`, b, {
|
|
377
|
+
kind: "tool",
|
|
378
|
+
key: "Bash",
|
|
379
|
+
})
|
|
380
|
+
).status,
|
|
381
|
+
).toBe(404);
|
|
382
|
+
expect(
|
|
383
|
+
(await call("DELETE", `/templates/${id}/items?kind=tool&key=x`, b))
|
|
384
|
+
.status,
|
|
385
|
+
).toBe(404);
|
|
386
|
+
}
|
|
387
|
+
expect(await prisma.agentTemplate.count()).toBe(2);
|
|
388
|
+
|
|
389
|
+
// Naming another account or the deployment scope: 403.
|
|
390
|
+
expect(
|
|
391
|
+
(await call("PUT", "/templates", b, { accountId: accountA, items: [] }))
|
|
392
|
+
.status,
|
|
393
|
+
).toBe(403);
|
|
394
|
+
expect(
|
|
395
|
+
(await call("PUT", "/templates", b, { accountId: null, items: [] }))
|
|
396
|
+
.status,
|
|
397
|
+
).toBe(403);
|
|
398
|
+
expect(
|
|
399
|
+
(await call("GET", `/templates?accountId=${accountA}`, b)).status,
|
|
400
|
+
).toBe(403);
|
|
401
|
+
|
|
402
|
+
// The list never includes others' templates.
|
|
403
|
+
const list = (await (await call("GET", "/templates", b)).json()) as {
|
|
404
|
+
templates: unknown[];
|
|
405
|
+
};
|
|
406
|
+
expect(list.templates).toEqual([]);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("403 for callers without an account, agent keys and non-scoped sessions", async () => {
|
|
410
|
+
expect(
|
|
411
|
+
(await call("GET", "/templates", await cookie("nobody@example.com")))
|
|
412
|
+
.status,
|
|
413
|
+
).toBe(403);
|
|
414
|
+
expect(
|
|
415
|
+
(await call("GET", "/templates", { Authorization: "Bearer tok-agent" }))
|
|
416
|
+
.status,
|
|
417
|
+
).toBe(403);
|
|
418
|
+
expect((await call("GET", "/templates", {})).status).toBe(401);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("an admin session manages deployment-wide templates", async () => {
|
|
422
|
+
const h = await cookie("root@example.com", true);
|
|
423
|
+
const res = await call("PUT", "/templates", h, { items: [] });
|
|
424
|
+
expect(res.status).toBe(201);
|
|
425
|
+
expect(((await res.json()) as { accountId: null }).accountId).toBeNull();
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/agent-templates-api.smoke.test.ts
|
|
3
|
+
* Smoke test for the /templates routes via app.request(): routing, auth and
|
|
4
|
+
* error mapping over an in-memory service. No DB, no network.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import { buildPlatformAdminSpec } from "./admin-spec.ts";
|
|
9
|
+
import type { TemplateView } from "./agent-templates.ts";
|
|
10
|
+
import { createAgentTemplatesApp } from "./agent-templates-api.ts";
|
|
11
|
+
import { parseAdminApiKeys } from "./api-auth.ts";
|
|
12
|
+
import { NotFoundError } from "./errors.ts";
|
|
13
|
+
|
|
14
|
+
const now = new Date("2026-11-14T00:00:00Z");
|
|
15
|
+
const view: TemplateView = {
|
|
16
|
+
id: "t1",
|
|
17
|
+
accountId: null,
|
|
18
|
+
specialty: null,
|
|
19
|
+
createdAt: now,
|
|
20
|
+
updatedAt: now,
|
|
21
|
+
items: [
|
|
22
|
+
{
|
|
23
|
+
kind: "env",
|
|
24
|
+
key: "TOKEN",
|
|
25
|
+
payload: { value: "***", secret: true },
|
|
26
|
+
updatedAt: now,
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function build() {
|
|
32
|
+
const app = createAgentTemplatesApp({
|
|
33
|
+
templateService: {
|
|
34
|
+
list: async () => [view],
|
|
35
|
+
get: async (_a, id) => {
|
|
36
|
+
if (id !== "t1") throw new NotFoundError("nope");
|
|
37
|
+
return view;
|
|
38
|
+
},
|
|
39
|
+
replace: async () => ({ template: view, created: true }),
|
|
40
|
+
putItem: async () => view,
|
|
41
|
+
deleteItem: async () => {},
|
|
42
|
+
delete: async () => {},
|
|
43
|
+
},
|
|
44
|
+
callerScopeResolver: async () => ({ kind: "all" }),
|
|
45
|
+
sessionSecret: "smoke-session-secret-32-chars-long",
|
|
46
|
+
adminApiKeys: parseAdminApiKeys("root:tok-admin:*,bot:tok-agent:agent-1"),
|
|
47
|
+
agentTokenService: { validate: async () => null },
|
|
48
|
+
});
|
|
49
|
+
return { app };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const admin = {
|
|
53
|
+
Authorization: "Bearer tok-admin",
|
|
54
|
+
"Content-Type": "application/json",
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
describe("/templates smoke", () => {
|
|
58
|
+
it("serves every route for an admin", async () => {
|
|
59
|
+
const { app } = build();
|
|
60
|
+
const list = await app.request("/templates", { headers: admin });
|
|
61
|
+
expect(list.status).toBe(200);
|
|
62
|
+
expect(
|
|
63
|
+
((await list.json()) as { templates: unknown[] }).templates,
|
|
64
|
+
).toHaveLength(1);
|
|
65
|
+
|
|
66
|
+
const get = await app.request("/templates/t1", { headers: admin });
|
|
67
|
+
expect(get.status).toBe(200);
|
|
68
|
+
expect(await get.json()).toMatchObject({
|
|
69
|
+
id: "t1",
|
|
70
|
+
items: [{ payload: { value: "***" } }],
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const put = await app.request("/templates", {
|
|
74
|
+
method: "PUT",
|
|
75
|
+
headers: admin,
|
|
76
|
+
body: JSON.stringify({ items: [] }),
|
|
77
|
+
});
|
|
78
|
+
expect(put.status).toBe(201);
|
|
79
|
+
|
|
80
|
+
const item = await app.request("/templates/t1/items", {
|
|
81
|
+
method: "PUT",
|
|
82
|
+
headers: admin,
|
|
83
|
+
body: JSON.stringify({ kind: "tool", key: "Bash" }),
|
|
84
|
+
});
|
|
85
|
+
expect(item.status).toBe(200);
|
|
86
|
+
|
|
87
|
+
expect(
|
|
88
|
+
(
|
|
89
|
+
await app.request("/templates/t1/items?kind=tool&key=Bash", {
|
|
90
|
+
method: "DELETE",
|
|
91
|
+
headers: admin,
|
|
92
|
+
})
|
|
93
|
+
).status,
|
|
94
|
+
).toBe(204);
|
|
95
|
+
expect(
|
|
96
|
+
(await app.request("/templates/t1", { method: "DELETE", headers: admin }))
|
|
97
|
+
.status,
|
|
98
|
+
).toBe(204);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("maps errors and rejects bad auth and bodies", async () => {
|
|
102
|
+
const { app } = build();
|
|
103
|
+
expect(
|
|
104
|
+
(await app.request("/templates/missing", { headers: admin })).status,
|
|
105
|
+
).toBe(404);
|
|
106
|
+
expect((await app.request("/templates")).status).toBe(401);
|
|
107
|
+
expect(
|
|
108
|
+
(
|
|
109
|
+
await app.request("/templates", {
|
|
110
|
+
headers: { Authorization: "Bearer tok-agent" },
|
|
111
|
+
})
|
|
112
|
+
).status,
|
|
113
|
+
).toBe(403);
|
|
114
|
+
expect(
|
|
115
|
+
(
|
|
116
|
+
await app.request("/templates", {
|
|
117
|
+
method: "PUT",
|
|
118
|
+
headers: admin,
|
|
119
|
+
body: JSON.stringify({ items: [{ kind: "bogus", key: "x" }] }),
|
|
120
|
+
})
|
|
121
|
+
).status,
|
|
122
|
+
).toBe(400);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("is documented in the platform OpenAPI spec", () => {
|
|
126
|
+
const spec = buildPlatformAdminSpec();
|
|
127
|
+
expect(Object.keys(spec.paths)).toEqual(
|
|
128
|
+
expect.arrayContaining([
|
|
129
|
+
"/templates",
|
|
130
|
+
"/templates/{id}",
|
|
131
|
+
"/templates/{id}/items",
|
|
132
|
+
]),
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
});
|