conversation-session-remote-data-sync-subagent-push.test.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. import { mkdir, readFile, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_PROMPT_SUGGESTION_OPTION,
  5. TID_PROMPT_SUGGESTION_PANEL,
  6. TID_SETTINGS_BACK_BUTTON,
  7. TID_SETTINGS_PAGE,
  8. TID_SETTINGS_SECTION_NAV,
  9. TID_TASK_SETTINGS_BUTTON,
  10. TID_V4_COMPOSER_INPUT,
  11. testId,
  12. } from "@zcode/shared/test-ids";
  13. import {
  14. clearAppData,
  15. clickTestIdByDom,
  16. getE2EAppDataPaths,
  17. setInputValueByTestIdDom,
  18. waitForTestIdByDom,
  19. } from "#e2e/helpers/desktop-app";
  20. import {
  21. execInRemoteContainer,
  22. readRemoteContainerFile,
  23. readRemoteDockerContainer,
  24. writeRemoteContainerFile,
  25. } from "#e2e/helpers/remote-docker-target";
  26. import {
  27. connectSSHWorkspace,
  28. prepareSSHConnectionPage,
  29. readSSHRuntimeConfig,
  30. } from "#e2e/helpers/ssh-remote-p0";
  31. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  32. // DSYNC-09:设置 → 数据同步 → 设备 → 推送子智能体(docs/remote/data-sync.md「子智能体」)。
  33. // 子智能体经新同步通道 user-config-sync 按原文件字节推送;远端断言用 docker exec 读容器内 ~/.zcode/agents。
  34. const CASE_MARKER = "E2E_DSYNC_09_SUBAGENT_PUSH";
  35. const AGENT_NAME = "E2E-DSync-Agent";
  36. const AGENT_KEY = AGENT_NAME.toLowerCase();
  37. const AGENT_FILE = "e2e-dsync-agent.md";
  38. const REMOTE_AGENTS = "/home/dev/.zcode/agents";
  39. const DEPENDENCY_SKILL = "e2e-dsync-skill";
  40. const DEPENDENCY_MCP = "e2e-dsync-mcp";
  41. const DEPENDENCY_HINT = `远端缺少 MCP:${DEPENDENCY_MCP};远端缺少技能:${DEPENDENCY_SKILL}`;
  42. // DSYNC-09-02:推送后已打开远程项目的斜杠(/)建议要出现新子智能体(R6)。两个名称都匹配同一个查询。
  43. // 子智能体候选只在 / 面板(SlashCommandPlugin → useSubagents),@ 面板不列子智能体。
  44. const SLASH_AGENT_NAME = "E2E-DSync-Slash";
  45. const SLASH_AGENT_KEY = SLASH_AGENT_NAME.toLowerCase();
  46. const SLASH_AGENT_FILE = "e2e-dsync-slash.md";
  47. const SLASH_QUERY = "/E2E-DSync";
  48. // DSYNC-09-03:内置智能体的模型覆盖(票 20)作为子智能体分类的条目推送。同步不校验远端 Provider,
  49. // 用例只断言行状态与远端状态文件,Provider 不需要在远端存在。
  50. const EXPLORE_KEY = "built-in:built-in:explore";
  51. const GENERAL_KEY = "built-in:built-in:general-purpose";
  52. const REMOTE_STATE_FILE = "/home/dev/.zcode/v2/agents-state.json";
  53. const OVERRIDE_PROVIDER = "e2e-dsync-provider";
  54. const EXPLORE_LOCAL = {
  55. providerId: OVERRIDE_PROVIDER,
  56. modelId: "e2e-dsync-explore-model",
  57. options: { reasoningLevel: "high" },
  58. };
  59. const GENERAL_LOCAL = { providerId: OVERRIDE_PROVIDER, modelId: "e2e-dsync-general-local" };
  60. const GENERAL_REMOTE = {
  61. providerId: OVERRIDE_PROVIDER,
  62. modelId: "e2e-dsync-general-remote",
  63. options: { reasoningLevel: "low" },
  64. };
  65. const GENERAL_REMOTE_CHANGED = {
  66. providerId: OVERRIDE_PROVIDER,
  67. modelId: "e2e-dsync-general-changed",
  68. };
  69. // 远端已有的其他字段:推送只改 builtInModelSelectionOverrides 中选中的项,这些必须原样保留。
  70. const REMOTE_OTHER_FIELDS = {
  71. disabledAgentIds: ["user:user:e2e-dsync-remote-only"],
  72. pluginAgentModelSelectionOverrides: {
  73. "plugin:e2e-dsync@example:reviewer": {
  74. providerId: OVERRIDE_PROVIDER,
  75. modelId: "e2e-dsync-plugin-model",
  76. },
  77. },
  78. "x-e2e-marker": CASE_MARKER,
  79. };
  80. // 带模型、工具、技能、MCP、记忆和一个未知字段:表单保存会丢掉未知字段,按原文件同步必须原样保留。
  81. // 依赖提示(票 12):e2e-dsync-skill / e2e-dsync-mcp 只在本机用户列表中,容器里没有,应提示;
  82. // e2e-dsync-builtin 两端用户列表都没有(等同内置或插件提供),不提示。提示不影响勾选和推送。
  83. const AGENT_CONTENT = [
  84. "---",
  85. `name: ${AGENT_NAME}`,
  86. "description: Reviews data sync changes",
  87. "model: account:example-provider/example-model",
  88. "thoughtLevel: high",
  89. "tools: [Read, Grep, Glob]",
  90. `skills: [${DEPENDENCY_SKILL}, e2e-dsync-builtin]`,
  91. `mcpServers: [${DEPENDENCY_MCP}, e2e-dsync-builtin]`,
  92. "memory: user",
  93. `x-e2e-marker: ${CASE_MARKER} # keep comment`,
  94. "---",
  95. "Review the change carefully.",
  96. "",
  97. ].join("\n");
  98. /** 本机用户级技能与 MCP:子智能体引用的依赖在本机存在,远端缺少时才提示。 */
  99. async function writeLocalDependencies(home: string): Promise<void> {
  100. const skillDir = join(home, ".zcode", "skills", DEPENDENCY_SKILL);
  101. await mkdir(skillDir, { recursive: true });
  102. await writeFile(
  103. join(skillDir, "SKILL.md"),
  104. `---\nname: ${DEPENDENCY_SKILL}\ndescription: dependency for data sync e2e\n---\nBody\n`,
  105. );
  106. const cliDir = join(home, ".zcode", "cli");
  107. await mkdir(cliDir, { recursive: true });
  108. await writeFile(
  109. join(cliDir, "config.json"),
  110. `${JSON.stringify(
  111. { mcp: { servers: { [DEPENDENCY_MCP]: { command: "node", args: ["-e", "0"] } } } },
  112. null,
  113. 2,
  114. )}\n`,
  115. );
  116. }
  117. async function readRowState(key: string) {
  118. return await browser.execute((rowTestId) => {
  119. const row = document.querySelector<HTMLButtonElement>(`[data-testid="${rowTestId}"]`);
  120. return row
  121. ? {
  122. status: row.dataset.syncStatus ?? null,
  123. checked: row.getAttribute("aria-checked"),
  124. text: row.textContent ?? "",
  125. }
  126. : null;
  127. }, `remote-sync-row-${key}`);
  128. }
  129. async function readRowTargetModel(key: string) {
  130. return await browser.execute(
  131. (testId) => document.querySelector(`[data-testid="${testId}"]`)?.textContent ?? null,
  132. `remote-sync-row-target-model-${key}`,
  133. );
  134. }
  135. async function readRowError(key: string) {
  136. return await browser.execute(
  137. (testId) => document.querySelector(`[data-testid="${testId}"]`)?.textContent ?? null,
  138. `remote-sync-row-error-${key}`,
  139. );
  140. }
  141. async function writeLocalAgentState(home: string, state: unknown): Promise<void> {
  142. const stateDir = join(home, ".zcode", "v2");
  143. await mkdir(stateDir, { recursive: true });
  144. await writeFile(join(stateDir, "agents-state.json"), `${JSON.stringify(state, null, 2)}\n`);
  145. }
  146. async function readRemoteAgentState(container: string): Promise<Record<string, unknown>> {
  147. return JSON.parse(await readRemoteContainerFile(container, REMOTE_STATE_FILE)) as Record<
  148. string,
  149. unknown
  150. >;
  151. }
  152. async function readRowHint(key: string) {
  153. return await browser.execute(
  154. (hintTestId) => document.querySelector(`[data-testid="${hintTestId}"]`)?.textContent ?? null,
  155. `remote-sync-row-hint-${key}`,
  156. );
  157. }
  158. async function waitForRowHint(key: string, hint: string, timeoutMsg: string) {
  159. await browser.waitUntil(async () => (await readRowHint(key)) === hint, {
  160. timeout: 60_000,
  161. timeoutMsg: `${CASE_MARKER}: ${timeoutMsg}`,
  162. });
  163. }
  164. /** 设置 → 数据同步 → 唯一在线设备 → 打开同步弹窗。 */
  165. async function openDataSyncDialog(): Promise<void> {
  166. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 30_000 });
  167. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 30_000 });
  168. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "dataSync"), {
  169. timeout: 30_000,
  170. timeoutMsg: `${CASE_MARKER}: 设置页没有数据同步分区`,
  171. });
  172. await waitForTestIdByDom("remote-sync-device-list", {
  173. timeout: 60_000,
  174. timeoutMsg: `${CASE_MARKER}: 数据同步页没有在线设备`,
  175. });
  176. await clickTestIdByDom("remote-sync-device-action");
  177. await waitForTestIdByDom("remote-sync-dialog", { timeout: 30_000 });
  178. }
  179. /** 关闭同步弹窗和设置页,回到当前(远程)tab。 */
  180. async function closeDataSyncDialogAndSettings(): Promise<void> {
  181. // 关闭按钮只在概览页底栏。
  182. await clickTestIdByDom("remote-sync-tab-overview", { timeout: 30_000 });
  183. await clickTestIdByDom("remote-sync-close", { timeout: 30_000 });
  184. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, { timeout: 30_000 });
  185. await browser.waitUntil(
  186. async () => !(await $(`[data-testid="${TID_SETTINGS_PAGE}"]`).isDisplayed()),
  187. { timeout: 30_000, timeoutMsg: `${CASE_MARKER}: 设置页没有关闭` },
  188. );
  189. }
  190. /** 输入框建议面板中子智能体候选的文字;面板未打开时返回 null。 */
  191. async function readSubagentSuggestions(): Promise<string[] | null> {
  192. return await browser.execute(
  193. (panelTestId, optionPrefix) => {
  194. if (!document.querySelector(`[data-testid="${panelTestId}"]`)) return null;
  195. return Array.from(document.querySelectorAll<HTMLElement>(`[data-testid^="${optionPrefix}-"]`))
  196. .filter((option) => (option.dataset.optionId ?? "").startsWith("subagent:"))
  197. .map((option) => option.textContent ?? "");
  198. },
  199. TID_PROMPT_SUGGESTION_PANEL,
  200. TID_PROMPT_SUGGESTION_OPTION,
  201. );
  202. }
  203. /** 在当前远程项目的输入框输入 / 查询,等待子智能体候选包含 name。 */
  204. async function waitForSubagentSuggestion(name: string, timeoutMsg: string): Promise<string[]> {
  205. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, SLASH_QUERY, {
  206. timeout: 30_000,
  207. timeoutMsg: `${CASE_MARKER}: 远程项目没有可输入的输入框`,
  208. });
  209. let suggestions: string[] | null = null;
  210. await browser
  211. .waitUntil(
  212. async () => {
  213. suggestions = await readSubagentSuggestions();
  214. return Boolean(suggestions?.some((text) => text.includes(name)));
  215. },
  216. { timeout: 30_000, interval: 500 },
  217. )
  218. .catch((error: unknown) => {
  219. throw new Error(`${CASE_MARKER}: ${timeoutMsg};当前候选=${JSON.stringify(suggestions)}`, {
  220. cause: error,
  221. });
  222. });
  223. return suggestions ?? [];
  224. }
  225. async function waitForRowStatus(key: string, status: string, timeoutMsg: string) {
  226. await browser.waitUntil(async () => (await readRowState(key))?.status === status, {
  227. timeout: 60_000,
  228. timeoutMsg: `${CASE_MARKER}: ${timeoutMsg}`,
  229. });
  230. return (await readRowState(key))!;
  231. }
  232. describe(`${CASE_MARKER}: 数据同步页推送子智能体`, () => {
  233. after(async () => {
  234. await browser.electron.restoreAllMocks();
  235. await clearAppData();
  236. });
  237. it("DSYNC-09-01 推送带模型和工具配置的子智能体,远端文件字节与本机一致", async function () {
  238. this.timeout(20 * 60_000);
  239. const config = readSSHRuntimeConfig(CASE_MARKER);
  240. const container = readRemoteDockerContainer(CASE_MARKER);
  241. const workspacePath = config.workspacePaths[0];
  242. const localHome = getE2EAppDataPaths().homeDir;
  243. const localAgents = join(localHome, ".zcode", "agents");
  244. await mkdir(localAgents, { recursive: true });
  245. await writeFile(join(localAgents, AGENT_FILE), AGENT_CONTENT);
  246. await writeLocalDependencies(localHome);
  247. await prepareSSHConnectionPage(CASE_MARKER);
  248. await prepareV4ConversationE2E({ skipProvider: true });
  249. await connectSSHWorkspace({ caseMarker: CASE_MARKER, config, workspacePath });
  250. await openDataSyncDialog();
  251. // 概览有子智能体一行;进入分类页后该项为「推送」且默认勾选。
  252. await waitForTestIdByDom("remote-sync-overview-subagents", {
  253. timeout: 60_000,
  254. timeoutMsg: `${CASE_MARKER}: 概览没有子智能体一行`,
  255. });
  256. await clickTestIdByDom("remote-sync-tab-subagents");
  257. const row = await waitForRowStatus(AGENT_KEY, "push", "本机子智能体没有显示为「推送」");
  258. expect(row.checked).toBe("true");
  259. expect(row.text).toContain(AGENT_NAME);
  260. // MCP、技能分类加载完成后,行内合并显示本机有、远端缺少的依赖;两端都没有的不提示。
  261. // 提示不改变行状态与默认勾选。
  262. await waitForRowHint(AGENT_KEY, DEPENDENCY_HINT, "子智能体没有显示缺失依赖提示");
  263. expect((await readRowState(AGENT_KEY))?.checked).toBe("true");
  264. await clickTestIdByDom("remote-sync-push", {
  265. timeout: 30_000,
  266. timeoutMsg: `${CASE_MARKER}: 推送子智能体按钮不可用`,
  267. });
  268. await waitForRowStatus(AGENT_KEY, "synced", "推送后子智能体没有变为「已同步」");
  269. // 依赖仍未推送:提示保留,且没有阻止推送。
  270. expect(await readRowHint(AGENT_KEY)).toBe(DEPENDENCY_HINT);
  271. const local = await readFile(join(localAgents, AGENT_FILE), "utf-8");
  272. expect(await readRemoteContainerFile(container, `${REMOTE_AGENTS}/${AGENT_FILE}`)).toBe(local);
  273. expect(local).toBe(AGENT_CONTENT);
  274. });
  275. it("DSYNC-09-02 推送后,已打开的远程项目输入 / 能看到新子智能体", async function () {
  276. this.timeout(10 * 60_000);
  277. const localAgents = join(getE2EAppDataPaths().homeDir, ".zcode", "agents");
  278. await writeFile(
  279. join(localAgents, SLASH_AGENT_FILE),
  280. `---\nname: ${SLASH_AGENT_NAME}\ndescription: ${CASE_MARKER} slash\n---\nSlash prompt.\n`,
  281. );
  282. // 先让远程项目的 / 建议加载并缓存:09-01 推送的子智能体在列,新子智能体还没有。
  283. await closeDataSyncDialogAndSettings();
  284. const before = await waitForSubagentSuggestion(
  285. AGENT_NAME,
  286. "已打开的远程项目 / 建议没有 09-01 推送的子智能体",
  287. );
  288. expect(before.some((text) => text.includes(SLASH_AGENT_NAME))).toBe(false);
  289. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "");
  290. await openDataSyncDialog();
  291. await clickTestIdByDom("remote-sync-tab-subagents", { timeout: 60_000 });
  292. const row = await waitForRowStatus(SLASH_AGENT_KEY, "push", "新子智能体没有显示为「推送」");
  293. expect(row.checked).toBe("true");
  294. await clickTestIdByDom("remote-sync-push", {
  295. timeout: 30_000,
  296. timeoutMsg: `${CASE_MARKER}: 推送子智能体按钮不可用`,
  297. });
  298. await waitForRowStatus(SLASH_AGENT_KEY, "synced", "推送后新子智能体没有变为「已同步」");
  299. // 不重开项目:同一个远程项目的 / 建议直接出现新子智能体。
  300. await closeDataSyncDialogAndSettings();
  301. await waitForSubagentSuggestion(SLASH_AGENT_NAME, "推送后远程项目 / 建议没有新子智能体");
  302. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "");
  303. });
  304. it("DSYNC-09-03 推送内置子智能体的模型覆盖,只改选中的覆盖并保留远端其他字段", async function () {
  305. this.timeout(10 * 60_000);
  306. const container = readRemoteDockerContainer(CASE_MARKER);
  307. const localHome = getE2EAppDataPaths().homeDir;
  308. await writeLocalAgentState(localHome, {
  309. disabledAgentIds: [],
  310. builtInModelSelectionOverrides: { Explore: EXPLORE_LOCAL, "general-purpose": GENERAL_LOCAL },
  311. pluginAgentModelSelectionOverrides: {},
  312. });
  313. await writeRemoteContainerFile(
  314. container,
  315. REMOTE_STATE_FILE,
  316. `${JSON.stringify(
  317. {
  318. ...REMOTE_OTHER_FIELDS,
  319. builtInModelSelectionOverrides: { "general-purpose": GENERAL_REMOTE },
  320. },
  321. null,
  322. 2,
  323. )}\n`,
  324. );
  325. await openDataSyncDialog();
  326. await clickTestIdByDom("remote-sync-tab-subagents", { timeout: 60_000 });
  327. // 只在本机有覆盖:「推送」默认勾选,说明行是本机的模型和推理档位。
  328. const explore = await waitForRowStatus(EXPLORE_KEY, "push", "Explore 覆盖没有显示为「推送」");
  329. expect(explore.checked).toBe("true");
  330. expect(explore.text).toContain("Explore");
  331. expect(explore.text).toContain("内置 · 模型 e2e-dsync-explore-model · 推理档位 high");
  332. // 两端不同:「差异」默认不勾选,说明下方显示远端当前的模型。
  333. const general = await waitForRowStatus(
  334. GENERAL_KEY,
  335. "difference",
  336. "general-purpose 覆盖没有显示为「差异」",
  337. );
  338. expect(general.checked).toBe("false");
  339. expect(general.text).toContain("内置 · 模型 e2e-dsync-general-local");
  340. expect(await readRowTargetModel(GENERAL_KEY)).toBe(
  341. "远端:e2e-dsync-general-remote · 推理档位 low",
  342. );
  343. expect(await readRowTargetModel(EXPLORE_KEY)).toBeNull();
  344. // 列表加载后远端又改了 general-purpose:覆盖返回冲突、不写入,Explore 照常新建。
  345. await clickTestIdByDom(`remote-sync-row-${GENERAL_KEY}`);
  346. await browser.waitUntil(async () => (await readRowState(GENERAL_KEY))?.checked === "true", {
  347. timeout: 10_000,
  348. timeoutMsg: `${CASE_MARKER}: general-purpose 行没有被勾选`,
  349. });
  350. await writeRemoteContainerFile(
  351. container,
  352. REMOTE_STATE_FILE,
  353. `${JSON.stringify(
  354. {
  355. ...REMOTE_OTHER_FIELDS,
  356. builtInModelSelectionOverrides: { "general-purpose": GENERAL_REMOTE_CHANGED },
  357. },
  358. null,
  359. 2,
  360. )}\n`,
  361. );
  362. await clickTestIdByDom("remote-sync-push", {
  363. timeout: 30_000,
  364. timeoutMsg: `${CASE_MARKER}: 推送子智能体按钮不可用`,
  365. });
  366. await waitForRowStatus(EXPLORE_KEY, "synced", "推送后 Explore 覆盖没有变为「已同步」");
  367. await browser.waitUntil(
  368. async () => (await readRowError(GENERAL_KEY)) === "远端已变化,请刷新后重试",
  369. { timeout: 60_000, timeoutMsg: `${CASE_MARKER}: general-purpose 没有提示远端已变化` },
  370. );
  371. let remote = await readRemoteAgentState(container);
  372. expect(remote.builtInModelSelectionOverrides).toEqual({
  373. Explore: EXPLORE_LOCAL,
  374. "general-purpose": GENERAL_REMOTE_CHANGED,
  375. });
  376. // 推送后列表重新加载:差异行显示远端新的模型。
  377. await browser.waitUntil(
  378. async () => (await readRowTargetModel(GENERAL_KEY)) === "远端:e2e-dsync-general-changed",
  379. { timeout: 60_000, timeoutMsg: `${CASE_MARKER}: 差异行没有显示远端新的模型` },
  380. );
  381. // 推送后刷新保留失败项的勾选,直接再次推送即覆盖远端;两行都「已同步」,远端其他字段原样保留。
  382. expect((await readRowState(GENERAL_KEY))?.checked).toBe("true");
  383. await clickTestIdByDom("remote-sync-push", {
  384. timeout: 30_000,
  385. timeoutMsg: `${CASE_MARKER}: 推送子智能体按钮不可用`,
  386. });
  387. await waitForRowStatus(GENERAL_KEY, "synced", "覆盖后 general-purpose 没有变为「已同步」");
  388. expect(await readRowTargetModel(GENERAL_KEY)).toBeNull();
  389. remote = await readRemoteAgentState(container);
  390. expect(remote.builtInModelSelectionOverrides).toEqual({
  391. Explore: EXPLORE_LOCAL,
  392. "general-purpose": GENERAL_LOCAL,
  393. });
  394. expect(remote.disabledAgentIds).toEqual(REMOTE_OTHER_FIELDS.disabledAgentIds);
  395. expect(remote.pluginAgentModelSelectionOverrides).toEqual(
  396. REMOTE_OTHER_FIELDS.pluginAgentModelSelectionOverrides,
  397. );
  398. expect(remote["x-e2e-marker"]).toBe(CASE_MARKER);
  399. // 本机清除 Explore 覆盖(继承默认):重开弹窗后不再列出 Explore,远端的覆盖不变。
  400. await writeLocalAgentState(localHome, {
  401. disabledAgentIds: [],
  402. builtInModelSelectionOverrides: { "general-purpose": GENERAL_LOCAL },
  403. pluginAgentModelSelectionOverrides: {},
  404. });
  405. await closeDataSyncDialogAndSettings();
  406. await openDataSyncDialog();
  407. await clickTestIdByDom("remote-sync-tab-subagents", { timeout: 60_000 });
  408. await waitForRowStatus(
  409. GENERAL_KEY,
  410. "synced",
  411. "重开弹窗后 general-purpose 没有显示为「已同步」",
  412. );
  413. expect(await readRowState(EXPLORE_KEY)).toBeNull();
  414. remote = await readRemoteAgentState(container);
  415. expect(remote.builtInModelSelectionOverrides).toEqual({
  416. Explore: EXPLORE_LOCAL,
  417. "general-purpose": GENERAL_LOCAL,
  418. });
  419. });
  420. it("DSYNC-09-04 列表加载后远端出现覆盖或状态文件损坏:新建返回冲突、不覆盖远端的值;损坏时覆盖项失败、文件不变", async function () {
  421. this.timeout(10 * 60_000);
  422. const container = readRemoteDockerContainer(CASE_MARKER);
  423. const localHome = getE2EAppDataPaths().homeDir;
  424. const remoteWith = (overrides: Record<string, unknown>) =>
  425. `${JSON.stringify({ ...REMOTE_OTHER_FIELDS, builtInModelSelectionOverrides: overrides }, null, 2)}\n`;
  426. // 接 09-03:本机两项覆盖都有,远端只有 general-purpose(两端一致)。
  427. await writeLocalAgentState(localHome, {
  428. disabledAgentIds: [],
  429. builtInModelSelectionOverrides: { Explore: EXPLORE_LOCAL, "general-purpose": GENERAL_LOCAL },
  430. pluginAgentModelSelectionOverrides: {},
  431. });
  432. await writeRemoteContainerFile(
  433. container,
  434. REMOTE_STATE_FILE,
  435. remoteWith({ "general-purpose": GENERAL_LOCAL }),
  436. );
  437. await closeDataSyncDialogAndSettings();
  438. await openDataSyncDialog();
  439. await clickTestIdByDom("remote-sync-tab-subagents", { timeout: 60_000 });
  440. expect(
  441. (await waitForRowStatus(EXPLORE_KEY, "push", "Explore 没有显示为「推送」")).checked,
  442. ).toBe("true");
  443. await waitForRowStatus(GENERAL_KEY, "synced", "general-purpose 没有显示为「已同步」");
  444. // 列表加载后远端出现 Explore 覆盖:新建(createOnly)返回冲突,不覆盖远端的值。
  445. const exploreRemote = { providerId: OVERRIDE_PROVIDER, modelId: "e2e-dsync-explore-remote" };
  446. await writeRemoteContainerFile(
  447. container,
  448. REMOTE_STATE_FILE,
  449. remoteWith({ "general-purpose": GENERAL_LOCAL, Explore: exploreRemote }),
  450. );
  451. await clickTestIdByDom("remote-sync-push", {
  452. timeout: 30_000,
  453. timeoutMsg: `${CASE_MARKER}: 推送子智能体按钮不可用`,
  454. });
  455. await browser.waitUntil(
  456. async () => (await readRowError(EXPLORE_KEY)) === "远端已变化,请刷新后重试",
  457. { timeout: 60_000, timeoutMsg: `${CASE_MARKER}: Explore 新建冲突没有提示远端已变化` },
  458. );
  459. // 按内容比较:现有状态文件迁移会按固定顺序(Explore、general-purpose)重排这个 map 并重写文件。
  460. const remote = await readRemoteAgentState(container);
  461. expect(remote.builtInModelSelectionOverrides).toEqual({
  462. "general-purpose": GENERAL_LOCAL,
  463. Explore: exploreRemote,
  464. });
  465. expect(remote.disabledAgentIds).toEqual(REMOTE_OTHER_FIELDS.disabledAgentIds);
  466. // 推送后刷新:远端已有 Explore 覆盖,该行变为「差异」并显示远端的模型。
  467. await waitForRowStatus(EXPLORE_KEY, "difference", "冲突后 Explore 没有变为「差异」");
  468. await browser.waitUntil(
  469. async () => (await readRowTargetModel(EXPLORE_KEY)) === "远端:e2e-dsync-explore-remote",
  470. { timeout: 30_000, timeoutMsg: `${CASE_MARKER}: 冲突后差异行没有显示远端的模型` },
  471. );
  472. // 远端状态文件损坏:列表按没有覆盖处理(两项都是「推送」);推送时这些项失败,文件原样保留。
  473. const corrupt = "{ not json\n";
  474. await writeRemoteContainerFile(container, REMOTE_STATE_FILE, corrupt);
  475. await closeDataSyncDialogAndSettings();
  476. await openDataSyncDialog();
  477. await clickTestIdByDom("remote-sync-tab-subagents", { timeout: 60_000 });
  478. await waitForRowStatus(EXPLORE_KEY, "push", "状态文件损坏时 Explore 没有显示为「推送」");
  479. await waitForRowStatus(
  480. GENERAL_KEY,
  481. "push",
  482. "状态文件损坏时 general-purpose 没有显示为「推送」",
  483. );
  484. await clickTestIdByDom("remote-sync-push", {
  485. timeout: 30_000,
  486. timeoutMsg: `${CASE_MARKER}: 推送子智能体按钮不可用`,
  487. });
  488. for (const key of [EXPLORE_KEY, GENERAL_KEY]) {
  489. await browser.waitUntil(
  490. async () => {
  491. const error = await readRowError(key);
  492. return Boolean(error?.startsWith("同步失败:") && error.includes("is not valid JSON"));
  493. },
  494. { timeout: 60_000, timeoutMsg: `${CASE_MARKER}: ${key} 没有提示状态文件损坏` },
  495. );
  496. }
  497. expect(await readRemoteContainerFile(container, REMOTE_STATE_FILE)).toBe(corrupt);
  498. // 同一 runner 里后续 spec 共用容器:恢复为没有状态文件,避免远端子智能体一直走内置回退。
  499. await execInRemoteContainer(container, ["rm", "-f", REMOTE_STATE_FILE]);
  500. });
  501. });