import { readFileSync } from 'node:fs' import { describe, expect, it, vi } from 'vitest' import consoleWorker, { handleConsoleRequest } from '../cloudflare_workers/console/index.ts' import { parseJsonc } from '../scripts/generate-plugin-route-hosts.ts' interface ConsoleWranglerConfig { main?: string assets?: { binding?: string, directory?: string, not_found_handling?: string, run_worker_first?: unknown } } function consoleWranglerConfig(): ConsoleWranglerConfig { return parseJsonc(readFileSync(new URL('../wrangler.jsonc', import.meta.url), 'utf8')) as ConsoleWranglerConfig } function assetsBinding(body = '') { return { fetch: vi.fn(async (_request: Request) => new Response(body, { status: 200, headers: { 'content-type': 'text/html' } })), } } describe('console worker for requests that match no file in dist', () => { it('is wired into the console wrangler config with an ASSETS binding and SPA fallback', () => { const config = consoleWranglerConfig() expect(config.main).toBe('./cloudflare_workers/console/index.ts') expect(config.assets?.binding).toBe('ASSETS') expect(config.assets?.directory).toBe('./dist') expect(config.assets?.not_found_handling).toBe('single-page-application') // run_worker_first would put every real asset behind the Worker; keep asset first routing. expect(config.assets?.run_worker_first).toBeUndefined() }) it('returns an uncached 404 for a missing build chunk instead of the SPA shell', async () => { const assets = assetsBinding() const response = await handleConsoleRequest(new Request('https://console.capgo.app/assets/settings-UAPX1pFM.js'), { ASSETS: assets }) expect(response.status).toBe(404) expect(response.headers.get('cache-control')).toBe('no-store') expect(response.headers.get('content-type')).not.toContain('text/html') expect(assets.fetch).not.toHaveBeenCalled() }) it('returns an uncached 404 for missing nested assets and CSS chunks', async () => { const assets = assetsBinding() for (const path of ['/assets/index-DcurNenV.css', '/assets/fonts/inter-latin.woff2', '/assets/icons~connect~settings-BFzaI-_F.js?v=1']) { const response = await handleConsoleRequest(new Request(`https://console.capgo.app${path}`), { ASSETS: assets }) expect(response.status).toBe(404) expect(response.headers.get('cache-control')).toBe('no-store') } expect(assets.fetch).not.toHaveBeenCalled() }) it('keeps the SPA fallback for app routes and paths that only look like assets', async () => { for (const path of ['/settings/account', '/settings/organization', '/app/com.demo.app/settings', '/assetsx/file.js', '/']) { const assets = assetsBinding('spa shell') const request = new Request(`https://console.capgo.app${path}`) const response = await consoleWorker.fetch(request, { ASSETS: assets }) expect(assets.fetch).toHaveBeenCalledWith(request) expect(response.status).toBe(200) expect(await response.text()).toBe('spa shell') } }) })