botsService.status.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { describe, expect, it } from "vitest";
  2. import * as f from "./botsService.fixtures.js";
  3. const actor = {
  4. provider: "webhook" as const,
  5. botId: "webhook-1",
  6. providerUserId: "user-1",
  7. chatType: "private" as const,
  8. };
  9. describe("botsService status", () => {
  10. it("reports bot counts and runtime contexts", async () => {
  11. const repo = f.createMemoryRepo(f.baseConfig);
  12. const service = f.createBotsService({
  13. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  14. legacyTaskService: f.createLegacyTaskService(),
  15. repo: repo as never,
  16. });
  17. const code = await service.createBindCode({ botId: "webhook-1" });
  18. await service.handleInboundMessage({ botId: "webhook-1", text: `/bind ${code.code}`, actor });
  19. await service.handleInboundMessage({ botId: "webhook-1", text: "/new", actor });
  20. await expect(service.getStatus()).resolves.toMatchObject({
  21. botsCount: 1,
  22. enabledBotsCount: 1,
  23. contextsCount: 1,
  24. });
  25. await expect(service.getBotStates()).resolves.toMatchObject([
  26. {
  27. botId: "webhook-1",
  28. mode: "draft",
  29. activeTaskId: null,
  30. },
  31. ]);
  32. service.disposeAll();
  33. });
  34. it("returns user-facing /status for a bound bot", async () => {
  35. const repo = f.createMemoryRepo(f.baseConfig);
  36. const service = f.createBotsService({
  37. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  38. legacyTaskService: f.createLegacyTaskService(),
  39. repo: repo as never,
  40. });
  41. const code = await service.createBindCode({ botId: "webhook-1" });
  42. await service.handleInboundMessage({ botId: "webhook-1", text: `/bind ${code.code}`, actor });
  43. const replies = await service.handleInboundMessage({
  44. botId: "webhook-1",
  45. text: "/status",
  46. actor,
  47. });
  48. expect(replies[0]?.text).toContain("工作区");
  49. expect(replies[0]?.text).not.toContain("Provider:");
  50. expect(replies[0]?.text).toContain("模型: default");
  51. service.disposeAll();
  52. });
  53. it("returns English /status when the app locale is en-US", async () => {
  54. const repo = f.createMemoryRepo(f.baseConfig);
  55. const service = f.createBotsService({
  56. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  57. legacyTaskService: f.createLegacyTaskService(),
  58. settingService: f.createSettingService({
  59. locale: "en-US",
  60. lastWorkspaceSession: [
  61. {
  62. kind: "remote",
  63. workspacePath: "/tmp/workspace",
  64. workspaceIdentity: "ssh://host/tmp/workspace",
  65. },
  66. ],
  67. }),
  68. repo: repo as never,
  69. });
  70. const code = await service.createBindCode({ botId: "webhook-1" });
  71. await service.handleInboundMessage({ botId: "webhook-1", text: `/bind ${code.code}`, actor });
  72. const replies = await service.handleInboundMessage({
  73. botId: "webhook-1",
  74. text: "/status",
  75. actor,
  76. });
  77. expect(replies[0]?.text).toContain("Workspace: workspace");
  78. expect(replies[0]?.text).toContain("Model: default");
  79. expect(replies[0]?.text).toContain("Task: draft");
  80. service.disposeAll();
  81. });
  82. it("reports active task model from task/config options", async () => {
  83. const repo = f.createMemoryRepo(
  84. {
  85. ...f.baseConfig,
  86. bots: [
  87. {
  88. ...f.baseConfig.bots[0]!,
  89. providerUserId: "user-1",
  90. },
  91. ],
  92. },
  93. {
  94. version: 3,
  95. bots: {
  96. "webhook-1": {
  97. botId: "webhook-1",
  98. workspacePath: "/tmp/workspace",
  99. workspaceIdentity: "ssh://host/tmp/workspace",
  100. workspaceId: "ssh://host/tmp/workspace",
  101. mode: "task",
  102. activeTaskId: "task-1",
  103. updatedAt: Date.now(),
  104. },
  105. },
  106. },
  107. );
  108. const legacyTaskService = f.createLegacyTaskService();
  109. legacyTaskService.listTasks = async () => [
  110. {
  111. taskId: "task-1",
  112. title: "Active task",
  113. workspacePath: "/tmp/workspace",
  114. workspaceIdentity: "ssh://host/tmp/workspace",
  115. provider: "codex",
  116. model: "meta-model",
  117. status: "completed",
  118. traceId: "trace",
  119. createdAt: 1,
  120. updatedAt: 1,
  121. mode: "default",
  122. },
  123. ];
  124. legacyTaskService.getTaskConfigOptions = async () => [
  125. {
  126. id: "model",
  127. category: "model",
  128. type: "select",
  129. currentValue: "gpt-5.4",
  130. options: [{ value: "gpt-5.4", name: "GPT-5.4" }],
  131. },
  132. ];
  133. const service = f.createBotsService({
  134. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  135. legacyTaskService,
  136. repo: repo as never,
  137. });
  138. const replies = await service.handleInboundMessage({
  139. botId: "webhook-1",
  140. text: "/status",
  141. actor,
  142. });
  143. expect(replies[0]?.text).not.toContain("Provider:");
  144. expect(replies[0]?.text).toContain("模型: gpt-5.4");
  145. service.disposeAll();
  146. });
  147. it("renders custom provider display name in status model", async () => {
  148. const providerId = "672a331d-423f-4c83-ab40-93288d9655a6";
  149. const repo = f.createMemoryRepo(
  150. {
  151. ...f.baseConfig,
  152. bots: [
  153. {
  154. ...f.baseConfig.bots[0]!,
  155. providerUserId: "user-1",
  156. },
  157. ],
  158. },
  159. {
  160. version: 3,
  161. bots: {
  162. "webhook-1": {
  163. botId: "webhook-1",
  164. workspacePath: "/tmp/workspace",
  165. workspaceIdentity: "ssh://host/tmp/workspace",
  166. workspaceId: "ssh://host/tmp/workspace",
  167. mode: "task",
  168. activeTaskId: "task-1",
  169. updatedAt: Date.now(),
  170. },
  171. },
  172. },
  173. );
  174. const legacyTaskService = f.createLegacyTaskService();
  175. legacyTaskService.listTasks = async () => [
  176. {
  177. taskId: "task-1",
  178. title: "Active task",
  179. workspacePath: "/tmp/workspace",
  180. workspaceIdentity: "ssh://host/tmp/workspace",
  181. provider: "glm",
  182. model: `${providerId}/deepseek-v4-flash`,
  183. status: "completed",
  184. traceId: "trace",
  185. createdAt: 1,
  186. updatedAt: 1,
  187. mode: "yolo",
  188. },
  189. ];
  190. const service = f.createBotsService({
  191. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  192. legacyTaskService,
  193. modelSelectionService: f.createModelSelectionService([
  194. {
  195. id: providerId,
  196. name: "DeepSeek",
  197. endpoints: { baseURL: "https://example.com", paths: {} },
  198. apiKey: "sk-test",
  199. models: ["deepseek-v4-flash"],
  200. createdAt: 1,
  201. updatedAt: 1,
  202. },
  203. ]),
  204. repo: repo as never,
  205. });
  206. const replies = await service.handleInboundMessage({
  207. botId: "webhook-1",
  208. text: "/status",
  209. actor,
  210. });
  211. expect(replies[0]?.text).toContain("模型: DeepSeek/deepseek-v4-flash");
  212. expect(replies[0]?.text).not.toContain(`${providerId}/deepseek-v4-flash`);
  213. service.disposeAll();
  214. });
  215. });