



























@@ -51,6 +51,28 @@ describe("googlechat message actions", () => {
5151vi.resetModules();
5252});
535354+function buildAccount(overrides: Record<string, unknown> = {}) {
55+return {
56+accountId: "default",
57+enabled: true,
58+credentialSource: "service-account",
59+config: {},
60+ ...overrides,
61+};
62+}
63+64+function expectJsonResult(result: unknown, details: Record<string, unknown>) {
65+expect(result).toEqual({
66+content: [
67+{
68+type: "text",
69+text: JSON.stringify(details, null, 2),
70+},
71+],
72+ details,
73+});
74+}
75+5476it("describes send and reaction actions only when enabled accounts exist", () => {
5577listEnabledGoogleChatAccounts.mockReturnValueOnce([]);
5678expect(googlechatMessageActions.describeMessageTool?.({ cfg: {} as never })).toBeNull();
@@ -90,19 +112,20 @@ describe("googlechat message actions", () => {
90112});
9111392114it("sends messages with uploaded media through the resolved space", async () => {
93-resolveGoogleChatAccount.mockReturnValue({
94-credentialSource: "service-account",
115+const account = buildAccount({
95116config: { mediaMaxMb: 5 },
96117});
118+resolveGoogleChatAccount.mockReturnValue(account);
97119resolveGoogleChatOutboundSpace.mockResolvedValue("spaces/AAA");
120+const fetchRemoteMedia = vi.fn(async () => ({
121+buffer: Buffer.from("remote-bytes"),
122+fileName: "remote.png",
123+contentType: "image/png",
124+}));
98125getGoogleChatRuntime.mockReturnValue({
99126channel: {
100127media: {
101-fetchRemoteMedia: vi.fn(async () => ({
102-buffer: Buffer.from("remote-bytes"),
103-fileName: "remote.png",
104-contentType: "image/png",
105-})),
128+ fetchRemoteMedia,
106129},
107130},
108131});
@@ -128,37 +151,36 @@ describe("googlechat message actions", () => {
128151accountId: "default",
129152} as never);
130153131-expect(resolveGoogleChatOutboundSpace).toHaveBeenCalledWith(
132-expect.objectContaining({
133-target: "spaces/AAA",
134-}),
135-);
136-expect(uploadGoogleChatAttachment).toHaveBeenCalledWith(
137-expect.objectContaining({
138-space: "spaces/AAA",
139-filename: "remote.png",
140-}),
141-);
142-expect(sendGoogleChatMessage).toHaveBeenCalledWith(
143-expect.objectContaining({
144-space: "spaces/AAA",
145-text: "caption",
146-thread: "thread-1",
147-}),
148-);
149-expect(result).toMatchObject({
150-details: {
151-ok: true,
152-to: "spaces/AAA",
153-},
154+expect(resolveGoogleChatOutboundSpace).toHaveBeenCalledWith({
155+ account,
156+target: "spaces/AAA",
157+});
158+expect(fetchRemoteMedia).toHaveBeenCalledWith({
159+url: "https://example.com/file.png",
160+maxBytes: 5 * 1024 * 1024,
154161});
162+expect(uploadGoogleChatAttachment).toHaveBeenCalledWith({
163+ account,
164+space: "spaces/AAA",
165+filename: "remote.png",
166+buffer: Buffer.from("remote-bytes"),
167+contentType: "image/png",
168+});
169+expect(sendGoogleChatMessage).toHaveBeenCalledWith({
170+ account,
171+space: "spaces/AAA",
172+text: "caption",
173+thread: "thread-1",
174+attachments: [{ attachmentUploadToken: "token-1", contentName: "remote.png" }],
175+});
176+expectJsonResult(result, { ok: true, to: "spaces/AAA" });
155177});
156178157179it("routes upload-file through the same attachment upload path with filename override", async () => {
158-resolveGoogleChatAccount.mockReturnValue({
159-credentialSource: "service-account",
180+const account = buildAccount({
160181config: { mediaMaxMb: 5 },
161182});
183+resolveGoogleChatAccount.mockReturnValue(account);
162184resolveGoogleChatOutboundSpace.mockResolvedValue("spaces/BBB");
163185const localRoot = "/tmp/googlechat-action-test";
164186const localPath = path.join(localRoot, "local.md");
@@ -195,33 +217,28 @@ describe("googlechat message actions", () => {
195217} as never);
196218197219expect(readFile).toHaveBeenCalledWith(localPath);
198-expect(uploadGoogleChatAttachment).toHaveBeenCalledWith(
199-expect.objectContaining({
200-space: "spaces/BBB",
201-filename: "renamed.txt",
202-buffer: Buffer.from("local-bytes"),
203-}),
204-);
205-expect(sendGoogleChatMessage).toHaveBeenCalledWith(
206-expect.objectContaining({
207-space: "spaces/BBB",
208-text: "notes",
209-attachments: [{ attachmentUploadToken: "token-2", contentName: "renamed.txt" }],
210-}),
211-);
212-expect(result).toMatchObject({
213-details: {
214-ok: true,
215-to: "spaces/BBB",
216-},
220+expect(uploadGoogleChatAttachment).toHaveBeenCalledWith({
221+ account,
222+space: "spaces/BBB",
223+filename: "renamed.txt",
224+buffer: Buffer.from("local-bytes"),
225+contentType: "text/markdown",
217226});
227+expect(sendGoogleChatMessage).toHaveBeenCalledWith({
228+ account,
229+space: "spaces/BBB",
230+text: "notes",
231+thread: undefined,
232+attachments: [{ attachmentUploadToken: "token-2", contentName: "renamed.txt" }],
233+});
234+expectJsonResult(result, { ok: true, to: "spaces/BBB" });
218235});
219236220237it("removes only matching app reactions on react remove", async () => {
221-resolveGoogleChatAccount.mockReturnValue({
222-credentialSource: "service-account",
238+const account = buildAccount({
223239config: { botUser: "users/app-bot" },
224240});
241+resolveGoogleChatAccount.mockReturnValue(account);
225242listGoogleChatReactions.mockResolvedValue([
226243{
227244name: "reactions/1",
@@ -254,20 +271,19 @@ describe("googlechat message actions", () => {
254271accountId: "default",
255272} as never);
256273274+expect(listGoogleChatReactions).toHaveBeenCalledWith({
275+ account,
276+messageName: "spaces/AAA/messages/msg-1",
277+});
257278expect(deleteGoogleChatReaction).toHaveBeenCalledTimes(2);
258279expect(deleteGoogleChatReaction).toHaveBeenNthCalledWith(1, {
259-account: expect.anything(),
280+ account,
260281reactionName: "reactions/1",
261282});
262283expect(deleteGoogleChatReaction).toHaveBeenNthCalledWith(2, {
263-account: expect.anything(),
284+ account,
264285reactionName: "reactions/2",
265286});
266-expect(result).toMatchObject({
267-details: {
268-ok: true,
269-removed: 2,
270-},
271-});
287+expectJsonResult(result, { ok: true, removed: 2 });
272288});
273289});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。