@app-vitals/agent-admin 1.97.0 → 1.99.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/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/Agent.ts +80 -1
- package/prisma/migrations/20261126000000_add_agent_slack_team_id/migration.sql +2 -0
- package/prisma/schema/schema.prisma +1 -0
- package/src/admin-ui-pages.ts +32 -20
- package/src/admin-ui-pages.unit.test.ts +75 -4
- package/src/admin-ui-slack-config-token-flows.smoke.test.ts +1 -1
- package/src/admin-ui.ts +1 -1
- package/src/agents.ts +5 -0
- package/src/fixtures/slack-provision-cassette.json +2 -1
- package/src/fixtures/slack-provisioning-http-cassette.json +4 -0
- package/src/slack-provisioning-client.integration.test.ts +11 -1
- package/src/slack-provisioning-client.ts +10 -3
- package/src/slack-provisioning-service.integration.test.ts +136 -5
- package/src/slack-provisioning-service.ts +4 -0
package/src/admin-ui-pages.ts
CHANGED
|
@@ -530,6 +530,30 @@ export interface NewAgentTemplateItem {
|
|
|
530
530
|
scope: string;
|
|
531
531
|
}
|
|
532
532
|
|
|
533
|
+
/**
|
|
534
|
+
* Shared Slack config-token copy (SWI-18.1). When a stored token resolves, the
|
|
535
|
+
* pasted token is an optional override that can target a different Slack
|
|
536
|
+
* workspace; otherwise it is required. One source so every surface (create
|
|
537
|
+
* form, Sync Manifest, Connect later) cannot drift.
|
|
538
|
+
*/
|
|
539
|
+
function slackTokenCopy(stored: boolean): {
|
|
540
|
+
label: string;
|
|
541
|
+
placeholder: string;
|
|
542
|
+
help: string;
|
|
543
|
+
} {
|
|
544
|
+
return stored
|
|
545
|
+
? {
|
|
546
|
+
label: "Slack App Configuration Token (override, optional)",
|
|
547
|
+
placeholder: "xoxe.xoxp-... (optional override)",
|
|
548
|
+
help: 'Leave blank to use the stored Slack config token; paste a token (<span class="mono">xoxe.xoxp-</span>) only to create the app in a different Slack workspace.',
|
|
549
|
+
}
|
|
550
|
+
: {
|
|
551
|
+
label: "Slack App Configuration Token",
|
|
552
|
+
placeholder: "xoxe.xoxp-...",
|
|
553
|
+
help: 'Requires a Slack app configuration token (<span class="mono">xoxe.xoxp-</span>).',
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
|
|
533
557
|
export function renderNewLocalAgentPage(
|
|
534
558
|
userName: string,
|
|
535
559
|
types: SpecialtyOption[],
|
|
@@ -621,7 +645,7 @@ export function renderNewLocalAgentPage(
|
|
|
621
645
|
}
|
|
622
646
|
<p style="font-size:13px;color:var(--vos-text-muted);margin-bottom:4px">Have these ready before you start:</p>
|
|
623
647
|
<ul style="font-size:13px;color:var(--vos-text-muted);margin-left:20px">
|
|
624
|
-
<li>A Slack App Configuration Token</li>
|
|
648
|
+
${opts?.slackConfigTokenStored ? "" : "<li>A Slack App Configuration Token</li>"}
|
|
625
649
|
<li>A GitHub org with an App, or a personal access token (PAT)</li>
|
|
626
650
|
<li>A Claude API key or OAuth token</li>
|
|
627
651
|
</ul>
|
|
@@ -815,20 +839,16 @@ export function renderNewLocalAgentPage(
|
|
|
815
839
|
</div>
|
|
816
840
|
<div id="slack-fields" style="display:none;margin-top:12px">
|
|
817
841
|
<div class="form-group">
|
|
818
|
-
<label class="form-label" for="xoxpToken"
|
|
842
|
+
<label class="form-label" for="xoxpToken">${slackTokenCopy(opts?.slackConfigTokenStored === true).label}</label>
|
|
819
843
|
<input
|
|
820
844
|
id="xoxpToken"
|
|
821
845
|
name="xoxpToken"
|
|
822
846
|
type="password"
|
|
823
847
|
class="form-input"
|
|
824
|
-
placeholder="
|
|
848
|
+
placeholder="${slackTokenCopy(opts?.slackConfigTokenStored === true).placeholder}"
|
|
825
849
|
/>
|
|
826
850
|
<p style="font-size:12px;color:var(--vos-text-muted);margin-top:4px">
|
|
827
|
-
${
|
|
828
|
-
opts?.slackConfigTokenStored
|
|
829
|
-
? "Optional: a stored Slack config token is configured in Settings and is used when this is left blank. A token pasted here is used instead. "
|
|
830
|
-
: ""
|
|
831
|
-
}A pasted token is used once to create the Slack app manifest. It is not stored. After
|
|
851
|
+
${opts?.slackConfigTokenStored ? `${slackTokenCopy(true).help} ` : ""}A pasted token is used once to create the Slack app manifest. It is not stored. After
|
|
832
852
|
creating the agent you'll be redirected to authorize the Slack app.
|
|
833
853
|
</p>
|
|
834
854
|
</div>
|
|
@@ -1350,17 +1370,13 @@ export function renderAgentDetailPage(
|
|
|
1350
1370
|
{
|
|
1351
1371
|
envKey: "SLACK_APP_TOKEN",
|
|
1352
1372
|
label: "Connect Slack",
|
|
1353
|
-
description: slackStored
|
|
1354
|
-
? `Connects this agent to a Slack app. Optional: a stored Slack config token is used unless you paste a Slack app configuration token (<span class="mono">xoxe.xoxp-</span>).`
|
|
1355
|
-
: `Connects this agent to a Slack app. Requires a Slack app configuration token (<span class="mono">xoxe.xoxp-</span>).`,
|
|
1373
|
+
description: `Connects this agent to a Slack app. ${slackTokenCopy(slackStored).help}`,
|
|
1356
1374
|
action: `/admin/agents/${escapeHtml(agent.id)}/connect-slack`,
|
|
1357
1375
|
inputs: [
|
|
1358
1376
|
{
|
|
1359
1377
|
name: "xoxpToken",
|
|
1360
1378
|
type: "password",
|
|
1361
|
-
placeholder: slackStored
|
|
1362
|
-
? "xoxe.xoxp-... (optional)"
|
|
1363
|
-
: "xoxe.xoxp-...",
|
|
1379
|
+
placeholder: slackTokenCopy(slackStored).placeholder,
|
|
1364
1380
|
mono: true,
|
|
1365
1381
|
optional: slackStored,
|
|
1366
1382
|
},
|
|
@@ -2368,17 +2384,13 @@ export function renderAgentDetailPage(
|
|
|
2368
2384
|
<details>
|
|
2369
2385
|
<summary class="btn btn-secondary" style="cursor:pointer;font-size:12px;list-style:none">Sync Manifest</summary>
|
|
2370
2386
|
<div style="position:absolute;right:24px;margin-top:6px;background:var(--vos-surface);border:1px solid var(--vos-border);border-radius:6px;padding:12px;box-shadow:0 4px 12px rgba(0,0,0,0.08);z-index:10;min-width:320px">
|
|
2371
|
-
<p style="font-size:12px;color:var(--vos-text-muted);margin:0 0 10px">Syncs the current manifest to the provisioned Slack app. ${
|
|
2372
|
-
slackStored
|
|
2373
|
-
? 'Optional: a stored Slack config token is used unless you paste a Slack app configuration token (<span class="mono">xoxe.xoxp-</span>).'
|
|
2374
|
-
: 'Requires a Slack app configuration token (<span class="mono">xoxe.xoxp-</span>).'
|
|
2375
|
-
}</p>
|
|
2387
|
+
<p style="font-size:12px;color:var(--vos-text-muted);margin:0 0 10px">Syncs the current manifest to the provisioned Slack app. ${slackTokenCopy(slackStored).help}</p>
|
|
2376
2388
|
<form method="POST" action="/admin/agents/${escapeHtml(agent.id)}/sync-manifest" style="display:flex;flex-direction:column;gap:8px">
|
|
2377
2389
|
<input
|
|
2378
2390
|
name="xoxpToken"
|
|
2379
2391
|
type="password"
|
|
2380
2392
|
class="form-input mono"
|
|
2381
|
-
placeholder="
|
|
2393
|
+
placeholder="${slackTokenCopy(slackStored).placeholder}"
|
|
2382
2394
|
${slackStored ? "" : "required"}
|
|
2383
2395
|
style="font-size:12px"
|
|
2384
2396
|
/>
|
|
@@ -1192,7 +1192,10 @@ describe("renderAgentDetailPage — connect-later actions", () => {
|
|
|
1192
1192
|
{ timezone: "UTC", slackConfigTokenStored: true },
|
|
1193
1193
|
);
|
|
1194
1194
|
expect(field(stored)).not.toMatch(/required/);
|
|
1195
|
-
expect(stored).toContain(
|
|
1195
|
+
expect(stored).toContain(
|
|
1196
|
+
"Leave blank to use the stored Slack config token",
|
|
1197
|
+
);
|
|
1198
|
+
expect(stored).toContain("xoxe.xoxp-... (optional override)");
|
|
1196
1199
|
});
|
|
1197
1200
|
|
|
1198
1201
|
test("new-agent form hint says the token is optional when a stored token resolves", () => {
|
|
@@ -1201,11 +1204,70 @@ describe("renderAgentDetailPage — connect-later actions", () => {
|
|
|
1201
1204
|
canProvision: true,
|
|
1202
1205
|
slackConfigTokenStored: true,
|
|
1203
1206
|
});
|
|
1204
|
-
expect(withStored).toContain(
|
|
1207
|
+
expect(withStored).toContain(
|
|
1208
|
+
"Slack App Configuration Token (override, optional)",
|
|
1209
|
+
);
|
|
1210
|
+
expect(withStored).toContain(
|
|
1211
|
+
'placeholder="xoxe.xoxp-... (optional override)"',
|
|
1212
|
+
);
|
|
1213
|
+
expect(withStored).toContain(
|
|
1214
|
+
"Leave blank to use the stored Slack config token",
|
|
1215
|
+
);
|
|
1216
|
+
expect(withStored).toContain("different Slack workspace");
|
|
1205
1217
|
const without = renderNewLocalAgentPage(USER_NAME, [CODING_TYPE], {
|
|
1206
1218
|
canProvision: true,
|
|
1207
1219
|
});
|
|
1208
|
-
expect(without).not.toContain(
|
|
1220
|
+
expect(without).not.toContain(
|
|
1221
|
+
"Leave blank to use the stored Slack config token",
|
|
1222
|
+
);
|
|
1223
|
+
expect(without).not.toContain("override, optional");
|
|
1224
|
+
expect(without).toContain('placeholder="xoxe.xoxp-..."');
|
|
1225
|
+
});
|
|
1226
|
+
|
|
1227
|
+
test("Sync Manifest uses the override copy when stored, required copy otherwise", () => {
|
|
1228
|
+
const sync = (stored: boolean) =>
|
|
1229
|
+
renderAgentDetailPage(
|
|
1230
|
+
{ ...AGENT, slackId: "U123" },
|
|
1231
|
+
{},
|
|
1232
|
+
[],
|
|
1233
|
+
[],
|
|
1234
|
+
[],
|
|
1235
|
+
[],
|
|
1236
|
+
[],
|
|
1237
|
+
USER_NAME,
|
|
1238
|
+
true,
|
|
1239
|
+
{ timezone: "UTC", slackConfigTokenStored: stored },
|
|
1240
|
+
).match(
|
|
1241
|
+
/action="\/admin\/agents\/agent-123\/sync-manifest"[\s\S]*?<\/form>/,
|
|
1242
|
+
)?.[0] as string;
|
|
1243
|
+
expect(sync(true)).toContain(
|
|
1244
|
+
'placeholder="xoxe.xoxp-... (optional override)"',
|
|
1245
|
+
);
|
|
1246
|
+
expect(sync(true)).not.toMatch(/required/);
|
|
1247
|
+
expect(sync(false)).toContain('placeholder="xoxe.xoxp-..."');
|
|
1248
|
+
expect(sync(false)).toMatch(/required/);
|
|
1249
|
+
});
|
|
1250
|
+
|
|
1251
|
+
test("Connect-later copy differs for stored vs not stored", () => {
|
|
1252
|
+
const stored = renderAgentDetailPage(
|
|
1253
|
+
AGENT,
|
|
1254
|
+
{},
|
|
1255
|
+
[],
|
|
1256
|
+
[],
|
|
1257
|
+
[],
|
|
1258
|
+
[],
|
|
1259
|
+
[],
|
|
1260
|
+
USER_NAME,
|
|
1261
|
+
true,
|
|
1262
|
+
{
|
|
1263
|
+
timezone: "UTC",
|
|
1264
|
+
slackConfigTokenStored: true,
|
|
1265
|
+
},
|
|
1266
|
+
);
|
|
1267
|
+
expect(stored).toContain(
|
|
1268
|
+
"Leave blank to use the stored Slack config token",
|
|
1269
|
+
);
|
|
1270
|
+
expect(render({})).toContain("Requires a Slack app configuration token");
|
|
1209
1271
|
});
|
|
1210
1272
|
|
|
1211
1273
|
test("Connect Slack action is hidden once SLACK_APP_TOKEN is set", () => {
|
|
@@ -4261,11 +4323,20 @@ describe("renderNewLocalAgentPage — self-serve account user (SSP-4.2)", () =>
|
|
|
4261
4323
|
selfServe,
|
|
4262
4324
|
});
|
|
4263
4325
|
expect(html).toContain("2 of 3");
|
|
4264
|
-
expect(html).toContain("Slack App Configuration Token");
|
|
4326
|
+
expect(html).toContain("A Slack App Configuration Token</li>");
|
|
4265
4327
|
expect(html).toContain("GitHub");
|
|
4266
4328
|
expect(html).toContain("Claude API key");
|
|
4267
4329
|
});
|
|
4268
4330
|
|
|
4331
|
+
test("checklist drops the Slack token item when a stored token resolves", () => {
|
|
4332
|
+
const html = renderNewLocalAgentPage(USER_NAME, [CODING_TYPE], {
|
|
4333
|
+
selfServe,
|
|
4334
|
+
slackConfigTokenStored: true,
|
|
4335
|
+
});
|
|
4336
|
+
expect(html).not.toContain("A Slack App Configuration Token</li>");
|
|
4337
|
+
expect(html).toContain("Claude API key");
|
|
4338
|
+
});
|
|
4339
|
+
|
|
4269
4340
|
describe("hidden type input", () => {
|
|
4270
4341
|
const saved = {
|
|
4271
4342
|
selfServe: process.env.SHIPWRIGHT_SELF_SERVE_AGENT_TYPE,
|
|
@@ -341,6 +341,6 @@ describe("admin UI — stored Slack config token flows", () => {
|
|
|
341
341
|
expect(without).toMatch(/name="xoxpToken"[^>]*required/);
|
|
342
342
|
const withStored = await get(makeResolver([null]).resolver);
|
|
343
343
|
expect(withStored).not.toMatch(/name="xoxpToken"[^>]*required/);
|
|
344
|
-
expect(withStored).toContain("
|
|
344
|
+
expect(withStored).toContain("optional override");
|
|
345
345
|
});
|
|
346
346
|
});
|
package/src/admin-ui.ts
CHANGED
|
@@ -232,7 +232,7 @@ export interface AdminUISlackClient {
|
|
|
232
232
|
clientSecret: string,
|
|
233
233
|
redirectUri: string,
|
|
234
234
|
): Promise<{ botToken: string }>;
|
|
235
|
-
authTest(botToken: string): Promise<{ userId: string }>;
|
|
235
|
+
authTest(botToken: string): Promise<{ userId: string; teamId?: string }>;
|
|
236
236
|
}
|
|
237
237
|
|
|
238
238
|
/**
|
package/src/agents.ts
CHANGED
|
@@ -254,6 +254,8 @@ export interface UpdateAgentFieldsInput {
|
|
|
254
254
|
restrictSlackToMembers?: boolean;
|
|
255
255
|
selfHosted?: boolean;
|
|
256
256
|
slackId?: string | null;
|
|
257
|
+
/** Slack workspace (team) id recorded at connect; null = unknown. */
|
|
258
|
+
slackTeamId?: string | null;
|
|
257
259
|
/**
|
|
258
260
|
* ATE-2.1: stamped internally by TrialExpiryWarningSweeper once a warning
|
|
259
261
|
* Slack alert has been sent — never accepted from PATCH /agents/:id (see
|
|
@@ -687,6 +689,9 @@ export class AgentService {
|
|
|
687
689
|
selfHosted: input.selfHosted,
|
|
688
690
|
}),
|
|
689
691
|
...(input.slackId !== undefined && { slackId: input.slackId }),
|
|
692
|
+
...(input.slackTeamId !== undefined && {
|
|
693
|
+
slackTeamId: input.slackTeamId,
|
|
694
|
+
}),
|
|
690
695
|
...(input.trialExpiryWarnedAt !== undefined && {
|
|
691
696
|
trialExpiryWarnedAt: input.trialExpiryWarnedAt,
|
|
692
697
|
}),
|
|
@@ -83,6 +83,10 @@
|
|
|
83
83
|
"status": 200,
|
|
84
84
|
"body": { "ok": true, "user_id": "U0AALR8M69X", "team_id": "T0123456789" }
|
|
85
85
|
},
|
|
86
|
+
"authTest_missing_team_id": {
|
|
87
|
+
"status": 200,
|
|
88
|
+
"body": { "ok": true, "user_id": "U0AALR8M69X" }
|
|
89
|
+
},
|
|
86
90
|
"authTest_http_error": {
|
|
87
91
|
"status": 500,
|
|
88
92
|
"body": { "ok": false, "error": "internal_error" }
|
|
@@ -270,7 +270,17 @@ describe("HttpSlackProvisioningClient — authTest", () => {
|
|
|
270
270
|
expect(req.url).toBe("https://slack.com/api/auth.test");
|
|
271
271
|
expect(req.headers.get("authorization")).toBe("Bearer xoxb-bot-token");
|
|
272
272
|
|
|
273
|
-
expect(result).toEqual({
|
|
273
|
+
expect(result).toEqual({
|
|
274
|
+
userId: "U0AALR8M69X",
|
|
275
|
+
teamId: "T0123456789",
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("returns an undefined teamId (and does not throw) when team_id is absent", async () => {
|
|
280
|
+
const { client } = makeClient("authTest_missing_team_id");
|
|
281
|
+
const result = await client.authTest("xoxb-bot-token");
|
|
282
|
+
expect(result.userId).toBe("U0AALR8M69X");
|
|
283
|
+
expect(result.teamId).toBeUndefined();
|
|
274
284
|
});
|
|
275
285
|
|
|
276
286
|
it("throws on an HTTP error response", async () => {
|
|
@@ -126,7 +126,7 @@ export interface SlackProvisioningClient {
|
|
|
126
126
|
* Call auth.test with a bot token to resolve the bot's own Slack user id.
|
|
127
127
|
* Used right after exchangeOAuthCode() to persist `agent.slackId`.
|
|
128
128
|
*/
|
|
129
|
-
authTest(botToken: string): Promise<{ userId: string }>;
|
|
129
|
+
authTest(botToken: string): Promise<{ userId: string; teamId?: string }>;
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
// ─── SlackConfigTokenClient ───────────────────────────────────────────────────
|
|
@@ -299,7 +299,9 @@ export class HttpSlackProvisioningClient
|
|
|
299
299
|
return { botToken: data.access_token };
|
|
300
300
|
}
|
|
301
301
|
|
|
302
|
-
async authTest(
|
|
302
|
+
async authTest(
|
|
303
|
+
botToken: string,
|
|
304
|
+
): Promise<{ userId: string; teamId?: string }> {
|
|
303
305
|
const url = `${this.apiBase}/auth.test`;
|
|
304
306
|
const resp = await this.fetchFn(url, {
|
|
305
307
|
method: "POST",
|
|
@@ -318,6 +320,7 @@ export class HttpSlackProvisioningClient
|
|
|
318
320
|
ok: boolean;
|
|
319
321
|
error?: string;
|
|
320
322
|
user_id?: string;
|
|
323
|
+
team_id?: string;
|
|
321
324
|
};
|
|
322
325
|
|
|
323
326
|
if (!data.ok) {
|
|
@@ -328,7 +331,11 @@ export class HttpSlackProvisioningClient
|
|
|
328
331
|
throw new Error("Slack auth.test response missing user_id");
|
|
329
332
|
}
|
|
330
333
|
|
|
331
|
-
|
|
334
|
+
// team_id is best-effort: absent => undefined, never a failure.
|
|
335
|
+
return {
|
|
336
|
+
userId: data.user_id,
|
|
337
|
+
...(data.team_id ? { teamId: data.team_id } : {}),
|
|
338
|
+
};
|
|
332
339
|
}
|
|
333
340
|
|
|
334
341
|
async updateAppManifest(
|
|
@@ -9,17 +9,19 @@
|
|
|
9
9
|
* going through the Hono app.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { describe, expect, it } from "bun:test";
|
|
12
|
+
import { afterAll, beforeEach, describe, expect, it } from "bun:test";
|
|
13
13
|
import { readFileSync } from "node:fs";
|
|
14
14
|
import { sign } from "hono/jwt";
|
|
15
15
|
import type { AdminUISlackClient } from "./admin-ui.ts";
|
|
16
|
-
import type
|
|
16
|
+
import { type AgentDetail, AgentService } from "./agents.ts";
|
|
17
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
17
18
|
import {
|
|
18
19
|
isSlackConfigTokenRevokedError,
|
|
19
20
|
type SlackConfigTokenResolver,
|
|
20
21
|
} from "./slack-config-token.ts";
|
|
21
22
|
import type { AppManifest } from "./slack-provisioning-client.ts";
|
|
22
23
|
import { SlackProvisioningService } from "./slack-provisioning-service.ts";
|
|
24
|
+
import type { SpecialtyTypeResolver } from "./specialty-types.ts";
|
|
23
25
|
|
|
24
26
|
// ─── Constants ────────────────────────────────────────────────────────────────
|
|
25
27
|
|
|
@@ -43,6 +45,7 @@ interface ProvisionCassette {
|
|
|
43
45
|
};
|
|
44
46
|
authTest: {
|
|
45
47
|
userId: string;
|
|
48
|
+
teamId?: string;
|
|
46
49
|
};
|
|
47
50
|
}
|
|
48
51
|
|
|
@@ -89,7 +92,9 @@ class RecordedSlackClient implements AdminUISlackClient {
|
|
|
89
92
|
return { botToken: "xoxb-test-cassette-bot-token" };
|
|
90
93
|
}
|
|
91
94
|
|
|
92
|
-
async authTest(
|
|
95
|
+
async authTest(
|
|
96
|
+
botToken: string,
|
|
97
|
+
): Promise<{ userId: string; teamId?: string }> {
|
|
93
98
|
this.authTestBotTokens.push(botToken);
|
|
94
99
|
return this.cassette.authTest;
|
|
95
100
|
}
|
|
@@ -589,7 +594,10 @@ describe("SlackProvisioningService.completeConnect", () => {
|
|
|
589
594
|
// The resolved user id (from the cassette) is persisted to agent.slackId.
|
|
590
595
|
expect(updateFieldsCalls.length).toBe(1);
|
|
591
596
|
expect(updateFieldsCalls[0].id).toBe(AGENT_ID);
|
|
592
|
-
expect(updateFieldsCalls[0].input).toEqual({
|
|
597
|
+
expect(updateFieldsCalls[0].input).toEqual({
|
|
598
|
+
slackId: "U0AALR8M69X",
|
|
599
|
+
slackTeamId: "T0123456789",
|
|
600
|
+
});
|
|
593
601
|
});
|
|
594
602
|
|
|
595
603
|
it("reinstall (SLACK_APP_TOKEN already set) → returns reinstalled outcome, still resolves and persists slackId via auth.test", async () => {
|
|
@@ -621,7 +629,10 @@ describe("SlackProvisioningService.completeConnect", () => {
|
|
|
621
629
|
|
|
622
630
|
expect(updateFieldsCalls.length).toBe(1);
|
|
623
631
|
expect(updateFieldsCalls[0].id).toBe(AGENT_ID);
|
|
624
|
-
expect(updateFieldsCalls[0].input).toEqual({
|
|
632
|
+
expect(updateFieldsCalls[0].input).toEqual({
|
|
633
|
+
slackId: "U0AALR8M69X",
|
|
634
|
+
slackTeamId: "T0123456789",
|
|
635
|
+
});
|
|
625
636
|
});
|
|
626
637
|
|
|
627
638
|
it("missing state cookie → invalid_state outcome, no Slack call, no env write", async () => {
|
|
@@ -823,3 +834,123 @@ describe("SlackProvisioningService.saveAppToken", () => {
|
|
|
823
834
|
expect(patchCalls.length).toBe(0);
|
|
824
835
|
});
|
|
825
836
|
});
|
|
837
|
+
|
|
838
|
+
// ─── Real-DB: slackTeamId persistence (SWI-18.2) ──────────────────────────────
|
|
839
|
+
|
|
840
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
841
|
+
const describeDbOrSkip = TEST_DB ? describe : describe.skip;
|
|
842
|
+
|
|
843
|
+
describeDbOrSkip(
|
|
844
|
+
"SlackProvisioningService.completeConnect — slackTeamId (real DB)",
|
|
845
|
+
() => {
|
|
846
|
+
const prisma = createAdminPrismaClient(TEST_DB as string);
|
|
847
|
+
const specialtyTypes = {
|
|
848
|
+
getType: () => ({
|
|
849
|
+
name: "coding",
|
|
850
|
+
displayName: "Coding Agent",
|
|
851
|
+
templatesDir: "workspace/",
|
|
852
|
+
tools: [],
|
|
853
|
+
requiredEnv: [],
|
|
854
|
+
}),
|
|
855
|
+
tryGetType: () => undefined,
|
|
856
|
+
listTypes: () => [],
|
|
857
|
+
} as unknown as SpecialtyTypeResolver;
|
|
858
|
+
const agentService = new AgentService(prisma, specialtyTypes);
|
|
859
|
+
|
|
860
|
+
beforeEach(async () => {
|
|
861
|
+
await prisma.agentEnv.deleteMany();
|
|
862
|
+
await prisma.agent.deleteMany();
|
|
863
|
+
await prisma.agent.create({
|
|
864
|
+
data: { id: AGENT_ID, name: "Test Agent", slackId: "UOLD" },
|
|
865
|
+
});
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
afterAll(async () => {
|
|
869
|
+
await prisma.agent.deleteMany();
|
|
870
|
+
await prisma.$disconnect();
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
function makeDbService(slackClient: AdminUISlackClient) {
|
|
874
|
+
return new SlackProvisioningService({
|
|
875
|
+
slackClient,
|
|
876
|
+
agentService,
|
|
877
|
+
agentEnvService: {
|
|
878
|
+
getConfigBundle: async () => null,
|
|
879
|
+
patch: async () => {},
|
|
880
|
+
},
|
|
881
|
+
agentCronJobService: {
|
|
882
|
+
reconcileSystemCrons: async () => ({
|
|
883
|
+
created: 0,
|
|
884
|
+
updated: 0,
|
|
885
|
+
deleted: 0,
|
|
886
|
+
}),
|
|
887
|
+
},
|
|
888
|
+
sessionSecret: SESSION_SECRET,
|
|
889
|
+
appBaseUrl: APP_BASE_URL,
|
|
890
|
+
secretEnvVars: new Set(["SLACK_BOT_TOKEN", "SLACK_APP_TOKEN"]),
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
function clientWithTeam(teamId: string | undefined): AdminUISlackClient {
|
|
895
|
+
return {
|
|
896
|
+
createAppManifest: async () => {
|
|
897
|
+
throw new Error("unused");
|
|
898
|
+
},
|
|
899
|
+
updateAppManifest: async () => {},
|
|
900
|
+
exchangeOAuthCode: async () => ({ botToken: "xoxb-db-test" }),
|
|
901
|
+
authTest: async () => ({
|
|
902
|
+
userId: "U0AALR8M69X",
|
|
903
|
+
...(teamId ? { teamId } : {}),
|
|
904
|
+
}),
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
it("persists slackTeamId alongside slackId", async () => {
|
|
909
|
+
const result = await makeDbService(
|
|
910
|
+
clientWithTeam("T0123456789"),
|
|
911
|
+
).completeConnect(
|
|
912
|
+
await makeProvisionStateToken(),
|
|
913
|
+
"code",
|
|
914
|
+
CONNECT_SLACK_CALLBACK,
|
|
915
|
+
);
|
|
916
|
+
expect(result.outcome).toBe("needs_app_token");
|
|
917
|
+
const row = await prisma.agent.findUniqueOrThrow({
|
|
918
|
+
where: { id: AGENT_ID },
|
|
919
|
+
});
|
|
920
|
+
expect(row.slackId).toBe("U0AALR8M69X");
|
|
921
|
+
expect(row.slackTeamId).toBe("T0123456789");
|
|
922
|
+
});
|
|
923
|
+
|
|
924
|
+
it("tolerates a missing team_id: connect succeeds and slackTeamId stays null", async () => {
|
|
925
|
+
const result = await makeDbService(
|
|
926
|
+
clientWithTeam(undefined),
|
|
927
|
+
).completeConnect(
|
|
928
|
+
await makeProvisionStateToken(),
|
|
929
|
+
"code",
|
|
930
|
+
CONNECT_SLACK_CALLBACK,
|
|
931
|
+
);
|
|
932
|
+
expect(result.outcome).toBe("needs_app_token");
|
|
933
|
+
const row = await prisma.agent.findUniqueOrThrow({
|
|
934
|
+
where: { id: AGENT_ID },
|
|
935
|
+
});
|
|
936
|
+
expect(row.slackId).toBe("U0AALR8M69X");
|
|
937
|
+
expect(row.slackTeamId).toBeNull();
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
it("a reinstall into another workspace overwrites the stored team id", async () => {
|
|
941
|
+
await prisma.agent.update({
|
|
942
|
+
where: { id: AGENT_ID },
|
|
943
|
+
data: { slackTeamId: "TOLD" },
|
|
944
|
+
});
|
|
945
|
+
await makeDbService(clientWithTeam("TNEW")).completeConnect(
|
|
946
|
+
await makeProvisionStateToken(),
|
|
947
|
+
"code",
|
|
948
|
+
CONNECT_SLACK_CALLBACK,
|
|
949
|
+
);
|
|
950
|
+
const row = await prisma.agent.findUniqueOrThrow({
|
|
951
|
+
where: { id: AGENT_ID },
|
|
952
|
+
});
|
|
953
|
+
expect(row.slackTeamId).toBe("TNEW");
|
|
954
|
+
});
|
|
955
|
+
},
|
|
956
|
+
);
|
|
@@ -422,6 +422,7 @@ export class SlackProvisioningService {
|
|
|
422
422
|
|
|
423
423
|
let botToken: string;
|
|
424
424
|
let slackUserId: string;
|
|
425
|
+
let slackTeamId: string | undefined;
|
|
425
426
|
try {
|
|
426
427
|
const result = await this.deps.slackClient.exchangeOAuthCode(
|
|
427
428
|
code,
|
|
@@ -439,6 +440,7 @@ export class SlackProvisioningService {
|
|
|
439
440
|
// stored but no resolved slackId.
|
|
440
441
|
const authTestResult = await this.deps.slackClient.authTest(botToken);
|
|
441
442
|
slackUserId = authTestResult.userId;
|
|
443
|
+
slackTeamId = authTestResult.teamId;
|
|
442
444
|
} catch (err) {
|
|
443
445
|
const msg =
|
|
444
446
|
err instanceof Error
|
|
@@ -464,6 +466,8 @@ export class SlackProvisioningService {
|
|
|
464
466
|
// also resolve a fresh/different user id.
|
|
465
467
|
await this.deps.agentService.updateFields(provisionState.agentId, {
|
|
466
468
|
slackId: slackUserId,
|
|
469
|
+
// Fail soft: a missing team_id leaves slackTeamId null, never fails connect.
|
|
470
|
+
slackTeamId: slackTeamId ?? null,
|
|
467
471
|
});
|
|
468
472
|
|
|
469
473
|
if (existingBundle?.env.SLACK_APP_TOKEN) {
|