|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + handlers: new Map<string, () => unknown>(), |
| 5 | + fetch: vi.fn(), |
| 6 | + getCookie: vi.fn(() => 'better-auth.session=token'), |
| 7 | + getSession: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('electron', () => ({ |
| 11 | + ipcMain: { |
| 12 | + handle: (channel: string, handler: () => unknown) => mocks.handlers.set(channel, handler), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('../cloud-auth/client', () => ({ |
| 17 | + authClient: { |
| 18 | + getCookie: mocks.getCookie, |
| 19 | + getSession: mocks.getSession, |
| 20 | + }, |
| 21 | + CLOUD_API_URL: 'https://api.linkcode.test', |
| 22 | +})); |
| 23 | + |
| 24 | +const summary = { |
| 25 | + denomination: 'nano_usd', |
| 26 | + availableAmount: '12500000000', |
| 27 | + reservedAmount: '500000000', |
| 28 | + displayBalance: { currency: 'USD', amount: '12.50' }, |
| 29 | +}; |
| 30 | + |
| 31 | +beforeEach(() => { |
| 32 | + mocks.handlers.clear(); |
| 33 | + mocks.fetch.mockReset(); |
| 34 | + mocks.getSession.mockReset(); |
| 35 | + mocks.getSession.mockResolvedValue({ |
| 36 | + data: { session: { activeOrganizationId: 'org/1' } }, |
| 37 | + error: null, |
| 38 | + }); |
| 39 | + vi.stubGlobal('fetch', mocks.fetch); |
| 40 | +}); |
| 41 | + |
| 42 | +afterEach(() => vi.unstubAllGlobals()); |
| 43 | + |
| 44 | +describe('desktop Cloud billing bridge', () => { |
| 45 | + it('reads and validates the active organization balance with the main-process session', async () => { |
| 46 | + mocks.fetch.mockResolvedValue(Response.json(summary, { status: 200, statusText: 'OK' })); |
| 47 | + const { registerCloudBillingBridge } = await import('../cloud-auth/billing'); |
| 48 | + const { CLOUD_GET_BILLING_SUMMARY_CHANNEL } = await import('../../shared/cloud'); |
| 49 | + registerCloudBillingBridge(); |
| 50 | + |
| 51 | + await expect(mocks.handlers.get(CLOUD_GET_BILLING_SUMMARY_CHANNEL)?.()).resolves.toEqual( |
| 52 | + summary, |
| 53 | + ); |
| 54 | + expect(mocks.fetch).toHaveBeenCalledWith( |
| 55 | + 'https://api.linkcode.test/organizations/org%2F1/billing/summary', |
| 56 | + { headers: { cookie: 'better-auth.session=token' } }, |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns null without an active organization', async () => { |
| 61 | + mocks.getSession.mockResolvedValue({ data: { session: {} }, error: null }); |
| 62 | + const { getCloudBillingSummary } = await import('../cloud-auth/billing'); |
| 63 | + |
| 64 | + await expect(getCloudBillingSummary()).resolves.toBeNull(); |
| 65 | + expect(mocks.fetch).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('rejects a non-integer nano-USD balance', async () => { |
| 69 | + mocks.fetch.mockResolvedValue( |
| 70 | + Response.json({ ...summary, availableAmount: '12.50' }, { status: 200 }), |
| 71 | + ); |
| 72 | + const { getCloudBillingSummary } = await import('../cloud-auth/billing'); |
| 73 | + |
| 74 | + await expect(getCloudBillingSummary()).rejects.toThrow(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments