@app-vitals/agent-admin 1.98.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.
@@ -7,6 +7,7 @@
7
7
  "signingSecret": "test-signing-secret-value"
8
8
  },
9
9
  "authTest": {
10
- "userId": "U0AALR8M69X"
10
+ "userId": "U0AALR8M69X",
11
+ "teamId": "T0123456789"
11
12
  }
12
13
  }
@@ -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({ userId: "U0AALR8M69X" });
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(botToken: string): Promise<{ userId: string }> {
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
- return { userId: data.user_id };
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 { AgentDetail } from "./agents.ts";
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(botToken: string): Promise<{ userId: string }> {
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({ slackId: "U0AALR8M69X" });
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({ slackId: "U0AALR8M69X" });
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) {