bot-effective-selection.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import {
  2. BUILTIN_MODEL_PROVIDER_IDS,
  3. encodeCustomModelValue,
  4. type ModelSelection,
  5. } from "@zcode/shared";
  6. import {
  7. createBotModelSelectionView,
  8. createBotsServiceHarness,
  9. createFeishuConfig,
  10. readBotSyntheticFixture,
  11. } from "#e2e/bots/helpers/bots-service-harness";
  12. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  13. describe("BOT-E2E-SR99 绑定 Session 后的有效选择", () => {
  14. for (const available of [true, false]) {
  15. it(`下一条空闲输入使用 Session 原意图:${available ? "可用" : "不可用"}`, async () => {
  16. const original: ModelSelection = {
  17. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  18. modelId: "GLM-5.3",
  19. options: { reasoningLevel: "high" },
  20. };
  21. const effective = {
  22. ...original,
  23. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  24. };
  25. const parsed: Array<ModelSelection | null | undefined> = [];
  26. const harness = createBotsServiceHarness({
  27. config: createFeishuConfig(),
  28. state: {
  29. version: 3,
  30. bots: {
  31. "feishu-e2e": {
  32. botId: "feishu-e2e",
  33. workspacePath: resolveE2ERuntimePath("bots", "e2e-bot-workspace"),
  34. mode: "task",
  35. activeTaskId: "task-e2e-bot",
  36. updatedAt: 1,
  37. },
  38. },
  39. },
  40. modelSelectionService: {
  41. getView: async (input) => {
  42. parsed.push(input?.selection);
  43. return {
  44. ...createBotModelSelectionView(),
  45. preferredSelection: effective,
  46. effectiveSelection: available ? effective : null,
  47. };
  48. },
  49. },
  50. });
  51. harness.zcodeTaskService.getTaskModelSelection = async () => original;
  52. try {
  53. const submission = harness.service.handleInboundMessage({
  54. botId: "feishu-e2e",
  55. actor: {
  56. provider: "feishu",
  57. botId: "feishu-e2e",
  58. providerUserId: "ou_e2e_user",
  59. chatType: "private",
  60. },
  61. text: "E2E_BOT_SR99_NEXT_INPUT",
  62. });
  63. if (available) {
  64. await submission;
  65. expect(harness.sendPromptCalls).toEqual([
  66. expect.objectContaining({ taskId: "task-e2e-bot", modelSelection: effective }),
  67. ]);
  68. } else {
  69. await expect(submission).rejects.toThrow("原选择已保留");
  70. expect(harness.sendPromptCalls).toHaveLength(0);
  71. }
  72. expect(parsed).toEqual([original]);
  73. expect(harness.createTaskCalls).toHaveLength(0);
  74. expect(harness.readState().bots["feishu-e2e"]?.draftOptions).toBeUndefined();
  75. expect(original.providerId).toBe(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan);
  76. } finally {
  77. harness.service.disposeAll();
  78. }
  79. });
  80. }
  81. });
  82. describe("BOT-E2E-SR87 有效选择", () => {
  83. it("主动 /model 保存最高档,但不自动创建任务", async () => {
  84. const view = createBotModelSelectionView();
  85. const provider = view.providers[0]!;
  86. const model = provider.models[0]!;
  87. const configuredView = {
  88. ...view,
  89. providers: [
  90. {
  91. ...provider,
  92. models: [
  93. {
  94. ...model,
  95. config: {
  96. ...model.config,
  97. optionSpecs: {
  98. ...model.config.optionSpecs,
  99. reasoningLevel: {
  100. ...model.config.optionSpecs.reasoningLevel,
  101. values: ["low", "high", "max"],
  102. },
  103. },
  104. },
  105. },
  106. ],
  107. },
  108. ],
  109. };
  110. const harness = createBotsServiceHarness({
  111. config: createFeishuConfig(),
  112. state: {
  113. version: 3,
  114. bots: {
  115. "feishu-e2e": {
  116. botId: "feishu-e2e",
  117. workspacePath: "/workspace/e2e-bot-selection",
  118. mode: "draft",
  119. activeTaskId: null,
  120. updatedAt: 1,
  121. draftOptions: { provider: "glm" },
  122. },
  123. },
  124. },
  125. modelSelectionService: { getView: async () => configuredView },
  126. });
  127. try {
  128. await harness.service.handleInboundMessage({
  129. botId: "feishu-e2e",
  130. actor: {
  131. provider: "feishu",
  132. botId: "feishu-e2e",
  133. providerUserId: "ou_e2e_user",
  134. chatType: "private",
  135. },
  136. text: `/model ${encodeCustomModelValue(provider.providerId, model.modelId)}`,
  137. });
  138. expect(harness.readState().bots["feishu-e2e"]?.draftOptions?.modelSelection).toEqual({
  139. providerId: provider.providerId,
  140. modelId: model.modelId,
  141. options: { reasoningLevel: "max" },
  142. });
  143. expect(harness.createTaskCalls).toHaveLength(0);
  144. } finally {
  145. harness.service.disposeAll();
  146. }
  147. });
  148. for (const available of [true, false]) {
  149. it(`菜单和首次派发保留原意图:有效选择${available ? "可用" : "失效"}`, async () => {
  150. expect(await readBotSyntheticFixture("effective-selection.json")).toMatchObject({
  151. caseId: "BOT-E2E-SR87",
  152. classification: "synthetic",
  153. });
  154. const original: ModelSelection = {
  155. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  156. modelId: "GLM-5.3",
  157. options: { reasoningLevel: "high" },
  158. };
  159. const effective = {
  160. ...original,
  161. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  162. };
  163. const base = createBotModelSelectionView();
  164. const provider = base.providers[0]!;
  165. const model = provider.models[0]!;
  166. const view = {
  167. ...base,
  168. preferredSelection: effective,
  169. providers: [
  170. {
  171. ...provider,
  172. providerId: effective.providerId,
  173. config: { ...provider.config, personalModelIds: [effective.modelId] },
  174. models: [
  175. {
  176. ...model,
  177. modelId: effective.modelId,
  178. config: {
  179. ...model.config,
  180. optionSpecs: {
  181. ...model.config.optionSpecs,
  182. reasoningLevel: {
  183. ...model.config.optionSpecs.reasoningLevel!,
  184. values: ["low", "high"],
  185. },
  186. },
  187. },
  188. },
  189. ],
  190. },
  191. ],
  192. };
  193. const harness = createBotsServiceHarness({
  194. config: createFeishuConfig(),
  195. state: {
  196. version: 3,
  197. bots: {
  198. "feishu-e2e": {
  199. botId: "feishu-e2e",
  200. workspacePath: "/workspace/e2e-bot-selection",
  201. mode: "draft",
  202. activeTaskId: null,
  203. draftOptions: { provider: "glm", modelSelection: original },
  204. updatedAt: 1,
  205. },
  206. },
  207. },
  208. modelSelectionService: {
  209. getView: async (input) => ({
  210. ...view,
  211. ...(input ? { effectiveSelection: available ? effective : null } : {}),
  212. }),
  213. },
  214. });
  215. const actor = {
  216. provider: "feishu" as const,
  217. botId: "feishu-e2e",
  218. providerUserId: "ou_e2e_user",
  219. chatType: "private" as const,
  220. };
  221. const send = (text: string) =>
  222. harness.service.handleInboundMessage({ botId: actor.botId, actor, text });
  223. try {
  224. const model = await send("/model");
  225. expect(model[0]?.selection?.currentId ?? null).toBe(
  226. available ? effective.providerId : null,
  227. );
  228. const thought = await send("/think");
  229. if (available) expect(thought[0]?.selection?.currentId).toBe("high");
  230. expect(harness.readState().bots[actor.botId]?.draftOptions?.modelSelection).toEqual(
  231. original,
  232. );
  233. if (!available) {
  234. await send("/think high");
  235. expect(harness.readState().bots[actor.botId]?.draftOptions?.modelSelection).toEqual(
  236. original,
  237. );
  238. }
  239. if (available) {
  240. await send("E2E_BOT_SR87_FIRST_INPUT");
  241. expect(harness.createTaskCalls).toHaveLength(1);
  242. expect(harness.createTaskCalls[0]).toMatchObject({ modelSelection: effective });
  243. expect(harness.sendPromptCalls).toHaveLength(1);
  244. } else {
  245. await expect(send("E2E_BOT_SR87_FIRST_INPUT")).rejects.toThrow("模型");
  246. expect(harness.createTaskCalls).toHaveLength(0);
  247. expect(harness.sendPromptCalls).toHaveLength(0);
  248. expect(harness.readState().bots[actor.botId]?.draftOptions?.modelSelection).toEqual(
  249. original,
  250. );
  251. }
  252. } finally {
  253. harness.service.disposeAll();
  254. }
  255. });
  256. }
  257. });