diff --git a/apps/stock/README.md b/apps/stock/README.md index dfac2f6e..7ccd06ef 100644 --- a/apps/stock/README.md +++ b/apps/stock/README.md @@ -32,6 +32,8 @@ bun run --cwd apps/stock/orchestrator dev Run these in separate terminals from the repository root. The frontend alone does not run backtests; the API and orchestrator handle admission and execution. Ingestion and pipeline are additional entrypoints when you need to collect source data or synchronize the catalog. +Open `http://localhost:5173` and sign in with `STOCK_BOT_ADMIN_TOKEN` from the root `.env`. Both `STOCK_BOT_ADMIN_TOKEN` and `STOCK_BOT_SERVICE_TOKEN` must be configured as distinct values of at least 32 characters before starting HTTP services. The API uses port `2003`; the orchestrator uses `2004`. The frontend has a fixed port to avoid colliding with Docker's Grafana (`3000`) and Bull Board (`3001`) or silently moving to an unapproved browser origin. + Stock configuration is loaded by [config-instance.ts](config/src/config-instance.ts), with stock-specific validation in [stock-app.schema.ts](config/src/schemas/stock-app.schema.ts). HTTP authentication/session behavior is in the shared [security module](../../libs/core/di/src/http/security.ts). Keep backend credentials out of frontend build variables. ## Supported execution diff --git a/apps/stock/web-app/.env b/apps/stock/web-app/.env index 7b014ba0..1a3eb788 100644 --- a/apps/stock/web-app/.env +++ b/apps/stock/web-app/.env @@ -1,9 +1,9 @@ -# API Configuration -VITE_API_BASE_URL=http://localhost:2003 -VITE_DATA_SERVICE_URL=http://localhost:3001 -VITE_PORTFOLIO_SERVICE_URL=http://localhost:3002 -VITE_STRATEGY_SERVICE_URL=http://localhost:3003 -VITE_EXECUTION_SERVICE_URL=http://localhost:3004 - -# Environment +# API Configuration +VITE_API_BASE_URL= +VITE_DATA_SERVICE_URL=http://localhost:3001 +VITE_PORTFOLIO_SERVICE_URL=http://localhost:3002 +VITE_STRATEGY_SERVICE_URL=http://localhost:3003 +VITE_EXECUTION_SERVICE_URL=http://localhost:3004 + +# Environment VITE_NODE_ENV=development \ No newline at end of file diff --git a/apps/stock/web-app/.env.example b/apps/stock/web-app/.env.example index 79c70873..8dd6a89d 100644 --- a/apps/stock/web-app/.env.example +++ b/apps/stock/web-app/.env.example @@ -1,6 +1,7 @@ # API Configuration -# Web API service URL (default port: 2003) -VITE_API_BASE_URL=http://localhost:2003 +# Leave blank to use this browser's hostname on port 2003 in development. +# Set an explicit URL only when the API is hosted elsewhere. +VITE_API_BASE_URL= # Other services (if needed in the future) VITE_DATA_SERVICE_URL=http://localhost:3001 @@ -9,4 +10,4 @@ VITE_STRATEGY_SERVICE_URL=http://localhost:3003 VITE_EXECUTION_SERVICE_URL=http://localhost:3004 # Environment -VITE_NODE_ENV=development \ No newline at end of file +VITE_NODE_ENV=development diff --git a/apps/stock/web-app/README.md b/apps/stock/web-app/README.md index 8fab6a22..1d7d0f70 100644 --- a/apps/stock/web-app/README.md +++ b/apps/stock/web-app/README.md @@ -43,7 +43,7 @@ bun run type-check These start Vite development, build static assets, preview a build, and check frontend types respectively; run the command you need. React, TypeScript, Vite, Tailwind, Headless UI and Recharts are the main UI dependencies. See [package.json](package.json) for scripts and pinned ranges. -Vite is configured for `127.0.0.1:3000`. The API origin is `VITE_API_BASE_URL` when supplied; otherwise development uses the browser's hostname on port `2003`, and production uses the page's own origin. The dev server has no configured API proxy. The backend must be running and allow the frontend origin for authenticated cross-origin requests. +Vite is configured for `127.0.0.1:5173`, with a fixed port so it cannot silently move to an origin the API does not allow. Port `3000` belongs to Grafana in the Docker stack. Open `http://localhost:5173` or `http://127.0.0.1:5173`. The API origin is `VITE_API_BASE_URL` when supplied; the checked-in value is blank, so development follows the browser's hostname on port `2003`, and production uses the page's own origin. This keeps the session cookie on the same hostname: mixing `127.0.0.1` and `localhost` can prevent sign-in from persisting. Restart Vite after changing environment configuration. The dev server has no configured API proxy. The backend must be running and allow the frontend origin for authenticated cross-origin requests. Browser authentication uses HttpOnly session cookies. `VITE_API_BASE_URL` is a public address, not a place for service credentials. Research runs also need the orchestrator and its configured infrastructure; starting Vite alone supplies the interface only. diff --git a/apps/stock/web-app/src/utils/chartErrorHandler.ts b/apps/stock/web-app/src/utils/chartErrorHandler.ts index 9d4e672d..e4f079d7 100644 --- a/apps/stock/web-app/src/utils/chartErrorHandler.ts +++ b/apps/stock/web-app/src/utils/chartErrorHandler.ts @@ -2,15 +2,16 @@ import { browserLog } from '@/utils/browserLog'; // Global error handler for chart disposal errors export function setupChartErrorHandler() { - // Only suppress chart disposal errors during page unload + // pagehide works with back/forward caching and documents that disallow unload. + // Restore normal reporting if the browser later restores this same document. let isUnloading = false; - window.addEventListener('beforeunload', () => { + window.addEventListener('pagehide', () => { isUnloading = true; }); - window.addEventListener('unload', () => { - isUnloading = true; + window.addEventListener('pageshow', () => { + isUnloading = false; }); // Intercept console errors for chart disposal diff --git a/apps/stock/web-app/vite.config.ts b/apps/stock/web-app/vite.config.ts index b9984b1c..fbbe2ee7 100644 --- a/apps/stock/web-app/vite.config.ts +++ b/apps/stock/web-app/vite.config.ts @@ -11,7 +11,8 @@ export default defineConfig({ }, }, server: { - port: 3000, + port: 5173, + strictPort: true, host: '127.0.0.1', }, }); diff --git a/test/integration/chart-error-handler.test.ts b/test/integration/chart-error-handler.test.ts new file mode 100644 index 00000000..3bd41b59 --- /dev/null +++ b/test/integration/chart-error-handler.test.ts @@ -0,0 +1,57 @@ +import { expect, test } from 'bun:test'; +import { browserLog } from '../../apps/stock/web-app/src/utils/browserLog'; +import { setupChartErrorHandler } from '../../apps/stock/web-app/src/utils/chartErrorHandler'; + +test('chart cleanup uses page lifecycle events and restores error reporting after history navigation', () => { + const previousWindow = Object.getOwnPropertyDescriptor(globalThis, 'window'); + const previousError = browserLog.error; + const target = new EventTarget(); + const registered: string[] = []; + const messages: unknown[][] = []; + Object.defineProperty(globalThis, 'window', { + configurable: true, + value: { + addEventListener(type: string, callback: EventListener) { + registered.push(type); + target.addEventListener(type, callback); + }, + }, + }); + browserLog.error = (...args) => messages.push(args); + const chartError = () => { + const event = new Event('error', { cancelable: true }); + Object.defineProperty(event, 'error', { value: new Error('Object is disposed') }); + target.dispatchEvent(event); + return event; + }; + try { + setupChartErrorHandler(); + expect(registered).toContain('pagehide'); + expect(registered).toContain('pageshow'); + expect(registered).not.toContain('unload'); + expect(registered).not.toContain('beforeunload'); + + browserLog.error('Object is disposed'); + expect(messages).toHaveLength(1); + expect(chartError().defaultPrevented).toBe(false); + + target.dispatchEvent(new Event('pagehide')); + browserLog.error('Object is disposed'); + expect(messages).toHaveLength(1); + expect(chartError().defaultPrevented).toBe(true); + browserLog.error('Unrelated application failure'); + expect(messages).toHaveLength(2); + + target.dispatchEvent(new Event('pageshow')); + browserLog.error('Object is disposed'); + expect(messages).toHaveLength(3); + expect(chartError().defaultPrevented).toBe(false); + } finally { + browserLog.error = previousError; + if (previousWindow) { + Object.defineProperty(globalThis, 'window', previousWindow); + } else { + Reflect.deleteProperty(globalThis, 'window'); + } + } +});