automations-workflows-hub.test.ts 63 KB

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