|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { net } from 'electron' |
| 3 | +import { readCursorAuth } from './cursor-auth' |
| 4 | +import { fetchCursorRateLimits } from './cursor-fetcher' |
| 5 | + |
| 6 | +vi.mock('electron', () => ({ net: { fetch: vi.fn() } })) |
| 7 | +vi.mock('./cursor-auth', () => ({ readCursorAuth: vi.fn() })) |
| 8 | +const auth = { |
| 9 | + status: 'ok', |
| 10 | + cookie: 'WorkosCursorSessionToken=synthetic', |
| 11 | + fingerprint: 'account-a' |
| 12 | +} as const |
| 13 | +beforeEach(() => { |
| 14 | + vi.resetAllMocks() |
| 15 | + vi.mocked(readCursorAuth).mockReturnValue(auth) |
| 16 | +}) |
| 17 | + |
| 18 | +describe('Cursor usage request', () => { |
| 19 | + it('reads the dashboard with bounded cancellation and never publishes the credential', async () => { |
| 20 | + vi.mocked(net.fetch).mockResolvedValue( |
| 21 | + new Response(JSON.stringify({ individualUsage: { plan: { totalPercentUsed: 35 } } })) |
| 22 | + ) |
| 23 | + const result = await fetchCursorRateLimits() |
| 24 | + expect(result.monthly?.usedPercent).toBe(35) |
| 25 | + expect(result.session).toBeNull() |
| 26 | + expect(JSON.stringify(result)).not.toContain(auth.cookie) |
| 27 | + expect(net.fetch).toHaveBeenCalledWith( |
| 28 | + 'https://cursor.com/api/usage-summary', |
| 29 | + expect.objectContaining({ |
| 30 | + redirect: 'error', |
| 31 | + signal: expect.any(AbortSignal), |
| 32 | + headers: { Cookie: auth.cookie, Accept: 'application/json' } |
| 33 | + }) |
| 34 | + ) |
| 35 | + }) |
| 36 | + it('makes no network request without credentials', async () => { |
| 37 | + vi.mocked(readCursorAuth).mockReturnValue({ status: 'missing' }) |
| 38 | + expect((await fetchCursorRateLimits()).status).toBe('unavailable') |
| 39 | + expect(net.fetch).not.toHaveBeenCalled() |
| 40 | + }) |
| 41 | + it.each([ |
| 42 | + [401, 'stale-token'], |
| 43 | + [403, 'stale-token'], |
| 44 | + [429, 'rate-limited'], |
| 45 | + [500, 'server'] |
| 46 | + ])('classifies HTTP %s without leaking the response', async (status, kind) => { |
| 47 | + vi.mocked(net.fetch).mockResolvedValue( |
| 48 | + new Response('secret response', { status: Number(status) }) |
| 49 | + ) |
| 50 | + const result = await fetchCursorRateLimits() |
| 51 | + expect(result.usageMetadata?.failureKind).toBe(kind) |
| 52 | + expect(JSON.stringify(result)).not.toContain('secret response') |
| 53 | + }) |
| 54 | + it('does not interpret an unknown payload as unused quota', async () => { |
| 55 | + vi.mocked(net.fetch).mockResolvedValue(new Response('{}')) |
| 56 | + expect((await fetchCursorRateLimits()).usageMetadata?.failureKind).toBe('usage-unavailable') |
| 57 | + }) |
| 58 | + it('drops an in-flight result when the account changes', async () => { |
| 59 | + vi.mocked(net.fetch).mockResolvedValue( |
| 60 | + new Response(JSON.stringify({ individualUsage: { plan: { totalPercentUsed: 35 } } })) |
| 61 | + ) |
| 62 | + vi.mocked(readCursorAuth) |
| 63 | + .mockReturnValueOnce(auth) |
| 64 | + .mockReturnValueOnce({ ...auth, fingerprint: 'account-b' }) |
| 65 | + const result = await fetchCursorRateLimits() |
| 66 | + expect(result.status).toBe('error') |
| 67 | + expect(result.monthly).toBeNull() |
| 68 | + expect(result.usageMetadata?.authProvenance).toBeUndefined() |
| 69 | + }) |
| 70 | + it('redacts exceptions and forwards caller cancellation', async () => { |
| 71 | + vi.mocked(net.fetch).mockRejectedValue(new Error(auth.cookie)) |
| 72 | + const controller = new AbortController() |
| 73 | + controller.abort() |
| 74 | + const result = await fetchCursorRateLimits({ signal: controller.signal }) |
| 75 | + expect(result.error).not.toContain(auth.cookie) |
| 76 | + expect(vi.mocked(net.fetch).mock.calls[0][1]?.signal?.aborted).toBe(true) |
| 77 | + }) |
| 78 | + it('honors Retry-After even for manual refreshes and releases it after account changes', async () => { |
| 79 | + vi.mocked(net.fetch).mockResolvedValue( |
| 80 | + new Response('', { status: 429, headers: { 'retry-after': '600' } }) |
| 81 | + ) |
| 82 | + const first = await fetchCursorRateLimits() |
| 83 | + expect(first.usageMetadata?.retryAtMs).toBeGreaterThan(Date.now()) |
| 84 | + const deferred = await fetchCursorRateLimits({ previous: first }) |
| 85 | + expect(deferred).toEqual(first) |
| 86 | + expect(net.fetch).toHaveBeenCalledTimes(1) |
| 87 | + vi.mocked(readCursorAuth).mockReturnValue({ ...auth, fingerprint: 'account-b' }) |
| 88 | + await fetchCursorRateLimits({ previous: first }) |
| 89 | + expect(net.fetch).toHaveBeenCalledTimes(2) |
| 90 | + }) |
| 91 | +}) |
0 commit comments