botsService.permissionsWorkspace.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { describe, expect, it, vi } 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: ["remote:server:test-owner:/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. {
  31. kind: "remote",
  32. workspacePath: "/tmp/workspace",
  33. workspaceIdentity: "remote:server:test-owner:/tmp/workspace",
  34. },
  35. { kind: "local", workspacePath: "/tmp/other" },
  36. ],
  37. }),
  38. repo: repo as never,
  39. });
  40. const replies = await service.handleInboundMessage({
  41. botId: "webhook-1",
  42. text: "/workspace",
  43. actor,
  44. });
  45. expect(replies[0]?.selection?.options.map((option) => option.id)).toEqual([
  46. JSON.stringify(["remote:server:test-owner", "project-0"]),
  47. ]);
  48. service.disposeAll();
  49. });
  50. it("updates bot-state after /workspace", async () => {
  51. const repo = f.createMemoryRepo(f.baseConfig);
  52. const service = f.createBotsService({
  53. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  54. legacyTaskService: f.createLegacyTaskService(),
  55. repo: repo as never,
  56. });
  57. await bind(service);
  58. const replies = await service.handleInboundMessage({
  59. botId: "webhook-1",
  60. text: "/workspace 1",
  61. actor,
  62. });
  63. expect(replies[0]?.text).toContain("工作区: workspace");
  64. expect(replies[0]?.text).toContain("任务: 草稿");
  65. expect((await repo.readState()).bots["webhook-1"]).toMatchObject({
  66. workspacePath: "/tmp/workspace",
  67. workspaceIdentity: "remote:server:test-owner:/tmp/workspace",
  68. workspaceId: JSON.stringify(["remote:server:test-owner", "project-0"]),
  69. mode: "draft",
  70. activeTaskId: null,
  71. });
  72. service.disposeAll();
  73. });
  74. it("does not update disabled command options", async () => {
  75. const repo = f.createMemoryRepo({
  76. ...f.baseConfig,
  77. bots: [
  78. {
  79. ...f.baseConfig.bots[0]!,
  80. providerUserId: "user-1",
  81. allowedCommands: { ...f.defaultUserCommands, model: false },
  82. },
  83. ],
  84. });
  85. const service = f.createBotsService({
  86. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  87. legacyTaskService: f.createLegacyTaskService(),
  88. repo: repo as never,
  89. });
  90. const replies = await service.handleInboundMessage({
  91. botId: "webhook-1",
  92. text: "/model gpt-5.4",
  93. actor,
  94. });
  95. expect(replies[0]?.text).toContain("未启用");
  96. expect((await repo.readConfig()).bots[0]?.currentOptions.model).toBeUndefined();
  97. service.disposeAll();
  98. });
  99. });
  100. describe("Bot R3 workspace identity projection", () => {
  101. it("normalizes legacy context and exact authorization to the same stable Source without path fallback", async () => {
  102. const legacy = "remote:ssh:source.test:22:dev:/work/project";
  103. const stable = "remote:server:source-a:/work/project";
  104. const other = "remote:server:source-b:/work/project";
  105. const repo = f.createMemoryRepo(
  106. {
  107. ...f.baseConfig,
  108. bots: [{ ...f.baseConfig.bots[0]!, providerUserId: "user-1", allowedWorkspaces: [legacy] }],
  109. },
  110. {
  111. version: 3,
  112. bots: {
  113. "webhook-1": {
  114. botId: "webhook-1",
  115. workspacePath: "/work/project",
  116. workspaceIdentity: legacy,
  117. workspaceId: legacy,
  118. mode: "task",
  119. activeTaskId: "old-task",
  120. updatedAt: 1,
  121. },
  122. },
  123. },
  124. );
  125. const resolver = vi.fn(async (target: { workspacePath: string; workspaceIdentity: string }) =>
  126. target.workspaceIdentity === legacy ? { ...target, workspaceIdentity: stable } : target,
  127. );
  128. const service = f.createBotsService({
  129. ...f.createBotProjectServices([
  130. { workspacePath: "/work/project", workspaceIdentity: stable },
  131. { workspacePath: "/work/project", workspaceIdentity: other },
  132. ]),
  133. credentialService: f.createCredentialService({ "secret-1": "secret" }),
  134. legacyTaskService: f.createLegacyTaskService(),
  135. repo: repo as never,
  136. settingService: f.createSettingService({
  137. lastWorkspaceSession: [
  138. { kind: "remote", workspacePath: "/work/project", workspaceIdentity: legacy },
  139. { kind: "remote", workspacePath: "/work/project", workspaceIdentity: other },
  140. ],
  141. }),
  142. remoteWorkspaceService: {
  143. resolveWorkspace: resolver,
  144. isConnected: async () => false,
  145. ensureConnected: async () => ({ ok: false }),
  146. },
  147. });
  148. const replies = await service.handleInboundMessage({
  149. botId: "webhook-1",
  150. text: "/workspace",
  151. actor,
  152. });
  153. expect(replies[0]?.selection?.options.map((option) => option.id)).toEqual([
  154. JSON.stringify(["remote:server:source-a", "project-0"]),
  155. ]);
  156. expect((await repo.readState()).bots["webhook-1"]).toMatchObject({
  157. workspaceIdentity: stable,
  158. workspaceId: JSON.stringify(["remote:server:source-a", "project-0"]),
  159. activeTaskId: "old-task",
  160. });
  161. expect((await repo.readConfig()).bots[0]!.allowedWorkspaces).toEqual([
  162. JSON.stringify(["remote:server:source-a", "project-0"]),
  163. ]);
  164. service.disposeAll();
  165. });
  166. it("reads fresh project membership without waiting for a workspace cache TTL", async () => {
  167. const stable = "remote:server:source-a:/work/project";
  168. const projects = f.createBotProjectServices([
  169. { workspacePath: "/work/project", workspaceIdentity: stable },
  170. ]);
  171. const readDirectory = projects.remoteProjectService.readDirectory;
  172. let ready = false;
  173. projects.remoteProjectService.readDirectory = vi.fn(async () =>
  174. ready ? readDirectory() : { hosts: [], entries: [] },
  175. );
  176. const service = f.createBotsService({
  177. ...projects,
  178. credentialService: f.createCredentialService(),
  179. legacyTaskService: f.createLegacyTaskService(),
  180. repo: f.createMemoryRepo(f.baseConfig) as never,
  181. });
  182. expect(await service.listWorkspaceRefs()).toEqual([]);
  183. ready = true;
  184. expect((await service.listWorkspaceRefs())[0]?.workspaceIdentity).toBe(stable);
  185. service.disposeAll();
  186. });
  187. it("deduplicates a current custom workspace id by workspace identity", async () => {
  188. const stable = "remote:server:source-a:/work/project";
  189. const service = f.createBotsService({
  190. ...f.createBotProjectServices([
  191. { workspacePath: "/work/project", workspaceIdentity: stable },
  192. ]),
  193. credentialService: f.createCredentialService(),
  194. legacyTaskService: f.createLegacyTaskService(),
  195. repo: f.createMemoryRepo(f.baseConfig) as never,
  196. settingService: f.createSettingService({
  197. lastWorkspaceSession: [
  198. { kind: "remote", workspacePath: "/work/project", workspaceIdentity: stable },
  199. ],
  200. }),
  201. remoteWorkspaceService: {
  202. resolveWorkspace: async (target) => target,
  203. isConnected: async () => false,
  204. ensureConnected: async () => ({ ok: false }),
  205. },
  206. });
  207. const refs = await service.listWorkspaceRefs({
  208. currentWorkspace: {
  209. id: "custom-id",
  210. label: "Custom",
  211. workspacePath: "/work/project",
  212. workspaceIdentity: stable,
  213. },
  214. });
  215. expect(refs.map((ref) => ref.id)).toEqual([
  216. JSON.stringify(["remote:server:source-a", "project-0"]),
  217. ]);
  218. service.disposeAll();
  219. });
  220. });