tui-sidebar.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import React from "react";
  4. import { AppShell } from "../src/app-components.js";
  5. import { SIDEBAR_CONTENT_WIDTH, Sidebar } from "../src/app-sidebar.js";
  6. import {
  7. DEFAULT_SIDEBAR_STATE,
  8. SIDEBAR_OVERLAY_BACKGROUND,
  9. SIDEBAR_WIDTH,
  10. sidebarLayoutForTerminal,
  11. toggleSidebarSectionState,
  12. toggleSidebarState,
  13. } from "../src/app-sidebar-layout.js";
  14. import { createSidebarShortcutState, resolveSidebarShortcut } from "../src/app-sidebar-shortcut.js";
  15. import type { SidebarState } from "../src/app-model.js";
  16. test("uses the responsive sidebar width from the v2 layout contract", () => {
  17. assert.equal(SIDEBAR_WIDTH, 42);
  18. assert.equal(SIDEBAR_CONTENT_WIDTH, 40);
  19. });
  20. test("renders as a borderless background block", () => {
  21. const style = reactElementStyle(Sidebar(baseSidebarState));
  22. assert.equal(style.border, false);
  23. assert.equal(style.backgroundColor, "#1f2937");
  24. });
  25. test("splits the current provider and model rows in the run section", () => {
  26. const lines = collectTextLines(
  27. Sidebar({
  28. ...baseSidebarState,
  29. developerMode: true,
  30. model: "gateway/openai/gpt-5.5",
  31. }),
  32. );
  33. assert.ok(lines.includes("Provider gateway"));
  34. assert.ok(lines.includes("Model openai/gpt-5.5"));
  35. });
  36. test("uses a blank provider placeholder for bare model ids", () => {
  37. const lines = collectTextLines(
  38. Sidebar({
  39. ...baseSidebarState,
  40. developerMode: true,
  41. model: "glm-4.6",
  42. }),
  43. );
  44. assert.ok(lines.includes("Provider -"));
  45. assert.ok(lines.includes("Model glm-4.6"));
  46. });
  47. test("compacts UUID-like provider labels in API request rows", () => {
  48. const providerId = "36965ea6-734a-46a8-8851-3dfec026589e";
  49. const lines = collectTextLines(
  50. Sidebar({
  51. ...baseSidebarState,
  52. developerMode: true,
  53. networkRequests: [
  54. {
  55. id: providerId,
  56. method: "POST",
  57. provider: providerId,
  58. requestId: providerId,
  59. source: "model",
  60. startedAt: "2026-05-19T00:00:00.000Z",
  61. status: "complete",
  62. updatedAt: "2026-05-19T00:00:01.000Z",
  63. url: "https://open.bigmodel.cn/api/paas/v4/chat/completions",
  64. },
  65. ],
  66. }),
  67. );
  68. const requestLine = lines.find((line) => line.includes("complete POST"));
  69. assert.ok(requestLine);
  70. assert.equal(requestLine.length, SIDEBAR_CONTENT_WIDTH);
  71. assert.match(requestLine, /^complete POST 36965ea6\.\.\.589e https:\//);
  72. assert.match(requestLine, /\.\.\.$/);
  73. assert.equal(requestLine.includes(providerId), false);
  74. });
  75. test("keeps named provider labels readable in API request rows", () => {
  76. const lines = collectTextLines(
  77. Sidebar({
  78. ...baseSidebarState,
  79. developerMode: true,
  80. networkRequests: [
  81. {
  82. id: "request-1",
  83. method: "POST",
  84. provider: "deepseek",
  85. requestId: "request-1",
  86. source: "model",
  87. startedAt: "2026-05-19T00:00:00.000Z",
  88. status: "pending",
  89. updatedAt: "2026-05-19T00:00:01.000Z",
  90. url: "https://api.deepseek.com",
  91. },
  92. ],
  93. }),
  94. );
  95. const requestLine = lines.find((line) => line.includes("pending POST"));
  96. assert.ok(requestLine);
  97. assert.equal(requestLine.length, SIDEBAR_CONTENT_WIDTH);
  98. assert.match(requestLine, /^pending POST deepseek https:\/\/api\.dee/);
  99. assert.match(requestLine, /\.\.\.$/);
  100. });
  101. test("renders current-session modified files with aligned counts", () => {
  102. const nodes = collectTextNodes(
  103. Sidebar({
  104. ...baseSidebarState,
  105. modifiedFiles: [
  106. { additions: 3, deletions: 1, filePath: "src/App.jsx" },
  107. { additions: 30, deletions: 12, filePath: "src/Scene3D.jsx" },
  108. ],
  109. }),
  110. );
  111. assert.ok(nodes.some((node) => node.text === "▼ Modified Files"));
  112. assert.equal(nodes.find((node) => node.text === "src/App.jsx")?.style.fg, "#94a3b8");
  113. assert.equal(nodes.find((node) => node.text.trim() === "+3")?.style.fg, "#7dd3fc");
  114. assert.equal(nodes.find((node) => node.text.trim() === "-1")?.style.fg, "#fca5a5");
  115. });
  116. test("collapses modified files and API sections independently", () => {
  117. const lines = collectTextLines(
  118. Sidebar({
  119. ...baseSidebarState,
  120. developerMode: true,
  121. modifiedFiles: [{ additions: 3, deletions: 1, filePath: "src/App.jsx" }],
  122. networkRequests: [
  123. {
  124. id: "request-1",
  125. method: "POST",
  126. requestId: "request-1",
  127. source: "model",
  128. startedAt: "2026-05-19T00:00:00.000Z",
  129. status: "pending",
  130. updatedAt: "2026-05-19T00:00:01.000Z",
  131. url: "https://api.deepseek.com",
  132. },
  133. ],
  134. sectionExpansion: {
  135. apis: false,
  136. mcp: true,
  137. modifiedFiles: false,
  138. },
  139. }),
  140. );
  141. assert.ok(lines.includes("▶ Modified Files"));
  142. assert.ok(lines.includes("▶ APIs"));
  143. assert.equal(lines.includes("src/App.jsx"), false);
  144. assert.equal(
  145. lines.some((line) => line.includes("pending POST")),
  146. false,
  147. );
  148. });
  149. test("production sidebar renders MCP server statuses", () => {
  150. const lines = collectTextLines(
  151. Sidebar({
  152. ...baseSidebarState,
  153. mcpStatus: {
  154. loading: false,
  155. servers: {
  156. filesystem: {
  157. status: "connected",
  158. toolCount: 2,
  159. transport: "stdio",
  160. updatedAt: "2026-05-28T00:00:00.000Z",
  161. },
  162. remote: {
  163. error: "Connection timed out",
  164. status: "failed",
  165. toolCount: 0,
  166. transport: "http",
  167. updatedAt: "2026-05-28T00:00:01.000Z",
  168. },
  169. },
  170. },
  171. }),
  172. );
  173. assert.ok(lines.includes("▼ MCP"));
  174. assert.ok(lines.includes("Servers 1/2 connected"));
  175. assert.ok(lines.some((line) => line.includes("connected") && line.includes("filesystem")));
  176. assert.ok(lines.some((line) => line.includes("failed") && line.includes("remote")));
  177. assert.ok(lines.includes("Connection timed out"));
  178. });
  179. test("production sidebar shows product header and hides developer diagnostics", () => {
  180. const node = Sidebar({
  181. ...baseSidebarState,
  182. version: "1.2.3",
  183. workspaceDirectory: "/Users/dev/test/z-m",
  184. workspaceGitBranch: "main",
  185. });
  186. const lines = collectTextLines(node);
  187. const textNodes = collectTextNodes(node);
  188. assert.equal(textNodes.find((item) => item.text === "ZCode")?.style.fg, "#7dd3fc");
  189. assert.equal(textNodes.find((item) => item.text === " 1.2.3")?.style.fg, "#94a3b8");
  190. assert.equal(lines.includes("OpenTUI shell"), false);
  191. assert.equal(lines.includes("Run"), false);
  192. assert.equal(lines.includes("Context"), false);
  193. assert.equal(lines.includes("APIs"), false);
  194. assert.ok(lines.includes("Status"));
  195. assert.ok(lines.includes("▼ MCP"));
  196. assert.ok(lines.includes("▼ Modified Files"));
  197. assert.ok(lines.includes("▼ Todos"));
  198. assert.ok(lines.includes("/Users/dev/test/z-m:main"));
  199. });
  200. test("clicking collapsible section headers toggles only that section", () => {
  201. const toggledSections: string[] = [];
  202. const stopped: string[] = [];
  203. const node = Sidebar({
  204. ...baseSidebarState,
  205. developerMode: true,
  206. onToggleSection: (section) => toggledSections.push(section),
  207. });
  208. findTextElement(node, "▼ Modified Files")?.props?.onMouseUp?.({
  209. stopPropagation: () => stopped.push("modifiedFiles"),
  210. });
  211. findTextElement(node, "▼ APIs")?.props?.onMouseUp?.({
  212. stopPropagation: () => stopped.push("apis"),
  213. });
  214. assert.deepEqual(toggledSections, ["modifiedFiles", "apis"]);
  215. assert.deepEqual(stopped, ["modifiedFiles", "apis"]);
  216. });
  217. test("auto sidebar is docked only above the 120 column breakpoint", () => {
  218. assert.deepEqual(sidebarLayoutForTerminal(121, DEFAULT_SIDEBAR_STATE), {
  219. overlay: false,
  220. reservedWidth: SIDEBAR_WIDTH,
  221. visible: true,
  222. wide: true,
  223. });
  224. assert.deepEqual(sidebarLayoutForTerminal(120, DEFAULT_SIDEBAR_STATE), {
  225. overlay: false,
  226. reservedWidth: 0,
  227. visible: false,
  228. wide: false,
  229. });
  230. });
  231. test("sidebar toggle hides visible panes and opens narrow panes as overlays", () => {
  232. const hidden = toggleSidebarState(DEFAULT_SIDEBAR_STATE, 140);
  233. assert.equal(sidebarLayoutForTerminal(140, hidden).visible, false);
  234. assert.equal(sidebarLayoutForTerminal(80, hidden).visible, false);
  235. const narrowOpen = toggleSidebarState(hidden, 80);
  236. assert.deepEqual(sidebarLayoutForTerminal(80, narrowOpen), {
  237. overlay: true,
  238. reservedWidth: 0,
  239. visible: true,
  240. wide: false,
  241. });
  242. });
  243. test("sidebar section toggles preserve the other section", () => {
  244. // 子代理与 Todos 也成为可折叠区块,默认展开,切换其一不影响其他区块。
  245. assert.deepEqual(toggleSidebarSectionState(DEFAULT_SIDEBAR_STATE.sections, "modifiedFiles"), {
  246. apis: true,
  247. mcp: true,
  248. modifiedFiles: false,
  249. subagents: true,
  250. todos: true,
  251. });
  252. assert.deepEqual(toggleSidebarSectionState(DEFAULT_SIDEBAR_STATE.sections, "apis"), {
  253. apis: false,
  254. mcp: true,
  255. modifiedFiles: true,
  256. subagents: true,
  257. todos: true,
  258. });
  259. });
  260. test("Ctrl-X leader toggles sidebar sections while unrelated keys clear the leader", () => {
  261. const state = createSidebarShortcutState();
  262. assert.equal(resolveSidebarShortcut(state, key("x", { ctrl: true }), 1_000), "arm");
  263. assert.equal(resolveSidebarShortcut(state, key("b"), 1_500), "toggle");
  264. assert.equal(resolveSidebarShortcut(state, key("b"), 1_600), "pass");
  265. assert.equal(resolveSidebarShortcut(state, key("x", { ctrl: true }), 2_000), "arm");
  266. assert.equal(resolveSidebarShortcut(state, key("m"), 2_100), "toggle-files");
  267. assert.equal(resolveSidebarShortcut(state, key("b"), 2_200), "pass");
  268. assert.equal(resolveSidebarShortcut(state, key("x", { ctrl: true }), 3_000), "arm");
  269. assert.equal(resolveSidebarShortcut(state, key("a"), 3_100), "toggle-apis");
  270. assert.equal(resolveSidebarShortcut(state, key("b"), 3_200), "pass");
  271. assert.equal(resolveSidebarShortcut(state, key("x", { ctrl: true }), 4_000), "arm");
  272. assert.equal(resolveSidebarShortcut(state, key("q"), 4_100), "pass");
  273. assert.equal(resolveSidebarShortcut(state, key("b"), 4_200), "pass");
  274. assert.equal(resolveSidebarShortcut(state, key("x", { ctrl: true }), 5_000), "arm");
  275. assert.equal(resolveSidebarShortcut(state, key("b"), 8_000), "pass");
  276. });
  277. test("app shell hides, docks, and overlays sidebar based on layout", () => {
  278. const sidebar = React.createElement("box", { id: "sidebar" });
  279. const hidden = reactElementChildren(
  280. AppShell({
  281. children: React.createElement("text", null, "main"),
  282. sidebar,
  283. sidebarLayout: {
  284. overlay: false,
  285. reservedWidth: 0,
  286. visible: false,
  287. wide: false,
  288. },
  289. }),
  290. );
  291. const hiddenRootStyle = reactElementStyle(
  292. AppShell({
  293. children: React.createElement("text", null, "main"),
  294. sidebar,
  295. sidebarLayout: {
  296. overlay: false,
  297. reservedWidth: 0,
  298. visible: false,
  299. wide: false,
  300. },
  301. }),
  302. );
  303. const hiddenMainStyle = reactElementStyle(hidden[0]);
  304. const docked = reactElementChildren(
  305. AppShell({
  306. children: React.createElement("text", null, "main"),
  307. sidebar,
  308. sidebarLayout: {
  309. overlay: false,
  310. reservedWidth: SIDEBAR_WIDTH,
  311. visible: true,
  312. wide: true,
  313. },
  314. }),
  315. );
  316. const overlay = reactElementChildren(
  317. AppShell({
  318. children: React.createElement("text", null, "main"),
  319. sidebar,
  320. sidebarLayout: {
  321. overlay: true,
  322. reservedWidth: 0,
  323. visible: true,
  324. wide: false,
  325. },
  326. }),
  327. );
  328. assert.equal(hiddenRootStyle.padding, 0);
  329. assert.equal(hiddenMainStyle.padding, 1);
  330. assert.equal(hidden[1], null);
  331. assert.equal(hidden[2], null);
  332. assert.equal(docked[1]?.props?.id, "sidebar");
  333. assert.equal(overlay[1], null);
  334. const overlayStyle = reactElementStyle(overlay[2]);
  335. assert.equal(overlayStyle.position, "absolute");
  336. assert.equal(overlayStyle.backgroundColor, SIDEBAR_OVERLAY_BACKGROUND);
  337. });
  338. const baseSidebarState: SidebarState = {
  339. busy: false,
  340. contextUsage: {},
  341. draft: "",
  342. lastEvent: "idle",
  343. messageCount: 0,
  344. mcpStatus: {
  345. loading: false,
  346. servers: {},
  347. },
  348. mode: "build",
  349. model: "glm-4.6",
  350. modifiedFiles: [],
  351. networkRequests: [],
  352. status: "Ready.",
  353. statusDetails: [],
  354. thoughtLevel: "medium",
  355. todos: [],
  356. };
  357. function collectTextLines(node: unknown): string[] {
  358. const lines: string[] = [];
  359. const visit = (value: unknown) => {
  360. if (typeof value === "string") {
  361. lines.push(value);
  362. return;
  363. }
  364. if (Array.isArray(value)) {
  365. for (const child of value) visit(child);
  366. return;
  367. }
  368. if (typeof value === "object" && value !== null && "props" in value) {
  369. const element = value as { props?: { children?: unknown } };
  370. visit(element.props?.children);
  371. }
  372. };
  373. visit(node);
  374. return lines;
  375. }
  376. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  377. const lines: ReturnType<typeof collectTextNodes> = [];
  378. const visit = (value: unknown) => {
  379. if (Array.isArray(value)) {
  380. for (const child of value) visit(child);
  381. return;
  382. }
  383. if (typeof value !== "object" || value === null || !("props" in value)) return;
  384. const element = value as {
  385. props?: {
  386. children?: unknown;
  387. style?: Record<string, unknown>;
  388. };
  389. type?: unknown;
  390. };
  391. if (element.type === "text") {
  392. const text = collectTextLines(element.props?.children).join("");
  393. lines.push({ style: element.props?.style ?? {}, text });
  394. }
  395. visit(element.props?.children);
  396. };
  397. visit(node);
  398. return lines;
  399. }
  400. function findTextElement(
  401. node: unknown,
  402. text: string,
  403. ):
  404. | {
  405. props?: { children?: unknown; onMouseUp?: (event: { stopPropagation?: () => void }) => void };
  406. }
  407. | undefined {
  408. let match:
  409. | {
  410. props?: {
  411. children?: unknown;
  412. onMouseUp?: (event: { stopPropagation?: () => void }) => void;
  413. };
  414. }
  415. | undefined;
  416. const visit = (value: unknown) => {
  417. if (match || Array.isArray(value)) {
  418. if (Array.isArray(value)) {
  419. for (const child of value) visit(child);
  420. }
  421. return;
  422. }
  423. if (typeof value !== "object" || value === null || !("props" in value)) return;
  424. const element = value as {
  425. props?: {
  426. children?: unknown;
  427. onMouseUp?: (event: { stopPropagation?: () => void }) => void;
  428. };
  429. type?: unknown;
  430. };
  431. if (element.type === "text" && collectTextLines(element.props?.children).join("") === text) {
  432. match = element;
  433. return;
  434. }
  435. visit(element.props?.children);
  436. };
  437. visit(node);
  438. return match;
  439. }
  440. function reactElementStyle(node: unknown): Record<string, unknown> {
  441. assert.ok(typeof node === "object" && node !== null && "props" in node);
  442. const element = node as { props?: { style?: Record<string, unknown> } };
  443. return element.props?.style ?? {};
  444. }
  445. function reactElementChildren(node: unknown): Array<null | { props?: Record<string, unknown> }> {
  446. assert.ok(typeof node === "object" && node !== null && "props" in node);
  447. const element = node as { props?: { children?: unknown } };
  448. const children = element.props?.children;
  449. if (children === undefined) return [];
  450. return (Array.isArray(children) ? children : [children]) as Array<null | {
  451. props?: Record<string, unknown>;
  452. }>;
  453. }
  454. function key(
  455. name: string,
  456. modifiers: { ctrl?: boolean; meta?: boolean; option?: boolean; shift?: boolean } = {},
  457. ): {
  458. ctrl?: boolean;
  459. meta?: boolean;
  460. name: string;
  461. option?: boolean;
  462. shift?: boolean;
  463. } {
  464. return {
  465. name,
  466. ...modifiers,
  467. };
  468. }