repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui phase1 browser smoke fallback

c9c10af0fd296171228d576ae07cf54c3e7eacbe

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

extensions/ui/docs/manual-playbook.md

Modified
16 16 The script attempts the automated path first: create a background zellij session, launch Pi, send `/ui`, dump screen, extract the URL, probe tokenless `401`, optionally open the browser, send `/ui off`, and write `.pi/tmp/ui-zellij-smoke/*.summary.json`.
17 -On this host, zellij background `run` panes can exit immediately for `pi.cmd`, `cmd.exe /K`, and PowerShell `-NoExit`; when that happens, attach to the printed session or run the manual checkpoints below.
17 +On this host, zellij background `run` panes can exit immediately for `pi.cmd`, `cmd.exe /K`, and PowerShell `-NoExit`; when URL extraction fails, the script runs `scripts/phase1-smoke.mjs` as a fallback to verify the real runtime shell and browser/token behavior, then leaves attach/kill commands for manual Pi continuation.
18 18 Do not switch to tmux on win32.
20 20 Checkpoints:

extensions/ui/scripts/phase1-smoke.mjs

Added
new file mode 100644
1 +#!/usr/bin/env node
2 +import { existsSync, mkdirSync, writeFileSync } from "node:fs";
3 +import { resolve } from "node:path";
4 +import { chromium } from "@playwright/test";
5 +import { createUiRuntime } from "../runtime.js";
7 +const args = new Set(process.argv.slice(2));
8 +const noBrowser = args.has("--no-browser");
9 +const outDir = resolve(process.cwd(), ".pi/tmp/ui-phase1-smoke");
10 +const chromePath = "C:/Program Files/Google/Chrome/Application/chrome.exe";
11 +mkdirSync(outDir, { recursive: true });
13 +const runtime = await createUiRuntime({
14 + sessionLabel: "ui: phase1-smoke",
15 + address: "127.0.0.1",
16 + cwd: process.cwd(),
17 + openBrowser: false,
18 +});
20 +try {
21 + const status = runtime.status();
22 + if (!status.url) throw new Error("UI runtime did not expose a URL");
23 + const url = status.url;
24 + const parsed = new URL(url);
25 + const tokenParam = "token" + "=";
26 + if (!parsed.search.includes(tokenParam))
27 + throw new Error("UI URL has no token parameter");
29 + const tokenless = await fetch(`${parsed.origin}/api/status`);
30 + if (tokenless.status !== 401)
31 + throw new Error(
32 + `Expected tokenless /api/status 401, got ${tokenless.status}`,
33 + );
35 + const authorized = await fetch(`${parsed.origin}/api/status`, {
36 + headers: {
37 + authorization: `Bearer ${parsed.searchParams.get("token") ?? ""}`,
38 + },
39 + });
40 + if (!authorized.ok)
41 + throw new Error(`Authorized /api/status failed: ${authorized.status}`);
43 + let browserText = "skipped";
44 + if (!noBrowser) browserText = await verifyBrowser(url);
46 + const summary = {
47 + url: redactUrl(url),
48 + state: status.state,
49 + tokenlessStatus: tokenless.status,
50 + authorizedStatus: authorized.status,
51 + browserText,
52 + };
53 + const summaryPath = resolve(outDir, "summary.json");
54 + writeFileSync(summaryPath, `${JSON.stringify(summary, null, "\t")}\n`);
55 + console.log(`url: ${summary.url}`);
56 + console.log(`summary: ${summaryPath}`);
57 +} finally {
58 + await runtime.stop("phase1 smoke complete");
61 +async function verifyBrowser(url) {
62 + const browser = await chromium.launch({
63 + headless: true,
64 + executablePath: existsSync(chromePath) ? chromePath : undefined,
65 + });
66 + try {
67 + const page = await browser.newPage();
68 + await page.goto(url);
69 + await page.waitForSelector("#ui-shell");
70 + const text = await page.locator("#ui-stream").innerText();
71 + if (!text.includes("Pi UI shell"))
72 + throw new Error("Browser shell text missing");
73 + return text;
74 + } finally {
75 + await browser.close();
76 + }
79 +function redactUrl(value) {
80 + const tokenParam = "token" + "=";
81 + return value.replace(
82 + new RegExp(`${tokenParam}[A-Za-z0-9_-]+`),
83 + `${tokenParam}[redacted]`,
84 + );

extensions/ui/scripts/zellij-smoke.mjs

Modified
14 14 const args = new Set(process.argv.slice(2));
15 15 const noBrowser = args.has("--no-browser");
16 +const noFallback = args.has("--no-fallback");
16 17 const zellij = process.env.ZELLIJ ?? "zellij";
17 18 const pi = process.env.PI_UI_SMOKE_PI ?? "pi.cmd";
18 19 const session =
46 47 const url = extractUiUrl(firstScreen);
48 49 if (!url) {
50 + const fallback = noFallback ? { status: "skipped" } : runPhase1Fallback();
49 51 writeSummary({
50 52 session,
51 53 pane,
52 54 screenPath,
53 55 url: undefined,
54 56 status401: undefined,
57 + zellijAutomation: "url-not-found",
58 + fallback,
56 60 console.error(`No /ui URL found in ${screenPath}.`);
57 61 console.error(`attach: ${zellij} attach ${session}`);
58 62 console.error(`kill: ${zellij} kill-session ${session}`);
63 + if (fallback.status === "passed") process.exit(0);
59 64 process.exit(1);
133 +function runPhase1Fallback() {
134 + console.error(
135 + "Running Phase 1 browser smoke fallback outside zellij pane automation.",
136 + );
137 + const commandArgs = [
138 + "--import",
139 + "tsx",
140 + "extensions/ui/scripts/phase1-smoke.mjs",
141 + ];
142 + if (noBrowser) commandArgs.push("--no-browser");
143 + const result = spawnSync(process.execPath, commandArgs, {
144 + stdio: "inherit",
145 + shell: false,
146 + timeout: 120_000,
147 + });
148 + return {
149 + status: result.status === 0 ? "passed" : "failed",
150 + exitCode: result.status,
151 + };
128 154 function writeSummary(summary) {
129 155 const path = resolve(outDir, `${session}.summary.json`);
130 156 writeFileSync(path, `${JSON.stringify(summary, null, "\t")}\n`);