import { beforeEach, describe, expect, it, vi } from 'vitest'; import { createWorksRechargeOrder, fetchWorksBillingSummary, fetchWorksPointHistory, fetchWorksRechargeOrder, openWorksBillingAccount } from '@/lib/works-billing'; import { billingSummary, rechargeOrder } from '../fixtures/permanent-points'; const host = vi.hoisted(() => ({ fetch: vi.fn(), ipc: vi.fn() })); vi.mock('@/lib/host-api', () => ({ hostApiFetch: host.fetch })); vi.mock('@/lib/api-client', () => ({ invokeIpc: host.ipc })); beforeEach(() => { host.fetch.mockReset(); host.ipc.mockReset(); }); describe('Main-owned billing client', () => { it('reads the current Main session wallet without Renderer credentials', async () => { const summary = billingSummary(); host.fetch.mockResolvedValue({ success: true, data: summary }); await expect(fetchWorksBillingSummary()).resolves.toEqual(summary); expect(host.fetch).toHaveBeenCalledWith('/api/works/billing/me', undefined); }); it('sends only the selected product and explicit attempt identity', async () => { const order = rechargeOrder(); host.fetch.mockResolvedValue({ success: true, data: order }); await expect(createWorksRechargeOrder('recharge-10', 'attempt-1')).resolves.toEqual(order); expect(host.fetch).toHaveBeenCalledWith('/api/works/billing/recharge/orders', { method: 'POST', body: JSON.stringify({ productId: 'recharge-10', requestId: 'attempt-1' }), }); }); it('encodes order ids and requests the selected ledger page', async () => { host.fetch.mockResolvedValue({ success: true, data: {} }); await fetchWorksRechargeOrder('order one'); await fetchWorksPointHistory(20); expect(host.fetch).toHaveBeenNthCalledWith(1, '/api/works/billing/recharge/orders/order%20one', undefined); expect(host.fetch).toHaveBeenNthCalledWith(2, '/api/works/billing/points/transactions?offset=20', undefined); }); it('opens the fixed web account page for payment-source management', async () => { await openWorksBillingAccount(); expect(host.ipc).toHaveBeenCalledWith('shell:openExternal', 'https://square.nianxx.cn/#profile'); }); });