import { fireEvent, waitFor } from '@testing-library/react'; import { afterEach, expect, it, vi } from 'vitest'; const mocks = vi.hoisted(() => ({ agreement: vi.fn(), purchase: vi.fn().mockResolvedValue({ kind: 'cancelled' }), })); vi.mock('@capacitor/app', () => ({ App: { addListener: vi.fn() } })); vi.mock('@capacitor/core', async (importOriginal) => ({ ...(await importOriginal()), CapacitorHttp: { get: mocks.agreement }, })); vi.mock('@capacitor/splash-screen', () => ({ SplashScreen: { hide: vi.fn().mockResolvedValue(undefined) } })); vi.mock('../../../native-shell/scenery', () => ({ WELCOME_CAMERA: [0, 0], cameraForPlan: () => [0, 0], startScenery: () => null, })); vi.mock('../../../src/native/trekShell', () => ({ TrekShell: { getServer: vi.fn().mockResolvedValue({ url: null }), requestServer: vi.fn(async ({ url }: { url: string }) => ({ status: 200, url, data: new URL(url).pathname === '/api/plans' ? { ordering: 'open', plans: [{ id: 'plus', name: 'Plus', storageGb: 50, aiDocumentLimit: null }], } : { available: true }, })), }, })); vi.mock('../../../src/native/iap', async (importOriginal) => ({ ...(await importOriginal()), getIapClient: () => ({ isAvailable: async () => true, products: async () => new Map([['cloud.plus.monthly', { displayPrice: '$11.99', freeTrial: true }]]), held: async () => false, onTransaction: vi.fn(), unfinished: async () => null, purchase: mocks.purchase, }), })); const agreement = { status: 200, data: { version: '2026-12-01', deviations: 'First agreement' } }; function element(selector: string): T { const found = document.querySelector(selector); if (!found) throw new Error(`The app did not show ${selector}`); return found; } afterEach(() => { vi.unstubAllGlobals(); document.body.replaceChildren(); }); it('keeps the store closed for a withdrawn agreement and asks again when the version changes', async () => { vi.stubGlobal('matchMedia', () => ({ matches: false, addEventListener: vi.fn() })); vi.stubGlobal('cancelAnimationFrame', vi.fn()); document.body.append(Object.assign(document.createElement('main'), { id: 'shell' })); const changed = { status: 200, data: { version: '2026-12-02', deviations: 'Updated agreement' } }; mocks.agreement .mockResolvedValueOnce(agreement) .mockResolvedValueOnce({ status: 503, data: { code: 'ordering-closed' } }) .mockResolvedValueOnce(agreement) .mockResolvedValue(changed); await import('../../../native-shell/main'); await waitFor(() => expect(element('.trial-badge').hidden).toBe(false)); fireEvent.click(element('button.discover')); await waitFor(() => expect(element('.dock button.cta').hidden).toBe(false)); fireEvent.click(element('.dock button.cta')); const consent = await waitFor(() => { expect(element('.consent-text').textContent).toBe(agreement.data.deviations); return element('.consent input'); }); fireEvent.input(element('.form input[type="text"]'), { target: { value: 'sample-journey' } }); fireEvent.input(element('.form input[type="email"]'), { target: { value: 'buyer@example.invalid' } }); fireEvent.input(element('.form input[type="password"]'), { target: { value: 'Training#Pass123' } }); const submit = element('.dock button.cta'); fireEvent.click(consent); fireEvent.click(submit); await waitFor(() => { expect(consent.disabled).toBe(true); expect(consent.checked).toBe(false); }); expect(mocks.purchase).not.toHaveBeenCalled(); fireEvent.click(submit); await waitFor(() => expect(element('.consent-text').textContent).toBe(agreement.data.deviations)); fireEvent.click(consent); fireEvent.click(submit); await waitFor(() => expect(element('.consent-text').textContent).toBe(changed.data.deviations)); expect(consent.checked).toBe(false); expect(mocks.purchase).not.toHaveBeenCalled(); fireEvent.click(consent); fireEvent.click(submit); await waitFor(() => expect(mocks.purchase).toHaveBeenCalledExactlyOnceWith('cloud.plus.monthly', '$11.99')); });