From 511978666a6646cf14e207f7592e02e8871cdb54 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 3 Jan 2026 11:49:41 -0500 Subject: [PATCH] Add mock API server for Playwright tests --- pikit-web/playwright.config.js | 2 +- pikit-web/tests/mock-api.js | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 pikit-web/tests/mock-api.js diff --git a/pikit-web/playwright.config.js b/pikit-web/playwright.config.js index 8a6b6bf..c030ea8 100644 --- a/pikit-web/playwright.config.js +++ b/pikit-web/playwright.config.js @@ -17,7 +17,7 @@ export default defineConfig({ trace: 'retain-on-failure', }, webServer: { - command: 'npm run dev', + command: 'node tests/mock-api.js & npm run dev', url: BASE_URL, reuseExistingServer: !process.env.CI, stdout: 'pipe', diff --git a/pikit-web/tests/mock-api.js b/pikit-web/tests/mock-api.js new file mode 100644 index 0000000..0a35fb8 --- /dev/null +++ b/pikit-web/tests/mock-api.js @@ -0,0 +1,85 @@ +import http from 'http'; + +const port = Number(process.env.PIKIT_MOCK_API_PORT || 4000); + +const updatesConfig = { + enabled: false, + scope: 'security', + cleanup: false, + bandwidth_limit_kbps: null, + auto_reboot: false, + reboot_time: '04:30', + reboot_with_users: false, + update_time: '04:00', + upgrade_time: '04:30', + state: { enabled: false }, +}; + +const routes = { + '/health': { ok: true }, + '/api/status': { + hostname: 'pikit-test', + uptime_seconds: 0, + load: [0, 0, 0], + memory_mb: { total: 1024, free: 1024 }, + disk_mb: { total: 1024, free: 1024 }, + cpu_temp_c: 35.0, + lan_ip: '127.0.0.1', + os_version: 'DietPi', + auto_updates_enabled: false, + auto_updates: { enabled: false }, + updates_config: updatesConfig, + reboot_required: false, + ready: true, + services: [], + }, + '/api/firstboot': { + state: 'done', + steps: [], + current_step: null, + log_tail: '', + error_present: false, + error_path: '/api/firstboot/error', + ca_hash: null, + ca_url: '/assets/pikit-ca.crt', + }, + '/api/firstboot/error': { present: false, text: '' }, + '/api/services': { services: [] }, + '/api/updates/config': updatesConfig, + '/api/updates/auto': { enabled: false, details: { enabled: false } }, + '/api/update/status': { + status: 'up_to_date', + current_version: '0.0.0', + latest_version: '0.0.0', + message: 'Mocked', + channel: 'stable', + in_progress: false, + }, + '/api/update/releases': { releases: [] }, + '/api/diag/log': { entries: [], state: { enabled: false, level: 'normal' } }, +}; + +const server = http.createServer((req, res) => { + const url = (req.url || '/').split('?')[0]; + const body = routes[url] || (url.startsWith('/api/') ? {} : null); + + if (body === null) { + res.writeHead(404, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'not found' })); + return; + } + + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(body)); +}); + +server.listen(port, '127.0.0.1', () => { + console.log(`[mock-api] listening on 127.0.0.1:${port}`); +}); + +const shutdown = () => { + server.close(() => process.exit(0)); +}; + +process.on('SIGTERM', shutdown); +process.on('SIGINT', shutdown);