conversation-session-model-availability-fallback.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { countUpstreamRequests } from "#e2e/helpers/conversation-session-network";
  2. import {
  3. TID_MODEL_PROVIDER_NAV_ITEM,
  4. TID_V4_COMPOSER_SEND,
  5. TID_SETTINGS_BACK_BUTTON,
  6. TID_SETTINGS_PAGE,
  7. TID_SETTINGS_SECTION_NAV,
  8. TID_TASK_SETTINGS_BUTTON,
  9. testId,
  10. } from "@zcode/shared/test-ids";
  11. import {
  12. DEFAULT_WORKSPACE,
  13. clearAppData,
  14. clickTestIdByDom,
  15. waitForWorkspaceApp,
  16. } from "#e2e/helpers/desktop-app";
  17. import {
  18. assertUpstreamRequestCapture,
  19. waitForUpstreamNetworkCapture,
  20. } from "#e2e/helpers/upstream-capture";
  21. import {
  22. UPSTREAM_ALTERNATE_MODEL,
  23. UPSTREAM_ALTERNATE_PROVIDER_ID,
  24. UPSTREAM_MODEL,
  25. UPSTREAM_PROVIDER_ID,
  26. selectUpstreamProviderModelById,
  27. } from "#e2e/helpers/upstream-provider";
  28. import {
  29. readLastSelectedAgentConfig,
  30. restartIntoWorkspacePreservingProfile,
  31. seedPersistedModelSelection,
  32. seedReplayProvider,
  33. waitForSelectedModel,
  34. } from "#e2e/helpers/model-provider-restart";
  35. import { sel } from "#e2e/helpers/selectors";
  36. import {
  37. E2E_REPLY_TOKEN,
  38. prepareV4ConversationE2E,
  39. sendV4Prompt,
  40. setV4ComposerText,
  41. getV4ComposerText,
  42. startNewV4Draft,
  43. waitForV4TimelineContaining,
  44. } from "#e2e/helpers/v4-conversation";
  45. describe("模型不可用时保留选择 E2E", () => {
  46. after(async () => {
  47. await browser.electron.restoreAllMocks();
  48. await clearAppData();
  49. });
  50. it("I56/MAF-01: idle session 禁用 Provider 后阻止发送,明确改选后恢复", async function () {
  51. this.timeout(180000);
  52. await prepareV4ConversationE2E({ skipProvider: true });
  53. await seedReplayProvider({
  54. // 回放使用 Messages 协议,不能用默认 Chat Completions 种子请求另一条路由。
  55. apiFormat: "anthropic-messages",
  56. id: UPSTREAM_PROVIDER_ID,
  57. // 修复原因:字符串模型只声明 disabled 档位,下面种入的 max 偏好会在冷启动时被拒绝,
  58. // 草稿没有合法档位,种子消息的发送按钮一直不可用。源模型声明与偏好一致的档位。
  59. models: [{ id: UPSTREAM_MODEL, reasoningLevel: { values: ["high", "max"], map: "{}" } }],
  60. name: "Upstream Fallback Source",
  61. });
  62. await seedReplayProvider({
  63. // 回放使用 Messages 协议,不能用默认 Chat Completions 种子请求另一条路由。
  64. apiFormat: "anthropic-messages",
  65. id: UPSTREAM_ALTERNATE_PROVIDER_ID,
  66. models: [UPSTREAM_ALTERNATE_MODEL],
  67. name: "Upstream Fallback Target",
  68. });
  69. await seedPersistedModelSelection({
  70. modelId: UPSTREAM_MODEL,
  71. providerId: UPSTREAM_PROVIDER_ID,
  72. reasoningLevel: "max",
  73. });
  74. // 完整重启必须保留浏览器 profile;只恢复模型偏好会丢失 API Key 登录模式。
  75. await restartIntoWorkspacePreservingProfile();
  76. await waitForSelectedModel(UPSTREAM_PROVIDER_ID, UPSTREAM_MODEL);
  77. const seedMarker = `E2E_MODEL_FALLBACK_SEED_${Date.now()}`;
  78. await sendV4Prompt(`${seedMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`);
  79. await waitForV4TimelineContaining(E2E_REPLY_TOKEN);
  80. await assertDisabledSelectionAndSelectTarget("Upstream Fallback Target");
  81. const marker = `E2E_MODEL_FALLBACK_TARGET_${Date.now()}`;
  82. const prompt = `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  83. await sendV4Prompt(prompt);
  84. const capture = await waitForUpstreamNetworkCapture(marker);
  85. assertUpstreamRequestCapture(capture, {
  86. expectedText: prompt,
  87. model: UPSTREAM_ALTERNATE_MODEL,
  88. });
  89. await waitForV4TimelineContaining(E2E_REPLY_TOKEN);
  90. });
  91. it("I61/MAF-02: 草稿禁用 Provider 后保留选择,明确改选后下一草稿继承", async function () {
  92. this.timeout(180000);
  93. await prepareV4ConversationE2E({ skipProvider: true });
  94. await seedReplayProvider({
  95. // 回放使用 Messages 协议,不能用默认 Chat Completions 种子请求另一条路由。
  96. apiFormat: "anthropic-messages",
  97. id: UPSTREAM_PROVIDER_ID,
  98. models: [UPSTREAM_MODEL],
  99. name: "Upstream Draft Fallback Source",
  100. });
  101. await seedReplayProvider({
  102. // 回放使用 Messages 协议,不能用默认 Chat Completions 种子请求另一条路由。
  103. apiFormat: "anthropic-messages",
  104. id: UPSTREAM_ALTERNATE_PROVIDER_ID,
  105. models: [UPSTREAM_ALTERNATE_MODEL],
  106. name: "Upstream Draft Fallback Target",
  107. });
  108. await seedPersistedModelSelection({
  109. modelId: UPSTREAM_MODEL,
  110. providerId: UPSTREAM_PROVIDER_ID,
  111. reasoningLevel: "max",
  112. });
  113. // 完整重启必须保留浏览器 profile;只恢复模型偏好会丢失 API Key 登录模式。
  114. await restartIntoWorkspacePreservingProfile();
  115. await waitForSelectedModel(UPSTREAM_PROVIDER_ID, UPSTREAM_MODEL);
  116. await assertDisabledSelectionAndSelectTarget("Upstream Draft Fallback Target");
  117. const marker = `E2E_MODEL_FALLBACK_DRAFT_TARGET_${Date.now()}`;
  118. const prompt = `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  119. await sendV4Prompt(prompt);
  120. const capture = await waitForUpstreamNetworkCapture(marker);
  121. assertUpstreamRequestCapture(capture, {
  122. expectedText: prompt,
  123. model: UPSTREAM_ALTERNATE_MODEL,
  124. });
  125. await waitForV4TimelineContaining(E2E_REPLY_TOKEN);
  126. await startNewV4Draft();
  127. await waitForSelectedModel(UPSTREAM_ALTERNATE_PROVIDER_ID, UPSTREAM_ALTERNATE_MODEL);
  128. });
  129. });
  130. async function disableProvider(providerId: string) {
  131. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 15000 });
  132. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  133. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "modelProvider"), {
  134. timeout: 15000,
  135. });
  136. await clickTestIdByDom(testId(TID_MODEL_PROVIDER_NAV_ITEM, `custom:${providerId}`), {
  137. timeout: 30000,
  138. timeoutMsg: `模型供应商导航没有出现 ${providerId}`,
  139. });
  140. // Provider 启停已改为有状态开关,旧的文字按钮定位不再对应实际设置入口。
  141. const toggle = $('[data-testid="model-provider-enabled-switch"]');
  142. await toggle.waitForClickable({ timeout: 15000 });
  143. expect(await toggle.getAttribute("aria-checked")).toBe("true");
  144. await toggle.click();
  145. await browser.waitUntil(async () => (await toggle.getAttribute("aria-checked")) === "false", {
  146. timeout: 15000,
  147. timeoutMsg: `模型供应商未停用: ${providerId}`,
  148. });
  149. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, { timeout: 15000 });
  150. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  151. }
  152. async function assertDisabledSelectionAndSelectTarget(providerName: string) {
  153. const marker = `E2E_MODEL_UNAVAILABLE_UNSENT_${Date.now()}`;
  154. await setV4ComposerText(marker);
  155. await disableProvider(UPSTREAM_PROVIDER_ID);
  156. // 旧用例把静默 fallback 当作成功。当前契约保留用户选择,只有明确改选才恢复发送。
  157. await browser.waitUntil(async () => !(await $(sel(TID_V4_COMPOSER_SEND)).isEnabled()), {
  158. timeout: 15000,
  159. timeoutMsg: "禁用当前 Provider 后发送按钮仍可用",
  160. });
  161. expect(await getV4ComposerText()).toBe(marker);
  162. expect((await readLastSelectedAgentConfig())?.model).toContain(UPSTREAM_MODEL);
  163. expect(await countUpstreamRequests({ includes: [marker] })).toBe(0);
  164. await selectUpstreamProviderModelById(UPSTREAM_ALTERNATE_MODEL, {
  165. providerId: UPSTREAM_ALTERNATE_PROVIDER_ID,
  166. providerName,
  167. });
  168. await waitForSelectedModel(UPSTREAM_ALTERNATE_PROVIDER_ID, UPSTREAM_ALTERNATE_MODEL);
  169. expect(await getV4ComposerText()).toBe(marker);
  170. await $(sel(TID_V4_COMPOSER_SEND)).waitForEnabled({ timeout: 15000 });
  171. }