@actagent/googlechat 2026.6.2

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.
Files changed (73) hide show
  1. package/README.md +11 -0
  2. package/actagent.plugin.json +17 -0
  3. package/api.ts +4 -0
  4. package/channel-config-api.ts +2 -0
  5. package/channel-plugin-api.ts +2 -0
  6. package/config-api.ts +3 -0
  7. package/contract-api.ts +6 -0
  8. package/directory-contract-api.ts +7 -0
  9. package/doctor-contract-api.ts +2 -0
  10. package/index.ts +21 -0
  11. package/npm-shrinkwrap.json +314 -0
  12. package/package.json +88 -0
  13. package/runtime-api.ts +61 -0
  14. package/secret-contract-api.ts +6 -0
  15. package/setup-entry.ts +14 -0
  16. package/setup-plugin-api.ts +3 -0
  17. package/src/accounts.ts +185 -0
  18. package/src/actions.test.ts +312 -0
  19. package/src/actions.ts +228 -0
  20. package/src/api.ts +346 -0
  21. package/src/approval-auth.test.ts +25 -0
  22. package/src/approval-auth.ts +38 -0
  23. package/src/approval-card-actions.test.ts +113 -0
  24. package/src/approval-card-actions.ts +307 -0
  25. package/src/approval-card-click.test.ts +279 -0
  26. package/src/approval-card-click.ts +94 -0
  27. package/src/approval-handler.runtime.test.ts +388 -0
  28. package/src/approval-handler.runtime.ts +413 -0
  29. package/src/approval-native.test.ts +399 -0
  30. package/src/approval-native.ts +246 -0
  31. package/src/auth.ts +219 -0
  32. package/src/channel-base.ts +123 -0
  33. package/src/channel-config.test.ts +174 -0
  34. package/src/channel.adapters.ts +363 -0
  35. package/src/channel.deps.runtime.ts +30 -0
  36. package/src/channel.runtime.ts +18 -0
  37. package/src/channel.setup.ts +7 -0
  38. package/src/channel.test.ts +845 -0
  39. package/src/channel.ts +214 -0
  40. package/src/config-schema.test.ts +32 -0
  41. package/src/config-schema.ts +4 -0
  42. package/src/doctor-contract.test.ts +76 -0
  43. package/src/doctor-contract.ts +181 -0
  44. package/src/doctor.ts +58 -0
  45. package/src/gateway.ts +84 -0
  46. package/src/google-auth.runtime.test.ts +571 -0
  47. package/src/google-auth.runtime.ts +570 -0
  48. package/src/group-policy.ts +18 -0
  49. package/src/monitor-access.test.ts +492 -0
  50. package/src/monitor-access.ts +466 -0
  51. package/src/monitor-durable.test.ts +40 -0
  52. package/src/monitor-durable.ts +24 -0
  53. package/src/monitor-reply-delivery.ts +162 -0
  54. package/src/monitor-routing.ts +66 -0
  55. package/src/monitor-types.ts +34 -0
  56. package/src/monitor-webhook.test.ts +670 -0
  57. package/src/monitor-webhook.ts +361 -0
  58. package/src/monitor.reply-delivery.test.ts +145 -0
  59. package/src/monitor.test.ts +389 -0
  60. package/src/monitor.ts +530 -0
  61. package/src/monitor.webhook-routing.test.ts +258 -0
  62. package/src/runtime.ts +10 -0
  63. package/src/secret-contract.test.ts +61 -0
  64. package/src/secret-contract.ts +162 -0
  65. package/src/setup-core.ts +41 -0
  66. package/src/setup-surface.ts +244 -0
  67. package/src/setup.test.ts +620 -0
  68. package/src/targets.test.ts +562 -0
  69. package/src/targets.ts +67 -0
  70. package/src/types.config.ts +4 -0
  71. package/src/types.ts +139 -0
  72. package/test-api.ts +3 -0
  73. package/tsconfig.json +16 -0
