feat(web): add xterm terminal panel (#5)
Use @xterm/xterm for terminal emulation and its official @xterm/addon-fit for container sizing. Lazy-load both on first open to keep them out of the main bundle. Preserve the mounted terminal on collapse and offer explicit reconnect; provide an echo transport for demos.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import {
|
||||
act,
|
||||
fireEvent,
|
||||
render,
|
||||
screen,
|
||||
waitFor,
|
||||
} from "@testing-library/react";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
const xterm = vi.hoisted(() => ({ instances: [] as any[] }));
|
||||
vi.mock("@xterm/xterm", () => ({
|
||||
Terminal: class {
|
||||
cols = 80;
|
||||
rows = 24;
|
||||
options = {};
|
||||
write = vi.fn();
|
||||
dispose = vi.fn();
|
||||
input?: (data: string) => void;
|
||||
constructor() {
|
||||
xterm.instances.push(this);
|
||||
}
|
||||
loadAddon() {}
|
||||
open() {}
|
||||
onData(callback: (data: string) => void) {
|
||||
this.input = callback;
|
||||
return { dispose: vi.fn() };
|
||||
}
|
||||
},
|
||||
}));
|
||||
vi.mock("@xterm/addon-fit", () => ({
|
||||
FitAddon: class {
|
||||
fit = vi.fn();
|
||||
},
|
||||
}));
|
||||
import { TerminalSlot } from "../TerminalSlot";
|
||||
class FakeSocket {
|
||||
static instances: FakeSocket[] = [];
|
||||
onopen?: () => void;
|
||||
onclose?: () => void;
|
||||
onerror?: () => void;
|
||||
onmessage?: (event: { data: string }) => void;
|
||||
send = vi.fn();
|
||||
close = vi.fn();
|
||||
constructor(public url: string) {
|
||||
FakeSocket.instances.push(this);
|
||||
}
|
||||
}
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
FakeSocket.instances = [];
|
||||
xterm.instances = [];
|
||||
});
|
||||
it("frames input and fitted resize, renders raw output and preserves connection when collapsed", async () => {
|
||||
vi.stubGlobal("WebSocket", FakeSocket);
|
||||
const { rerender, container } = render(<TerminalSlot />);
|
||||
const socket = FakeSocket.instances[0]!;
|
||||
expect(socket.url).toBe(`ws://${location.host}/api/terminal?session=main`);
|
||||
const element = container.querySelector(".terminal-screen")!;
|
||||
vi.spyOn(element, "getBoundingClientRect").mockReturnValue({
|
||||
width: 500,
|
||||
} as DOMRect);
|
||||
act(() => socket.onopen?.());
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Connected");
|
||||
await waitFor(() =>
|
||||
expect(socket.send).toHaveBeenCalledWith(
|
||||
JSON.stringify({ type: "resize", cols: 80, rows: 24 }),
|
||||
),
|
||||
);
|
||||
act(() => xterm.instances[0].input("claude\r"));
|
||||
expect(socket.send).toHaveBeenCalledWith(
|
||||
JSON.stringify({ type: "input", data: "claude\r" }),
|
||||
);
|
||||
act(() => socket.onmessage?.({ data: "\u001b[31mraw output" }));
|
||||
expect(xterm.instances[0].write).toHaveBeenCalledWith("\u001b[31mraw output");
|
||||
rerender(<TerminalSlot collapsed />);
|
||||
expect(container.querySelector("aside")).toHaveAttribute("hidden");
|
||||
expect(socket.close).not.toHaveBeenCalled();
|
||||
act(() => socket.onmessage?.({ data: "still receiving" }));
|
||||
rerender(<TerminalSlot />);
|
||||
expect(FakeSocket.instances).toHaveLength(1);
|
||||
expect(xterm.instances[0].write).toHaveBeenCalledWith("still receiving");
|
||||
expect(xterm.instances[0].dispose).not.toHaveBeenCalled();
|
||||
});
|
||||
it("offers explicit reconnect after close and cleans up the old connection", () => {
|
||||
vi.stubGlobal("WebSocket", FakeSocket);
|
||||
const view = render(<TerminalSlot />);
|
||||
act(() => FakeSocket.instances[0]!.onclose?.());
|
||||
fireEvent.click(screen.getByRole("button", { name: "Reconnect" }));
|
||||
expect(FakeSocket.instances).toHaveLength(2);
|
||||
expect(FakeSocket.instances[0]!.close).toHaveBeenCalledTimes(1);
|
||||
act(() => FakeSocket.instances[1]!.onopen?.());
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Connected");
|
||||
view.unmount();
|
||||
expect(FakeSocket.instances[1]!.close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("uses a fake echo terminal in demo mode without a server", async () => {
|
||||
vi.stubEnv("VITE_MOCK", "1");
|
||||
const { terminalTransport } = await import("../terminalTransport");
|
||||
try {
|
||||
const socket = terminalTransport("main");
|
||||
const opened = vi.fn();
|
||||
const output = vi.fn();
|
||||
socket.onopen = opened;
|
||||
socket.onmessage = output;
|
||||
await Promise.resolve();
|
||||
expect(opened).toHaveBeenCalledOnce();
|
||||
socket.send(JSON.stringify({ type: "input", data: "hello\r" }));
|
||||
expect(output).toHaveBeenLastCalledWith({ data: "hello\r\n" });
|
||||
socket.close();
|
||||
socket.send(JSON.stringify({ type: "input", data: "closed" }));
|
||||
expect(output).toHaveBeenCalledTimes(2);
|
||||
} finally {
|
||||
vi.unstubAllEnvs();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user