conversation-session-new-session-inherits-model.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { clearAppData } from "#e2e/helpers/desktop-app";
  2. import {
  3. assertUpstreamRequestCapture,
  4. assertUpstreamThoughtLevelCapture,
  5. waitForUpstreamNetworkCapture,
  6. } from "#e2e/helpers/upstream-capture";
  7. import {
  8. UPSTREAM_PROVIDER_ID,
  9. UPSTREAM_SECONDARY_MODEL,
  10. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  11. ensureUpstreamModelForE2E,
  12. getUpstreamProviderModelValues,
  13. getUpstreamThoughtLevelLabels,
  14. getSelectedUpstreamModelLabel,
  15. getSelectedUpstreamThoughtLevelLabel,
  16. selectUpstreamModelById,
  17. selectUpstreamThoughtLevelValue,
  18. } from "#e2e/helpers/upstream-provider";
  19. import {
  20. E2E_REPLY_TOKEN,
  21. prepareV4ConversationE2E,
  22. sendV4Prompt,
  23. startNewV4Draft,
  24. waitForV4AssistantMessageContaining,
  25. waitForV4ConversationState,
  26. waitForV4ComposerText,
  27. waitForV4UserMessageContaining,
  28. } from "#e2e/helpers/v4-conversation";
  29. describe("会话区新建会话继承模型 E2E", () => {
  30. after(async () => {
  31. await browser.electron.restoreAllMocks();
  32. await clearAppData();
  33. });
  34. it("I11: 新建空草稿应继承上一次 provider/model/thought 选择", async function () {
  35. this.timeout(220000);
  36. await prepareV4ConversationE2E();
  37. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  38. // Recent 按项目/任务范围隔离;前后两次都从全局新任务入口建立同一范围。
  39. await startNewV4Draft();
  40. await waitForEmptyDraft();
  41. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL, {
  42. includePlainModelFallback: false,
  43. providerId: UPSTREAM_PROVIDER_ID,
  44. });
  45. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  46. await assertSelectedSecondaryConfig();
  47. const runId = Date.now();
  48. const markerA = `E2E_NEW_SESSION_INHERITS_MODEL_A_${runId}`;
  49. const promptA = `${markerA}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  50. await sendV4Prompt(promptA);
  51. await waitForV4ComposerText("", "继承模型 case A 发送后输入框没有清空");
  52. await waitForV4UserMessageContaining(markerA);
  53. const recordA = await waitForUpstreamNetworkCapture(markerA);
  54. assertSecondaryUpstreamConfig(recordA, promptA);
  55. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  56. await waitForIdleQueueEmpty("继承模型 case A 没有完成");
  57. await startNewV4Draft();
  58. await waitForEmptyDraft();
  59. await assertSelectedSecondaryConfig();
  60. const markerB = `E2E_NEW_SESSION_INHERITS_MODEL_B_${runId}`;
  61. const promptB = `${markerB}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  62. await sendV4Prompt(promptB);
  63. await waitForV4ComposerText("", "继承模型 case B 发送后输入框没有清空");
  64. await waitForV4UserMessageContaining(markerB);
  65. const recordB = await waitForUpstreamNetworkCapture(markerB);
  66. assertSecondaryUpstreamConfig(recordB, promptB);
  67. await waitForIdleQueueEmpty("继承模型 case B 没有完成");
  68. });
  69. });
  70. function assertSecondaryUpstreamConfig(
  71. record: Awaited<ReturnType<typeof waitForUpstreamNetworkCapture>>,
  72. expectedText: string,
  73. ) {
  74. assertUpstreamRequestCapture(record, {
  75. expectedText,
  76. model: UPSTREAM_SECONDARY_MODEL,
  77. });
  78. if (UPSTREAM_SECONDARY_THOUGHT_LEVEL) {
  79. assertUpstreamThoughtLevelCapture(record, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  80. }
  81. }
  82. async function assertSelectedSecondaryConfig() {
  83. const modelLabel = await getSelectedUpstreamModelLabel();
  84. const acceptedModelValues = getUpstreamProviderModelValues(UPSTREAM_SECONDARY_MODEL, {
  85. includePlainModelFallback: false,
  86. providerId: UPSTREAM_PROVIDER_ID,
  87. });
  88. const modelSelected =
  89. acceptedModelValues.includes(modelLabel.currentValue) ||
  90. modelLabel.text.includes(UPSTREAM_SECONDARY_MODEL) ||
  91. modelLabel.title.includes(UPSTREAM_SECONDARY_MODEL);
  92. if (!modelSelected) {
  93. throw new Error(
  94. `当前模型不是继承的 secondary 上游 模型: ${JSON.stringify({
  95. acceptedModelValues,
  96. model: UPSTREAM_SECONDARY_MODEL,
  97. modelLabel,
  98. })}`,
  99. );
  100. }
  101. if (!UPSTREAM_SECONDARY_THOUGHT_LEVEL) {
  102. return;
  103. }
  104. const thoughtLabel = await getSelectedUpstreamThoughtLevelLabel();
  105. const acceptedThoughtValues = [
  106. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  107. ...getUpstreamThoughtLevelLabels(UPSTREAM_SECONDARY_THOUGHT_LEVEL),
  108. ].map((value) => value.toLowerCase());
  109. const currentThoughtCandidates = [
  110. thoughtLabel.currentValue,
  111. thoughtLabel.ariaLabel,
  112. thoughtLabel.text,
  113. thoughtLabel.title,
  114. ].map((value) => value.replace(/\s+/g, " ").trim().toLowerCase());
  115. if (!currentThoughtCandidates.some((candidate) => acceptedThoughtValues.includes(candidate))) {
  116. throw new Error(
  117. `当前思考深度不是继承的 secondary 配置: ${JSON.stringify({
  118. acceptedThoughtValues,
  119. thoughtLabel,
  120. })}`,
  121. );
  122. }
  123. }
  124. async function waitForEmptyDraft() {
  125. await waitForV4ConversationState(
  126. (snapshot) =>
  127. snapshot.state === "idle" &&
  128. snapshot.sessionId === null &&
  129. snapshot.taskId === null &&
  130. snapshot.queueCount === 0 &&
  131. !snapshot.modelSwitchPending,
  132. "没有进入空草稿态",
  133. 30000,
  134. );
  135. }
  136. async function waitForIdleQueueEmpty(timeoutMsg: string) {
  137. await waitForV4ConversationState(
  138. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  139. timeoutMsg,
  140. 90000,
  141. );
  142. }