|
| 1 | +import type { |
| 2 | + ChannelAccountSnapshot, |
| 3 | + ChannelGatewayContext, |
| 4 | + OpenClawConfig, |
| 5 | +} from "openclaw/plugin-sdk"; |
| 6 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import { createRuntimeEnv } from "../../test-utils/runtime-env.js"; |
| 8 | +import type { ResolvedGoogleChatAccount } from "./accounts.js"; |
| 9 | + |
| 10 | +const hoisted = vi.hoisted(() => ({ |
| 11 | + startGoogleChatMonitor: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("./monitor.js", async () => { |
| 15 | + const actual = await vi.importActual<typeof import("./monitor.js")>("./monitor.js"); |
| 16 | + return { |
| 17 | + ...actual, |
| 18 | + startGoogleChatMonitor: hoisted.startGoogleChatMonitor, |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +import { googlechatPlugin } from "./channel.js"; |
| 23 | + |
| 24 | +function createStartAccountCtx(params: { |
| 25 | + account: ResolvedGoogleChatAccount; |
| 26 | + abortSignal: AbortSignal; |
| 27 | + statusPatchSink?: (next: ChannelAccountSnapshot) => void; |
| 28 | +}): ChannelGatewayContext<ResolvedGoogleChatAccount> { |
| 29 | + const snapshot: ChannelAccountSnapshot = { |
| 30 | + accountId: params.account.accountId, |
| 31 | + configured: true, |
| 32 | + enabled: true, |
| 33 | + running: false, |
| 34 | + }; |
| 35 | + return { |
| 36 | + accountId: params.account.accountId, |
| 37 | + account: params.account, |
| 38 | + cfg: {} as OpenClawConfig, |
| 39 | + runtime: createRuntimeEnv(), |
| 40 | + abortSignal: params.abortSignal, |
| 41 | + log: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 42 | + getStatus: () => snapshot, |
| 43 | + setStatus: (next) => { |
| 44 | + Object.assign(snapshot, next); |
| 45 | + params.statusPatchSink?.(snapshot); |
| 46 | + }, |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +describe("googlechatPlugin gateway.startAccount", () => { |
| 51 | + afterEach(() => { |
| 52 | + vi.clearAllMocks(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("keeps startAccount pending until abort, then unregisters", async () => { |
| 56 | + const unregister = vi.fn(); |
| 57 | + hoisted.startGoogleChatMonitor.mockResolvedValue(unregister); |
| 58 | + |
| 59 | + const account: ResolvedGoogleChatAccount = { |
| 60 | + accountId: "default", |
| 61 | + enabled: true, |
| 62 | + credentialSource: "inline", |
| 63 | + credentials: {}, |
| 64 | + config: { |
| 65 | + webhookPath: "/googlechat", |
| 66 | + webhookUrl: "https://example.com/googlechat", |
| 67 | + audienceType: "app-url", |
| 68 | + audience: "https://example.com/googlechat", |
| 69 | + }, |
| 70 | + }; |
| 71 | + |
| 72 | + const patches: ChannelAccountSnapshot[] = []; |
| 73 | + const abort = new AbortController(); |
| 74 | + const task = googlechatPlugin.gateway!.startAccount!( |
| 75 | + createStartAccountCtx({ |
| 76 | + account, |
| 77 | + abortSignal: abort.signal, |
| 78 | + statusPatchSink: (next) => patches.push({ ...next }), |
| 79 | + }), |
| 80 | + ); |
| 81 | + |
| 82 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 83 | + |
| 84 | + let settled = false; |
| 85 | + void task.then(() => { |
| 86 | + settled = true; |
| 87 | + }); |
| 88 | + |
| 89 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 90 | + expect(settled).toBe(false); |
| 91 | + |
| 92 | + expect(hoisted.startGoogleChatMonitor).toHaveBeenCalledOnce(); |
| 93 | + expect(unregister).not.toHaveBeenCalled(); |
| 94 | + |
| 95 | + abort.abort(); |
| 96 | + await task; |
| 97 | + |
| 98 | + expect(unregister).toHaveBeenCalledOnce(); |
| 99 | + expect(patches.some((entry) => entry.running === true)).toBe(true); |
| 100 | + expect(patches.some((entry) => entry.running === false)).toBe(true); |
| 101 | + }); |
| 102 | +}); |
0 commit comments