|
10 | 10 | */ |
11 | 11 |
|
12 | 12 | import { expectDefined } from "@openclaw/normalization-core"; |
| 13 | +import { createDeferred } from "openclaw/plugin-sdk/extension-shared"; |
13 | 14 | import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
14 | 15 | import { resolveTwitchToken } from "./token.js"; |
15 | 16 | import { TwitchClientManager } from "./twitch-client.js"; |
@@ -297,6 +298,94 @@ describe("TwitchClientManager", () => { |
297 | 298 | expect(mockQuit).toHaveBeenCalledTimes(2); |
298 | 299 | }); |
299 | 300 |
|
| 301 | + it.each(["account", "all"] as const)( |
| 302 | + "does not create a client when %s disconnect completes during authentication setup", |
| 303 | + async (scope) => { |
| 304 | + const authentication = createDeferred<string>(); |
| 305 | + mockAddUserForToken.mockImplementationOnce(() => authentication.promise); |
| 306 | + const account = { |
| 307 | + ...testAccount, |
| 308 | + clientSecret: "test-client-secret", |
| 309 | + refreshToken: "test-refresh-token", |
| 310 | + }; |
| 311 | + manager.onMessage(account, vi.fn()); |
| 312 | + const connection = manager.getClient(account); |
| 313 | + |
| 314 | + expect(mockAddUserForToken).toHaveBeenCalledOnce(); |
| 315 | + if (scope === "account") { |
| 316 | + await manager.disconnect(account); |
| 317 | + } else { |
| 318 | + await manager.disconnectAll(); |
| 319 | + } |
| 320 | + authentication.resolve("123456"); |
| 321 | + |
| 322 | + await expect(connection).rejects.toThrow("Twitch connection cancelled"); |
| 323 | + expect(mockConnect).not.toHaveBeenCalled(); |
| 324 | + expect(managerState(manager).clients.size).toBe(0); |
| 325 | + expect(managerState(manager).messageHandlers.has(manager.getAccountKey(account))).toBe( |
| 326 | + false, |
| 327 | + ); |
| 328 | + }, |
| 329 | + ); |
| 330 | + |
| 331 | + it("keeps a restarted account connection when stale authentication resolves afterward", async () => { |
| 332 | + const authentication = createDeferred<string>(); |
| 333 | + mockAddUserForToken.mockImplementationOnce(() => authentication.promise); |
| 334 | + const account = { |
| 335 | + ...testAccount, |
| 336 | + clientSecret: "test-client-secret", |
| 337 | + refreshToken: "test-refresh-token", |
| 338 | + }; |
| 339 | + const staleConnection = manager.getClient(account); |
| 340 | + const staleOutcome = staleConnection.then( |
| 341 | + () => "connected" as const, |
| 342 | + () => "cancelled" as const, |
| 343 | + ); |
| 344 | + |
| 345 | + await manager.disconnect(account); |
| 346 | + const currentConnection = manager.getClient(account); |
| 347 | + |
| 348 | + try { |
| 349 | + expect(mockAddUserForToken).toHaveBeenCalledTimes(2); |
| 350 | + const activeClient = await currentConnection; |
| 351 | + authentication.resolve("123456"); |
| 352 | + |
| 353 | + expect(await staleOutcome).toBe("cancelled"); |
| 354 | + expect(managerState(manager).clients.get(manager.getAccountKey(account))).toBe( |
| 355 | + activeClient, |
| 356 | + ); |
| 357 | + expect(mockConnect).toHaveBeenCalledOnce(); |
| 358 | + } finally { |
| 359 | + authentication.resolve("123456"); |
| 360 | + await Promise.allSettled([staleConnection, currentConnection]); |
| 361 | + } |
| 362 | + }); |
| 363 | + |
| 364 | + it("keeps another account connected when cancelling an authentication-pending account", async () => { |
| 365 | + const authentication = createDeferred<string>(); |
| 366 | + mockAddUserForToken.mockImplementationOnce(() => authentication.promise); |
| 367 | + const account = { |
| 368 | + ...testAccount, |
| 369 | + clientSecret: "test-client-secret", |
| 370 | + refreshToken: "test-refresh-token", |
| 371 | + }; |
| 372 | + const staleConnection = manager.getClient(account); |
| 373 | + const staleOutcome = staleConnection.then( |
| 374 | + () => "connected" as const, |
| 375 | + () => "cancelled" as const, |
| 376 | + ); |
| 377 | + const activeClient = await manager.getClient(testAccount2); |
| 378 | + |
| 379 | + await manager.disconnect(account); |
| 380 | + authentication.resolve("123456"); |
| 381 | + |
| 382 | + expect(await staleOutcome).toBe("cancelled"); |
| 383 | + expect(managerState(manager).clients.get(manager.getAccountKey(testAccount2))).toBe( |
| 384 | + activeClient, |
| 385 | + ); |
| 386 | + expect(mockConnect).toHaveBeenCalledOnce(); |
| 387 | + }); |
| 388 | + |
300 | 389 | it("should create separate clients for different accounts", async () => { |
301 | 390 | await manager.getClient(testAccount); |
302 | 391 | await manager.getClient(testAccount2); |
|
0 commit comments