tui-thought.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { SessionEventType, type SessionEvent } from "@zcode/contracts";
  4. import { getZCodeCopy } from "@zcode/i18n";
  5. import { applySessionEventToState } from "../src/app-events.js";
  6. import type { Message, ThoughtTranscriptPart } from "../src/app-model.js";
  7. import {
  8. ThoughtTranscriptPartFrame,
  9. thoughtPlaceholderLabel,
  10. } from "../src/app-thought-components.js";
  11. const EN_COPY = getZCodeCopy("en-US").tui;
  12. test("projects streamed reasoning as an expandable thought placeholder", () => {
  13. const state = createSessionEventState();
  14. state.apply(
  15. sessionEvent(SessionEventType.ModelStreaming, {
  16. assistantMessageId: "assistant-1",
  17. delta: "private thinking",
  18. kind: "reasoning_delta",
  19. }),
  20. );
  21. const part = onlyThoughtPart(state.messages);
  22. assert.equal(part.status, "thinking");
  23. assert.equal(part.text, "private thinking");
  24. assert.equal(part.contentCharCount, "private thinking".length);
  25. assert.equal(state.messages[0]?.content.includes("private thinking"), false);
  26. state.apply(
  27. sessionEvent(SessionEventType.ModelStreaming, {
  28. assistantMessageId: "assistant-1",
  29. delta: "",
  30. kind: "reasoning_end",
  31. }),
  32. );
  33. assert.equal(onlyThoughtPart(state.messages).status, "thought");
  34. });
  35. test("does not create a thought placeholder for an empty reasoning start", () => {
  36. const state = createSessionEventState();
  37. state.apply(
  38. sessionEvent(SessionEventType.ModelStreaming, {
  39. assistantMessageId: "assistant-1",
  40. delta: "",
  41. kind: "reasoning_start",
  42. }),
  43. );
  44. assert.deepEqual(state.messages, []);
  45. });
  46. test("marks an active thought complete when the stream finishes", () => {
  47. const state = createSessionEventState();
  48. state.apply(
  49. sessionEvent(SessionEventType.ModelStreaming, {
  50. assistantMessageId: "assistant-1",
  51. delta: "hidden",
  52. kind: "reasoning_delta",
  53. }),
  54. );
  55. state.apply(
  56. sessionEvent(SessionEventType.ModelStreaming, {
  57. assistantMessageId: "assistant-1",
  58. delta: "",
  59. done: true,
  60. kind: "finish",
  61. }),
  62. );
  63. assert.equal(onlyThoughtPart(state.messages).status, "thought");
  64. assert.equal(state.messages[0]?.streaming, false);
  65. });
  66. test("keeps thought text and tool parts in stream order", () => {
  67. const state = createSessionEventState();
  68. state.apply(
  69. sessionEvent(SessionEventType.ModelStreaming, {
  70. assistantMessageId: "assistant-1",
  71. delta: "hidden",
  72. kind: "reasoning_delta",
  73. }),
  74. );
  75. state.apply(
  76. sessionEvent(SessionEventType.ModelStreaming, {
  77. assistantMessageId: "assistant-1",
  78. delta: "",
  79. kind: "reasoning_end",
  80. }),
  81. );
  82. state.apply(
  83. sessionEvent(SessionEventType.ModelStreaming, {
  84. assistantMessageId: "assistant-1",
  85. delta: "Visible answer.",
  86. kind: "text_delta",
  87. }),
  88. );
  89. state.apply(
  90. sessionEvent(SessionEventType.ModelStreaming, {
  91. assistantMessageId: "assistant-1",
  92. delta: "",
  93. kind: "tool_call",
  94. toolCallId: "call-1",
  95. toolName: "Read",
  96. }),
  97. );
  98. state.apply(
  99. sessionEvent(SessionEventType.ToolCallScheduled, {
  100. input: { file_path: "package.json" },
  101. toolCallId: "call-1",
  102. toolName: "Read",
  103. }),
  104. );
  105. assert.deepEqual(
  106. state.messages[0]?.parts?.map((part) => part.type),
  107. ["thought", "text", "tool"],
  108. );
  109. });
  110. test("renders expanded thought content with plus and minus markers", () => {
  111. const part: ThoughtTranscriptPart = {
  112. contentCharCount: 14,
  113. status: "thought",
  114. text: "private thinking",
  115. type: "thought",
  116. };
  117. const frame = ThoughtTranscriptPartFrame({
  118. copy: EN_COPY,
  119. expanded: true,
  120. onToggle: () => {},
  121. part,
  122. });
  123. const lines = collectTextLines(frame);
  124. assert.equal(thoughtPlaceholderLabel(part, EN_COPY), "Thought");
  125. assert.ok(lines.some((line) => line.includes("- Thought")));
  126. assert.ok(lines.some((line) => line.includes("private thinking")));
  127. assert.equal(lines.some((line) => line.includes("hidden")), false);
  128. assert.equal(collectRootStyle(frame).marginBottom, 1);
  129. });
  130. function createSessionEventState(): {
  131. apply: (event: SessionEvent) => void;
  132. messages: Message[];
  133. } {
  134. const state: { messages: Message[] } = { messages: [] };
  135. const assistantMessageIdsByToolCallId = new Map<string, string>();
  136. const toolNamesById = new Map<string, string>();
  137. return {
  138. apply: (event) => {
  139. applySessionEventToState(event, {
  140. assistantMessageIdsByToolCallId,
  141. setActiveTurnId: () => {},
  142. setCacheStats: () => {},
  143. setContextUsage: () => {},
  144. setLastError: () => {},
  145. setLiveModelText: () => {},
  146. setMessages: (updater) => {
  147. state.messages = typeof updater === "function" ? updater(state.messages) : updater;
  148. },
  149. setModel: () => {},
  150. setNetworkRequests: () => {},
  151. setStatus: () => {},
  152. setTodos: () => {},
  153. setUsage: () => {},
  154. toolNamesById,
  155. });
  156. },
  157. get messages() {
  158. return state.messages;
  159. },
  160. };
  161. }
  162. function onlyThoughtPart(messages: Message[]): ThoughtTranscriptPart {
  163. const part = messages[0]?.parts?.[0];
  164. assert.equal(part?.type, "thought");
  165. return part;
  166. }
  167. function collectTextLines(node: unknown): string[] {
  168. const lines: string[] = [];
  169. const visit = (value: unknown) => {
  170. if (typeof value === "string") {
  171. lines.push(value);
  172. return;
  173. }
  174. if (Array.isArray(value)) {
  175. for (const child of value) visit(child);
  176. return;
  177. }
  178. if (typeof value === "object" && value !== null && "props" in value) {
  179. const element = value as { props?: { children?: unknown } };
  180. visit(element.props?.children);
  181. }
  182. };
  183. visit(node);
  184. return lines;
  185. }
  186. function collectRootStyle(node: unknown): Record<string, unknown> {
  187. assert.equal(typeof node, "object");
  188. assert.notEqual(node, null);
  189. assert.ok("props" in node);
  190. const element = node as { props?: { style?: Record<string, unknown> } };
  191. return element.props?.style ?? {};
  192. }
  193. function sessionEvent(type: SessionEventType, payload: unknown): SessionEvent {
  194. return {
  195. id: "event-1",
  196. payload,
  197. sequenceNumber: 1,
  198. sessionId: "session-1",
  199. timestamp: new Date(0),
  200. traceId: "trace-1",
  201. type,
  202. } as SessionEvent;
  203. }