|
1 | | -// Demo smoke check for the mocked console. Serves nothing itself — point it at |
2 | | -// a running static server of the VITE_DEMO build (SPA fallback required): |
| 1 | +// Demo smoke check for the mocked console. Self-contained — it serves the built |
| 2 | +// bundle itself (SPA fallback included) and drives a headless browser over it: |
3 | 3 | // |
4 | 4 | // VITE_DEMO=1 pnpm --filter @cozystack/console build |
5 | | -// cp apps/console/dist/index.html apps/console/dist/404.html |
6 | | -// (serve apps/console/dist on $SMOKE_BASE with SPA fallback) |
7 | | -// node apps/console/demo-smoke.mjs |
| 5 | +// SMOKE_DIST=apps/console/dist node apps/console/demo-smoke.mjs |
8 | 6 | // |
9 | | -// It walks every sidebar/tab destination reachable from the three top sections, |
10 | | -// visits each, and fails on any un-mocked Kubernetes call (4xx/5xx to /api or |
11 | | -// /apis), any page error. Exit code is non-zero on |
| 7 | +// It crawls every destination reachable from the three top sections — list |
| 8 | +// screens, detail tabs, create forms — and fails on any un-mocked Kubernetes |
| 9 | +// call (4xx/5xx to /api or /apis) or any page error. Exit code is non-zero on |
12 | 10 | // failure so CI can gate a deploy on it. |
13 | 11 |
|
14 | 12 | import { chromium } from "@playwright/test" |
15 | 13 | import http from "node:http" |
16 | 14 | import fs from "node:fs" |
17 | 15 | import path from "node:path" |
18 | 16 |
|
19 | | -// Self-contained: serve the built demo (SPA fallback) so CI just runs |
20 | | -// VITE_DEMO=1 pnpm --filter @cozystack/console build && node demo-smoke.mjs |
21 | 17 | const DIST = process.env.SMOKE_DIST || "dist" |
22 | 18 | const TYPES = { ".html":"text/html", ".js":"text/javascript", ".css":"text/css", ".json":"application/json", ".svg":"image/svg+xml", ".ico":"image/x-icon", ".woff2":"font/woff2", ".png":"image/png" } |
23 | 19 | const server = http.createServer((req, res) => { |
24 | 20 | const url = req.url.split("?")[0] |
| 21 | + // Kubernetes paths never fall back to index.html. MSW passes a request it has |
| 22 | + // no handler for straight through to the network, and with a blanket SPA |
| 23 | + // fallback that request would come back 200 text/html — so a screen the mock |
| 24 | + // layer has never heard of would look exactly like a working one. Answering |
| 25 | + // 404 here is what makes an un-mocked call visible to the check below. |
| 26 | + if (/^\/(api|apis)(\/|$)/.test(url)) { |
| 27 | + res.statusCode = 404 |
| 28 | + res.end("not mocked") |
| 29 | + return |
| 30 | + } |
25 | 31 | let file = path.join(DIST, url) |
26 | 32 | if (!fs.existsSync(file) || fs.statSync(file).isDirectory()) file = path.join(DIST, "index.html") |
27 | 33 | res.setHeader("Content-Type", TYPES[path.extname(file)] || "application/octet-stream") |
@@ -53,21 +59,32 @@ page.on("response", (r) => { |
53 | 59 | }) |
54 | 60 | page.on("pageerror", (e) => pageErrors.push(String(e).slice(0, 160))) |
55 | 61 |
|
56 | | -const routes = new Set(ENTRIES) |
57 | | -for (const entry of ENTRIES) { |
58 | | - await page.goto(BASE + entry, { waitUntil: "domcontentloaded" }) |
59 | | - await page.waitForTimeout(1500) |
60 | | - for (const href of await page |
61 | | - .locator("aside a, nav a, [role=tablist] a, a[href^='/console'], a[href^='/admin'], a[href^='/marketplace']") |
62 | | - .evaluateAll((as) => as.map((a) => a.getAttribute("href")))) |
63 | | - if (href && href.startsWith("/") && !href.startsWith("//")) routes.add(href) |
64 | | -} |
| 62 | +const LINK_SEL = |
| 63 | + "aside a, nav a, [role=tablist] a, a[href^='/console'], a[href^='/admin'], a[href^='/marketplace']" |
| 64 | +const harvest = async () => |
| 65 | + (await page.locator(LINK_SEL).evaluateAll((as) => as.map((a) => a.getAttribute("href")))) |
| 66 | + .filter((h) => h && h.startsWith("/") && !h.startsWith("//")) |
65 | 67 |
|
66 | | -console.log(`\n########## visiting ${routes.size} destinations\n`) |
67 | | -for (const route of [...routes].sort()) { |
68 | | - await page.goto(BASE + route, { waitUntil: "domcontentloaded" }) |
69 | | - await page.waitForTimeout(1200) |
| 68 | +// Crawl outwards from the three top sections rather than only one hop: a hop |
| 69 | +// reaches the list screens, and it takes a second to reach the tabs of a detail |
| 70 | +// page and the create forms behind a "New …" button — which is where the mock |
| 71 | +// layer actually tends to have holes. |
| 72 | +const seen = new Set() |
| 73 | +let frontier = [...ENTRIES] |
| 74 | +for (let wave = 0; wave < 3 && frontier.length; wave++) { |
| 75 | + console.log(`\n########## wave ${wave}: ${frontier.length} destinations\n`) |
| 76 | + const next = new Set() |
| 77 | + for (const route of [...frontier].sort()) { |
| 78 | + if (seen.has(route)) continue |
| 79 | + seen.add(route) |
| 80 | + console.log(" " + route) |
| 81 | + await page.goto(BASE + route, { waitUntil: "domcontentloaded" }) |
| 82 | + await page.waitForTimeout(1200) |
| 83 | + for (const h of await harvest()) if (!seen.has(h)) next.add(h) |
| 84 | + } |
| 85 | + frontier = [...next] |
70 | 86 | } |
| 87 | +console.log(`\n########## visited ${seen.size} destinations\n`) |
71 | 88 | // This console has no distinct "not found" screen (an unknown route just |
72 | 89 | // renders the shell), so a broken screen shows up as an un-mocked API call or |
73 | 90 | // a page error, both collected above — not as body text. |
|
0 commit comments