106 lines
3.9 KiB
TypeScript
106 lines
3.9 KiB
TypeScript
import { afterAll, afterEach, beforeAll, beforeEach, expect, test } from 'bun:test';
|
|
import { GlobalWindow } from 'happy-dom';
|
|
import { act, createElement } from 'react';
|
|
import type { Root } from 'react-dom/client';
|
|
|
|
const browser = new GlobalWindow({ url: 'http://localhost:5173' });
|
|
const keys = ['window', 'document', 'fetch', 'ResizeObserver', 'IS_REACT_ACT_ENVIRONMENT'] as const;
|
|
const originals = new Map(keys.map(key => [key, Object.getOwnPropertyDescriptor(globalThis, key)]));
|
|
let createRoot: typeof import('react-dom/client').createRoot;
|
|
let App: typeof import('../../apps/stock/web-app/src/app/App').App;
|
|
let root: Root | undefined;
|
|
let host: HTMLDivElement;
|
|
let authenticated = true;
|
|
const paths: string[] = [];
|
|
|
|
async function flush(action: () => void = () => {}) {
|
|
await act(async () => {
|
|
action();
|
|
await Promise.resolve();
|
|
});
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
const values = {
|
|
window: browser,
|
|
document: browser.document,
|
|
ResizeObserver: browser.ResizeObserver,
|
|
IS_REACT_ACT_ENVIRONMENT: true,
|
|
};
|
|
for (const [key, value] of Object.entries(values)) {
|
|
Object.defineProperty(globalThis, key, { configurable: true, value });
|
|
}
|
|
({ createRoot } = await import('react-dom/client'));
|
|
({ App } = await import('../../apps/stock/web-app/src/app/App'));
|
|
});
|
|
|
|
beforeEach(() => {
|
|
authenticated = true;
|
|
paths.length = 0;
|
|
const request = async (input: RequestInfo | URL): Promise<Response> => {
|
|
const path = new URL(
|
|
typeof input === 'string' ? input : input instanceof URL ? input.href : input.url
|
|
).pathname;
|
|
paths.push(path);
|
|
if (path === '/api/auth/session') {
|
|
return Response.json(
|
|
{ authenticated, role: authenticated ? 'admin' : null },
|
|
{ status: authenticated ? 200 : 401 }
|
|
);
|
|
}
|
|
if (path === '/api/v2/backtests') {
|
|
return Response.json([]);
|
|
}
|
|
throw new Error(`Unexpected home-page request: ${path}`);
|
|
};
|
|
Object.defineProperty(globalThis, 'fetch', { configurable: true, value: request });
|
|
browser.fetch = request as unknown as typeof browser.fetch;
|
|
const element = browser.document.createElement('div');
|
|
browser.document.body.append(element);
|
|
host = element as unknown as HTMLDivElement;
|
|
root = createRoot(host);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await flush(() => root?.unmount());
|
|
root = undefined;
|
|
host.remove();
|
|
});
|
|
|
|
afterAll(() => {
|
|
for (const key of keys) {
|
|
const descriptor = originals.get(key);
|
|
if (descriptor) {
|
|
Object.defineProperty(globalThis, key, descriptor);
|
|
} else {
|
|
Reflect.deleteProperty(globalThis, key);
|
|
}
|
|
}
|
|
browser.happyDOM.abort();
|
|
});
|
|
|
|
for (const path of ['/', '/dashboard', '/backtests']) {
|
|
test(`${path} opens the authenticated backtest workbench without demo portfolio data`, async () => {
|
|
browser.history.replaceState({}, '', path);
|
|
await flush(() => root!.render(createElement(App)));
|
|
expect(browser.location.pathname).toBe('/backtests');
|
|
expect(paths).toEqual(['/api/auth/session', '/api/v2/backtests']);
|
|
expect(host.textContent).toContain('Create and manage backtest configurations');
|
|
expect(host.textContent).toContain('No active backtests.');
|
|
expect(host.textContent).toContain('New Backtest');
|
|
expect(host.querySelector('a[href="/backtests"]')).not.toBeNull();
|
|
expect(host.querySelector('a[href="/dashboard"]')).toBeNull();
|
|
expect(host.textContent).not.toContain('Portfolio Value');
|
|
expect(host.textContent).not.toContain('Market data loading...');
|
|
expect(host.textContent).not.toContain('Welcome to Stock Bot Dashboard');
|
|
});
|
|
}
|
|
|
|
test('the research home still requires a session before requesting saved backtests', async () => {
|
|
authenticated = false;
|
|
browser.history.replaceState({}, '', '/');
|
|
await flush(() => root!.render(createElement(App)));
|
|
expect(host.textContent).toContain('Stock Bot access');
|
|
expect(paths).toEqual(['/api/auth/session']);
|
|
expect(host.textContent).not.toContain('New Backtest');
|
|
});
|