group-permission-isolation.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { ensureGroupTaskPermissionScope } from "../src/runtime/methods/input-intent-persistence.js";
  2. import { describe, expect, it, vi } from "vitest";
  3. import {
  4. loadProjectPermissionRuleset,
  5. persistProjectPermissionUpdates,
  6. } from "../src/tool/executor/permission-rules-persistence.js";
  7. describe("group task permission isolation", () => {
  8. it("reads and writes session-scoped grants without inheriting workspace approval", async () => {
  9. const sessionStore = {
  10. getSession: vi.fn(async () => ({
  11. id: "s",
  12. projectID: "project",
  13. permission: { version: 1, scope: "session" },
  14. })),
  15. getProjectPermission: vi.fn(async () => ({ version: 1, allow: [{ toolName: "Bash" }] })),
  16. saveProjectPermission: vi.fn(),
  17. updateSession: vi.fn(async () => undefined),
  18. };
  19. const deps = { sessionId: "s", sessionStore } as never;
  20. expect(await loadProjectPermissionRuleset(deps)).toEqual({ version: 1, scope: "session" });
  21. await persistProjectPermissionUpdates(
  22. deps,
  23. [{ type: "addRules", behavior: "allow", rules: [{ toolName: "Read" }] }],
  24. {} as never,
  25. );
  26. expect(sessionStore.getProjectPermission).not.toHaveBeenCalled();
  27. expect(sessionStore.saveProjectPermission).not.toHaveBeenCalled();
  28. expect(sessionStore.updateSession).toHaveBeenCalledWith(
  29. expect.objectContaining({
  30. id: "s",
  31. permission: expect.objectContaining({ scope: "session", allow: [{ toolName: "Read" }] }),
  32. }),
  33. );
  34. });
  35. it("initializes a group task with approval and fails closed without persistence", async () => {
  36. const intent = { botGroupSource: { botId: "bot", chatId: "group" } } as never;
  37. await expect(ensureGroupTaskPermissionScope(undefined, "s", intent)).rejects.toThrow("persist");
  38. const session = { permission: { mode: "yolo", allow: [{ toolName: "Bash" }] } };
  39. const store = {
  40. getSession: vi.fn(async () => session),
  41. updateSession: vi.fn(async () => undefined),
  42. };
  43. await ensureGroupTaskPermissionScope(store as never, "s", intent);
  44. expect(store.updateSession).toHaveBeenCalledWith({
  45. id: "s",
  46. permission: { version: 1, scope: "session", mode: "build" },
  47. });
  48. store.getSession.mockResolvedValueOnce({
  49. permission: { version: 1, scope: "session", mode: "yolo" },
  50. } as never);
  51. await ensureGroupTaskPermissionScope(store as never, "s", intent);
  52. expect(store.updateSession).toHaveBeenCalledOnce();
  53. });
  54. });