conversation-session-remote-composer-plan-reselect.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { mkdir, writeFile } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { clearAppData } from "#e2e/helpers/desktop-app";
  4. import {
  5. INDIVIDUAL_PROVIDER_ID,
  6. PERSONAL_PLAN_CONNECTION_KEY,
  7. REMOTE_DATA_DIR,
  8. SYNC_TIMEOUT_MS,
  9. TEAM_PLAN_KEY,
  10. closeSettingsPage as closeSettingsPageFor,
  11. collectRemoteDiagnostics,
  12. readAccountSettingsSyncs as readAccountSettingsSyncsFor,
  13. readLocalAccountSettings,
  14. readRemoteSelectionKind,
  15. seedSignedInIndividualPlan as seedSignedInIndividualPlanFor,
  16. selectConnectionModeInSettings as selectConnectionModeInSettingsFor,
  17. switchComposerProvider as switchComposerProviderFor,
  18. waitForAccountSettingsSyncs as waitForAccountSettingsSyncsFor,
  19. } from "#e2e/helpers/remote-composer-plan-connection";
  20. import {
  21. chmodRemoteContainerPath,
  22. execInRemoteContainer,
  23. readRemoteDockerContainer,
  24. } from "#e2e/helpers/remote-docker-target";
  25. import {
  26. connectSSHWorkspace,
  27. createSSHReplayTunnel,
  28. prepareSSHConnectionPage,
  29. readSSHRuntimeConfig,
  30. } from "#e2e/helpers/ssh-remote-p0";
  31. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  32. // ZCT-2118278184274395136:SSH 工作区里「个人套餐 → 普通 API → 同一个人套餐」每次切回 1.5~8 秒。
  33. // 普通 API 不写套餐设置,切回时连接仍已生效;旧实现仍保存一次,向远端推一轮 account-settings
  34. // Provisioning,并让目标 refresh 等它写完。
  35. //
  36. // 事件顺序:
  37. // 本机预置 OAuth 与个人套餐 → SSH 反向隧道让容器访问同一账号 mock → 连接 SSH 项目(首次下发)
  38. // ① 切回已生效连接:API → 个人 → API → 个人,不写设置、不推 account-settings、切换成功
  39. // ② 远端未同步时回退:设置页改团队(远端同步)→ 远端只读 → 设置页改回个人(远端同步失败,
  40. // 远端仍是团队)→ 恢复可写 → Composer API → 个人:走原流程保存并重新推送,远端变回个人
  41. const CASE_MARKER = "E2E_REMOTE_PLAN_RESELECT";
  42. const artifactDir = resolve(
  43. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/remote-composer-plan-reselect",
  44. );
  45. // 共用步骤见 remote-composer-plan-connection(RPR-01 / RPR-02 共用);这里绑定本用例的 marker。
  46. const readAccountSettingsSyncs = () => readAccountSettingsSyncsFor(CASE_MARKER);
  47. const waitForAccountSettingsSyncs = (count: number, timeoutMsg: string) =>
  48. waitForAccountSettingsSyncsFor(CASE_MARKER, count, timeoutMsg);
  49. const seedSignedInIndividualPlan = () => seedSignedInIndividualPlanFor(CASE_MARKER);
  50. const switchComposerProvider = (providerId: string, label: string) =>
  51. switchComposerProviderFor(CASE_MARKER, providerId, label);
  52. const selectConnectionModeInSettings = (optionKey: string) =>
  53. selectConnectionModeInSettingsFor(CASE_MARKER, optionKey);
  54. const closeSettingsPage = () => closeSettingsPageFor(CASE_MARKER);
  55. describe(`${CASE_MARKER}: 远端 Composer 切回已生效套餐连接`, () => {
  56. const facts: Record<string, unknown> = {};
  57. let closeTunnel: (() => Promise<void>) | undefined;
  58. let diagnosticsContainer: string | undefined;
  59. after(async () => {
  60. if (diagnosticsContainer)
  61. facts.remoteDiagnostics = await collectRemoteDiagnostics(diagnosticsContainer);
  62. await mkdir(artifactDir, { recursive: true });
  63. await writeFile(resolve(artifactDir, "plan-reselect.json"), JSON.stringify(facts, null, 2));
  64. await closeTunnel?.();
  65. await browser.electron.restoreAllMocks();
  66. await clearAppData();
  67. });
  68. it("RPR-01 切回已生效连接不重推远端;远端未同步时回退原流程并重新推送", async function () {
  69. this.timeout(25 * 60_000);
  70. const config = readSSHRuntimeConfig(CASE_MARKER);
  71. const container = readRemoteDockerContainer(CASE_MARKER);
  72. diagnosticsContainer = container;
  73. const workspacePath = config.workspacePaths[0];
  74. const apiProviderId = process.env.E2E_PROVIDER_ID?.trim();
  75. if (!apiProviderId) throw new Error(`${CASE_MARKER}: 缺少 E2E API Provider`);
  76. await seedSignedInIndividualPlan();
  77. await prepareSSHConnectionPage(CASE_MARKER);
  78. await prepareV4ConversationE2E();
  79. // 远端按下发的 ZCODE_BASE_URL(宿主 127.0.0.1:<port>)查询套餐权益;在容器内监听同一端口转发到宿主。
  80. const tunnel = await createSSHReplayTunnel(config, CASE_MARKER, {
  81. username: "tunnel",
  82. mirrorLocalPort: true,
  83. targetBaseURL: process.env.ZCODE_E2E_CODING_PLAN_MOCK_URL,
  84. });
  85. closeTunnel = tunnel.close;
  86. // 远端查个人/团队套餐权益读 BIGMODEL_API_BASE_URL,它不在 Desktop 转发给远端的运行时变量里;
  87. // 经 PAM 读取的 /etc/environment 让这台机器的 SSH 会话指向同一 mock,相当于远端本机环境配置。
  88. await execInRemoteContainer(
  89. container,
  90. ["sh", "-c", `echo 'BIGMODEL_API_BASE_URL=${tunnel.baseURL}' >> /etc/environment`],
  91. { user: "0" },
  92. );
  93. await connectSSHWorkspace({ caseMarker: CASE_MARKER, config, workspacePath });
  94. facts.remoteSelectionAfterConnect = await readRemoteSelectionKind(container);
  95. expect(facts.remoteSelectionAfterConnect).toBe("individual-coding-plan");
  96. // ① 工单场景:设置一直是个人套餐,经普通 API 切回时不应再写设置或推远端。
  97. const syncsBefore = (await readAccountSettingsSyncs()).length;
  98. const settingsBefore = await readLocalAccountSettings();
  99. const durations: Record<string, number> = {};
  100. durations.apiToIndividualFirst = await switchComposerProvider(
  101. INDIVIDUAL_PROVIDER_ID,
  102. "个人套餐",
  103. );
  104. durations.individualToApi = await switchComposerProvider(apiProviderId, "普通 API");
  105. durations.apiToIndividual = await switchComposerProvider(INDIVIDUAL_PROVIDER_ID, "个人套餐");
  106. // 写入后 Provisioning 会在 1~2 秒内记录结果;留出时间,确保没有迟到的 account-settings 轮次。
  107. await browser.pause(5_000);
  108. const settingsAfter = await readLocalAccountSettings();
  109. const syncsAfter = (await readAccountSettingsSyncs()).length;
  110. facts.effective = { durations, settingsBefore, settingsAfter, syncsBefore, syncsAfter };
  111. expect(settingsAfter).toEqual(settingsBefore);
  112. expect(syncsAfter).toBe(syncsBefore);
  113. // ② 制造「本机已是个人、远端仍是团队」:设置页改团队并等远端同步,再在远端只读时改回个人。
  114. await switchComposerProvider(apiProviderId, "普通 API");
  115. const baseline = (await readAccountSettingsSyncs()).length;
  116. await selectConnectionModeInSettings(TEAM_PLAN_KEY);
  117. const toTeam = await waitForAccountSettingsSyncs(baseline + 1, "改团队后没有远端同步结果");
  118. expect(toTeam.at(-1)?.status).toMatch(/^(applied|already-applied)$/u);
  119. await browser.waitUntil(
  120. async () => (await readRemoteSelectionKind(container)) === "team-coding-plan",
  121. { timeout: SYNC_TIMEOUT_MS, timeoutMsg: `${CASE_MARKER}: 远端没有收到团队连接` },
  122. );
  123. // 以 dev 身份执行才对远端 server 生效;用例结束前一定恢复。
  124. await chmodRemoteContainerPath(container, "a-w", REMOTE_DATA_DIR);
  125. let restored = false;
  126. try {
  127. await selectConnectionModeInSettings(PERSONAL_PLAN_CONNECTION_KEY);
  128. const failed = await waitForAccountSettingsSyncs(
  129. baseline + 2,
  130. "远端只读时改个人没有同步结果",
  131. );
  132. facts.readonlySync = failed.at(-1);
  133. expect(failed.at(-1)?.status).not.toMatch(/^(applied|already-applied)$/u);
  134. } finally {
  135. await chmodRemoteContainerPath(container, "u+w", REMOTE_DATA_DIR);
  136. restored = true;
  137. }
  138. expect(restored).toBe(true);
  139. expect((await readLocalAccountSettings()).selection?.kind).toBe("individual-coding-plan");
  140. facts.remoteSelectionBeforeFallback = await readRemoteSelectionKind(container);
  141. expect(facts.remoteSelectionBeforeFallback).toBe("team-coding-plan");
  142. await closeSettingsPage();
  143. const fallbackBefore = await readLocalAccountSettings();
  144. const fallbackSyncsBefore = (await readAccountSettingsSyncs()).length;
  145. durations.fallbackApiToIndividual = await switchComposerProvider(
  146. INDIVIDUAL_PROVIDER_ID,
  147. "个人套餐(远端未同步)",
  148. );
  149. const fallbackAfter = await readLocalAccountSettings();
  150. const fallbackSyncs = await readAccountSettingsSyncs();
  151. facts.fallback = {
  152. before: fallbackBefore,
  153. after: fallbackAfter,
  154. syncs: fallbackSyncs.slice(fallbackSyncsBefore),
  155. remoteSelection: await readRemoteSelectionKind(container),
  156. };
  157. // 远端与本机身份不一致:必须走原流程保存(版本递增)并重新推送,远端回到个人套餐。
  158. expect(fallbackAfter.selection?.kind).toBe("individual-coding-plan");
  159. expect(fallbackAfter.providerFamilyDomainUpdatedAt).not.toBe(
  160. fallbackBefore.providerFamilyDomainUpdatedAt,
  161. );
  162. expect(fallbackSyncs.length).toBeGreaterThan(fallbackSyncsBefore);
  163. expect(fallbackSyncs.at(-1)?.status).toMatch(/^(applied|already-applied)$/u);
  164. expect(await readRemoteSelectionKind(container)).toBe("individual-coding-plan");
  165. });
  166. });