mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
166 lines
6.7 KiB
TypeScript
166 lines
6.7 KiB
TypeScript
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<typeof import('@capacitor/core')>()),
|
|
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<typeof import('../../../src/native/iap')>()),
|
|
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<T extends HTMLElement>(selector: string): T {
|
|
const found = document.querySelector<T>(selector);
|
|
if (!found) throw new Error(`The app did not show ${selector}`);
|
|
return found;
|
|
}
|
|
|
|
function mountShell(): void {
|
|
vi.resetModules();
|
|
vi.stubGlobal('matchMedia', () => ({ matches: false, addEventListener: vi.fn() }));
|
|
vi.stubGlobal('cancelAnimationFrame', vi.fn());
|
|
document.body.append(Object.assign(document.createElement('main'), { id: 'shell' }));
|
|
mocks.agreement.mockReset();
|
|
mocks.purchase.mockClear();
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
document.body.replaceChildren();
|
|
});
|
|
|
|
it('keeps the store closed for a withdrawn agreement and asks again when the version changes', async () => {
|
|
mountShell();
|
|
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<HTMLElement>('.trial-badge').hidden).toBe(false));
|
|
fireEvent.click(element('button.discover'));
|
|
await waitFor(() => expect(element<HTMLButtonElement>('.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<HTMLInputElement>('.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<HTMLButtonElement>('.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'));
|
|
});
|
|
|
|
it('requires a sandbox agreement before presenting a store purchase', async () => {
|
|
mountShell();
|
|
const sandbox = 'https://store-sandbox.example.com';
|
|
vi.stubGlobal('__TREK_STORE_SANDBOX_ORIGIN__', sandbox);
|
|
const sandboxAgreement = {
|
|
status: 200,
|
|
data: { ...agreement.data, iapEnvironment: 'sandbox' },
|
|
};
|
|
mocks.agreement.mockImplementation(async ({ url }: { url: string }) => {
|
|
const path = new URL(url).pathname;
|
|
if (path === '/api/plans') {
|
|
return {
|
|
status: 200,
|
|
data: { ordering: 'open', plans: [{ id: 'plus', name: 'Plus', storageGb: 50, aiDocumentLimit: null }] },
|
|
};
|
|
}
|
|
if (path === '/api/slug/check') return { status: 200, data: { status: 'available' } };
|
|
return agreement;
|
|
});
|
|
|
|
await import('../../../native-shell/main');
|
|
await waitFor(() => expect(element<HTMLElement>('.trial-badge').hidden).toBe(false));
|
|
fireEvent.click(element('button.discover'));
|
|
await waitFor(() => expect(element<HTMLButtonElement>('.dock button.cta').hidden).toBe(false));
|
|
fireEvent.click(element('.dock button.cta'));
|
|
|
|
const consent = await waitFor(() => {
|
|
const input = element<HTMLInputElement>('.consent input');
|
|
expect(input.disabled).toBe(true);
|
|
return input;
|
|
});
|
|
const submit = element<HTMLButtonElement>('.dock button.cta');
|
|
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' } });
|
|
fireEvent.click(submit);
|
|
await waitFor(() => expect(element('.notice').textContent).toBeTruthy());
|
|
expect(mocks.purchase).not.toHaveBeenCalled();
|
|
|
|
mocks.agreement.mockImplementation(async ({ url }: { url: string }) =>
|
|
new URL(url).pathname === '/api/slug/check' ? { status: 200, data: { status: 'available' } } : sandboxAgreement
|
|
);
|
|
fireEvent.click(submit);
|
|
await waitFor(() => expect(consent.disabled).toBe(false));
|
|
fireEvent.click(consent);
|
|
fireEvent.click(submit);
|
|
await waitFor(() => expect(mocks.purchase).toHaveBeenCalledExactlyOnceWith('cloud.plus.monthly', '$11.99'));
|
|
for (const [options] of mocks.agreement.mock.calls) {
|
|
expect(new URL(options.url).origin).toBe(sandbox);
|
|
expect(options.disableRedirects).toBe(true);
|
|
}
|
|
});
|