| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- import assert from "node:assert/strict";
- import test from "node:test";
- import React, { act } from "react";
- import { testRender } from "@mbears/opentui-react/test-utils";
- import { getZCodeCopy } from "@zcode/i18n";
- import { TuiApp } from "../src/app.js";
- import { LoginRequiredPanel } from "../src/app-components.js";
- import { ZCODE_LOGO_LINES } from "../src/app-empty-transcript.js";
- import { InputPane } from "../src/app-input-pane.js";
- import { InputActiveStatus } from "../src/app-input-status.js";
- import { QueuedInputPanel } from "../src/app-queued-inputs.js";
- import { SelectionPanel } from "../src/app-selection-panel.js";
- import { ComposerInputArea } from "../src/app-view.js";
- import type { TuiOptions } from "../src/types.js";
- test("starts without a runtime readiness system message", async () => {
- let view: Awaited<ReturnType<typeof testRender>> | undefined;
- const options: TuiOptions = {
- noColor: true,
- stderr: process.stderr,
- stdin: process.stdin,
- stdout: process.stdout,
- submitPrompt: async () => ({ response: "done" }),
- };
- await act(async () => {
- view = await testRender(
- React.createElement(TuiApp, {
- copySelection: async () => ({ kind: "empty" }),
- hasCopyableSelection: () => false,
- onExit: () => undefined,
- options,
- }),
- {
- height: 24,
- kittyKeyboard: true,
- otherModifiersMode: true,
- width: 100,
- },
- );
- await view.renderOnce();
- });
- try {
- assert.ok(view);
- const frame = view.captureCharFrame();
- const rows = frame.split("\n");
- const firstLogoRow = rows.findIndex((row) => row.includes(ZCODE_LOGO_LINES[0]));
- const lastLogoRow = rows.findIndex((row) => row.includes(ZCODE_LOGO_LINES.at(-1) ?? ""));
- const composerTopRow = rows.findIndex((row) => row.includes("╭─ Build"));
- const logoCenterRow = Math.floor((firstLogoRow + lastLogoRow) / 2);
- const transcriptCenterRow = Math.floor((composerTopRow - 1) / 2);
- assert.equal(frame.includes("OpenTUI Node runtime loaded."), false);
- assert.notEqual(firstLogoRow, -1);
- assert.notEqual(lastLogoRow, -1);
- assert.notEqual(composerTopRow, -1);
- assert.ok(Math.abs(logoCenterRow - transcriptCenterRow) <= 1);
- } finally {
- await act(async () => {
- view?.renderer.destroy();
- });
- }
- });
- test("places the login notice before the prompt input in the composer area", () => {
- const children = reactElementChildren(
- ComposerInputArea({
- busy: false,
- contentWidth: 80,
- contextUsage: {},
- copy: getZCodeCopy("en-US").tui,
- draft: "",
- editorRef: { current: null },
- inputCursorToEndVersion: 0,
- loginRequired: true,
- mode: "build",
- model: "default-deepseek/deepseek-v4-pro",
- queuedInputs: [],
- setDraftValue: () => undefined,
- slashCommands: [],
- submitValue: () => undefined,
- thoughtLevel: "max",
- }),
- );
- assert.equal(children[0]?.type, LoginRequiredPanel);
- assert.equal(children.at(-2)?.type, InputPane);
- assert.equal(children.at(-2)?.props?.model, "default-deepseek/deepseek-v4-pro");
- assert.equal(children.at(-2)?.props?.thoughtLevel, "max");
- assert.equal(children.at(-1)?.type, InputActiveStatus);
- assert.equal(children.at(-1)?.props?.active, false);
- });
- test("places composer selections above the prompt input", () => {
- const children = reactElementChildren(
- ComposerInputArea({
- busy: false,
- contentWidth: 80,
- contextUsage: {},
- copy: getZCodeCopy("en-US").tui,
- draft: "",
- editorRef: { current: null },
- inputCursorToEndVersion: 0,
- loginRequired: false,
- mode: "build",
- model: "default-deepseek/deepseek-v4-pro",
- queuedInputs: [],
- selection: {
- emptyMessage: "No saved sessions found.",
- filter: "",
- items: [
- {
- command: "/resume sess_one",
- id: "sess_one",
- primary: "Session one",
- },
- ],
- placement: "composer",
- prompt: "Choose a session to resume.",
- selectedIndex: 0,
- title: "Resume Session",
- },
- setDraftValue: () => undefined,
- slashCommands: [],
- submitValue: () => undefined,
- thoughtLevel: "max",
- }),
- );
- assert.equal(children.find((child) => child.type === SelectionPanel)?.type, SelectionPanel);
- assert.equal(children.at(-2)?.type, InputPane);
- assert.equal(children.at(-1)?.type, InputActiveStatus);
- });
- test("passes the busy active status outside the prompt input", () => {
- const children = reactElementChildren(
- ComposerInputArea({
- busy: true,
- contentWidth: 80,
- contextUsage: { contextUsed: 11_100, contextWindow: 185_000 },
- copy: getZCodeCopy("en-US").tui,
- draft: "",
- editorRef: { current: null },
- inputCursorToEndVersion: 0,
- loginRequired: false,
- mode: "build",
- model: "default-deepseek/deepseek-v4-pro",
- queuedInputs: [],
- setDraftValue: () => undefined,
- slashCommands: [],
- submitValue: () => undefined,
- thoughtLevel: "max",
- }),
- );
- assert.equal(children.at(-2)?.type, InputPane);
- assert.equal(children.at(-2)?.props?.busy, true);
- assert.equal(children.at(-2)?.props?.model, "default-deepseek/deepseek-v4-pro");
- assert.equal(children.at(-2)?.props?.thoughtLevel, "max");
- assert.equal(children.at(-1)?.type, InputActiveStatus);
- assert.equal(children.at(-1)?.props?.active, true);
- assert.deepEqual(children.at(-1)?.props?.contextUsage, {
- contextUsed: 11_100,
- contextWindow: 185_000,
- });
- });
- test("places queued input panel immediately above the prompt input", () => {
- const children = reactElementChildren(
- ComposerInputArea({
- busy: true,
- contentWidth: 80,
- contextUsage: {},
- copy: getZCodeCopy("en-US").tui,
- draft: "",
- editorRef: { current: null },
- inputCursorToEndVersion: 0,
- loginRequired: false,
- mode: "build",
- model: "default-deepseek/deepseek-v4-pro",
- queuedInputs: [{ id: "pending-1", text: "hello queue" }],
- setDraftValue: () => undefined,
- slashCommands: [],
- submitValue: () => undefined,
- thoughtLevel: "max",
- }),
- );
- assert.equal(children.at(-3)?.type, QueuedInputPanel);
- assert.deepEqual(children.at(-3)?.props?.inputs, [{ id: "pending-1", text: "hello queue" }]);
- assert.equal(children.at(-2)?.type, InputPane);
- assert.equal(children.at(-1)?.type, InputActiveStatus);
- });
- test("Shift+Tab switches mode through the injected mode setter", async () => {
- let view: Awaited<ReturnType<typeof testRender>> | undefined;
- const requestedModes: string[] = [];
- const options: TuiOptions = {
- initialMode: "build",
- noColor: true,
- stderr: process.stderr,
- stdin: process.stdin,
- stdout: process.stdout,
- setMode: async (mode) => {
- requestedModes.push(mode);
- return { mode };
- },
- submitPrompt: async () => ({ response: "done" }),
- };
- await act(async () => {
- view = await testRender(
- React.createElement(TuiApp, {
- copySelection: async () => ({ kind: "empty" }),
- hasCopyableSelection: () => false,
- onExit: () => undefined,
- options,
- }),
- {
- height: 24,
- kittyKeyboard: true,
- otherModifiersMode: true,
- width: 100,
- },
- );
- await view.renderOnce();
- });
- try {
- await act(async () => {
- assert.ok(view);
- await view.mockInput.typeText("keep this draft");
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressTab({ shift: true });
- await Promise.resolve();
- });
- await act(async () => {
- await view.renderOnce();
- });
- assert.deepEqual(requestedModes, ["guarded"]);
- const frame = view.captureCharFrame();
- assert.equal(frame.includes("Input - Guarded"), false);
- assert.equal(frame.includes("Guarded"), true);
- assert.equal(frame.includes("keep this draft"), true);
- } finally {
- await act(async () => {
- view?.renderer.destroy();
- });
- }
- });
- test("Shift+Tab can cycle guarded mode to yolo mode", async () => {
- let view: Awaited<ReturnType<typeof testRender>> | undefined;
- const requestedModes: string[] = [];
- const options: TuiOptions = {
- initialMode: "guarded",
- noColor: true,
- stderr: process.stderr,
- stdin: process.stdin,
- stdout: process.stdout,
- setMode: async (mode) => {
- requestedModes.push(mode);
- return { mode };
- },
- submitPrompt: async () => ({ response: "done" }),
- };
- await act(async () => {
- view = await testRender(
- React.createElement(TuiApp, {
- copySelection: async () => ({ kind: "empty" }),
- hasCopyableSelection: () => false,
- onExit: () => undefined,
- options,
- }),
- {
- height: 24,
- kittyKeyboard: true,
- otherModifiersMode: true,
- width: 100,
- },
- );
- await view.renderOnce();
- });
- try {
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressTab({ shift: true });
- await Promise.resolve();
- await view.renderOnce();
- });
- assert.deepEqual(requestedModes, ["yolo"]);
- } finally {
- await act(async () => {
- view?.renderer.destroy();
- });
- }
- });
- test("recalls input history while model output is active", async () => {
- let view: Awaited<ReturnType<typeof testRender>> | undefined;
- const pendingTurn = deferred<TuiOptions["initialResult"]>();
- let recalled = 0;
- const options: TuiOptions = {
- noColor: true,
- recallPreviousInput: async () => {
- recalled += 1;
- return { text: "/help" };
- },
- stderr: process.stderr,
- stdin: process.stdin,
- stdout: process.stdout,
- submitPrompt: async () => (await pendingTurn.promise) ?? { response: "done" },
- };
- await act(async () => {
- view = await testRender(
- React.createElement(TuiApp, {
- copySelection: async () => ({ kind: "empty" }),
- hasCopyableSelection: () => false,
- onExit: () => undefined,
- options,
- }),
- {
- height: 24,
- kittyKeyboard: true,
- otherModifiersMode: true,
- width: 100,
- },
- );
- await view.renderOnce();
- });
- try {
- await act(async () => {
- assert.ok(view);
- await view.mockInput.typeText("first prompt");
- await Promise.resolve();
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressEnter();
- await Promise.resolve();
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressArrow("up");
- await Promise.resolve();
- await view.renderOnce();
- });
- // Bugfix: the shared OpenTUI test renderer buffer is noisy while package tests
- // run concurrently; the regression contract here is that busy Up reaches history.
- assert.equal(recalled, 1);
- } finally {
- pendingTurn.resolve({ response: "done" });
- await act(async () => {
- await Promise.resolve();
- await view?.renderOnce();
- view?.renderer.destroy();
- });
- }
- });
- test("mode composer popup submits the highlighted explicit mode command", async () => {
- let view: Awaited<ReturnType<typeof testRender>> | undefined;
- const submitted: string[] = [];
- const options: TuiOptions = {
- initialMode: "build",
- noColor: true,
- stderr: process.stderr,
- stdin: process.stdin,
- stdout: process.stdout,
- submitPrompt: async (input) => {
- submitted.push(typeof input === "string" ? input : input.text);
- return { mode: "yolo", response: "done" };
- },
- };
- await act(async () => {
- view = await testRender(
- React.createElement(TuiApp, {
- copySelection: async () => ({ kind: "empty" }),
- hasCopyableSelection: () => false,
- onExit: () => undefined,
- options,
- }),
- {
- height: 24,
- kittyKeyboard: true,
- otherModifiersMode: true,
- width: 100,
- },
- );
- await view.renderOnce();
- });
- try {
- await act(async () => {
- assert.ok(view);
- await view.mockInput.typeText("/mode");
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressArrow("down");
- await Promise.resolve();
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressArrow("down");
- await Promise.resolve();
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressArrow("down");
- await Promise.resolve();
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressEnter();
- await Promise.resolve();
- await view.renderOnce();
- });
- assert.deepEqual(submitted, ["/mode yolo"]);
- } finally {
- await act(async () => {
- view?.renderer.destroy();
- });
- }
- });
- test("mode composer popup can submit guarded mode command", async () => {
- let view: Awaited<ReturnType<typeof testRender>> | undefined;
- const submitted: string[] = [];
- const options: TuiOptions = {
- initialMode: "build",
- noColor: true,
- stderr: process.stderr,
- stdin: process.stdin,
- stdout: process.stdout,
- submitPrompt: async (input) => {
- submitted.push(typeof input === "string" ? input : input.text);
- return { mode: "edit", response: "done" };
- },
- };
- await act(async () => {
- view = await testRender(
- React.createElement(TuiApp, {
- copySelection: async () => ({ kind: "empty" }),
- hasCopyableSelection: () => false,
- onExit: () => undefined,
- options,
- }),
- {
- height: 24,
- kittyKeyboard: true,
- otherModifiersMode: true,
- width: 100,
- },
- );
- await view.renderOnce();
- });
- try {
- await act(async () => {
- assert.ok(view);
- await view.mockInput.typeText("/mode");
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressArrow("down");
- await Promise.resolve();
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressArrow("down");
- await Promise.resolve();
- await view.renderOnce();
- });
- await act(async () => {
- assert.ok(view);
- view.mockInput.pressEnter();
- await Promise.resolve();
- await view.renderOnce();
- });
- assert.deepEqual(submitted, ["/mode guarded"]);
- } finally {
- await act(async () => {
- view?.renderer.destroy();
- });
- }
- });
- function reactElementChildren(
- node: unknown,
- ): Array<{ props?: Record<string, unknown>; type?: unknown }> {
- if (typeof node !== "object" || node === null || !("props" in node)) return [];
- const element = node as { props?: { children?: unknown } };
- const children = element.props?.children;
- if (children === undefined) return [];
- return (Array.isArray(children) ? children : [children]).filter(
- (child): child is { type?: unknown } => typeof child === "object" && child !== null,
- );
- }
- function deferred<T>(): {
- promise: Promise<T>;
- resolve: (value: T) => void;
- } {
- let resolve!: (value: T) => void;
- const promise = new Promise<T>((innerResolve) => {
- resolve = innerResolve;
- });
- return { promise, resolve };
- }
|