|
| 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): |
| 3 | +// |
| 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 |
| 8 | +// |
| 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 |
| 12 | +// failure so CI can gate a deploy on it. |
| 13 | + |
| 14 | +import { chromium } from "@playwright/test" |
| 15 | +import http from "node:http" |
| 16 | +import fs from "node:fs" |
| 17 | +import path from "node:path" |
| 18 | + |
| 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 | +const DIST = process.env.SMOKE_DIST || "dist" |
| 22 | +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 | +const server = http.createServer((req, res) => { |
| 24 | + const url = req.url.split("?")[0] |
| 25 | + let file = path.join(DIST, url) |
| 26 | + if (!fs.existsSync(file) || fs.statSync(file).isDirectory()) file = path.join(DIST, "index.html") |
| 27 | + res.setHeader("Content-Type", TYPES[path.extname(file)] || "application/octet-stream") |
| 28 | + fs.createReadStream(file).pipe(res) |
| 29 | +}) |
| 30 | +await new Promise((r) => server.listen(0, r)) |
| 31 | +const PORT = server.address().port |
| 32 | + |
| 33 | +const BASE = `http://localhost:${PORT}` |
| 34 | +const ENTRIES = ["/marketplace", "/console", "/admin/capacity/cluster"] |
| 35 | + |
| 36 | +// Deliberately un-mocked, handled gracefully by the app — not failures. |
| 37 | +const ALLOWED_404 = [ |
| 38 | + "cozy-dashboard-console-config", // config configmap, swallowed to {} |
| 39 | + "buckets/cozy-backups", // optional backup bucket |
| 40 | +] |
| 41 | + |
| 42 | +const browser = await chromium.launch() |
| 43 | +const page = await browser.newPage({ viewport: { width: 1440, height: 900 } }) |
| 44 | + |
| 45 | +const badApi = new Set() |
| 46 | +const pageErrors = [] |
| 47 | +page.on("response", (r) => { |
| 48 | + const u = new URL(r.url()).pathname |
| 49 | + if (!/^\/(api|apis)\b/.test(u) && !u.startsWith("/api/") && !u.startsWith("/apis/")) return |
| 50 | + if (r.status() < 400) return |
| 51 | + if (ALLOWED_404.some((a) => u.includes(a))) return |
| 52 | + badApi.add(`${r.status()} ${u}`) |
| 53 | +}) |
| 54 | +page.on("pageerror", (e) => pageErrors.push(String(e).slice(0, 160))) |
| 55 | + |
| 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 | +} |
| 65 | + |
| 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) |
| 70 | +} |
| 71 | +// This console has no distinct "not found" screen (an unknown route just |
| 72 | +// renders the shell), so a broken screen shows up as an un-mocked API call or |
| 73 | +// a page error, both collected above — not as body text. |
| 74 | + |
| 75 | +await browser.close() |
| 76 | +server.close() |
| 77 | + |
| 78 | +const fail = badApi.size || pageErrors.length |
| 79 | +console.log("un-mocked API calls:", badApi.size ? [...badApi] : "none") |
| 80 | +console.log("page errors:", pageErrors.length ? pageErrors : "none") |
| 81 | +console.log(fail ? "\nSMOKE FAILED" : "\nsmoke OK") |
| 82 | +process.exit(fail ? 1 : 0) |
0 commit comments