repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui command harness smoke

3863d531ce97473accfb109ca4f05c78f916b368

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

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/ui/__tests__/harness.test.ts

Modified
27 27 expect([...extension.tools.keys()]).toHaveLength(0);
28 28 expect(t.events.uiCallsFor("setWidget")).toHaveLength(0);
31 + it("starts web shell, serves auth pages, and stops from /ui off", async () => {
32 + const extensionPath = path.resolve(import.meta.dirname, "../index.ts");
33 + t = await createTestSession({ extensions: [extensionPath] });
34 + const command = t.session.extensionRunner.extensions[0].commands.get("ui");
35 + expect(command).toBeDefined();
36 + const ctx = t.session.extensionRunner.createCommandContext();
38 + await command?.handler("", ctx);
40 + const widget = latestWidgetLines(t);
41 + expect(widget.join("\n")).toContain("pi ui");
42 + expect(widget.join("\n")).toContain("mobile qr");
43 + const url = extractUrl(widget);
44 + expect(url.searchParams.has("token")).toBe(true);
45 + expect(await fetchText(url)).toContain("Pi UI shell");
47 + const badUrl = new URL(url);
48 + badUrl.searchParams.set("token", "bad");
49 + const unauthorized = await fetch(badUrl);
50 + expect(unauthorized.status).toBe(401);
52 + await command?.handler("status", ctx);
53 + expect(latestNotify(t)).toContain("ui connected");
54 + expect(latestNotify(t)).toContain("<redacted>");
56 + await command?.handler("qr", ctx);
57 + expect(latestWidgetLines(t).join("\n")).toContain("mobile qr");
59 + await command?.handler("off", ctx);
60 + expect(t.events.uiCallsFor("setWidget").at(-1)?.args).toEqual([
61 + "ui",
62 + undefined,
63 + ]);
64 + await expect(fetch(url)).rejects.toThrow();
65 + });
68 +function latestWidgetLines(t: TestSession): string[] {
69 + const call = t.events.uiCallsFor("setWidget").at(-1);
70 + expect(call).toBeDefined();
71 + return call?.args[1] as string[];
74 +function latestNotify(t: TestSession): string {
75 + return String(t.events.uiCallsFor("notify").at(-1)?.args[0] ?? "");
78 +function extractUrl(lines: string[]): URL {
79 + const text = lines.join("\n");
80 + const match = /http:\/\/[^\u001b\s]+/.exec(text);
81 + expect(match).toBeTruthy();
82 + return new URL(match?.[0] ?? "http://127.0.0.1");
85 +async function fetchText(url: URL): Promise<string> {
86 + const response = await fetch(url);
87 + expect(response.status).toBe(200);
88 + return await response.text();

extensions/ui/index.ts

Modified
29 29 runtime = undefined;
30 30 if (ctx.hasUI) {
31 31 ctx.ui.setWidget(WIDGET_KEY, undefined);
32 - ctx.ui.setWorkingVisible(true);
32 + ctx.ui.setWorkingVisible?.(true);
34 34 if (current) await current.stop(reason);
44 44 return;
47 - if (ctx.hasUI) ctx.ui.setWorkingVisible(false);
47 + if (ctx.hasUI) ctx.ui.setWorkingVisible?.(false);
48 48 runtime = await createUiRuntime({
49 49 sessionLabel: sessionLabel(pi, ctx),
50 50 onChange: () => setWidget(ctx),