|
7 | 7 | import { redisConfigMockFns } from '@sim/testing' |
8 | 8 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
9 | 9 |
|
| 10 | +const { capturedLeaderLockOptions } = vi.hoisted(() => ({ |
| 11 | + capturedLeaderLockOptions: [] as Array<Record<string, unknown>>, |
| 12 | +})) |
| 13 | +vi.mock('@/lib/concurrency/leader-lock', async (importOriginal) => { |
| 14 | + const actual = await importOriginal<typeof import('@/lib/concurrency/leader-lock')>() |
| 15 | + return { |
| 16 | + ...actual, |
| 17 | + withLeaderLock: vi.fn((options) => { |
| 18 | + capturedLeaderLockOptions.push(options as unknown as Record<string, unknown>) |
| 19 | + return actual.withLeaderLock(options) |
| 20 | + }), |
| 21 | + } |
| 22 | +}) |
| 23 | + |
10 | 24 | vi.mock('@/lib/oauth/oauth', () => ({ |
11 | 25 | refreshOAuthToken: vi.fn(), |
12 | 26 | OAUTH_PROVIDERS: {}, |
@@ -70,6 +84,7 @@ function mockUpdateChain() { |
70 | 84 | describe('OAuth Utils', () => { |
71 | 85 | beforeEach(() => { |
72 | 86 | vi.clearAllMocks() |
| 87 | + capturedLeaderLockOptions.length = 0 |
73 | 88 | __resetCoalesceLocallyForTests() |
74 | 89 | redisConfigMockFns.mockGetRedisClient.mockReturnValue(null) |
75 | 90 | redisConfigMockFns.mockAcquireLock.mockResolvedValue(true) |
@@ -420,6 +435,62 @@ describe('OAuth Utils', () => { |
420 | 435 | }) |
421 | 436 | }) |
422 | 437 |
|
| 438 | + describe('QuickBooks refresh locking', () => { |
| 439 | + it('keeps the lock and follower wait alive beyond the provider timeout', async () => { |
| 440 | + const credential = { |
| 441 | + id: 'quickbooks-row', |
| 442 | + accessToken: 'expired-token', |
| 443 | + refreshToken: 'rotating-refresh-token', |
| 444 | + accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), |
| 445 | + providerId: 'quickbooks', |
| 446 | + } |
| 447 | + mockRefreshOAuthToken.mockResolvedValueOnce({ |
| 448 | + ok: true, |
| 449 | + accessToken: 'new-token', |
| 450 | + expiresIn: 3600, |
| 451 | + refreshToken: 'new-rotating-refresh-token', |
| 452 | + }) |
| 453 | + mockUpdateChain() |
| 454 | + |
| 455 | + const result = await refreshTokenIfNeeded('request-id', credential, credential.id) |
| 456 | + |
| 457 | + expect(result).toEqual({ accessToken: 'new-token', refreshed: true }) |
| 458 | + expect(redisConfigMockFns.mockAcquireLock).toHaveBeenCalledWith( |
| 459 | + 'oauth:refresh:quickbooks-row', |
| 460 | + expect.any(String), |
| 461 | + 30 |
| 462 | + ) |
| 463 | + expect(capturedLeaderLockOptions[0]).toMatchObject({ ttlSec: 30, maxWaitMs: 30_000 }) |
| 464 | + }) |
| 465 | + |
| 466 | + it('keeps the default refresh-lock budget for other providers', async () => { |
| 467 | + const credential = { |
| 468 | + id: 'google-row', |
| 469 | + accessToken: 'expired-token', |
| 470 | + refreshToken: 'refresh-token', |
| 471 | + accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), |
| 472 | + providerId: 'google', |
| 473 | + } |
| 474 | + mockRefreshOAuthToken.mockResolvedValueOnce({ |
| 475 | + ok: true, |
| 476 | + accessToken: 'new-token', |
| 477 | + expiresIn: 3600, |
| 478 | + refreshToken: 'new-refresh-token', |
| 479 | + }) |
| 480 | + mockUpdateChain() |
| 481 | + |
| 482 | + await refreshTokenIfNeeded('request-id', credential, credential.id) |
| 483 | + |
| 484 | + expect(redisConfigMockFns.mockAcquireLock).toHaveBeenCalledWith( |
| 485 | + 'oauth:refresh:google-row', |
| 486 | + expect.any(String), |
| 487 | + 10 |
| 488 | + ) |
| 489 | + expect(capturedLeaderLockOptions[0]).not.toHaveProperty('ttlSec') |
| 490 | + expect(capturedLeaderLockOptions[0]).not.toHaveProperty('maxWaitMs') |
| 491 | + }) |
| 492 | + }) |
| 493 | + |
423 | 494 | describe('resolveServiceAccountToken', () => { |
424 | 495 | it('throws loudly for an unknown provider (never silently attempts Google)', async () => { |
425 | 496 | await expect(resolveServiceAccountToken('cred-1', 'mystery-provider')).rejects.toThrow( |
|
0 commit comments