conversation-session-data-sync-migration-entry.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_SETTINGS_PAGE,
  5. TID_SETTINGS_SECTION_NAV,
  6. TID_TASK_SETTINGS_BUTTON,
  7. testId,
  8. } from "@zcode/shared/test-ids";
  9. import {
  10. clearAppData,
  11. clickTestIdByDom,
  12. getE2EAppDataPaths,
  13. pinE2ELocale,
  14. waitForTestIdByDom,
  15. } from "#e2e/helpers/desktop-app";
  16. import { dismissOccupationOnboarding } from "#e2e/helpers/occupation-onboarding";
  17. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  18. // DSYNC-16:迁移入口移到数据同步页(docs/remote/data-sync.md「迁移入口」)。
  19. // 只用本机桌面,不需要远端设备。
  20. const CASE_MARKER = "E2E_DSYNC_16_MIGRATION_ENTRY";
  21. async function readMigrationDialog() {
  22. return await browser.execute(() => {
  23. const dialog = document.querySelector<HTMLElement>(
  24. '[data-testid="onboarding-migration-dialog"]',
  25. );
  26. const footerButtons = dialog
  27. ? [...dialog.querySelectorAll<HTMLButtonElement>("button")]
  28. .map((button) => button.textContent?.trim() ?? "")
  29. .filter((text) => text.length > 0)
  30. : [];
  31. return {
  32. open: Boolean(dialog),
  33. step: dialog?.dataset.wizardStep ?? null,
  34. text: dialog?.textContent ?? "",
  35. buttons: footerButtons,
  36. hasCancel: Boolean(dialog?.querySelector('[data-testid="onboarding-wizard-cancel"]')),
  37. };
  38. });
  39. }
  40. const SKILL_NAME = "e2e-dsync16-skill";
  41. async function readWorkspaceTabState() {
  42. return await browser.execute(() => {
  43. const store = (
  44. window as Window & {
  45. __zcodeTabStoreE2E?: {
  46. getState(): {
  47. tabs: Array<{ id: string; kind: string }>;
  48. activeWorkspacePath: string | null;
  49. };
  50. };
  51. }
  52. ).__zcodeTabStoreE2E;
  53. const state = store?.getState();
  54. return {
  55. workspaceTabCount: state?.tabs.filter((tab) => tab.kind === "workspace").length ?? -1,
  56. activeWorkspacePath: state?.activeWorkspacePath ?? null,
  57. };
  58. });
  59. }
  60. /** 关闭当前窗口全部 workspace tab(与用户逐个关闭相同,走 tab store 的 closeTab)。 */
  61. async function closeAllWorkspaceTabs() {
  62. await browser.execute(() => {
  63. const store = (
  64. window as Window & {
  65. __zcodeTabStoreE2E?: {
  66. getState(): {
  67. tabs: Array<{ id: string; kind: string }>;
  68. closeTab(id: string): void;
  69. };
  70. };
  71. }
  72. ).__zcodeTabStoreE2E;
  73. if (!store) throw new Error("tab store 未暴露");
  74. for (const tab of store.getState().tabs.filter((item) => item.kind === "workspace")) {
  75. store.getState().closeTab(tab.id);
  76. }
  77. });
  78. }
  79. async function clickDialogButton(label: string) {
  80. await browser.waitUntil(
  81. async () =>
  82. await browser.execute((text) => {
  83. const dialog = document.querySelector('[data-testid="onboarding-migration-dialog"]');
  84. const button = [...(dialog?.querySelectorAll<HTMLButtonElement>("button") ?? [])].find(
  85. (item) => item.textContent?.trim() === text && !item.disabled,
  86. );
  87. button?.click();
  88. return Boolean(button);
  89. }, label),
  90. { timeout: 30_000, timeoutMsg: `${CASE_MARKER}: 迁移向导没有可点击的「${label}」` },
  91. );
  92. }
  93. async function waitForWizardStep(step: string) {
  94. await browser.waitUntil(async () => (await readMigrationDialog()).step === step, {
  95. timeout: 30_000,
  96. timeoutMsg: `${CASE_MARKER}: 迁移向导没有进入 ${step} 步骤`,
  97. });
  98. }
  99. describe(`${CASE_MARKER}: 迁移入口移到数据同步页`, () => {
  100. after(async () => {
  101. const { homeDir } = getE2EAppDataPaths();
  102. await rm(join(homeDir, ".claude"), { recursive: true, force: true });
  103. await clearAppData();
  104. });
  105. it("DSYNC-16-01 侧栏没有 Onboarding 入口;数据同步页只有「迁移」,直接进入「会话」步骤,「取消」关闭弹窗;「通用」页「打开引导」仍打开职业引导", async function () {
  106. this.timeout(5 * 60_000);
  107. await prepareV4ConversationE2E({ skipProvider: true });
  108. // 首帧跟随宿主语言,mac runner 上是英文;本 case 断言中文文案,须固定语言。
  109. await pinE2ELocale("zh-CN");
  110. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 30_000 });
  111. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 30_000 });
  112. // 设置侧栏不再有单独的 Onboarding(引导)入口。
  113. const sidebarLabels = await browser.execute(() =>
  114. [...document.querySelectorAll<HTMLElement>('[data-testid="settings-page"] nav button')].map(
  115. (button) => button.textContent?.trim() ?? "",
  116. ),
  117. );
  118. expect(sidebarLabels.length).toBeGreaterThan(0);
  119. expect(sidebarLabels).not.toContain("引导");
  120. expect(sidebarLabels).not.toContain("Onboard");
  121. // 数据同步页顶部操作行只有「迁移」。
  122. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "dataSync"), {
  123. timeout: 30_000,
  124. timeoutMsg: `${CASE_MARKER}: 设置页没有数据同步分区`,
  125. });
  126. await waitForTestIdByDom("remote-sync-list-actions", {
  127. timeout: 30_000,
  128. timeoutMsg: `${CASE_MARKER}: 数据同步页没有顶部操作行`,
  129. });
  130. const actions = await browser.execute(() =>
  131. [
  132. ...document.querySelectorAll<HTMLButtonElement>(
  133. '[data-testid="remote-sync-list-actions"] button',
  134. ),
  135. ].map((button) => button.textContent?.trim() ?? ""),
  136. );
  137. expect(actions).toEqual(["迁移数据"]);
  138. expect(await $('[data-testid="remote-sync-description"]').getText()).toBe(
  139. "同步本机配置到远端。",
  140. );
  141. expect(await $$('[data-testid="settings-page"] h2').length).toBe(1);
  142. // 规则通过键盘打开,验证图标入口可访问,且没有触发同步或迁移。
  143. const rules = await $('[data-testid="remote-sync-rules-trigger"]');
  144. await rules.click();
  145. await waitForTestIdByDom("remote-sync-rules");
  146. // 修复原因:Popover 元素先于文案可读(verify 日志:元素已存在但 getText 为空串)。
  147. // 等规则文案真正渲染出来再断言,而不是元素一存在就读一次。
  148. await browser.waitUntil(
  149. async () =>
  150. (
  151. await $('[data-testid="remote-sync-rules"]')
  152. .getText()
  153. .catch(() => "")
  154. ).includes("仅新增或更新,保留远端独有内容。"),
  155. {
  156. timeout: 5000,
  157. timeoutMsg: `${CASE_MARKER}: 规则弹层没有显示「仅新增或更新,保留远端独有内容。」`,
  158. },
  159. );
  160. await browser.keys("Escape");
  161. await $('[data-testid="remote-sync-rules"]').waitForExist({ reverse: true });
  162. await browser.keys("Enter");
  163. await waitForTestIdByDom("remote-sync-rules");
  164. expect((await readMigrationDialog()).open).toBe(false);
  165. expect(await $('[data-testid="remote-sync-dialog"]').isExisting()).toBe(false);
  166. await browser.keys("Escape");
  167. // 「迁移」直接进入向导第一步「会话」,没有欢迎页;第一步左下角是「取消」。
  168. await clickTestIdByDom("remote-sync-migrate");
  169. await waitForTestIdByDom("onboarding-migration-dialog", {
  170. timeout: 30_000,
  171. timeoutMsg: `${CASE_MARKER}: 点「迁移」后没有打开迁移向导`,
  172. });
  173. const dialog = await readMigrationDialog();
  174. expect(dialog.step).toBe("session");
  175. expect(dialog.text).toContain("迁移向导");
  176. expect(dialog.text).not.toContain("欢迎使用 ZCode");
  177. expect(dialog.hasCancel).toBe(true);
  178. expect(dialog.buttons).toContain("取消");
  179. expect(dialog.buttons).not.toContain("上一步");
  180. await clickTestIdByDom("onboarding-wizard-cancel");
  181. await browser.waitUntil(async () => !(await readMigrationDialog()).open, {
  182. timeout: 15_000,
  183. timeoutMsg: `${CASE_MARKER}: 第一步「取消」没有关闭迁移向导`,
  184. });
  185. await waitForTestIdByDom("remote-sync-section", {
  186. timeout: 15_000,
  187. timeoutMsg: `${CASE_MARKER}: 关闭迁移向导后不在数据同步页`,
  188. });
  189. // 「通用」页「打开引导」不变,仍打开职业引导。
  190. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "general"), { timeout: 30_000 });
  191. const openOnboarding = await $('//button[normalize-space(.)="打开引导"]');
  192. await openOnboarding.waitForExist({ timeout: 30_000 });
  193. await openOnboarding.click();
  194. await $('[data-testid="onboarding-page"]').waitForExist({
  195. timeout: 30_000,
  196. timeoutMsg: `${CASE_MARKER}: 「打开引导」没有打开职业引导`,
  197. });
  198. expect((await readMigrationDialog()).open).toBe(false);
  199. await dismissOccupationOnboarding();
  200. await $('[data-testid="onboarding-page"]').waitForExist({ reverse: true, timeout: 15_000 });
  201. });
  202. it("DSYNC-16-02 没有打开的项目时从「迁移」导入用户级技能:完成页显示已导入,文件写入本机 ~/.zcode/skills", async function () {
  203. this.timeout(5 * 60_000);
  204. const { homeDir } = getE2EAppDataPaths();
  205. const sourceDir = join(homeDir, ".claude", "skills", SKILL_NAME);
  206. await mkdir(sourceDir, { recursive: true });
  207. await writeFile(
  208. join(sourceDir, "SKILL.md"),
  209. `---\nname: ${SKILL_NAME}\ndescription: DSYNC-16-02 migration\n---\nBody\n`,
  210. );
  211. await prepareV4ConversationE2E({ skipProvider: true });
  212. // 首帧跟随宿主语言,mac runner 上是英文;本 case 断言中文文案,须固定语言。
  213. await pinE2ELocale("zh-CN");
  214. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 30_000 });
  215. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 30_000 });
  216. // 决定 24:数据同步页不要求项目。关闭全部 workspace tab,制造「没有打开的项目」。
  217. await closeAllWorkspaceTabs();
  218. await browser.waitUntil(
  219. async () => {
  220. const state = await readWorkspaceTabState();
  221. return state.workspaceTabCount === 0 && state.activeWorkspacePath === null;
  222. },
  223. { timeout: 15_000, timeoutMsg: `${CASE_MARKER}: 关闭 workspace tab 后仍有活动项目` },
  224. );
  225. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "dataSync"), { timeout: 30_000 });
  226. await clickTestIdByDom("remote-sync-migrate", { timeout: 30_000 });
  227. await waitForWizardStep("session");
  228. await clickDialogButton("继续");
  229. await waitForWizardStep("skills-import");
  230. // 技能导入步骤列出 ~/.claude/skills 下的来源,全选后导入到全局(默认目标)。
  231. await browser.waitUntil(
  232. async () => (await readMigrationDialog()).text.includes(join(".claude", "skills")),
  233. { timeout: 30_000, timeoutMsg: `${CASE_MARKER}: 技能导入步骤没有列出 ~/.claude/skills` },
  234. );
  235. await browser.execute(() => {
  236. const dialog = document.querySelector('[data-testid="onboarding-migration-dialog"]');
  237. dialog?.querySelector<HTMLButtonElement>('[role="checkbox"][aria-label="全选"]')?.click();
  238. });
  239. await waitForWizardStep("skills-import");
  240. await clickDialogButton("继续");
  241. await waitForWizardStep("mcp-import");
  242. await clickDialogButton("继续");
  243. await waitForWizardStep("commands-import");
  244. await clickDialogButton("继续");
  245. await waitForWizardStep("agents-file");
  246. await clickDialogButton("开始迁移");
  247. await waitForWizardStep("migration");
  248. await browser.waitUntil(
  249. async () => (await readMigrationDialog()).buttons.includes("开始使用"),
  250. {
  251. timeout: 60_000,
  252. timeoutMsg: `${CASE_MARKER}: 迁移没有完成`,
  253. },
  254. );
  255. // 修复前:没有 workspacePath 时导入被静默跳过,完成页显示完成但设置导入 0 项。
  256. const settingsSummary = await browser.execute(() => {
  257. const dialog = document.querySelector('[data-testid="onboarding-migration-dialog"]');
  258. const card = [...(dialog?.querySelectorAll<HTMLElement>("div.rounded-xl") ?? [])].find(
  259. (item) => item.textContent?.startsWith("设置导入"),
  260. );
  261. const values = [...(card?.querySelectorAll("span") ?? [])].map(
  262. (span) => span.textContent?.trim() ?? "",
  263. );
  264. return {
  265. imported: values[values.indexOf("已导入") + 1] ?? null,
  266. failed: values[values.indexOf("失败") + 1] ?? null,
  267. workspaceRequired: Boolean(
  268. dialog?.querySelector('[data-testid="onboarding-finish-workspace-required"]'),
  269. ),
  270. };
  271. });
  272. expect(settingsSummary).toEqual({ imported: "1", failed: "0", workspaceRequired: false });
  273. expect(
  274. await readFile(join(homeDir, ".zcode", "skills", SKILL_NAME, "SKILL.md"), "utf-8"),
  275. ).toContain("DSYNC-16-02 migration");
  276. // 整个流程中没有项目被重新打开。
  277. expect(await readWorkspaceTabState()).toEqual({
  278. workspaceTabCount: 0,
  279. activeWorkspacePath: null,
  280. });
  281. await clickDialogButton("开始使用");
  282. await browser.waitUntil(async () => !(await readMigrationDialog()).open, {
  283. timeout: 15_000,
  284. timeoutMsg: `${CASE_MARKER}: 完成后迁移向导没有关闭`,
  285. });
  286. });
  287. });