|
1 | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import type { ResolvedGoogleChatAccount } from "./accounts.js"; |
3 | | -import { downloadGoogleChatMedia } from "./api.js"; |
| 3 | +import { downloadGoogleChatMedia, sendGoogleChatMessage } from "./api.js"; |
4 | 4 |
|
5 | 5 | vi.mock("./auth.js", () => ({ |
6 | 6 | getGoogleChatAccessToken: vi.fn().mockResolvedValue("token"), |
@@ -59,3 +59,50 @@ describe("downloadGoogleChatMedia", () => { |
59 | 59 | ).rejects.toThrow(/max bytes/i); |
60 | 60 | }); |
61 | 61 | }); |
| 62 | + |
| 63 | +describe("sendGoogleChatMessage", () => { |
| 64 | + afterEach(() => { |
| 65 | + vi.unstubAllGlobals(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("adds messageReplyOption when sending to an existing thread", async () => { |
| 69 | + const fetchMock = vi |
| 70 | + .fn() |
| 71 | + .mockResolvedValue( |
| 72 | + new Response(JSON.stringify({ name: "spaces/AAA/messages/123" }), { status: 200 }), |
| 73 | + ); |
| 74 | + vi.stubGlobal("fetch", fetchMock); |
| 75 | + |
| 76 | + await sendGoogleChatMessage({ |
| 77 | + account, |
| 78 | + space: "spaces/AAA", |
| 79 | + text: "hello", |
| 80 | + thread: "spaces/AAA/threads/xyz", |
| 81 | + }); |
| 82 | + |
| 83 | + const [url, init] = fetchMock.mock.calls[0] ?? []; |
| 84 | + expect(String(url)).toContain("messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"); |
| 85 | + expect(JSON.parse(String(init?.body))).toMatchObject({ |
| 86 | + text: "hello", |
| 87 | + thread: { name: "spaces/AAA/threads/xyz" }, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it("does not set messageReplyOption for non-thread sends", async () => { |
| 92 | + const fetchMock = vi |
| 93 | + .fn() |
| 94 | + .mockResolvedValue( |
| 95 | + new Response(JSON.stringify({ name: "spaces/AAA/messages/124" }), { status: 200 }), |
| 96 | + ); |
| 97 | + vi.stubGlobal("fetch", fetchMock); |
| 98 | + |
| 99 | + await sendGoogleChatMessage({ |
| 100 | + account, |
| 101 | + space: "spaces/AAA", |
| 102 | + text: "hello", |
| 103 | + }); |
| 104 | + |
| 105 | + const [url] = fetchMock.mock.calls[0] ?? []; |
| 106 | + expect(String(url)).not.toContain("messageReplyOption="); |
| 107 | + }); |
| 108 | +}); |
0 commit comments