tui-sidebar.test.ts 15 KB

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