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

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