botsService.permissionsWorkspace.test.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. async function bind(service: ReturnType<typeof f.createBotsService>) {
  10. const code = await service.createBindCode({ botId: "webhook-1" });
  11. await service.handleInboundMessage({ botId: "webhook-1", text: `/bind ${code.code}`, actor });
  12. }
  13. describe("botsService permissions and workspace", () => {
  14. it("filters workspace selection by allowed workspace key", async () => {
  15. const repo = f.createMemoryRepo({
  16. ...f.baseConfig,
  17. bots: [
  18. {
  19. ...f.baseConfig.bots[0]!,
  20. providerUserId: "user-1",
  21. allowedWorkspaces: ["ssh://host/tmp/workspace"],
  22. },
  23. ],
  24. });
  25. const service = f.createBotsService({
  26. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  27. legacyTaskService: f.createLegacyTaskService(),
  28. settingService: f.createSettingService({
  29. lastWorkspaceSession: [
  30. { kind: "remote", workspacePath: "/tmp/workspace", workspaceIdentity: "ssh://host/tmp/workspace" },
  31. { kind: "local", workspacePath: "/tmp/other" },
  32. ],
  33. }),
  34. repo: repo as never,
  35. });
  36. const replies = await service.handleInboundMessage({ botId: "webhook-1", text: "/workspace", actor });
  37. expect(replies[0]?.selection?.options.map((option) => option.id)).toEqual(["ssh://host/tmp/workspace"]);
  38. service.disposeAll();
  39. });
  40. it("updates bot-state after /workspace", async () => {
  41. const repo = f.createMemoryRepo(f.baseConfig);
  42. const service = f.createBotsService({
  43. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  44. legacyTaskService: f.createLegacyTaskService(),
  45. repo: repo as never,
  46. });
  47. await bind(service);
  48. const replies = await service.handleInboundMessage({ botId: "webhook-1", text: "/workspace 1", actor });
  49. expect(replies[0]?.text).toContain("工作区: workspace");
  50. expect(replies[0]?.text).toContain("任务: 草稿");
  51. expect((await repo.readState()).bots["webhook-1"]).toMatchObject({
  52. workspacePath: "/tmp/workspace",
  53. workspaceIdentity: "ssh://host/tmp/workspace",
  54. workspaceId: "ssh://host/tmp/workspace",
  55. mode: "draft",
  56. activeTaskId: null,
  57. });
  58. service.disposeAll();
  59. });
  60. it("does not update disabled command options", async () => {
  61. const repo = f.createMemoryRepo({
  62. ...f.baseConfig,
  63. bots: [
  64. {
  65. ...f.baseConfig.bots[0]!,
  66. providerUserId: "user-1",
  67. allowedCommands: { ...f.defaultUserCommands, model: false },
  68. },
  69. ],
  70. });
  71. const service = f.createBotsService({
  72. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  73. legacyTaskService: f.createLegacyTaskService(),
  74. repo: repo as never,
  75. });
  76. const replies = await service.handleInboundMessage({ botId: "webhook-1", text: "/model gpt-5.4", actor });
  77. expect(replies[0]?.text).toContain("未启用");
  78. expect((await repo.readConfig()).bots[0]?.currentOptions.model).toBeUndefined();
  79. service.disposeAll();
  80. });
  81. });