Harden ui cleanup
f7267a88407c2938ad097d1c272837e84a529188
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
- Parent 1: 10a3cb2c705b
bugabingas pi extensions
f7267a88407c2938ad097d1c272837e84a529188
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
+82 / −0 text lines
new file mode 100644+82 / −0 text lines
import path from "node:path";import { afterEach, describe, expect, it } from "vitest";import { createTestSession, type TestSession } from "../../../test/harness";import { createUiRuntime } from "../runtime";const runtimes: Array<{ stop(reason: string): Promise<void> }> = [];let t: TestSession | undefined;describe("ui cleanup", () => { afterEach(async () => { while (runtimes.length) await runtimes.pop()?.stop("test cleanup"); t?.dispose(); t = undefined; }); it("stops runtime idempotently after socket close", async () => { const sent: unknown[] = []; const runtime = await createUiRuntime({ sessionLabel: "ui: cleanup", address: "127.0.0.1", cwd: "C:/repo", openBrowser: false, sessionManager: fakeSessionManager(), startRpcBridge: ({ sessionPath }) => fakeRpc(sent, sessionPath), }); runtimes.push(runtime); const url = new URL(runtime.status().url ?? ""); const socketUrl = new URL("/ws", url); socketUrl.protocol = "ws:"; socketUrl.searchParams.set("token", url.searchParams.get("token") ?? ""); const socket = new WebSocket(socketUrl); await new Promise((resolve) => socket.addEventListener("open", resolve, { once: true }), ); socket.close(); await new Promise((resolve) => setTimeout(resolve, 20)); await runtime.stop("first"); await runtime.stop("second"); expect(runtime.status().active).toBe(false); expect(sent).toContainEqual({ type: "close", reason: "first" }); }); it("/ui off removes terminal widget even when inactive", async () => { const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); t = await createTestSession({ extensions: [extensionPath] }); const command = t.session.extensionRunner.extensions[0].commands.get("ui"); expect(command).toBeDefined(); await command?.handler( "off", t.session.extensionRunner.createCommandContext(), ); expect(t.events.uiCallsFor("setWidget").at(-1)?.args).toEqual([ "ui", undefined, ]); });});function fakeSessionManager() { return { getLeafId: () => "leaf", createBranchedSession: () => ({ path: "C:/repo/.pi/sessions/ui.json" }), };}function fakeRpc(sent: unknown[], sessionPath: string) { return { state: () => "connected", sessionPath, send(request: unknown) { sent.push(request); return Promise.resolve({ type: "response", success: true }); }, close(reason: string) { sent.push({ type: "close", reason }); return Promise.resolve(); }, };}