conversation-session-dynamic-workflow-gray-always-on-snippet.test.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
  2. import { dirname } from "node:path";
  3. import { TID_CHAT_WORKFLOW_RUN_DIGEST } from "@zcode/shared";
  4. import { clearAppData, pinE2ELocale } from "#e2e/helpers/desktop-app";
  5. import { readDynamicWorkflowGrayMockStatus } from "#e2e/helpers/dynamic-workflow-gray";
  6. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  7. import {
  8. approveV4Permission,
  9. getV4ConfigProjection,
  10. prepareV4ConversationE2E,
  11. sendV4Prompt,
  12. startNewV4Draft,
  13. switchV4Mode,
  14. waitForV4AssistantMessageContaining,
  15. waitForV4Pane,
  16. waitForV4PermissionDialogInComposerDock,
  17. } from "#e2e/helpers/v4-conversation";
  18. import {
  19. getLatestUpstreamToolResultByToolCallId,
  20. type UpstreamToolResultSnapshot,
  21. } from "#e2e/helpers/conversation-session-network";
  22. import {
  23. isOutsideClosedHistory,
  24. openClosedHistoryFolds,
  25. revealOutsideClosedHistory,
  26. } from "#e2e/helpers/v4-history-folds";
  27. // EvalWorkflowSnippet:工作流创作的试验台(docs/dynamic-workflow/authoring.md「EvalWorkflowSnippet」
  28. // 「The skill gate」;编译反馈卡见 docs/dynamic-workflow/presentation.md「Compiler feedback」)。
  29. //
  30. // 文件名带 `gray-always-on`:配置 mock 网关按文件名下发 alwaysOn,工作流工具才在工具集里。
  31. // case-local 回放只固定模型的 Skill / EvalWorkflowSnippet 选择与收尾文本;真实 Skill 加载、技能门、
  32. // 片段编译、沙箱执行、world 读写、确认门与工具结果都走产品链路。片段完全瞬态,不起 run。
  33. // 每个 it 开一个新会话:技能是否加载按会话历史判定。
  34. const RUN_DIGEST = `[data-testid^="${TID_CHAT_WORKFLOW_RUN_DIGEST}"]`;
  35. const OK_SNIPPET = "dwy-ok-snippet.ts";
  36. const BAD_SNIPPET = "dwy-bad-snippet.ts";
  37. const GATE_OUTPUT = "dwy-gate-output.txt";
  38. const RUNNING_GATE = "dwy-gate-running";
  39. const NOT_EXECUTED_NOTE =
  40. "NOTE: The snippet was NOT executed — fix the errors above and call the tool again.";
  41. const triggerOf = (toolCallId: string) => `[data-testid="tool-summary-trigger-${toolCallId}"]`;
  42. interface SnippetCardState {
  43. /** 摘要行文字(折叠动画期间 getText 可能为空,读 textContent)。 */
  44. row: string;
  45. /** 摘要行有没有展开入口:不可展开的行没有 aria-expanded。 */
  46. expanded: string | null;
  47. hasBody: boolean;
  48. bodyText: string;
  49. headings: string[];
  50. result: string | null;
  51. resultLanguage: string | null;
  52. runningCode: string | null;
  53. logs: string | null;
  54. hasCodeFold: boolean;
  55. codeText: string | null;
  56. feedbackCount: string | null;
  57. feedbackLede: string | null;
  58. feedbackLines: string[];
  59. }
  60. function readSnippetCard(toolCallId: string): Promise<SnippetCardState | null> {
  61. return browser.execute((selector: string) => {
  62. const trigger = document.querySelector<HTMLElement>(selector);
  63. if (!trigger) return null;
  64. const root = trigger.parentElement;
  65. const body = root?.querySelector<HTMLElement>('[data-testid="workflow-snippet-body"]') ?? null;
  66. // 代码块由 @pierre/diffs 画在 shadow DOM 里,textContent 读不到:连 shadow root 一起走,跳过样式节点。
  67. const collect = (node: Node): string => {
  68. if (node.nodeType === Node.TEXT_NODE) return node.textContent ?? "";
  69. if (node.nodeName === "STYLE" || node.nodeName === "SCRIPT") return "";
  70. const shadow = node instanceof Element ? (node.shadowRoot?.childNodes ?? []) : [];
  71. return [...Array.from(shadow), ...Array.from(node.childNodes)].map(collect).join("");
  72. };
  73. const text = (element: Element | null | undefined) =>
  74. element ? collect(element).replace(/\s+/g, " ").trim() : null;
  75. const within = (testId: string) => body?.querySelector(`[data-testid="${testId}"]`) ?? null;
  76. const feedback = within("workflow-compiler-feedback");
  77. const result = within("snippet-result");
  78. return {
  79. row: text(trigger) ?? "",
  80. expanded: trigger.getAttribute("aria-expanded"),
  81. hasBody: body !== null,
  82. bodyText: text(body) ?? "",
  83. headings: Array.from(body?.querySelectorAll("h4, summary") ?? []).map(
  84. (element) => text(element) ?? "",
  85. ),
  86. result: text(result),
  87. resultLanguage:
  88. result?.querySelector("[data-language]")?.getAttribute("data-language") ?? null,
  89. runningCode: text(within("snippet-running-code")),
  90. logs: text(within("snippet-logs")?.querySelector("pre")),
  91. hasCodeFold: within("snippet-code") !== null,
  92. codeText: text(within("snippet-code")),
  93. feedbackCount: text(
  94. feedback?.querySelector('[data-testid="workflow-compiler-feedback-count"]'),
  95. ),
  96. feedbackLede: text(
  97. feedback?.querySelector('[data-testid="workflow-compiler-feedback-lede"]'),
  98. ),
  99. feedbackLines: Array.from(
  100. feedback?.querySelectorAll('[data-testid="workflow-compiler-feedback-line"]') ?? [],
  101. )
  102. // 坐标、正文、码是三个相邻元素:逐个读再用空格连。
  103. .map((line) =>
  104. Array.from(line.children)
  105. .map((part) => text(part) ?? "")
  106. .join(" "),
  107. ),
  108. };
  109. }, triggerOf(toolCallId));
  110. }
  111. // 已结束一轮的工具行收在「已处理」折叠里(默认收起),行不在展开处就展开所有收起的折叠直到它出现。
  112. async function revealToolRow(toolCallId: string) {
  113. await revealOutsideClosedHistory(triggerOf(toolCallId), `工具行没有出现:${toolCallId}`);
  114. await browser.execute((selector: string) => {
  115. document.querySelector(selector)?.scrollIntoView({ block: "center" });
  116. }, triggerOf(toolCallId));
  117. }
  118. /** 展开片段卡并等到满足条件的内容(终态切换时卡片会换 key 重挂,不能只点一次)。 */
  119. async function openSnippetCard(
  120. toolCallId: string,
  121. accept: (card: SnippetCardState) => boolean,
  122. timeoutMsg: string,
  123. ): Promise<SnippetCardState> {
  124. await revealToolRow(toolCallId);
  125. let last: SnippetCardState | null = null;
  126. await browser
  127. .waitUntil(
  128. async () => {
  129. // 轮次结束时折叠原地收起(见 helpers/v4-history-folds.ts):收起中的卡不算,补开折叠再读。
  130. if (!(await isOutsideClosedHistory(triggerOf(toolCallId)))) await openClosedHistoryFolds();
  131. last = await readSnippetCard(toolCallId);
  132. if (last?.hasBody && accept(last)) return true;
  133. await browser.execute((selector: string) => {
  134. const trigger = document.querySelector<HTMLElement>(selector);
  135. if (trigger?.getAttribute("aria-expanded") === "false") trigger.click();
  136. }, triggerOf(toolCallId));
  137. return false;
  138. },
  139. { timeout: 30000 },
  140. )
  141. .catch((error: unknown) => {
  142. throw new Error(`${timeoutMsg};最后读到 ${JSON.stringify(last)}`, { cause: error });
  143. });
  144. return last as unknown as SnippetCardState;
  145. }
  146. async function waitForToolResult(toolCallId: string): Promise<UpstreamToolResultSnapshot> {
  147. let result: UpstreamToolResultSnapshot | null = null;
  148. await browser.waitUntil(
  149. async () => {
  150. result = await getLatestUpstreamToolResultByToolCallId(toolCallId);
  151. return result !== null;
  152. },
  153. { timeout: 90000, timeoutMsg: `${toolCallId} 的工具结果没有回到模型请求里` },
  154. );
  155. return result as unknown as UpstreamToolResultSnapshot;
  156. }
  157. /** 模型面的返回值段:序列化方式与完成通知相同(带缩进),按值比较。 */
  158. function returnValueOf(content: string): unknown {
  159. const start = content.indexOf("Return value:\n") + "Return value:\n".length;
  160. const end = content.indexOf("\n\nLogs:\n");
  161. return JSON.parse(end === -1 ? content.slice(start) : content.slice(start, end));
  162. }
  163. async function waitForTurnIdle(timeoutMsg: string) {
  164. await waitForV4Pane((pane) => !pane.canStop, timeoutMsg, 60000);
  165. }
  166. async function startSession(mode: "build" | "yolo") {
  167. await startNewV4Draft();
  168. await switchV4Mode(mode);
  169. await browser.waitUntil(async () => (await getV4ConfigProjection()).mode === mode, {
  170. timeout: 30000,
  171. timeoutMsg: `composer 没有切到 mode=${mode}`,
  172. });
  173. await watchPermissionDialogs();
  174. }
  175. // 「不弹窗」是一段时间里的否定:发送前装上观察器,数权限弹窗从无到有出现了几次,不靠固定等待去抽样。
  176. async function watchPermissionDialogs() {
  177. await browser.execute(() => {
  178. const probe = window as Window & {
  179. __dwyDialogs?: number;
  180. __dwyObserver?: MutationObserver;
  181. };
  182. probe.__dwyObserver?.disconnect();
  183. probe.__dwyDialogs = 0;
  184. const seen = () =>
  185. Array.from(document.querySelectorAll<HTMLElement>('[role="listbox"]')).some((element) => {
  186. const label = (element.getAttribute("aria-label") ?? "").trim();
  187. return label === "Permission required" || label === "需要权限";
  188. });
  189. let present = seen();
  190. const observer = new MutationObserver(() => {
  191. const now = seen();
  192. if (now && !present) probe.__dwyDialogs = (probe.__dwyDialogs ?? 0) + 1;
  193. present = now;
  194. });
  195. observer.observe(document.body, { childList: true, subtree: true, attributes: true });
  196. probe.__dwyObserver = observer;
  197. });
  198. }
  199. function permissionDialogCount(): Promise<number> {
  200. return browser.execute(() => (window as Window & { __dwyDialogs?: number }).__dwyDialogs ?? 0);
  201. }
  202. async function writeRuntimeFile(name: string, content: string) {
  203. const path = resolveE2ERuntimePath(name);
  204. await mkdir(dirname(path), { recursive: true });
  205. await writeFile(path, content);
  206. }
  207. describe("动态工作流 · EvalWorkflowSnippet", () => {
  208. before(async () => {
  209. await prepareV4ConversationE2E();
  210. // 断言中文文案:固定界面语言,不跟随宿主系统语言(容器里默认英文)。
  211. await pinE2ELocale("zh-CN");
  212. await Promise.all(
  213. [GATE_OUTPUT, RUNNING_GATE].map((name) => rm(resolveE2ERuntimePath(name), { force: true })),
  214. );
  215. await writeRuntimeFile(OK_SNIPPET, 'log("E2E_DWY_PATH_LOG");\nreturn "E2E_DWY_PATH_VALUE";\n');
  216. await writeRuntimeFile(
  217. BAD_SNIPPET,
  218. '// 第 1 行是注释\nconst broken: number = "x";\nreturn broken;\n',
  219. );
  220. });
  221. after(async () => {
  222. await browser.electron.restoreAllMocks();
  223. await clearAppData();
  224. });
  225. it("DWY-01 只读片段:不弹窗不起 run;模型拿到返回值与日志;卡片只画「工作流片段 · N ms」和返回值,没有返回值的片段行不可展开", async function () {
  226. this.timeout(240000);
  227. expect((await readDynamicWorkflowGrayMockStatus()).scenario).toBe("alwaysOn");
  228. await startSession("build");
  229. await sendV4Prompt("E2E_DWY_VALUE_START 先试一段片段。");
  230. await waitForV4AssistantMessageContaining("E2E_DWY_VALUE_DONE", 120000);
  231. await waitForTurnIdle("片段那一轮没有结束");
  232. const value = await waitForToolResult("toolu_dwy_value_eval");
  233. expect(value.isError).toBe(false);
  234. expect(value.content).toMatch(/^The snippet completed in \d+ms\.\nReturn value:\n/);
  235. expect(returnValueOf(value.content)).toEqual({
  236. marker: "E2E_DWY_VALUE",
  237. sum: 3,
  238. });
  239. expect(value.content).toContain("\n\nLogs:\n- E2E_DWY_LOG_FIRST\n- E2E_DWY_LOG_SECOND");
  240. const empty = await waitForToolResult("toolu_dwy_value_empty");
  241. expect(empty.isError).toBe(false);
  242. expect(empty.content).toMatch(
  243. /^The snippet completed in \d+ms\.\nIt returned no value\.\n\nLogs:\n- E2E_DWY_LOG_ONLY$/,
  244. );
  245. // 只读片段静默放行;片段不进 run 列表。
  246. expect(await permissionDialogCount()).toBe(0);
  247. expect(await $(RUN_DIGEST).isExisting()).toBe(false);
  248. // 完成:只显示返回值(JSON 美化),无标题、日志与代码折叠,不重复耗时、PASS 与给模型的那段话。
  249. const card = await openSnippetCard(
  250. "toolu_dwy_value_eval",
  251. (state) => state.result?.includes("E2E_DWY_VALUE") === true,
  252. "片段卡没有画出返回值",
  253. );
  254. expect(card.row).toMatch(/工作流片段.*\d+ms/);
  255. expect(card.row).not.toContain("执行失败");
  256. expect(card.resultLanguage).toBe("json");
  257. expect(card.headings).toEqual([]);
  258. expect(card.logs).toBeNull();
  259. expect(card.hasCodeFold).toBe(false);
  260. expect(card.feedbackCount).toBeNull();
  261. expect(card.bodyText).not.toMatch(/E2E_DWY_LOG_|completed in|Return value:|\d+ms|PASS|通过/);
  262. // 没有返回值:行上只有耗时,没有展开入口,不留空面板;日志只给模型。
  263. await revealToolRow("toolu_dwy_value_empty");
  264. const emptyCard = await readSnippetCard("toolu_dwy_value_empty");
  265. expect(emptyCard?.row).toMatch(/工作流片段.*\d+ms/);
  266. expect(emptyCard?.expanded).toBeNull();
  267. expect(emptyCard?.hasBody).toBe(false);
  268. });
  269. it("DWY-02 失败:抛错带日志、编不过(facade 没有 agent)、返回值超限、读工作区外、墙钟超时都以结构化形式回到模型;卡片走通用失败摘要或编译反馈卡", async function () {
  270. this.timeout(240000);
  271. await startSession("build");
  272. await sendV4Prompt("E2E_DWY_FAIL_START 试几段会失败的片段。");
  273. await waitForV4AssistantMessageContaining("E2E_DWY_FAIL_DONE", 120000);
  274. await waitForTurnIdle("失败片段那一轮没有结束");
  275. const thrown = await waitForToolResult("toolu_dwy_fail_throw");
  276. expect(thrown.content).toMatch(/^The snippet failed \(\w+\): .*E2E_DWY_BOOM/);
  277. expect(thrown.content).toContain("\n\nLogs:\n- E2E_DWY_BEFORE_THROW");
  278. const compile = await waitForToolResult("toolu_dwy_fail_compile");
  279. expect(compile.content).toMatch(/^The snippet has errors:\nL2:C\d+ Cannot find name 'agent'\./);
  280. expect(compile.content).toContain(NOT_EXECUTED_NOTE);
  281. // 编不过就什么都没跑:第一行的 log 没有留下。
  282. expect(compile.content).not.toContain("E2E_DWY_NEVER_LOGGED");
  283. const large = await waitForToolResult("toolu_dwy_fail_large");
  284. expect(large.content).toMatch(/^The snippet failed \(ArtifactTooLarge\): /);
  285. expect(large.content).toContain("Return a summary");
  286. // 与真实 run 同一条 world 读:工作区外的路径被拒。
  287. const outside = await waitForToolResult("toolu_dwy_fail_outside");
  288. expect(outside.content).toMatch(/^The snippet failed \(\w+\): .*is outside the workspace/);
  289. const timedOut = await waitForToolResult("toolu_dwy_fail_timeout");
  290. expect(timedOut.content).toMatch(/^The snippet failed \(\w+\): .*1000ms/);
  291. expect(await permissionDialogCount()).toBe(0);
  292. expect(await $(RUN_DIGEST).isExisting()).toBe(false);
  293. // 抛错:通用失败摘要(不写耗时),展开依次是错误、日志、代码折叠,错误信息只出现一次。
  294. const thrownCard = await openSnippetCard(
  295. "toolu_dwy_fail_throw",
  296. (state) => state.result?.includes("E2E_DWY_BOOM") === true,
  297. "抛错的片段卡没有展开出错误",
  298. );
  299. expect(thrownCard.row).toContain("执行失败");
  300. expect(thrownCard.row).not.toMatch(/\d+ms/);
  301. expect(thrownCard.logs).toBe("E2E_DWY_BEFORE_THROW");
  302. expect(thrownCard.hasCodeFold).toBe(true);
  303. expect(thrownCard.codeText).toContain("代码");
  304. // 日志只在日志段:错误段里解包掉了给模型的 Logs 尾巴。
  305. expect(thrownCard.result).not.toContain("E2E_DWY_BEFORE_THROW");
  306. // 编不过:编译反馈卡(与 CreateWorkflow 同一张),诊断不再以错误文本重复一遍。
  307. const compileCard = await openSnippetCard(
  308. "toolu_dwy_fail_compile",
  309. (state) => state.feedbackCount !== null,
  310. "编不过的片段卡没有展开出编译反馈",
  311. );
  312. expect(compileCard.row).toContain("执行失败");
  313. expect(compileCard.feedbackCount).toBe("1 条");
  314. expect(compileCard.feedbackLede).toBe(
  315. "类型检查未通过,脚本未运行;反馈已交回模型,由它修改后重新提交。",
  316. );
  317. expect(compileCard.feedbackLines).toHaveLength(1);
  318. expect(compileCard.feedbackLines[0]).toMatch(/^L2:C\d+ Cannot find name 'agent'.*TS2304$/);
  319. expect(compileCard.result).toBeNull();
  320. expect(compileCard.logs).toBeNull();
  321. expect(compileCard.hasCodeFold).toBe(true);
  322. const largeCard = await openSnippetCard(
  323. "toolu_dwy_fail_large",
  324. (state) => state.result?.includes("ArtifactTooLarge") === true,
  325. "超限的片段卡没有展开出错误",
  326. );
  327. expect(largeCard.row).toContain("执行失败");
  328. const timeoutCard = await openSnippetCard(
  329. "toolu_dwy_fail_timeout",
  330. (state) => state.result?.includes("1000ms") === true,
  331. "超时的片段卡没有展开出错误",
  332. );
  333. expect(timeoutCard.row).toContain("执行失败");
  334. });
  335. it("DWY-03 技能门:没加载技能时片段被拒(工具错误点名技能),不弹窗、不执行;加载后同一片段带 world.run,确认门询问,批准后才执行", async function () {
  336. this.timeout(240000);
  337. await startSession("build");
  338. await sendV4Prompt("E2E_DWY_GATE_START 跑一段会写文件的片段。");
  339. const refused = await waitForToolResult("toolu_dwy_gate_eval_1");
  340. expect(refused.isError).toBe(true);
  341. expect(refused.content).toContain(
  342. "EvalWorkflowSnippet needs the `dynamic-workflows` skill loaded in this session before it accepts a script.",
  343. );
  344. expect(refused.content).toContain('Call the Skill tool with skill "dynamic-workflows" first');
  345. expect(refused.content).toContain("then call EvalWorkflowSnippet again. Nothing was started.");
  346. // 模型加载技能后重发:第一个、也是唯一一个窗属于第二次调用;批准前片段没有执行。
  347. await waitForV4PermissionDialogInComposerDock(120000);
  348. expect((await waitForToolResult("toolu_dwy_gate_skill")).isError).toBe(false);
  349. expect(await permissionDialogCount()).toBe(1);
  350. await expect(readFile(resolveE2ERuntimePath(GATE_OUTPUT), "utf8")).rejects.toMatchObject({
  351. code: "ENOENT",
  352. });
  353. await approveV4Permission();
  354. const ran = await waitForToolResult("toolu_dwy_gate_eval_2");
  355. expect(ran.isError).toBe(false);
  356. expect(returnValueOf(ran.content)).toEqual({ marker: "E2E_DWY_GATE_VALUE", exitCode: 0 });
  357. expect(await readFile(resolveE2ERuntimePath(GATE_OUTPUT), "utf8")).toBe("ran\n");
  358. await waitForV4AssistantMessageContaining("E2E_DWY_GATE_DONE", 60000);
  359. await waitForTurnIdle("技能门那一轮没有结束");
  360. expect(await permissionDialogCount()).toBe(1);
  361. // 被门拒绝的行走通用失败摘要:展开是拒绝文本,没有日志与编译反馈。
  362. const refusedCard = await openSnippetCard(
  363. "toolu_dwy_gate_eval_1",
  364. (state) => state.result?.includes("dynamic-workflows") === true,
  365. "被技能门拒绝的行没有展开出错误",
  366. );
  367. expect(refusedCard.row).toContain("执行失败");
  368. expect(refusedCard.logs).toBeNull();
  369. expect(refusedCard.feedbackCount).toBeNull();
  370. const ranCard = await openSnippetCard(
  371. "toolu_dwy_gate_eval_2",
  372. (state) => state.result?.includes("E2E_DWY_GATE_VALUE") === true,
  373. "批准后的片段卡没有画出返回值",
  374. );
  375. expect(ranCard.row).toMatch(/工作流片段.*\d+ms/);
  376. });
  377. it("DWY-04 运行中:完全访问下带 world.run 的片段不询问;行写「正在运行工作流片段 · N s」,展开是代码;闸文件放行后变成「工作流片段 · N ms」和返回值", async function () {
  378. this.timeout(240000);
  379. await startSession("yolo");
  380. await sendV4Prompt("E2E_DWY_RUNNING_START 跑一段要等的片段。");
  381. const running = await openSnippetCard(
  382. "toolu_dwy_running_eval",
  383. (state) => state.runningCode?.includes("E2E_DWY_RUNNING_VALUE") === true,
  384. "运行中的片段卡展开后没有代码",
  385. );
  386. expect(running.row).toMatch(/正在运行工作流片段.*\d+s/);
  387. expect(running.row).not.toMatch(/\d+ms/);
  388. expect(running.result).toBeNull();
  389. expect(await getLatestUpstreamToolResultByToolCallId("toolu_dwy_running_eval")).toBeNull();
  390. await writeRuntimeFile(RUNNING_GATE, "open\n");
  391. const done = await waitForToolResult("toolu_dwy_running_eval");
  392. expect(done.isError).toBe(false);
  393. expect(returnValueOf(done.content)).toEqual({
  394. marker: "E2E_DWY_RUNNING_VALUE",
  395. out: "E2E_DWY_RUNNING_STDOUT",
  396. // 片段的 world.run 与 files.* 落在同一个工作区:命令写下的文件,glob 给相对路径,read 读得到。
  397. found: ["dwy-running.txt"],
  398. written: "E2E_DWY_WRITTEN",
  399. });
  400. await waitForV4AssistantMessageContaining("E2E_DWY_RUNNING_DONE", 60000);
  401. await waitForTurnIdle("运行中片段那一轮没有结束");
  402. expect(await permissionDialogCount()).toBe(0);
  403. const card = await openSnippetCard(
  404. "toolu_dwy_running_eval",
  405. (state) => state.result?.includes("E2E_DWY_RUNNING_STDOUT") === true,
  406. "片段完成后卡片没有换成返回值",
  407. );
  408. expect(card.row).toMatch(/工作流片段.*\d+ms/);
  409. expect(card.row).not.toContain("正在运行");
  410. expect(card.runningCode).toBeNull();
  411. });
  412. it("DWY-05 来源:path 读文件当片段跑,诊断按文件行且点名文件;文件读不了、code 与 path 都给、都不给各有自己的拒绝", async function () {
  413. this.timeout(240000);
  414. await startSession("build");
  415. await sendV4Prompt("E2E_DWY_PATH_START 按文件跑片段。");
  416. await waitForV4AssistantMessageContaining("E2E_DWY_PATH_DONE", 120000);
  417. await waitForTurnIdle("path 片段那一轮没有结束");
  418. const ok = await waitForToolResult("toolu_dwy_path_ok");
  419. expect(ok.isError).toBe(false);
  420. expect(ok.content).toContain("Return value:\n");
  421. expect(ok.content).toContain("E2E_DWY_PATH_VALUE");
  422. expect(ok.content).toContain("Logs:\n- E2E_DWY_PATH_LOG");
  423. const bad = await waitForToolResult("toolu_dwy_path_bad");
  424. expect(bad.content).toContain("The snippet has errors:");
  425. expect(bad.content).toMatch(
  426. new RegExp(`${BAD_SNIPPET.replace(".", "\\.")}:L2:C7 Type 'string' is not assignable`),
  427. );
  428. expect(bad.content).toContain(NOT_EXECUTED_NOTE);
  429. const missing = await waitForToolResult("toolu_dwy_path_missing");
  430. expect(missing.isError).toBe(true);
  431. expect(missing.content).toContain("dwy-absent.ts");
  432. expect(missing.content).toContain("could not be read");
  433. const both = await waitForToolResult("toolu_dwy_path_both");
  434. expect(both.isError).toBe(true);
  435. expect(both.content).toContain(
  436. "Provide exactly one snippet source: `code` for the snippet inline, or `path` for a file holding it. Passing both is ambiguous.",
  437. );
  438. const neither = await waitForToolResult("toolu_dwy_path_neither");
  439. expect(neither.isError).toBe(true);
  440. expect(neither.content).toContain(
  441. "No snippet source arrived. Pass `code` for the snippet inline, or `path` for a file holding it.",
  442. );
  443. expect(await permissionDialogCount()).toBe(0);
  444. // path 编不过:与内联同一张编译反馈卡,坐标是文件行。
  445. const badCard = await openSnippetCard(
  446. "toolu_dwy_path_bad",
  447. (state) => state.feedbackCount !== null,
  448. "path 编不过的片段卡没有展开出编译反馈",
  449. );
  450. expect(badCard.feedbackLines[0]).toMatch(/^L2:C7 .*TS2322$/);
  451. // 诊断只归编译反馈卡:这里曾把给模型的全文连同本机绝对路径又画一遍。
  452. expect(badCard.bodyText).not.toContain("The snippet has errors");
  453. expect(badCard.bodyText).not.toContain(BAD_SNIPPET);
  454. });
  455. });