@@ -0,0 +1,562 @@
1
+ // Googlechat tests cover targets plugin behavior.
2
+ import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
3
+ import type { ResolvedGoogleChatAccount } from "./accounts.js";
4
+ import { downloadGoogleChatMedia, sendGoogleChatMessage, updateGoogleChatMessage } from "./api.js";
5
+ import {
6
+ clearGoogleChatApprovalCardBindingsForTest,
7
+ registerGoogleChatManualApprovalFollowupSuppression,
8
+ } from "./approval-card-actions.js";
9
+ import { resolveGoogleChatGroupRequireMention } from "./group-policy.js";
10
+ import {
11
+ isGoogleChatSpaceTarget,
12
+ isGoogleChatUserTarget,
13
+ normalizeGoogleChatTarget,
14
+ } from "./targets.js";
15
+
16
+ const mocks = vi.hoisted(() => ({
17
+ buildHostnameAllowlistPolicyFromSuffixAllowlist: vi.fn((hosts: string[]) => ({
18
+ hostnameAllowlist: hosts,
19
+ })),
20
+ fetchWithSsrFGuard: vi.fn(async (params: { url: string; init?: RequestInit }) => ({
21
+ response: await fetch(params.url, params.init),
22
+ release: async () => {},
23
+ })),
24
+ googleAuthCtor: vi.fn(),
25
+ gaxiosCtor: vi.fn(),
26
+ getAccessToken: vi.fn().mockResolvedValue({ token: "access-token" }),
27
+ oauthCtor: vi.fn(),
28
+ verifySignedJwtWithCertsAsync: vi.fn(),
29
+ verifyIdToken: vi.fn(),
30
+ getGoogleChatAccessToken: vi.fn().mockResolvedValue("token"),
31
+ }));
32
+
33
+ vi.mock("actagent/plugin-sdk/ssrf-runtime", () => {
34
+ return {
35
+ buildHostnameAllowlistPolicyFromSuffixAllowlist:
36
+ mocks.buildHostnameAllowlistPolicyFromSuffixAllowlist,
37
+ fetchWithSsrFGuard: mocks.fetchWithSsrFGuard,
38
+ };
39
+ });
40
+
41
+ vi.mock("gaxios", () => ({
42
+ Gaxios: class {
43
+ defaults: unknown;
44
+ interceptors = {
45
+ request: { add: vi.fn() },
46
+ response: { add: vi.fn() },
47
+ };
48
+
49
+ constructor(defaults?: unknown) {
50
+ this.defaults = defaults;
51
+ mocks.gaxiosCtor(defaults);
52
+ }
53
+ },
54
+ }));
55
+
56
+ vi.mock("google-auth-library", () => ({
57
+ GoogleAuth: class {
58
+ constructor(options?: unknown) {
59
+ mocks.googleAuthCtor(options);
60
+ }
61
+
62
+ getClient = vi.fn().mockResolvedValue({
63
+ getAccessToken: mocks.getAccessToken,
64
+ });
65
+ },
66
+ OAuth2Client: class {
67
+ constructor(options?: unknown) {
68
+ mocks.oauthCtor(options);
69
+ }
70
+
71
+ verifyIdToken = mocks.verifyIdToken;
72
+ verifySignedJwtWithCertsAsync = mocks.verifySignedJwtWithCertsAsync;
73
+ },
74
+ }));
75
+
76
+ vi.mock("./auth.js", async () => {
77
+ const actual = await vi.importActual<typeof import("./auth.js")>("./auth.js");
78
+ return {
79
+ ...actual,
80
+ getGoogleChatAccessToken: mocks.getGoogleChatAccessToken,
81
+ };
82
+ });
83
+
84
+ const authActual = await vi.importActual<typeof import("./auth.js")>("./auth.js");
85
+ const { testing: authTesting, getGoogleChatAccessToken, verifyGoogleChatRequest } = authActual;
86
+
87
+ afterAll(() => {
88
+ vi.doUnmock("actagent/plugin-sdk/ssrf-runtime");
89
+ vi.doUnmock("gaxios");
90
+ vi.doUnmock("google-auth-library");
91
+ vi.doUnmock("./auth.js");
92
+ vi.resetModules();
93
+ });
94
+
95
+ const account = {
96
+ accountId: "default",
97
+ enabled: true,
98
+ credentialSource: "inline",
99
+ config: {},
100
+ } as ResolvedGoogleChatAccount;
101
+
102
+ function stubSuccessfulSend(name: string) {
103
+ const fetchMock = vi
104
+ .fn()
105
+ .mockResolvedValue(new Response(JSON.stringify({ name }), { status: 200 }));
106
+ vi.stubGlobal("fetch", fetchMock);
107
+ return fetchMock;
108
+ }
109
+
110
+ async function expectDownloadToRejectForResponse(
111
+ response: Response,
112
+ expected: string | RegExp = /max bytes/i,
113
+ ) {
114
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue(response));
115
+ await expect(
116
+ downloadGoogleChatMedia({ account, resourceName: "media/123", maxBytes: 10 }),
117
+ ).rejects.toThrow(expected);
118
+ }
119
+
120
+ function mockCallArg(mock: ReturnType<typeof vi.fn>, callIndex = 0, argIndex = 0): unknown {
121
+ const call = mock.mock.calls[callIndex];
122
+ if (!call) {
123
+ throw new Error(`Expected mock call ${callIndex}`);
124
+ }
125
+ return call[argIndex];
126
+ }
127
+
128
+ describe("normalizeGoogleChatTarget", () => {
129
+ it("normalizes provider prefixes", () => {
130
+ expect(normalizeGoogleChatTarget("googlechat:users/123")).toBe("users/123");
131
+ expect(normalizeGoogleChatTarget("google-chat:spaces/AAA")).toBe("spaces/AAA");
132
+ expect(normalizeGoogleChatTarget("gchat:user:User@Example.com")).toBe("users/user@example.com");
133
+ });
134
+
135
+ it("normalizes email targets to users/<email>", () => {
136
+ expect(normalizeGoogleChatTarget("User@Example.com")).toBe("users/user@example.com");
137
+ expect(normalizeGoogleChatTarget("users/User@Example.com")).toBe("users/user@example.com");
138
+ });
139
+
140
+ it("preserves space targets", () => {
141
+ expect(normalizeGoogleChatTarget("space:spaces/BBB")).toBe("spaces/BBB");
142
+ expect(normalizeGoogleChatTarget("spaces/CCC")).toBe("spaces/CCC");
143
+ });
144
+ });
145
+
146
+ describe("target helpers", () => {
147
+ it("detects user and space targets", () => {
148
+ expect(isGoogleChatUserTarget("users/abc")).toBe(true);
149
+ expect(isGoogleChatSpaceTarget("spaces/abc")).toBe(true);
150
+ expect(isGoogleChatUserTarget("spaces/abc")).toBe(false);
151
+ });
152
+ });
153
+
154
+ describe("googlechat group policy", () => {
155
+ it("uses generic channel group policy helpers", () => {
156
+ const cfg = {
157
+ channels: {
158
+ googlechat: {
159
+ groups: {
160
+ "spaces/AAA": {
161
+ requireMention: false,
162
+ },
163
+ "*": {
164
+ requireMention: true,
165
+ },
166
+ },
167
+ },
168
+ },
169
+ } as any;
170
+
171
+ expect(resolveGoogleChatGroupRequireMention({ cfg, groupId: "spaces/AAA" })).toBe(false);
172
+ expect(resolveGoogleChatGroupRequireMention({ cfg, groupId: "spaces/BBB" })).toBe(true);
173
+ });
174
+ });
175
+
176
+ describe("downloadGoogleChatMedia", () => {
177
+ afterEach(() => {
178
+ clearGoogleChatApprovalCardBindingsForTest();
179
+ authTesting.resetGoogleChatAuthForTests();
180
+ mocks.fetchWithSsrFGuard.mockClear();
181
+ vi.unstubAllGlobals();
182
+ });
183
+
184
+ it("rejects when content-length exceeds max bytes", async () => {
185
+ const body = new ReadableStream({
186
+ start(controller) {
187
+ controller.enqueue(new Uint8Array([1, 2, 3]));
188
+ controller.close();
189
+ },
190
+ });
191
+ const response = new Response(body, {
192
+ status: 200,
193
+ headers: { "content-length": "50", "content-type": "application/octet-stream" },
194
+ });
195
+ await expectDownloadToRejectForResponse(response);
196
+ });
197
+
198
+ it("rejects malformed content-length before reading media", async () => {
199
+ const arrayBuffer = vi.fn(async () => new ArrayBuffer(0));
200
+ const response = {
201
+ ok: true,
202
+ status: 200,
203
+ headers: new Headers({
204
+ "content-length": "0x3",
205
+ "content-type": "application/octet-stream",
206
+ }),
207
+ arrayBuffer,
208
+ } as unknown as Response;
209
+
210
+ await expectDownloadToRejectForResponse(response, "invalid content-length header: 0x3");
211
+ expect(arrayBuffer).not.toHaveBeenCalled();
212
+ });
213
+
214
+ it("rejects when streamed payload exceeds max bytes", async () => {
215
+ const chunks = [new Uint8Array(6), new Uint8Array(6)];
216
+ let index = 0;
217
+ const body = new ReadableStream({
218
+ pull(controller) {
219
+ if (index < chunks.length) {
220
+ controller.enqueue(chunks[index++]);
221
+ } else {
222
+ controller.close();
223
+ }
224
+ },
225
+ });
226
+ const response = new Response(body, {
227
+ status: 200,
228
+ headers: { "content-type": "application/octet-stream" },
229
+ });
230
+ await expectDownloadToRejectForResponse(response);
231
+ });
232
+ });
233
+
234
+ describe("sendGoogleChatMessage", () => {
235
+ afterEach(() => {
236
+ authTesting.resetGoogleChatAuthForTests();
237
+ mocks.fetchWithSsrFGuard.mockClear();
238
+ vi.unstubAllGlobals();
239
+ });
240
+
241
+ it("adds messageReplyOption when sending to an existing thread", async () => {
242
+ const fetchMock = stubSuccessfulSend("spaces/AAA/messages/123");
243
+
244
+ await sendGoogleChatMessage({
245
+ account,
246
+ space: "spaces/AAA",
247
+ text: "hello",
248
+ thread: "spaces/AAA/threads/xyz",
249
+ });
250
+
251
+ const url = mockCallArg(fetchMock);
252
+ const init = mockCallArg(fetchMock, 0, 1) as RequestInit | undefined;
253
+ expect(String(url)).toContain("messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD");
254
+ if (typeof init?.body !== "string") {
255
+ throw new Error("Expected Google Chat request body");
256
+ }
257
+ const body = JSON.parse(init.body) as {
258
+ text?: unknown;
259
+ thread?: { name?: unknown };
260
+ };
261
+ expect(body.text).toBe("hello");
262
+ expect(body.thread?.name).toBe("spaces/AAA/threads/xyz");
263
+ });
264
+
265
+ it("does not set messageReplyOption for non-thread sends", async () => {
266
+ const fetchMock = stubSuccessfulSend("spaces/AAA/messages/124");
267
+
268
+ await sendGoogleChatMessage({
269
+ account,
270
+ space: "spaces/AAA",
271
+ text: "hello",
272
+ });
273
+
274
+ const url = mockCallArg(fetchMock);
275
+ expect(String(url)).not.toContain("messageReplyOption=");
276
+ });
277
+
278
+ it("sends cardsV2 with the text fallback", async () => {
279
+ const fetchMock = stubSuccessfulSend("spaces/AAA/messages/125");
280
+ const cardsV2 = [
281
+ {
282
+ cardId: "approval",
283
+ card: {
284
+ header: { title: "Approval" },
285
+ sections: [{ widgets: [{ textParagraph: { text: "Approve?" } }] }],
286
+ },
287
+ },
288
+ ];
289
+
290
+ await sendGoogleChatMessage({
291
+ account,
292
+ space: "spaces/AAA",
293
+ text: "Approval required",
294
+ cardsV2,
295
+ });
296
+
297
+ const init = mockCallArg(fetchMock, 0, 1) as RequestInit | undefined;
298
+ if (typeof init?.body !== "string") {
299
+ throw new Error("Expected Google Chat request body");
300
+ }
301
+ expect(JSON.parse(init.body)).toEqual({
302
+ text: "Approval required",
303
+ cardsV2,
304
+ });
305
+ });
306
+
307
+ it("suppresses text-only duplicate manual approval follow-ups at the API send boundary", async () => {
308
+ registerGoogleChatManualApprovalFollowupSuppression({
309
+ approvalId: "12345678-1234-1234-1234-123456789012",
310
+ approvalKind: "exec",
311
+ allowedDecisions: ["allow-once", "deny"],
312
+ expiresAtMs: Date.now() + 60_000,
313
+ });
314
+
315
+ const result = await sendGoogleChatMessage({
316
+ account,
317
+ space: "spaces/AAA",
318
+ text: "Please reply with:\n/approve 12345678 allow-once",
319
+ });
320
+
321
+ expect(result).toBeNull();
322
+ expect(mocks.fetchWithSsrFGuard).not.toHaveBeenCalled();
323
+ });
324
+
325
+ it("reports malformed send JSON with a stable API error", async () => {
326
+ vi.stubGlobal(
327
+ "fetch",
328
+ vi.fn().mockResolvedValue(
329
+ new Response("{ nope", {
330
+ status: 200,
331
+ headers: { "content-type": "application/json" },
332
+ }),
333
+ ),
334
+ );
335
+
336
+ await expect(
337
+ sendGoogleChatMessage({
338
+ account,
339
+ space: "spaces/AAA",
340
+ text: "hello",
341
+ }),
342
+ ).rejects.toThrow("Google Chat API request failed: malformed JSON response");
343
+ });
344
+ });
345
+
346
+ describe("updateGoogleChatMessage", () => {
347
+ afterEach(() => {
348
+ authTesting.resetGoogleChatAuthForTests();
349
+ mocks.fetchWithSsrFGuard.mockClear();
350
+ vi.unstubAllGlobals();
351
+ });
352
+
353
+ it("updates text and cardsV2 with a matching update mask", async () => {
354
+ const fetchMock = stubSuccessfulSend("spaces/AAA/messages/123");
355
+ const cardsV2 = [
356
+ {
357
+ cardId: "approval",
358
+ card: {
359
+ header: { title: "Resolved" },
360
+ sections: [{ widgets: [{ textParagraph: { text: "Done" } }] }],
361
+ },
362
+ },
363
+ ];
364
+
365
+ await updateGoogleChatMessage({
366
+ account,
367
+ messageName: "spaces/AAA/messages/123",
368
+ text: "Resolved",
369
+ cardsV2,
370
+ });
371
+
372
+ expect(String(mockCallArg(fetchMock))).toContain(
373
+ "spaces/AAA/messages/123?updateMask=text,cardsV2",
374
+ );
375
+ const init = mockCallArg(fetchMock, 0, 1) as RequestInit | undefined;
376
+ if (typeof init?.body !== "string") {
377
+ throw new Error("Expected Google Chat request body");
378
+ }
379
+ expect(JSON.parse(init.body)).toEqual({ text: "Resolved", cardsV2 });
380
+ });
381
+ });
382
+
383
+ function mockTicket(payload: Record<string, unknown>) {
384
+ mocks.verifyIdToken.mockResolvedValue({
385
+ getPayload: () => payload,
386
+ });
387
+ }
388
+
389
+ describe("verifyGoogleChatRequest", () => {
390
+ afterEach(() => {
391
+ authTesting.resetGoogleChatAuthForTests();
392
+ mocks.getAccessToken.mockClear();
393
+ mocks.gaxiosCtor.mockClear();
394
+ mocks.googleAuthCtor.mockClear();
395
+ mocks.oauthCtor.mockClear();
396
+ });
397
+
398
+ it("injects a scoped transporter into GoogleAuth access-token clients", async () => {
399
+ await expect(
400
+ getGoogleChatAccessToken({
401
+ ...account,
402
+ credentials: {
403
+ auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
404
+ auth_uri: "https://accounts.google.com/o/oauth2/auth",
405
+ client_email: "bot@example.iam.gserviceaccount.com",
406
+ private_key: "key",
407
+ token_uri: "https://oauth2.googleapis.com/token",
408
+ type: "service_account",
409
+ universe_domain: "googleapis.com",
410
+ },
411
+ }),
412
+ ).resolves.toBe("access-token");
413
+
414
+ const googleAuthOptions = mockCallArg(mocks.googleAuthCtor) as {
415
+ clientOptions?: { transporter?: { defaults?: { fetchImplementation?: unknown } } };
416
+ credentials?: { client_email?: string; token_uri?: string };
417
+ };
418
+
419
+ expect(mocks.gaxiosCtor).toHaveBeenCalledOnce();
420
+ expect(googleAuthOptions.credentials?.client_email).toBe("bot@example.iam.gserviceaccount.com");
421
+ expect(googleAuthOptions.credentials?.token_uri).toBe("https://oauth2.googleapis.com/token");
422
+ expect(typeof googleAuthOptions.clientOptions?.transporter?.defaults?.fetchImplementation).toBe(
423
+ "function",
424
+ );
425
+ expect(mocks.getAccessToken).toHaveBeenCalledOnce();
426
+ expect("window" in globalThis).toBe(false);
427
+ });
428
+
429
+ it("accepts Google Chat app-url tokens from the Chat issuer", async () => {
430
+ mocks.verifyIdToken.mockReset();
431
+ mockTicket({
432
+ email: "chat@system.gserviceaccount.com",
433
+ email_verified: true,
434
+ });
435
+
436
+ await expect(
437
+ verifyGoogleChatRequest({
438
+ bearer: "token",
439
+ audienceType: "app-url",
440
+ audience: "https://example.com/googlechat",
441
+ }),
442
+ ).resolves.toEqual({ ok: true });
443
+
444
+ const oauthOptions = mockCallArg(mocks.oauthCtor) as {
445
+ transporter?: { defaults?: { fetchImplementation?: unknown } };
446
+ };
447
+ expect(typeof oauthOptions.transporter?.defaults?.fetchImplementation).toBe("function");
448
+ });
449
+
450
+ it("rejects add-on tokens when no principal binding is configured", async () => {
451
+ mocks.verifyIdToken.mockReset();
452
+ mockTicket({
453
+ email: "service-123@gcp-sa-gsuiteaddons.iam.gserviceaccount.com",
454
+ email_verified: true,
455
+ sub: "principal-1",
456
+ });
457
+
458
+ await expect(
459
+ verifyGoogleChatRequest({
460
+ bearer: "token",
461
+ audienceType: "app-url",
462
+ audience: "https://example.com/googlechat",
463
+ }),
464
+ ).resolves.toEqual({
465
+ ok: false,
466
+ reason: "missing add-on principal binding",
467
+ });
468
+ });
469
+
470
+ it("accepts add-on tokens only when the bound principal matches", async () => {
471
+ mocks.verifyIdToken.mockReset();
472
+ mockTicket({
473
+ email: "service-123@gcp-sa-gsuiteaddons.iam.gserviceaccount.com",
474
+ email_verified: true,
475
+ sub: "principal-1",
476
+ });
477
+
478
+ await expect(
479
+ verifyGoogleChatRequest({
480
+ bearer: "token",
481
+ audienceType: "app-url",
482
+ audience: "https://example.com/googlechat",
483
+ expectedAddOnPrincipal: "principal-1",
484
+ }),
485
+ ).resolves.toEqual({ ok: true });
486
+ });
487
+
488
+ it("rejects add-on tokens when the bound principal does not match", async () => {
489
+ mocks.verifyIdToken.mockReset();
490
+ mockTicket({
491
+ email: "service-123@gcp-sa-gsuiteaddons.iam.gserviceaccount.com",
492
+ email_verified: true,
493
+ sub: "principal-2",
494
+ });
495
+
496
+ await expect(
497
+ verifyGoogleChatRequest({
498
+ bearer: "token",
499
+ audienceType: "app-url",
500
+ audience: "https://example.com/googlechat",
501
+ expectedAddOnPrincipal: "principal-1",
502
+ }),
503
+ ).resolves.toEqual({
504
+ ok: false,
505
+ reason: "unexpected add-on principal: principal-2",
506
+ });
507
+ });
508
+
509
+ it("fetches Chat certs through the guarded fetch for project-number tokens", async () => {
510
+ const release = vi.fn();
511
+ mocks.fetchWithSsrFGuard.mockClear();
512
+ mocks.fetchWithSsrFGuard.mockResolvedValueOnce({
513
+ response: new Response(JSON.stringify({ "kid-1": "cert-body" }), { status: 200 }),
514
+ release,
515
+ });
516
+ mocks.verifySignedJwtWithCertsAsync.mockReset().mockResolvedValue(undefined);
517
+
518
+ await expect(
519
+ verifyGoogleChatRequest({
520
+ bearer: "token",
521
+ audienceType: "project-number",
522
+ audience: "123456789",
523
+ }),
524
+ ).resolves.toEqual({ ok: true });
525
+
526
+ expect(mocks.fetchWithSsrFGuard).toHaveBeenCalledWith({
527
+ url: "https://www.googleapis.com/service_accounts/v1/metadata/x509/chat@system.gserviceaccount.com",
528
+ auditContext: "googlechat.auth.certs",
529
+ });
530
+ expect(mocks.verifySignedJwtWithCertsAsync).toHaveBeenCalledWith(
531
+ "token",
532
+ { "kid-1": "cert-body" },
533
+ "123456789",
534
+ ["chat@system.gserviceaccount.com"],
535
+ );
536
+ expect(release).toHaveBeenCalledOnce();
537
+ });
538
+
539
+ it("reports malformed Chat cert JSON with a stable auth error", async () => {
540
+ authTesting.resetGoogleChatAuthForTests();
541
+ const release = vi.fn(async () => {});
542
+ mocks.fetchWithSsrFGuard.mockResolvedValueOnce({
543
+ response: new Response("{ nope", {
544
+ status: 200,
545
+ headers: { "content-type": "application/json" },
546
+ }),
547
+ release,
548
+ });
549
+
550
+ await expect(
551
+ verifyGoogleChatRequest({
552
+ bearer: "token",
553
+ audienceType: "project-number",
554
+ audience: "123456789",
555
+ }),
556
+ ).resolves.toEqual({
557
+ ok: false,
558
+ reason: "Google Chat cert fetch failed: malformed JSON response",
559
+ });
560
+ expect(release).toHaveBeenCalledOnce();
561
+ });
562
+ });
package/src/targets.ts ADDED
@@ -0,0 +1,67 @@
1
+ // Googlechat plugin module implements targets behavior.
2
+ import { normalizeLowercaseStringOrEmpty } from "actagent/plugin-sdk/string-coerce-runtime";
3
+ import type { ResolvedGoogleChatAccount } from "./accounts.js";
4
+ import { findGoogleChatDirectMessage } from "./api.js";
5
+
6
+ export function normalizeGoogleChatTarget(raw?: string | null): string | undefined {
7
+ const trimmed = raw?.trim();
8
+ if (!trimmed) {
9
+ return undefined;
10
+ }
11
+ const withoutPrefix = trimmed.replace(/^(googlechat|google-chat|gchat):/i, "");
12
+ const normalized = withoutPrefix
13
+ .replace(/^user:(users\/)?/i, "users/")
14
+ .replace(/^space:(spaces\/)?/i, "spaces/");
15
+ if (isGoogleChatUserTarget(normalized)) {
16
+ const suffix = normalized.slice("users/".length);
17
+ return suffix.includes("@") ? `users/${normalizeLowercaseStringOrEmpty(suffix)}` : normalized;
18
+ }
19
+ if (isGoogleChatSpaceTarget(normalized)) {
20
+ return normalized;
21
+ }
22
+ if (normalized.includes("@")) {
23
+ return `users/${normalizeLowercaseStringOrEmpty(normalized)}`;
24
+ }
25
+ return normalized;
26
+ }
27
+
28
+ export function isGoogleChatUserTarget(value: string): boolean {
29
+ return normalizeLowercaseStringOrEmpty(value).startsWith("users/");
30
+ }
31
+
32
+ export function isGoogleChatSpaceTarget(value: string): boolean {
33
+ return normalizeLowercaseStringOrEmpty(value).startsWith("spaces/");
34
+ }
35
+
36
+ function stripMessageSuffix(target: string): string {
37
+ const index = target.indexOf("/messages/");
38
+ if (index === -1) {
39
+ return target;
40
+ }
41
+ return target.slice(0, index);
42
+ }
43
+
44
+ export async function resolveGoogleChatOutboundSpace(params: {
45
+ account: ResolvedGoogleChatAccount;
46
+ target: string;
47
+ }): Promise<string> {
48
+ const normalized = normalizeGoogleChatTarget(params.target);
49
+ if (!normalized) {
50
+ throw new Error("Missing Google Chat target.");
51
+ }
52
+ const base = stripMessageSuffix(normalized);
53
+ if (isGoogleChatSpaceTarget(base)) {
54
+ return base;
55
+ }
56
+ if (isGoogleChatUserTarget(base)) {
57
+ const dm = await findGoogleChatDirectMessage({
58
+ account: params.account,
59
+ userName: base,
60
+ });
61
+ if (!dm?.name) {
62
+ throw new Error(`No Google Chat DM found for ${base}`);
63
+ }
64
+ return dm.name;
65
+ }
66
+ return base;
67
+ }
@@ -0,0 +1,4 @@
1
+ // Googlechat helper module supports types behavior.
2
+ import type { GoogleChatAccountConfig, GoogleChatConfig } from "../runtime-api.js";
3
+
4
+ export type { GoogleChatAccountConfig, GoogleChatConfig };