import { describe, expect, test } from 'bun:test' describe('server config', () => { test('server config function is exported', async () => { try { const mod = await import('../src/index') expect(mod.server).toBeDefined() expect(typeof mod.server).toBe('function') } catch (e: any) { // Server module may fail due to missing plugin deps — verify it's a known issue expect(e.message).toMatch(/not found in module|Cannot find/) } }) }) describe('server maintenance', () => { test('maintenanceFilePath is exported', async () => { const mod = await import('../src/maintenance') expect(typeof mod.maintenanceFilePath).toBe('function') }) test('maintenanceFilePath returns a string path', async () => { const { maintenanceFilePath } = await import('../src/maintenance') const path = maintenanceFilePath() expect(typeof path).toBe('string') expect(path).toContain('framework/down') }) test('isAllowedIp returns false for empty allowed list', async () => { const { isAllowedIp } = await import('../src/maintenance') expect(isAllowedIp('192.168.1.1', [])).toBe(false) }) test('isAllowedIp allows localhost IPs', async () => { const { isAllowedIp } = await import('../src/maintenance') expect(isAllowedIp('127.0.0.1', ['10.0.0.1'])).toBe(true) expect(isAllowedIp('::1', ['10.0.0.1'])).toBe(true) expect(isAllowedIp('localhost', ['10.0.0.1'])).toBe(true) }) test('isAllowedIp can reject proxy loopback outside development', async () => { const { isAllowedIp } = await import('../src/maintenance') expect(isAllowedIp('127.0.0.1', [], false)).toBe(false) expect(isAllowedIp('::1', [], false)).toBe(false) expect(isAllowedIp('127.0.0.1', ['127.0.0.1'], false)).toBe(true) }) test('isAllowedIp checks against allowed list', async () => { const { isAllowedIp } = await import('../src/maintenance') expect(isAllowedIp('10.0.0.1', ['10.0.0.1', '10.0.0.2'])).toBe(true) expect(isAllowedIp('10.0.0.3', ['10.0.0.1', '10.0.0.2'])).toBe(false) }) test('hasValidBypassCookie checks cookie value', async () => { const { hasValidBypassCookie } = await import('../src/maintenance') expect(hasValidBypassCookie({ stacks_maintenance_bypass: 'secret123' }, 'secret123')).toBe(true) expect(hasValidBypassCookie({ stacks_maintenance_bypass: 'wrong' }, 'secret123')).toBe(false) expect(hasValidBypassCookie({}, 'secret123')).toBe(false) expect(hasValidBypassCookie({ stacks_coming_soon_bypass: 'secret123' }, 'secret123', 'coming-soon')).toBe(true) }) test('isSecretPath matches secret URL path', async () => { const { isSecretPath } = await import('../src/maintenance') expect(isSecretPath('/my-secret', 'my-secret')).toBe(true) expect(isSecretPath('/my-secret/extra', 'my-secret')).toBe(true) expect(isSecretPath('/other', 'my-secret')).toBe(false) }) test('maintenanceHtml returns valid HTML', async () => { const { maintenanceHtml } = await import('../src/maintenance') const html = maintenanceHtml({ time: Date.now(), message: 'Test maintenance' }) expect(html).toContain('') expect(html).toContain('Test maintenance') expect(html).toContain('Down for maintenance') }) test('maintenanceHtml carries no other project\'s brand', async () => { // Every app renders this page. It used to be stacksjs.com's park theme, // loading fonts and illustrations only stacksjs.com's public/ has. const { maintenanceHtml } = await import('../src/maintenance') const previous = process.env.APP_NAME delete process.env.APP_NAME try { for (const mode of ['maintenance', 'coming-soon'] as const) { const html = maintenanceHtml({ mode, time: Date.now() }) expect(html).not.toContain('Stacks') expect(html).not.toContain('/assets/') expect(html).not.toContain('url(') } } finally { if (previous !== undefined) process.env.APP_NAME = previous } }) test('maintenanceHtml names the app from APP_NAME', async () => { const { maintenanceHtml } = await import('../src/maintenance') const previous = process.env.APP_NAME process.env.APP_NAME = 'Acme ' try { const html = maintenanceHtml({ time: Date.now() }) expect(html).toContain('

Acme <Co>

') expect(html).toContain('Down for maintenance - Acme <Co>') } finally { if (previous === undefined) delete process.env.APP_NAME else process.env.APP_NAME = previous } }) test('maintenanceHtml supports coming soon mode', async () => { const { maintenanceHtml } = await import('../src/maintenance') const html = maintenanceHtml({ mode: 'coming-soon', time: Date.now(), message: 'Launching soon' }) expect(html).toContain('Coming soon') expect(html).toContain('Launching soon') }) test('activeSiteModePayload reads APP_COMING_SOON fallback', async () => { const { activeSiteModePayload } = await import('../src/maintenance') const previousMode = process.env.APP_COMING_SOON const previousSecret = process.env.APP_COMING_SOON_SECRET process.env.APP_COMING_SOON = 'true' process.env.APP_COMING_SOON_SECRET = 'trailhead' const payload = await activeSiteModePayload() expect(payload?.mode).toBe('coming-soon') expect(payload?.secret).toBe('trailhead') if (previousMode === undefined) delete process.env.APP_COMING_SOON else process.env.APP_COMING_SOON = previousMode if (previousSecret === undefined) delete process.env.APP_COMING_SOON_SECRET else process.env.APP_COMING_SOON_SECRET = previousSecret }) test('maintenanceGate secret path sets bypass cookie and redirects', async () => { const { maintenanceGate } = await import('../src/maintenance') const previousMode = process.env.APP_COMING_SOON const previousSecret = process.env.APP_COMING_SOON_SECRET process.env.APP_COMING_SOON = 'true' process.env.APP_COMING_SOON_SECRET = 'trailhead' // Coming-soon mode lands on `/?preview=` so the client-side // site-mode gate unlocks in the same visit. const comingSoonResp = await maintenanceGate(new Request('http://localhost/trailhead')) expect(comingSoonResp?.status).toBe(302) expect(comingSoonResp?.headers.get('Location')).toBe('/?preview=trailhead') expect(comingSoonResp?.headers.get('Set-Cookie')).toContain('stacks_coming_soon_bypass=trailhead') delete process.env.APP_COMING_SOON if (previousMode !== undefined) process.env.APP_COMING_SOON = previousMode if (previousSecret === undefined) delete process.env.APP_COMING_SOON_SECRET else process.env.APP_COMING_SOON_SECRET = previousSecret }) test('maintenanceGate does not treat a production proxy as a localhost bypass', async () => { const { maintenanceGate } = await import('../src/maintenance') const previousMode = process.env.APP_COMING_SOON const previousAppEnv = process.env.APP_ENV process.env.APP_COMING_SOON = 'true' process.env.APP_ENV = 'production' const response = await maintenanceGate(new Request('http://127.0.0.1/')) expect(response?.status).toBe(302) expect(response?.headers.get('Location')).toBe('/coming-soon') if (previousMode === undefined) delete process.env.APP_COMING_SOON else process.env.APP_COMING_SOON = previousMode if (previousAppEnv === undefined) delete process.env.APP_ENV else process.env.APP_ENV = previousAppEnv }) test('maintenanceGate returns synchronously when no site mode is active', async () => { const { maintenanceGate } = await import('../src/maintenance') const previousMaintenance = process.env.APP_MAINTENANCE const previousComingSoon = process.env.APP_COMING_SOON process.env.APP_MAINTENANCE = 'false' process.env.APP_COMING_SOON = 'false' try { const result = maintenanceGate(new Request('http://localhost/')) expect(result).not.toBeInstanceOf(Promise) expect(result).toBeNull() } finally { if (previousMaintenance === undefined) delete process.env.APP_MAINTENANCE else process.env.APP_MAINTENANCE = previousMaintenance if (previousComingSoon === undefined) delete process.env.APP_COMING_SOON else process.env.APP_COMING_SOON = previousComingSoon } }) test('maintenanceHtml includes retry info when provided', async () => { const { maintenanceHtml } = await import('../src/maintenance') const html = maintenanceHtml({ time: Date.now(), retry: 300 }) expect(html).toContain('Expected back in about 5 minutes') }) test('maintenanceResponse returns 503 by default', async () => { const { maintenanceResponse } = await import('../src/maintenance') const resp = maintenanceResponse({ time: Date.now() }) expect(resp.status).toBe(503) expect(resp.headers.get('Content-Type')).toContain('text/html') }) test('siteModeResponse returns 200 for coming soon by default', async () => { const { siteModeResponse } = await import('../src/maintenance') const resp = siteModeResponse({ mode: 'coming-soon', time: Date.now() }) expect(resp.status).toBe(200) expect(resp.headers.get('Content-Type')).toContain('text/html') }) test('maintenanceResponse redirects when redirect is set', async () => { const { maintenanceResponse } = await import('../src/maintenance') const resp = maintenanceResponse({ time: Date.now(), redirect: '/coming-soon' }) expect(resp.status).toBe(302) expect(resp.headers.get('Location')).toBe('/coming-soon') }) test('maintenanceResponse includes Retry-After header', async () => { const { maintenanceResponse } = await import('../src/maintenance') const resp = maintenanceResponse({ time: Date.now(), retry: 120 }) expect(resp.headers.get('Retry-After')).toBe('120') }) test('bypassCookieValue returns valid cookie string', async () => { const { bypassCookieValue } = await import('../src/maintenance') const cookie = bypassCookieValue('test-secret') expect(cookie).toContain('stacks_maintenance_bypass=test-secret') expect(cookie).toContain('HttpOnly') expect(cookie).toContain('SameSite=Lax') expect(bypassCookieValue('test-secret', 'coming-soon')).toContain('stacks_coming_soon_bypass=test-secret') }) }) describe('server proxy', () => { test('proxyToBackend and isApiBoundRequest are exported', async () => { try { const mod = await import('../src/index') expect(typeof mod.proxyToBackend).toBe('function') expect(typeof mod.isApiBoundRequest).toBe('function') } catch (e: any) { // Server module may fail due to missing plugin deps — verify it's a known issue expect(e.message).toMatch(/not found in module|Cannot find/) } }) }) describe('server controller base', () => { test('Controller class is exported', async () => { const mod = await import('../src/controllers/base') expect(mod.Controller).toBeDefined() expect(typeof mod.Controller).toBe('function') }) test('Controller can be instantiated', async () => { const { Controller } = await import('../src/controllers/base') const ctrl = new Controller() expect(ctrl).toBeDefined() expect(ctrl).toBeInstanceOf(Controller) }) })