automations-workflows-hub.test.ts 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  2. import { dirname, join } from "node:path";
  3. import {
  4. TID_AUTOMATIONS_OPEN,
  5. TID_AUTOMATIONS_PAGE_TAB,
  6. TID_CHAT_SUMMARY_PANEL,
  7. TID_CHAT_WORKFLOW_RUN_DIGEST,
  8. TID_CONFIRM_DIALOG_CONFIRM,
  9. TID_TASK_ITEM,
  10. TID_V4_COMPOSER_BACKGROUND_WORK_TRIGGER,
  11. TID_V4_TIMELINE,
  12. TID_WORKFLOW_ACTION_DELETE,
  13. TID_WORKFLOW_ACTION_MOVE,
  14. TID_WORKFLOW_ARTIFACT_CARD,
  15. TID_WORKFLOW_ARTIFACT_PANE,
  16. TID_WORKFLOW_CARD,
  17. TID_WORKFLOW_CARD_MENU,
  18. TID_WORKFLOW_CARD_RUN,
  19. TID_WORKFLOW_DETAIL,
  20. TID_WORKFLOW_DETAIL_DESCRIPTION,
  21. TID_WORKFLOW_DETAIL_MENU,
  22. TID_WORKFLOW_GLOBAL_GROUP,
  23. TID_WORKFLOW_LAUNCH_ARG,
  24. TID_WORKFLOW_LAUNCH_DIALOG,
  25. TID_WORKFLOW_LAUNCH_ERROR,
  26. TID_WORKFLOW_LAUNCH_SUBMIT,
  27. TID_WORKFLOW_META_SAVE,
  28. TID_WORKFLOW_MOVE_DIALOG,
  29. TID_WORKFLOW_MOVE_DIALOG_SUBMIT,
  30. TID_WORKFLOW_PROJECT_GROUP,
  31. TID_WORKFLOWS_CREATE_VIA_CHAT,
  32. TID_WORKFLOWS_EMPTY,
  33. testId,
  34. } from "@zcode/shared";
  35. import {
  36. DEFAULT_WORKSPACE,
  37. clearAppData,
  38. clickTestIdByDom,
  39. clickTestIdByWebDriver,
  40. setInputValueByTestIdDom,
  41. waitForTestIdByDom,
  42. } from "./helpers/desktop-app.js";
  43. import {
  44. getMessages,
  45. prepareConversationE2E,
  46. waitForUserMessageContaining,
  47. } from "./helpers/conversation-session.js";
  48. import { getV4ComposerBackgroundWorkCounts } from "./helpers/v4-conversation.js";
  49. const WORKFLOW_NAME = "release-check";
  50. const WORKFLOWS_DIR = join(DEFAULT_WORKSPACE, ".zcode", "workflows");
  51. const WORKFLOW_FILE = join(WORKFLOWS_DIR, `${WORKFLOW_NAME}.dwf.ts`);
  52. const EDITED_DESCRIPTION = "发布前检查(E2E 已编辑)";
  53. // 脚本正文:元数据编辑只改文件顶部的 frontmatter,这一段必须逐字节保留(不变式 4)。
  54. const SCRIPT = [
  55. "export default async ({ agent }) => {",
  56. ' await agent("跑测试并整理发布说明");',
  57. "};",
  58. "",
  59. ].join("\n");
  60. const SEED = [
  61. "/* zcode-workflow",
  62. "description: 发布前检查",
  63. "whenToUse: 准备发版时",
  64. "args:",
  65. " branch:",
  66. " type: string",
  67. " required: true",
  68. "*/",
  69. SCRIPT,
  70. ].join("\n");
  71. interface GroupHeaderInfo {
  72. groupTestId: string | null;
  73. text: string;
  74. createTestId: string | null;
  75. }
  76. async function fileExists(path: string): Promise<boolean> {
  77. try {
  78. await access(path);
  79. return true;
  80. } catch {
  81. return false;
  82. }
  83. }
  84. async function readTestIdText(currentTestId: string): Promise<string | null> {
  85. return browser.execute((tid) => {
  86. const element = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  87. (item) => item.dataset.testid === tid,
  88. );
  89. return element?.textContent?.trim() ?? null;
  90. }, currentTestId);
  91. }
  92. /**
  93. * 已保存工作流中枢「列表 → 详情 → 改元数据 → 删除」端到端(docs/dynamic-workflow/launch.md)。
  94. *
  95. * 只碰 UI + 项目目录里的 `.zcode/workflows/`:不发送 prompt、不依赖模型回放,因此是确定性的。
  96. * 「运行」走对话的用例在 conversation-session/ 下单独立案(需要回放 provider)。
  97. */
  98. describe("已保存工作流中枢 E2E", () => {
  99. before(async () => {
  100. await mkdir(WORKFLOWS_DIR, { recursive: true });
  101. await writeFile(WORKFLOW_FILE, SEED, "utf-8");
  102. });
  103. after(async () => {
  104. await browser.electron.restoreAllMocks();
  105. await rm(WORKFLOWS_DIR, { recursive: true, force: true });
  106. await clearAppData();
  107. });
  108. it("预置的工作流可见,改说明只重写 frontmatter,删除后回到空态", async function () {
  109. this.timeout(150000);
  110. // 只需要登录 + workspace 就绪即可进入自动化页,不需要配置模型 provider。
  111. await prepareConversationE2E({ skipProvider: true });
  112. // 1) 打开自动化页,页标题切到「工作流」。
  113. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, {
  114. timeoutMsg: "侧栏没有出现自动化入口",
  115. });
  116. await clickTestIdByDom(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"), {
  117. timeoutMsg: "自动化页标题没有「工作流」切换",
  118. });
  119. // 2) 全局视图按项目分组:默认工作区(folder 名 ZCodeProject)自成一组,组头带名字,
  120. // 组内「通过对话创建」按钮带以 workspaceKey 收尾的 test id。
  121. // group / create 的 test id 后缀是 workspaceKey(本地通常 = workspacePath),但
  122. // resolveWorkspaceKey 若对本地路径做归一化(大小写 / 去尾斜杠 / realpath)后缀就不等于
  123. // DEFAULT_WORKSPACE——所以只按前缀等待与断言,不钉死整串。
  124. const readGroupInfo = () =>
  125. browser.execute(
  126. (groupPrefix: string, createPrefix: string) => {
  127. const group = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  128. (item) => item.dataset.testid?.startsWith(groupPrefix),
  129. );
  130. if (!group) return null;
  131. const createButton = group.querySelector<HTMLElement>(`[data-testid^="${createPrefix}"]`);
  132. return {
  133. groupTestId: group.dataset.testid ?? null,
  134. text: group.textContent ?? "",
  135. createTestId: createButton?.dataset.testid ?? null,
  136. };
  137. },
  138. TID_WORKFLOW_PROJECT_GROUP,
  139. // testId(base, "") = `${base}-`:create 按钮 test id 的前缀。
  140. testId(TID_WORKFLOWS_CREATE_VIA_CHAT, ""),
  141. ) as Promise<GroupHeaderInfo | null>;
  142. let groupInfo: GroupHeaderInfo | null = null;
  143. await browser.waitUntil(
  144. async () => {
  145. groupInfo = await readGroupInfo();
  146. return Boolean(groupInfo && groupInfo.createTestId);
  147. },
  148. { timeout: 20000, timeoutMsg: "工作流页没有出现默认工作区的项目组" },
  149. );
  150. const seen = groupInfo as GroupHeaderInfo | null;
  151. expect(seen?.groupTestId?.startsWith(TID_WORKFLOW_PROJECT_GROUP)).toBe(true);
  152. expect(seen?.text).toContain("ZCodeProject");
  153. expect(seen?.createTestId?.startsWith(testId(TID_WORKFLOWS_CREATE_VIA_CHAT, ""))).toBe(true);
  154. expect(seen?.createTestId).toContain("ZCodeProject");
  155. // 3) 预置文件以卡片出现:名字 + frontmatter 里的说明。
  156. const cardTestId = testId(TID_WORKFLOW_CARD, WORKFLOW_NAME);
  157. await waitForTestIdByDom(cardTestId, {
  158. timeout: 20000,
  159. timeoutMsg: "工作流列表没有出现预置的 release-check 卡片",
  160. });
  161. expect(await readTestIdText(cardTestId)).toContain("发布前检查");
  162. // 4) 点卡片进详情,改说明并保存。
  163. await clickTestIdByDom(cardTestId, { timeoutMsg: "无法点击工作流卡片进入详情" });
  164. await waitForTestIdByDom(TID_WORKFLOW_DETAIL, { timeoutMsg: "没有进入工作流详情页" });
  165. await setInputValueByTestIdDom(TID_WORKFLOW_DETAIL_DESCRIPTION, EDITED_DESCRIPTION, {
  166. timeoutMsg: "详情页没有出现说明输入框",
  167. });
  168. await clickTestIdByDom(TID_WORKFLOW_META_SAVE, {
  169. timeoutMsg: "改说明后没有出现「保存元数据」",
  170. });
  171. // 5) 磁盘:frontmatter 更新、脚本正文逐字节不变。
  172. await browser.waitUntil(
  173. async () => (await readFile(WORKFLOW_FILE, "utf-8")).includes(EDITED_DESCRIPTION),
  174. { timeout: 15000, timeoutMsg: "保存后磁盘文件的说明没有更新" },
  175. );
  176. const saved = await readFile(WORKFLOW_FILE, "utf-8");
  177. expect(saved.startsWith("/* zcode-workflow\n")).toBe(true);
  178. expect(saved.endsWith(`*/\n${SCRIPT}`)).toBe(true);
  179. expect(saved).toContain("whenToUse: 准备发版时");
  180. expect(saved).toContain("required: true");
  181. // 6) 详情页 ⋯ 菜单 → 删除 → 二次确认 → 回到列表空态,文件消失。
  182. await clickTestIdByWebDriver(TID_WORKFLOW_DETAIL_MENU, {
  183. timeoutMsg: "详情页没有「更多操作」菜单",
  184. });
  185. await clickTestIdByDom(testId(TID_WORKFLOW_ACTION_DELETE, WORKFLOW_NAME), {
  186. timeoutMsg: "详情页菜单没有「删除工作流」项",
  187. });
  188. await clickTestIdByDom(TID_CONFIRM_DIALOG_CONFIRM, {
  189. timeoutMsg: "删除二次确认弹窗没有出现",
  190. });
  191. await waitForTestIdByDom(TID_WORKFLOWS_EMPTY, {
  192. timeout: 15000,
  193. timeoutMsg: "删除后没有回到工作流空态",
  194. });
  195. await browser.waitUntil(async () => !(await fileExists(WORKFLOW_FILE)), {
  196. timeout: 10000,
  197. timeoutMsg: "删除后磁盘文件仍然存在",
  198. });
  199. });
  200. });
  201. // ── 全局工作流:置顶「全局」组 + 全局→项目搬文件 + 项目→全局 AI 概括(docs/dynamic-workflow/launch.md)──
  202. // E2E_HOME_DIR 由 wdio 配置设为隔离目录并写进 HOME(wdio.conf.ts:520),app / agent 进程都继承它,
  203. // 因此 agent 的 os.homedir() 解析到这里,全局根 = `<E2E_HOME>/.zcode/workflows/`。
  204. const E2E_HOME = dirname(DEFAULT_WORKSPACE);
  205. const GLOBAL_WORKFLOW_NAME = "global-note";
  206. const GLOBAL_WORKFLOWS_DIR = join(E2E_HOME, ".zcode", "workflows");
  207. const GLOBAL_WORKFLOW_FILE = join(GLOBAL_WORKFLOWS_DIR, `${GLOBAL_WORKFLOW_NAME}.dwf.ts`);
  208. const GLOBAL_PROJECT_WORKFLOWS_DIR = join(DEFAULT_WORKSPACE, ".zcode", "workflows");
  209. const GLOBAL_PROJECT_WORKFLOW_FILE = join(
  210. GLOBAL_PROJECT_WORKFLOWS_DIR,
  211. `${GLOBAL_WORKFLOW_NAME}.dwf.ts`,
  212. );
  213. // 无实参的全局工作流:「移到项目…」是逐字节搬文件,脚本正文两端必须完全一致。
  214. const GLOBAL_SCRIPT = [
  215. "export default async ({ agent }) => {",
  216. ' await agent("做一次深度调研");',
  217. "};",
  218. "",
  219. ].join("\n");
  220. const GLOBAL_SEED = [
  221. "/* zcode-workflow",
  222. "description: 深度调研(全局)",
  223. "*/",
  224. GLOBAL_SCRIPT,
  225. ].join("\n");
  226. describe("全局工作流中枢 E2E", () => {
  227. before(async () => {
  228. await mkdir(GLOBAL_WORKFLOWS_DIR, { recursive: true });
  229. await writeFile(GLOBAL_WORKFLOW_FILE, GLOBAL_SEED, "utf-8");
  230. });
  231. after(async () => {
  232. await browser.electron.restoreAllMocks();
  233. await rm(GLOBAL_WORKFLOW_FILE, { force: true });
  234. await rm(GLOBAL_PROJECT_WORKFLOW_FILE, { force: true });
  235. await clearAppData();
  236. });
  237. it("全局组置顶;全局→项目搬文件;项目→全局开新会话发概括提示、文件不动", async function () {
  238. this.timeout(150000);
  239. await prepareConversationE2E({ skipProvider: true });
  240. // 1) 自动化页 →「工作流」标签。
  241. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, { timeoutMsg: "侧栏没有出现自动化入口" });
  242. await clickTestIdByDom(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"), {
  243. timeoutMsg: "自动化页标题没有「工作流」切换",
  244. });
  245. // 2) 顶部「全局」组出现,且含预置的 global-note 卡片。
  246. await waitForTestIdByDom(TID_WORKFLOW_GLOBAL_GROUP, {
  247. timeout: 20000,
  248. timeoutMsg: "工作流页没有出现「全局」组",
  249. });
  250. const globalCardTestId = testId(TID_WORKFLOW_CARD, GLOBAL_WORKFLOW_NAME);
  251. await waitForTestIdByDom(globalCardTestId, {
  252. timeout: 20000,
  253. timeoutMsg: "「全局」组里没有出现预置的 global-note 卡片",
  254. });
  255. // 3) 卡片 ⋯ →「移到项目…」→ 移动窗 → 提交。
  256. await clickTestIdByWebDriver(testId(TID_WORKFLOW_CARD_MENU, GLOBAL_WORKFLOW_NAME), {
  257. timeoutMsg: "全局卡片没有「更多操作」菜单",
  258. });
  259. await clickTestIdByDom(testId(TID_WORKFLOW_ACTION_MOVE, GLOBAL_WORKFLOW_NAME), {
  260. timeoutMsg: "全局卡片菜单没有「移到项目…」项",
  261. });
  262. await waitForTestIdByDom(TID_WORKFLOW_MOVE_DIALOG, { timeoutMsg: "没有出现「移到项目」窗" });
  263. await clickTestIdByDom(TID_WORKFLOW_MOVE_DIALOG_SUBMIT, {
  264. timeoutMsg: "「移到项目」窗没有提交按钮",
  265. });
  266. // 4) 磁盘:文件已搬到项目目录,从 HOME 消失。
  267. await browser.waitUntil(async () => fileExists(GLOBAL_PROJECT_WORKFLOW_FILE), {
  268. timeout: 15000,
  269. timeoutMsg: "移到项目后文件没有出现在项目目录",
  270. });
  271. await browser.waitUntil(async () => !(await fileExists(GLOBAL_WORKFLOW_FILE)), {
  272. timeout: 10000,
  273. timeoutMsg: "移到项目后 HOME 里的文件仍然存在",
  274. });
  275. // 5) 卡片现在落在项目组里,且「全局」组仍在项目组之上(比较 DOM 顺序)。
  276. const projectGroupTestId = await browser.waitUntil(
  277. async () => {
  278. const tid = await browser.execute((prefix: string) => {
  279. const group = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  280. (item) => item.dataset.testid?.startsWith(prefix),
  281. );
  282. return group?.dataset.testid ?? null;
  283. }, TID_WORKFLOW_PROJECT_GROUP);
  284. return tid ?? false;
  285. },
  286. { timeout: 15000, timeoutMsg: "移到项目后没有出现项目组" },
  287. );
  288. const order = await browser.execute(
  289. (globalTid: string, projectTid: string) => {
  290. const globalEl = document.querySelector<HTMLElement>(`[data-testid="${globalTid}"]`);
  291. const projectEl = document.querySelector<HTMLElement>(`[data-testid="${projectTid}"]`);
  292. if (!globalEl || !projectEl) return null;
  293. // Node.DOCUMENT_POSITION_FOLLOWING = 4:project 在 global 之后。
  294. return (globalEl.compareDocumentPosition(projectEl) & 4) !== 0
  295. ? "global-first"
  296. : "project-first";
  297. },
  298. TID_WORKFLOW_GLOBAL_GROUP,
  299. projectGroupTestId as string,
  300. );
  301. expect(order).toBe("global-first");
  302. // 卡片仍可见(此刻在项目组里)。
  303. await waitForTestIdByDom(globalCardTestId, { timeoutMsg: "移到项目后卡片消失了" });
  304. // 6) 项目卡片 ⋯ →「提升为全局」(2026-09-04 追记):不搬文件,而是在本项目开新会话、自动发送
  305. // 概括提示(createSession.firstInput)。断言:切到新会话,首条用户消息含工作流名、路径与
  306. // scope: "global" 指令;两处文件都不动(不变式 9:提升不搬、不删)。模型回合本身不断言
  307. // (skipProvider 下没有 provider,那一跳失败与否与本特性无关)。
  308. await clickTestIdByWebDriver(testId(TID_WORKFLOW_CARD_MENU, GLOBAL_WORKFLOW_NAME), {
  309. timeoutMsg: "项目卡片没有「更多操作」菜单",
  310. });
  311. await clickTestIdByDom(testId(TID_WORKFLOW_ACTION_MOVE, GLOBAL_WORKFLOW_NAME), {
  312. timeoutMsg: "项目卡片菜单没有「提升为全局」项",
  313. });
  314. await waitForUserMessageContaining(GLOBAL_WORKFLOW_NAME);
  315. const promoteMessage = (await getMessages("user")).find((message) =>
  316. message.text.includes(GLOBAL_WORKFLOW_NAME),
  317. );
  318. expect(promoteMessage?.text ?? "").toContain(GLOBAL_PROJECT_WORKFLOW_FILE);
  319. expect(promoteMessage?.text ?? "").toContain('scope: "global"');
  320. // 文件不动:项目档还在,HOME 里没有被搬回去的同名文件。
  321. expect(await fileExists(GLOBAL_PROJECT_WORKFLOW_FILE)).toBe(true);
  322. expect(await fileExists(GLOBAL_WORKFLOW_FILE)).toBe(false);
  323. });
  324. });
  325. // ── 直接启动:中枢「运行」→ 新会话顶部启动卡(docs/dynamic-workflow/launch.md)─────────
  326. // 与前两组一样只碰 UI + 项目目录里的 `.zcode/workflows/`,且刻意用**无 agent()/模型调用**的
  327. // 常量工作流:run 的子代理一个都不起,因此整条路(建会话 → startSavedWorkflow → run 结算)
  328. // 都不需要 provider 回放,skipProvider 下即可跑到 completed。run 终态之后会触发既有的完成
  329. // 通知回合(那一跳才第一次要模型),本组**刻意断言到启动卡 completed 为止**,不依赖模型——
  330. // 与 spec 测试地图「完成通知回合按既有 replay 惯例处理或断言到通知行为止」一致。
  331. const LAUNCH_OK_NAME = "direct-launch-ok";
  332. const LAUNCH_OK_ARG = "topic";
  333. const LAUNCH_OK_ARG_VALUE = "adaptive concurrency in agent runtimes";
  334. const LAUNCH_OK_FILE = join(WORKFLOWS_DIR, `${LAUNCH_OK_NAME}.dwf.ts`);
  335. // 脚本体:无 agent()、立即返回常量。声明一个必填实参 `topic`,好让「运行」弹出实参窗
  336. // (无实参的项目档点击即跑、不弹窗,见 spec)。编译干净(`return <字面量>` 是合法脚本体)。
  337. const LAUNCH_OK_SEED = [
  338. "/* zcode-workflow",
  339. "description: 常量工作流(直接启动 E2E)",
  340. "args:",
  341. " topic:",
  342. " type: string",
  343. " required: true",
  344. "*/",
  345. "return { ok: true };",
  346. "",
  347. ].join("\n");
  348. // 在跑一段时间的常量工作流(composer 徽标直达 E2E,docs/dynamic-workflow/presentation.md):
  349. // 零 agent、零 provider,靠 `world.run("sleep", …)` 停留在 running——这是沙箱里唯一不需要模型
  350. // 又能让 run 停住的手段(沙箱 realm 只有 ES intrinsics,没有 setTimeout)。cmd 是编译期字面量,
  351. // 直接启动把它编进 declaredRunCommands,不需要确认窗。无实参的项目档点「运行」即跑、不弹实参窗。
  352. // 注:`sleep` 依赖 POSIX 环境;桌面 e2e 只在 macOS / Linux 容器里跑。
  353. const LAUNCH_SLEEP_NAME = "direct-launch-sleeping";
  354. const LAUNCH_SLEEP_FILE = join(WORKFLOWS_DIR, `${LAUNCH_SLEEP_NAME}.dwf.ts`);
  355. const LAUNCH_SLEEP_SEED = [
  356. "/* zcode-workflow",
  357. "description: 停留 running 的常量工作流(composer 徽标直达 E2E)",
  358. "*/",
  359. 'await world.run("sleep", ["40"]);',
  360. "return { ok: true };",
  361. "",
  362. ].join("\n");
  363. // 保存 / 再次运行(docs/dynamic-workflow/transcript-and-notifications.md「Saving the run, and running
  364. // it again」):无实参的项目档,点「运行」即跑。脚本体与本组其他常量工作流逐字不同——「按 run 跑的
  365. // 那一份认出已保存」这条规则必须只能命中它自己。
  366. const LAUNCH_SAVE_NAME = "direct-launch-save";
  367. const LAUNCH_SAVE_FILE = join(WORKFLOWS_DIR, `${LAUNCH_SAVE_NAME}.dwf.ts`);
  368. const LAUNCH_SAVE_SCRIPT = "return { ok: true, savedFromRun: 1 };\n";
  369. const LAUNCH_SAVE_SEED = [
  370. "/* zcode-workflow",
  371. "description: 保存与再次运行(直接启动 E2E)",
  372. "*/",
  373. LAUNCH_SAVE_SCRIPT,
  374. ].join("\n");
  375. // 「作为文件打开」(docs/dynamic-workflow/authoring.md「How the user sees them」):零 agent 的常量工作流
  376. // 发布一份 markdown 产物,run 完成后从侧板打开产物 tab,点「作为文件打开」。系统默认 App 那一跳由
  377. // shell.openPath 的 mock 接住——断言的是它收到的路径,以及那个路径上真的落了这一版的字节。
  378. const LAUNCH_ARTIFACT_NAME = "direct-launch-artifact";
  379. const LAUNCH_ARTIFACT_FILE = join(WORKFLOWS_DIR, `${LAUNCH_ARTIFACT_NAME}.dwf.ts`);
  380. const LAUNCH_ARTIFACT_ID = "e2e-report";
  381. const LAUNCH_ARTIFACT_MARKDOWN = "# E2E 报告\n\n作为文件打开。\n";
  382. const LAUNCH_ARTIFACT_SEED = [
  383. "/* zcode-workflow",
  384. "description: 发布 markdown 产物(作为文件打开 E2E)",
  385. "*/",
  386. `await artifact.markdown(${JSON.stringify(LAUNCH_ARTIFACT_ID)}, ${JSON.stringify(
  387. LAUNCH_ARTIFACT_MARKDOWN,
  388. )}, { title: "E2E 报告" });`,
  389. "return { ok: true };",
  390. "",
  391. ].join("\n");
  392. const LAUNCH_FAIL_NAME = "direct-launch-broken";
  393. const LAUNCH_FAIL_FILE = join(WORKFLOWS_DIR, `${LAUNCH_FAIL_NAME}.dwf.ts`);
  394. // frontmatter 仍然合法(卡片照常出现、实参窗照常弹),脚本体带一个类型错误(TS2322:把字符串
  395. // 赋给 number)。编译只在启动的 ② 阶段发生,因此错误在 startSavedWorkflow 的 ACK 里以
  396. // compile_failed 回来,被实参窗行内错误区接住——会话在此之前不存在(不变式 2)。
  397. const LAUNCH_FAIL_SEED = [
  398. "/* zcode-workflow",
  399. "description: 编译失败工作流(直接启动 E2E)",
  400. "args:",
  401. " topic:",
  402. " type: string",
  403. " required: true",
  404. "*/",
  405. 'const answer: number = "not a number";',
  406. "return answer;",
  407. "",
  408. ].join("\n");
  409. // 脚本点名了一个本机目录里没有的模型(docs/dynamic-workflow/launch.md「Models the script names」):
  410. // 编译本身干净,解不出来的名字按 9011 与编译诊断同一条路拒绝(compile_failed)。脚本里只**声明**
  411. // 这个子代理、从不 ask 它,所以即使解析意外放行也不会碰 provider。
  412. const LAUNCH_MODEL_NAME = "direct-launch-unknown-model";
  413. const LAUNCH_MODEL_FILE = join(WORKFLOWS_DIR, `${LAUNCH_MODEL_NAME}.dwf.ts`);
  414. const LAUNCH_MODEL_UNKNOWN = "no-such-model-e2e";
  415. const LAUNCH_MODEL_SEED = [
  416. "/* zcode-workflow",
  417. "description: 点名未知模型的工作流(直接启动 E2E)",
  418. "args:",
  419. " topic:",
  420. " type: string",
  421. " required: true",
  422. "*/",
  423. `agent("judge", { model: "${LAUNCH_MODEL_UNKNOWN}" });`,
  424. "return 1;",
  425. "",
  426. ].join("\n");
  427. /** 按前缀读某个 testid 元素的可见文本(启动卡 / 任务项的 testid 后缀是运行期才知道的 key)。 */
  428. async function readTestIdPrefixText(prefix: string): Promise<string | null> {
  429. return browser.execute((currentPrefix) => {
  430. const element = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  431. (item) => item.dataset.testid?.startsWith(currentPrefix),
  432. );
  433. return element?.textContent?.replace(/ /g, " ").trim() ?? null;
  434. }, prefix);
  435. }
  436. /**
  437. * 启动轮 run 卡当前的 run 状态(`data-workflow-run-status`);卡不在时为 null。直接启动的会话顶部长出的
  438. * 就是普通的轮尾 run 卡(docs/dynamic-workflow/presentation.md「The run card」),不是别的卡。
  439. */
  440. async function readLaunchCardStatus(): Promise<string | null> {
  441. return browser.execute(
  442. (prefix) => {
  443. const card = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  444. (item) => item.dataset.testid?.startsWith(prefix),
  445. );
  446. return card?.getAttribute("data-workflow-run-status") ?? null;
  447. },
  448. testId(TID_CHAT_WORKFLOW_RUN_DIGEST, ""),
  449. );
  450. }
  451. /** 读 run 详情侧板来龙去脉节的可见文本(docs/dynamic-workflow/launch.md「The launch turn」)。 */
  452. async function readLaunchProvenanceText(): Promise<string | null> {
  453. return browser.execute(() => {
  454. const element = document.querySelector<HTMLElement>('[data-testid="workflow-run-provenance"]');
  455. return element?.textContent?.replace(/ /g, " ").trim() ?? null;
  456. });
  457. }
  458. /** 侧栏任务项数量(compile_failed 用例比较提交前后不变)。 */
  459. async function countTaskItems(): Promise<number> {
  460. return browser.execute(
  461. (prefix) => document.querySelectorAll(`[data-testid^="${prefix}"]`).length,
  462. testId(TID_TASK_ITEM, ""),
  463. );
  464. }
  465. /** 侧栏所有任务项的文本拼接(断言会话标题里出现工作流名)。 */
  466. async function readTaskItemsText(): Promise<string> {
  467. return browser.execute(
  468. (prefix) =>
  469. Array.from(document.querySelectorAll<HTMLElement>(`[data-testid^="${prefix}"]`))
  470. .map((item) => item.textContent?.replace(/ /g, " ") ?? "")
  471. .join("\n"),
  472. testId(TID_TASK_ITEM, ""),
  473. );
  474. }
  475. /** 会话转写(v4 timeline)的全文(断言没有旧的合成 prompt 文案)。 */
  476. async function readTimelineText(): Promise<string> {
  477. return browser.execute((timelineTestId) => {
  478. const element = document.querySelector<HTMLElement>(`[data-testid="${timelineTestId}"]`);
  479. return element?.textContent?.replace(/ /g, " ") ?? "";
  480. }, TID_V4_TIMELINE);
  481. }
  482. describe("直接启动 E2E", () => {
  483. before(async () => {
  484. await mkdir(WORKFLOWS_DIR, { recursive: true });
  485. await writeFile(LAUNCH_OK_FILE, LAUNCH_OK_SEED, "utf-8");
  486. await writeFile(LAUNCH_FAIL_FILE, LAUNCH_FAIL_SEED, "utf-8");
  487. await writeFile(LAUNCH_MODEL_FILE, LAUNCH_MODEL_SEED, "utf-8");
  488. await writeFile(LAUNCH_SLEEP_FILE, LAUNCH_SLEEP_SEED, "utf-8");
  489. await writeFile(LAUNCH_SAVE_FILE, LAUNCH_SAVE_SEED, "utf-8");
  490. await writeFile(LAUNCH_ARTIFACT_FILE, LAUNCH_ARTIFACT_SEED, "utf-8");
  491. });
  492. after(async () => {
  493. await browser.electron.restoreAllMocks();
  494. await rm(WORKFLOWS_DIR, { recursive: true, force: true });
  495. await clearAppData();
  496. });
  497. it("中枢「运行」常量工作流:新会话顶部启动卡跑到 completed,无用户气泡、无确认窗", async function () {
  498. this.timeout(180000);
  499. // 只需要登录 + workspace,就绪即可;常量工作流不碰 provider(见组注释)。
  500. await prepareConversationE2E({ skipProvider: true });
  501. // 1) 自动化页 →「工作流」标签 → 常量工作流的卡片。
  502. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, { timeoutMsg: "侧栏没有出现自动化入口" });
  503. await clickTestIdByDom(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"), {
  504. timeoutMsg: "自动化页标题没有「工作流」切换",
  505. });
  506. const okCardTestId = testId(TID_WORKFLOW_CARD, LAUNCH_OK_NAME);
  507. await waitForTestIdByDom(okCardTestId, {
  508. timeout: 20000,
  509. timeoutMsg: "工作流列表没有出现常量工作流卡片",
  510. });
  511. // 2) 点卡片的「运行」→ 有实参的项目档弹出实参窗:头部 = 名字 + 作用域徽标,且带「新会话」说明。
  512. await clickTestIdByDom(testId(TID_WORKFLOW_CARD_RUN, LAUNCH_OK_NAME), {
  513. timeoutMsg: "常量工作流卡片没有「运行」按钮",
  514. });
  515. await waitForTestIdByDom(TID_WORKFLOW_LAUNCH_DIALOG, {
  516. timeout: 15000,
  517. timeoutMsg: "点「运行」后没有弹出实参窗",
  518. });
  519. const dialogText = (await readTestIdPrefixText(TID_WORKFLOW_LAUNCH_DIALOG)) ?? "";
  520. expect(dialogText).toContain(LAUNCH_OK_NAME); // 名字(mono)
  521. expect(dialogText).toContain("项目"); // 作用域徽标(scope.project)
  522. expect(dialogText).toContain("的新会话中运行"); // 「将立即在 X 的新会话中运行」说明
  523. // 3) 填实参并提交。提交前记下任务数,稍后新会话应比它多一个。
  524. const tasksBeforeLaunch = await countTaskItems();
  525. await setInputValueByTestIdDom(
  526. testId(TID_WORKFLOW_LAUNCH_ARG, LAUNCH_OK_ARG),
  527. LAUNCH_OK_ARG_VALUE,
  528. {
  529. timeoutMsg: "实参窗没有出现 topic 输入框",
  530. },
  531. );
  532. await clickTestIdByDom(TID_WORKFLOW_LAUNCH_SUBMIT, {
  533. timeoutMsg: "实参窗没有「运行」提交按钮",
  534. });
  535. // 4) 应用切到新会话,转写顶部长出 run 卡(不是用户气泡、不是 CreateWorkflow 工具行),
  536. // 卡上带工作流名;实参值不在卡上,而在 run 详情侧板的来龙去脉节里。
  537. const launchCardPrefix = testId(TID_CHAT_WORKFLOW_RUN_DIGEST, "");
  538. await browser.waitUntil(
  539. async () =>
  540. (await readLaunchCardStatus()) !== null ||
  541. Boolean(await readTestIdPrefixText(launchCardPrefix)),
  542. {
  543. timeout: 60000,
  544. timeoutMsg: "新会话顶部没有出现 run 卡",
  545. },
  546. );
  547. const cardText = (await readTestIdPrefixText(launchCardPrefix)) ?? "";
  548. expect(cardText).toContain(LAUNCH_OK_NAME);
  549. expect(cardText).not.toContain(LAUNCH_OK_ARG_VALUE);
  550. // 4b) 卡上的 ⤢ 开 run 详情侧板:来龙去脉节写着「由你从工作流中枢启动」、作用域与刚填的实参值。
  551. await clickTestIdByDom("workflow-card-open-details", {
  552. timeoutMsg: "run 卡上没有「打开运行详情」",
  553. });
  554. await browser.waitUntil(async () => (await readLaunchProvenanceText()) !== null, {
  555. timeout: 30000,
  556. timeoutMsg: "run 详情侧板没有出现来龙去脉节",
  557. });
  558. const provenanceText = (await readLaunchProvenanceText()) ?? "";
  559. expect(provenanceText).toContain(LAUNCH_OK_ARG_VALUE);
  560. // 5) 无用户气泡、无旧的合成 prompt 文案:转写里不出现「Run the saved workflow」这类
  561. // 被本 spec 删除的 composer 文案。
  562. expect(await readTimelineText()).not.toContain("Run the saved workflow");
  563. // 6) 无确认窗:直接启动是记录在案的确认旁路,通用二次确认窗不应出现。
  564. const hasConfirmDialog = await browser.execute(
  565. (confirmTestId) => Boolean(document.querySelector(`[data-testid="${confirmTestId}"]`)),
  566. TID_CONFIRM_DIALOG_CONFIRM,
  567. );
  568. expect(hasConfirmDialog).toBe(false);
  569. // 7) run 卡的状态跑到 completed(常量工作流零 agent,立即结算)。宽超时:
  570. // run-started → run-settled 走真实 dwf 运行时的子进程。
  571. await browser.waitUntil(async () => (await readLaunchCardStatus()) === "completed", {
  572. timeout: 90000,
  573. timeoutMsg: "run 卡的状态没有跑到 completed",
  574. });
  575. // 8) 侧栏会话标题 = 工作流名(titleSource: first_input,不用消息全文、不等 LLM);
  576. // 且确实新开了一个会话。
  577. await browser.waitUntil(async () => (await countTaskItems()) === tasksBeforeLaunch + 1, {
  578. timeout: 20000,
  579. timeoutMsg: "直接启动后没有新增一个会话",
  580. });
  581. expect(await readTaskItemsText()).toContain(LAUNCH_OK_NAME);
  582. });
  583. it("编译失败的工作流:实参窗行内错误显示 compile_failed,会话列表不新增", async function () {
  584. this.timeout(150000);
  585. await prepareConversationE2E({ skipProvider: true });
  586. // 1) 自动化页 →「工作流」标签 → 编译失败工作流的卡片(frontmatter 合法,卡片照常出现)。
  587. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, { timeoutMsg: "侧栏没有出现自动化入口" });
  588. await clickTestIdByDom(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"), {
  589. timeoutMsg: "自动化页标题没有「工作流」切换",
  590. });
  591. const failCardTestId = testId(TID_WORKFLOW_CARD, LAUNCH_FAIL_NAME);
  592. await waitForTestIdByDom(failCardTestId, {
  593. timeout: 20000,
  594. timeoutMsg: "工作流列表没有出现编译失败工作流卡片",
  595. });
  596. // 2) 「运行」→ 实参窗 → 填实参并提交。记下提交前的任务数。
  597. await clickTestIdByDom(testId(TID_WORKFLOW_CARD_RUN, LAUNCH_FAIL_NAME), {
  598. timeoutMsg: "编译失败工作流卡片没有「运行」按钮",
  599. });
  600. await waitForTestIdByDom(TID_WORKFLOW_LAUNCH_DIALOG, {
  601. timeout: 15000,
  602. timeoutMsg: "点「运行」后没有弹出实参窗",
  603. });
  604. const tasksBeforeReject = await countTaskItems();
  605. await setInputValueByTestIdDom(testId(TID_WORKFLOW_LAUNCH_ARG, LAUNCH_OK_ARG), "x", {
  606. timeoutMsg: "实参窗没有出现 topic 输入框",
  607. });
  608. await clickTestIdByDom(TID_WORKFLOW_LAUNCH_SUBMIT, {
  609. timeoutMsg: "实参窗没有「运行」提交按钮",
  610. });
  611. // 3) 行内错误区出现,标题是 compile_failed 的文案;实参窗留着(不导航、不留会话)。
  612. await waitForTestIdByDom(TID_WORKFLOW_LAUNCH_ERROR, {
  613. timeout: 30000,
  614. timeoutMsg: "编译失败没有在实参窗行内错误区显示",
  615. });
  616. const errorText = (await readTestIdPrefixText(TID_WORKFLOW_LAUNCH_ERROR)) ?? "";
  617. expect(errorText).toContain("工作流脚本编译失败");
  618. // 实参窗仍在(失败不关窗)。
  619. await waitForTestIdByDom(TID_WORKFLOW_LAUNCH_DIALOG, {
  620. timeoutMsg: "编译失败后实参窗被意外关闭",
  621. });
  622. // 4) 会话列表没有新增:agent 侧 ① ② 阶段失败前不建任何行,GUI 侧随即 deleteSession
  623. // 收回刚建的空会话(不变式 2)。
  624. await browser.waitUntil(async () => (await countTaskItems()) === tasksBeforeReject, {
  625. timeout: 15000,
  626. timeoutMsg: "编译失败却新增了一个会话",
  627. });
  628. });
  629. it("脚本点名的模型解不出来:实参窗行内显示 compile_failed 与那个名字的 9011,会话列表不新增", async function () {
  630. this.timeout(150000);
  631. await prepareConversationE2E({ skipProvider: true });
  632. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, { timeoutMsg: "侧栏没有出现自动化入口" });
  633. await clickTestIdByDom(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"), {
  634. timeoutMsg: "自动化页标题没有「工作流」切换",
  635. });
  636. await waitForTestIdByDom(testId(TID_WORKFLOW_CARD, LAUNCH_MODEL_NAME), {
  637. timeout: 20000,
  638. timeoutMsg: "工作流列表没有出现点名未知模型的工作流卡片",
  639. });
  640. await clickTestIdByDom(testId(TID_WORKFLOW_CARD_RUN, LAUNCH_MODEL_NAME), {
  641. timeoutMsg: "点名未知模型的工作流卡片没有「运行」按钮",
  642. });
  643. await waitForTestIdByDom(TID_WORKFLOW_LAUNCH_DIALOG, {
  644. timeout: 15000,
  645. timeoutMsg: "点「运行」后没有弹出实参窗",
  646. });
  647. const tasksBefore = await countTaskItems();
  648. await setInputValueByTestIdDom(testId(TID_WORKFLOW_LAUNCH_ARG, LAUNCH_OK_ARG), "x", {
  649. timeoutMsg: "实参窗没有出现 topic 输入框",
  650. });
  651. await clickTestIdByDom(TID_WORKFLOW_LAUNCH_SUBMIT, {
  652. timeoutMsg: "实参窗没有「运行」提交按钮",
  653. });
  654. // 与脚本编不过同一个出口:标题是 compile_failed 的文案,细节是一条带位置的诊断。
  655. // 宿主有目录时细节点出名字;没有目录时说「这台宿主选不了模型」——两种都是脚本侧的改法。
  656. await waitForTestIdByDom(TID_WORKFLOW_LAUNCH_ERROR, {
  657. timeout: 30000,
  658. timeoutMsg: "解不出来的模型没有在实参窗行内错误区显示",
  659. });
  660. const errorText = (await readTestIdPrefixText(TID_WORKFLOW_LAUNCH_ERROR)) ?? "";
  661. expect(errorText).toContain("工作流脚本编译失败");
  662. expect(errorText).toMatch(/L\d+:C\d+/);
  663. expect(
  664. errorText.includes(LAUNCH_MODEL_UNKNOWN) || errorText.includes("cannot choose models"),
  665. ).toBe(true);
  666. await waitForTestIdByDom(TID_WORKFLOW_LAUNCH_DIALOG, {
  667. timeoutMsg: "解不出来的模型让实参窗被意外关闭",
  668. });
  669. await browser.waitUntil(async () => (await countTaskItems()) === tasksBefore, {
  670. timeout: 15000,
  671. timeoutMsg: "解不出来的模型却新增了一个会话",
  672. });
  673. });
  674. // composer 徽标直达(docs/dynamic-workflow/presentation.md「Other places a run appears」):
  675. // 会话里唯一在跑的是一条工作流时,点徽标不展开状态胶囊,直接打开该 run 的详情 side tab。
  676. it("唯一在跑的工作流:composer 徽标直达详情 side tab,状态胶囊不展开", async function () {
  677. this.timeout(180000);
  678. await prepareConversationE2E({ skipProvider: true });
  679. // 1) 自动化页 →「工作流」标签 → 无实参的项目档点「运行」即跑(不弹实参窗、不弹确认窗)。
  680. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, { timeoutMsg: "侧栏没有出现自动化入口" });
  681. await clickTestIdByDom(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"), {
  682. timeoutMsg: "自动化页标题没有「工作流」切换",
  683. });
  684. await waitForTestIdByDom(testId(TID_WORKFLOW_CARD, LAUNCH_SLEEP_NAME), {
  685. timeout: 20000,
  686. timeoutMsg: "工作流列表没有出现停留 running 的常量工作流卡片",
  687. });
  688. await clickTestIdByDom(testId(TID_WORKFLOW_CARD_RUN, LAUNCH_SLEEP_NAME), {
  689. timeoutMsg: "常量工作流卡片没有「运行」按钮",
  690. });
  691. // 2) 新会话顶部长出 run 卡,run 进入 running(world.run 的 sleep 让它停在这里)。
  692. const launchCardPrefix = testId(TID_CHAT_WORKFLOW_RUN_DIGEST, "");
  693. await browser.waitUntil(async () => Boolean(await readTestIdPrefixText(launchCardPrefix)), {
  694. timeout: 60000,
  695. timeoutMsg: "新会话顶部没有出现 run 卡",
  696. });
  697. // 2b) 侧栏运行行(docs/dynamic-workflow/presentation.md「The sidebar run line」):发起它的会话行在标题下
  698. // 长出一条 running 的运行行;sleep 脚本没有 phase() 标记,所以轨道是一个隐含站点。
  699. await browser.waitUntil(
  700. async () =>
  701. browser.execute((prefix) => {
  702. const rows = Array.from(
  703. document.querySelectorAll<HTMLElement>(`[data-testid^="${prefix}"]`),
  704. );
  705. return rows.some((row) => {
  706. const line = row.querySelector<HTMLElement>(
  707. '[data-workflow-run-line][data-run-status="running"]',
  708. );
  709. return (
  710. line !== null &&
  711. line.querySelector('[data-workflow-run-rail][data-implicit="true"]') !== null
  712. );
  713. });
  714. }, testId(TID_TASK_ITEM, "")),
  715. { timeout: 60000, timeoutMsg: "侧栏里发起工作流的会话行没有长出 running 的运行行" },
  716. );
  717. // 2c) 状态胶囊(docs/dynamic-workflow/presentation.md「Other places a run appears」):在跑的工作流
  718. // 排在胶囊最前,按名字显示(名字 · 灯 · 词),不再是「1 后台」这样的计数。
  719. await browser.waitUntil(
  720. async () =>
  721. browser.execute((panelTestId) => {
  722. const panel = document.querySelector<HTMLElement>(`[data-testid="${panelTestId}"]`);
  723. const pill = panel?.querySelector<HTMLElement>("[data-workflow-pill]");
  724. const button = pill?.closest("button");
  725. return (
  726. pill !== null &&
  727. pill !== undefined &&
  728. (pill.textContent ?? "").trim().length > 0 &&
  729. button?.querySelector('[data-workflow-run-lamp="running"]') !== null
  730. );
  731. }, TID_CHAT_SUMMARY_PANEL),
  732. { timeout: 30000, timeoutMsg: "状态胶囊没有按名字显示在跑的工作流" },
  733. );
  734. // 3) composer 徽标:恰好一条 workflow、没有终端与子代理,且自述落点是 workflow-run。
  735. await browser.waitUntil(
  736. async () => {
  737. const counts = await getV4ComposerBackgroundWorkCounts();
  738. return (
  739. counts?.workflowCount === 1 &&
  740. counts.bashCount === 0 &&
  741. counts.subagentCount === 0 &&
  742. counts.totalCount === 1 &&
  743. counts.openTarget === "workflow-run"
  744. );
  745. },
  746. { timeout: 60000, timeoutMsg: "composer 徽标没有以 workflow-run 落点显示唯一在跑的工作流" },
  747. );
  748. const panelModeBefore = await browser.execute(
  749. (panelTestId) =>
  750. document.querySelector<HTMLElement>(`[data-testid="${panelTestId}"]`)?.dataset
  751. .displayMode ?? null,
  752. TID_CHAT_SUMMARY_PANEL,
  753. );
  754. expect(panelModeBefore).not.toBe("panel");
  755. // 4) 点徽标 → 侧板出现该 run 的 workflow-run tab(结构化 id 前缀),胶囊没有被置成 panel。
  756. await clickTestIdByDom(TID_V4_COMPOSER_BACKGROUND_WORK_TRIGGER, {
  757. timeoutMsg: "composer 后台任务入口不可点击",
  758. });
  759. await browser.waitUntil(
  760. async () =>
  761. browser.execute(() =>
  762. Boolean(document.querySelector('[data-side-pane-tab-id^="workflow-run:"]')),
  763. ),
  764. { timeout: 30000, timeoutMsg: "点徽标后侧板没有打开 workflow-run tab" },
  765. );
  766. const panelModeAfter = await browser.execute(
  767. (panelTestId) =>
  768. document.querySelector<HTMLElement>(`[data-testid="${panelTestId}"]`)?.dataset
  769. .displayMode ?? null,
  770. TID_CHAT_SUMMARY_PANEL,
  771. );
  772. expect(panelModeAfter).not.toBe("panel");
  773. // 5) 点胶囊展开:「工作流」分区随之打开,不用再点分区头——展开后看到的不能比胶囊还少
  774. // (docs/dynamic-workflow/presentation.md「Other places a run appears」)。run 行两行定高:第一行
  775. // 名字 + 时长,第二行灯 + 词(sleep 脚本没有 phase() 标记,所以是一盏运行灯 + 步数)。悬停这一行
  776. // 时 Stop 叠在时长上出现。
  777. await browser.execute((panelTestId) => {
  778. const panel = document.querySelector<HTMLElement>(`[data-testid="${panelTestId}"]`);
  779. panel?.querySelector<HTMLElement>("[data-workflow-pill]")?.closest("button")?.click();
  780. }, TID_CHAT_SUMMARY_PANEL);
  781. const rowSelector = 'li[data-background-task-kind="workflow"]';
  782. await browser.waitUntil(
  783. async () =>
  784. browser.execute((selector) => {
  785. const row = document.querySelector<HTMLElement>(selector);
  786. return (
  787. row?.querySelector('[data-workflow-island-line="name"] [data-workflow-run-elapsed]') !==
  788. null &&
  789. row?.querySelector(
  790. '[data-workflow-island-line="phase"] [data-workflow-run-lamp="running"]',
  791. ) !== null
  792. );
  793. }, rowSelector),
  794. {
  795. timeout: 15000,
  796. timeoutMsg: "点开胶囊后「工作流」分区没有直接显示 run 行(名字 + 时长 / 灯 + 词两行)",
  797. },
  798. );
  799. await $(rowSelector).moveTo();
  800. await browser.waitUntil(
  801. async () =>
  802. browser.execute((selector) => {
  803. const stop = document.querySelector<HTMLElement>(
  804. `${selector} [data-testid^="v4-background-work-cancel-"]`,
  805. );
  806. return stop !== null && getComputedStyle(stop).opacity === "1";
  807. }, rowSelector),
  808. { timeout: 5000, timeoutMsg: "悬停 run 行时 Stop 没有出现" },
  809. );
  810. });
  811. // 并发芯片(docs/dynamic-workflow/concurrency.md「What the user sees」):芯片的数是一条界,字是
  812. // 「最大并发数」;它与 Configure 同进同退——run 在跑时就地调小界,芯片出现;run 完成后芯片离开,
  813. // 不再把一条已经没有东西在它下面跑的界挂成实时读数。
  814. it("并发芯片:在跑的 run 就地调小界后显示「最大并发数 N」,run 完成后芯片离开", async function () {
  815. this.timeout(180000);
  816. await prepareConversationE2E({ skipProvider: true });
  817. // 1) 跑那条停留 running 的常量工作流(world.run sleep 40s),打开它的 run 侧板。
  818. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, { timeoutMsg: "侧栏没有出现自动化入口" });
  819. await clickTestIdByDom(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"), {
  820. timeoutMsg: "自动化页标题没有「工作流」切换",
  821. });
  822. await waitForTestIdByDom(testId(TID_WORKFLOW_CARD, LAUNCH_SLEEP_NAME), {
  823. timeout: 20000,
  824. timeoutMsg: "工作流列表没有出现停留 running 的常量工作流卡片",
  825. });
  826. await clickTestIdByDom(testId(TID_WORKFLOW_CARD_RUN, LAUNCH_SLEEP_NAME), {
  827. timeoutMsg: "常量工作流卡片没有「运行」按钮",
  828. });
  829. await browser.waitUntil(async () => (await readLaunchCardStatus()) === "running", {
  830. timeout: 60000,
  831. timeoutMsg: "run 卡没有进入 running",
  832. });
  833. await clickTestIdByDom("workflow-card-open-details", {
  834. timeoutMsg: "run 卡上没有「打开运行详情」",
  835. });
  836. await waitForTestIdByDom("workflow-run-configure", {
  837. timeout: 30000,
  838. timeoutMsg: "在跑的 run 侧板上没有 Configure",
  839. });
  840. // 2) 跑在默认并发 D 上(没有自己的界):没有芯片。
  841. expect(await readTestIdText("workflow-run-concurrency")).toBeNull();
  842. // 3) Configure → 步进器减一 → 应用:只改界、run 在跑,就地生效(不另起 run)。
  843. await clickTestIdByDom("workflow-run-configure", { timeoutMsg: "Configure 不可点击" });
  844. await waitForTestIdByDom("workflow-run-settings-popover", {
  845. timeoutMsg: "点 Configure 后没有设置弹层",
  846. });
  847. // 步进器停在默认并发 D = max(4, min(16, 核数 − 2)) 上,减一总有可调小的余地。
  848. const defaultBound = Number(await readTestIdText("workflow-run-settings-bound-value"));
  849. // 还没改:没有后果句(改动之前念哪一句都可能说错)。
  850. expect(await readTestIdText("workflow-run-settings-consequence")).toBe("");
  851. await clickTestIdByDom("workflow-run-settings-bound-decrease", {
  852. timeoutMsg: "设置弹层没有「降低最大并发数」",
  853. });
  854. // 只改界、run 在跑:后果句是「就地生效」,不是「将停止当前运行」。
  855. expect(await readTestIdText("workflow-run-settings-consequence")).toMatch(
  856. /^(立即应用到当前运行,不会新起一次运行。|Applies to this run right away; no new run is started\.)$/,
  857. );
  858. await clickTestIdByDom("workflow-run-settings-apply", { timeoutMsg: "设置弹层没有「应用」" });
  859. // 4) 芯片出现,字是界的字,数是调小后的界。
  860. await browser.waitUntil(
  861. async () => {
  862. const text = await readTestIdText("workflow-run-concurrency");
  863. return (
  864. text !== null &&
  865. new RegExp(`^(最大并发数|Max concurrency) ${defaultBound - 1}\\b`).test(text)
  866. );
  867. },
  868. { timeout: 30000, timeoutMsg: "就地调小界后 run 侧板没有出现「最大并发数 N」芯片" },
  869. );
  870. // 5) run 跑完(sleep 结束):Configure 与芯片一起离开。
  871. await browser.waitUntil(async () => (await readLaunchCardStatus()) === "completed", {
  872. timeout: 90000,
  873. timeoutMsg: "run 卡的状态没有跑到 completed",
  874. });
  875. await browser.waitUntil(
  876. async () =>
  877. (await readTestIdText("workflow-run-concurrency")) === null &&
  878. (await readTestIdText("workflow-run-configure")) === null,
  879. { timeout: 20000, timeoutMsg: "run 完成后侧板上仍有并发芯片或 Configure" },
  880. );
  881. });
  882. it("完成的 run:侧板有「已保存」与「再次运行」,芯片直达中枢;删掉后「保存」按原样写回同一份脚本", async function () {
  883. this.timeout(240000);
  884. await prepareConversationE2E({ skipProvider: true });
  885. // 1) 中枢「运行」无实参的项目档:点即跑,新会话顶部的 run 卡跑到 completed。
  886. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, { timeoutMsg: "侧栏没有出现自动化入口" });
  887. await clickTestIdByDom(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"), {
  888. timeoutMsg: "自动化页标题没有「工作流」切换",
  889. });
  890. await waitForTestIdByDom(testId(TID_WORKFLOW_CARD, LAUNCH_SAVE_NAME), {
  891. timeout: 20000,
  892. timeoutMsg: "工作流列表没有出现保存用例的工作流卡片",
  893. });
  894. await clickTestIdByDom(testId(TID_WORKFLOW_CARD_RUN, LAUNCH_SAVE_NAME), {
  895. timeoutMsg: "保存用例的工作流卡片没有「运行」按钮",
  896. });
  897. await browser.waitUntil(async () => (await readLaunchCardStatus()) === "completed", {
  898. timeout: 90000,
  899. timeoutMsg: "run 卡的状态没有跑到 completed",
  900. });
  901. // 2) ⤢ 打开 run 侧板:从中枢启动的 run 按名字规则天然「已保存」——芯片在状态行,动词是「再次运行」。
  902. await clickTestIdByDom("workflow-card-open-details", {
  903. timeoutMsg: "run 卡上没有「打开运行详情」",
  904. });
  905. await waitForTestIdByDom("workflow-saved-chip", {
  906. timeout: 30000,
  907. timeoutMsg: "run 侧板状态行没有出现「已保存」芯片",
  908. });
  909. await waitForTestIdByDom("workflow-save-run-again", {
  910. timeout: 10000,
  911. timeoutMsg: "run 侧板没有「再次运行」",
  912. });
  913. // 3) 芯片直达中枢里那个工作流的详情页。
  914. await clickTestIdByDom("workflow-saved-chip", { timeoutMsg: "「已保存」芯片不可点击" });
  915. await waitForTestIdByDom(TID_WORKFLOW_DETAIL, {
  916. timeout: 20000,
  917. timeoutMsg: "点芯片后没有进入工作流详情页",
  918. });
  919. expect((await readTestIdText(TID_WORKFLOW_DETAIL)) ?? "").toContain(LAUNCH_SAVE_NAME);
  920. // 4) 在中枢把它删掉:中枢刷新会作废卡片的读缓存,这次 run 不再「已保存」。
  921. await clickTestIdByWebDriver(TID_WORKFLOW_DETAIL_MENU, {
  922. timeoutMsg: "详情页没有「更多操作」菜单",
  923. });
  924. await clickTestIdByDom(testId(TID_WORKFLOW_ACTION_DELETE, LAUNCH_SAVE_NAME), {
  925. timeoutMsg: "详情页菜单没有「删除工作流」项",
  926. });
  927. await clickTestIdByDom(TID_CONFIRM_DIALOG_CONFIRM, {
  928. timeoutMsg: "删除二次确认弹窗没有出现",
  929. });
  930. await browser.waitUntil(async () => !(await fileExists(LAUNCH_SAVE_FILE)), {
  931. timeout: 10000,
  932. timeoutMsg: "删除后磁盘文件仍然存在",
  933. });
  934. // 5) 回到发起它的会话,侧板上的动词翻回「保存」。
  935. const sessionTaskTestId = await browser.execute(
  936. (prefix, name) =>
  937. Array.from(document.querySelectorAll<HTMLElement>(`[data-testid^="${prefix}"]`)).find(
  938. (item) => item.textContent?.includes(name),
  939. )?.dataset.testid ?? null,
  940. testId(TID_TASK_ITEM, ""),
  941. LAUNCH_SAVE_NAME,
  942. );
  943. expect(sessionTaskTestId).not.toBeNull();
  944. await clickTestIdByDom(sessionTaskTestId!, { timeoutMsg: "侧栏里点不到发起它的会话" });
  945. await clickTestIdByDom("workflow-card-open-details", {
  946. timeout: 20000,
  947. timeoutMsg: "回到会话后 run 卡上没有「打开运行详情」",
  948. });
  949. await waitForTestIdByDom("workflow-save-open", {
  950. timeout: 30000,
  951. timeoutMsg: "删掉工作流之后 run 侧板没有翻回「保存」",
  952. });
  953. // 6) 「保存」→ 弹层 → 按原样直接保存:不经模型回合,写下的是这次 run 实际执行的那一份脚本。
  954. await clickTestIdByDom("workflow-save-open", { timeoutMsg: "「保存」不可点击" });
  955. await waitForTestIdByDom("workflow-save-popover", { timeoutMsg: "点「保存」后没有弹层" });
  956. await setInputValueByTestIdDom("workflow-save-name", LAUNCH_SAVE_NAME, {
  957. timeoutMsg: "保存弹层没有名称输入框",
  958. });
  959. await clickTestIdByDom("workflow-save-submit", { timeoutMsg: "保存弹层没有「直接保存」" });
  960. await browser.waitUntil(async () => fileExists(LAUNCH_SAVE_FILE), {
  961. timeout: 20000,
  962. timeoutMsg: "直接保存后磁盘上没有出现工作流文件",
  963. });
  964. const written = await readFile(LAUNCH_SAVE_FILE, "utf-8");
  965. expect(written.startsWith("/* zcode-workflow")).toBe(true);
  966. expect(written.endsWith(`*/\n${LAUNCH_SAVE_SCRIPT}`)).toBe(true);
  967. // 7) 卡片立刻翻面:芯片回来,动词又是「再次运行」。
  968. await waitForTestIdByDom("workflow-saved-chip", {
  969. timeout: 20000,
  970. timeoutMsg: "直接保存后 run 侧板没有出现「已保存」芯片",
  971. });
  972. await waitForTestIdByDom("workflow-save-run-again", {
  973. timeout: 10000,
  974. timeoutMsg: "直接保存后 run 侧板没有翻成「再次运行」",
  975. });
  976. });
  977. it("产物 tab「作为文件打开」:把这一版的 markdown 落成本机副本,交给系统默认 App", async function () {
  978. this.timeout(240000);
  979. await prepareConversationE2E({ skipProvider: true });
  980. // 1) 中枢「运行」无实参的项目档:点即跑,run 发布一份 markdown 产物后 completed。
  981. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, { timeoutMsg: "侧栏没有出现自动化入口" });
  982. await clickTestIdByDom(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"), {
  983. timeoutMsg: "自动化页标题没有「工作流」切换",
  984. });
  985. await waitForTestIdByDom(testId(TID_WORKFLOW_CARD, LAUNCH_ARTIFACT_NAME), {
  986. timeout: 20000,
  987. timeoutMsg: "工作流列表没有出现产物用例的工作流卡片",
  988. });
  989. await clickTestIdByDom(testId(TID_WORKFLOW_CARD_RUN, LAUNCH_ARTIFACT_NAME), {
  990. timeoutMsg: "产物用例的工作流卡片没有「运行」按钮",
  991. });
  992. await browser.waitUntil(async () => (await readLaunchCardStatus()) === "completed", {
  993. timeout: 90000,
  994. timeoutMsg: "run 卡的状态没有跑到 completed",
  995. });
  996. // 2) ⤢ 打开 run 侧板 → 唯一的产物即主产物行 → 产物 tab,正文是 markdown。
  997. await clickTestIdByDom("workflow-card-open-details", {
  998. timeoutMsg: "run 卡上没有「打开运行详情」",
  999. });
  1000. await clickTestIdByDom(TID_WORKFLOW_ARTIFACT_CARD, {
  1001. timeout: 30000,
  1002. timeoutMsg: "run 侧板没有出现产物行",
  1003. });
  1004. await waitForTestIdByDom(TID_WORKFLOW_ARTIFACT_PANE, {
  1005. timeout: 20000,
  1006. timeoutMsg: "点产物行后没有打开产物 tab",
  1007. });
  1008. await browser.waitUntil(
  1009. async () =>
  1010. browser.execute(() => Boolean(document.querySelector('[data-artifact-body="markdown"]'))),
  1011. { timeout: 20000, timeoutMsg: "产物 tab 没有画出 markdown 正文" },
  1012. );
  1013. // 3) 点「作为文件打开」:main 落副本,再经 shell.openPath 交给系统默认 App(这里被 mock 接住)。
  1014. const openPath = await browser.electron.mock("shell", "openPath");
  1015. await openPath.mockResolvedValue("");
  1016. try {
  1017. const readOpenedPaths = async (): Promise<string[]> => {
  1018. await openPath.update();
  1019. return openPath.mock.calls.map(([path]) => String(path));
  1020. };
  1021. await clickTestIdByDom("workflow-artifact-open-as-file", {
  1022. timeoutMsg: "产物 tab 头部没有「作为文件打开」",
  1023. });
  1024. await browser.waitUntil(async () => (await readOpenedPaths()).length === 1, {
  1025. timeout: 15000,
  1026. timeoutMsg: "「作为文件打开」没有经 shell.openPath 打开文件",
  1027. });
  1028. const [openedPath] = await readOpenedPaths();
  1029. expect(openedPath).toContain(join("tmp", "workflow-artifacts"));
  1030. expect(openedPath!.endsWith(join("v1", `${LAUNCH_ARTIFACT_ID}.md`))).toBe(true);
  1031. expect(await readFile(openedPath!, "utf-8")).toBe(LAUNCH_ARTIFACT_MARKDOWN);
  1032. // 4) 再点一次:同一版开的是同一个副本。
  1033. await clickTestIdByDom("workflow-artifact-open-as-file", {
  1034. timeoutMsg: "产物 tab 头部没有「作为文件打开」",
  1035. });
  1036. await browser.waitUntil(async () => (await readOpenedPaths()).length === 2, {
  1037. timeout: 15000,
  1038. timeoutMsg: "第二次「作为文件打开」没有打开文件",
  1039. });
  1040. expect((await readOpenedPaths())[1]).toBe(openedPath);
  1041. } finally {
  1042. await browser.electron.restoreAllMocks();
  1043. }
  1044. });
  1045. });