|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +// Mock the SDK so we can observe how `getWorkspaceUsers` invokes |
| 4 | +// `client.workspaceUsers.getWorkspaceUsers`. The real filtering of |
| 5 | +// removed users lives in the SDK (≥0.3.0), so the contract this test |
| 6 | +// guards is "we pass `includeRemoved` through unchanged." |
| 7 | +const getWorkspaceUsersMock = vi.hoisted(() => vi.fn().mockResolvedValue([])) |
| 8 | + |
| 9 | +vi.mock('@doist/comms-sdk', () => ({ |
| 10 | + CommsApi: class { |
| 11 | + workspaceUsers = { getWorkspaceUsers: getWorkspaceUsersMock } |
| 12 | + }, |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('./auth.js', () => ({ |
| 16 | + getApiToken: vi.fn().mockResolvedValue('test-token'), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('./permissions.js', () => ({ |
| 20 | + ensureWriteAllowed: vi.fn(), |
| 21 | + isMutatingMethod: vi.fn().mockReturnValue(false), |
| 22 | +})) |
| 23 | + |
| 24 | +vi.mock('./spinner.js', () => ({ |
| 25 | + withSpinner: <T>(_label: unknown, fn: () => Promise<T>) => fn(), |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('./progress.js', () => ({ |
| 29 | + getProgressTracker: () => ({ isEnabled: () => false, emitApiCall: vi.fn() }), |
| 30 | +})) |
| 31 | + |
| 32 | +const { clearWorkspaceUserCache, getWorkspaceUsers } = await import('./api.js') |
| 33 | + |
| 34 | +describe('getWorkspaceUsers', () => { |
| 35 | + beforeEach(() => { |
| 36 | + getWorkspaceUsersMock.mockClear() |
| 37 | + clearWorkspaceUserCache() |
| 38 | + }) |
| 39 | + |
| 40 | + it('passes includeRemoved: undefined by default so the SDK applies its default filter', async () => { |
| 41 | + await getWorkspaceUsers(1585) |
| 42 | + expect(getWorkspaceUsersMock).toHaveBeenCalledWith({ |
| 43 | + workspaceId: 1585, |
| 44 | + includeRemoved: undefined, |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + it('forwards includeRemoved: true to the SDK', async () => { |
| 49 | + await getWorkspaceUsers(1585, { includeRemoved: true }) |
| 50 | + expect(getWorkspaceUsersMock).toHaveBeenCalledWith({ |
| 51 | + workspaceId: 1585, |
| 52 | + includeRemoved: true, |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('caches active and include-removed variants separately', async () => { |
| 57 | + // First call seeds the active-only cache entry. |
| 58 | + await getWorkspaceUsers(1585) |
| 59 | + // Second call (same workspace, default flag) must hit cache → no extra SDK call. |
| 60 | + await getWorkspaceUsers(1585) |
| 61 | + expect(getWorkspaceUsersMock).toHaveBeenCalledTimes(1) |
| 62 | + |
| 63 | + // Switching to include-removed must NOT collide with the active entry. |
| 64 | + await getWorkspaceUsers(1585, { includeRemoved: true }) |
| 65 | + expect(getWorkspaceUsersMock).toHaveBeenCalledTimes(2) |
| 66 | + expect(getWorkspaceUsersMock).toHaveBeenLastCalledWith({ |
| 67 | + workspaceId: 1585, |
| 68 | + includeRemoved: true, |
| 69 | + }) |
| 70 | + |
| 71 | + // And the include-removed variant is itself cached. |
| 72 | + await getWorkspaceUsers(1585, { includeRemoved: true }) |
| 73 | + expect(getWorkspaceUsersMock).toHaveBeenCalledTimes(2) |
| 74 | + }) |
| 75 | +}) |
0 commit comments