permission-full-access-registry.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { describe, expect, it, vi } from "vitest";
  2. import { V4InteractionRegistry } from "../src/zcode-protocol-v4/interaction-registry.js";
  3. describe("完全访问唯一应答", () => {
  4. it("更新完成前不应答,重复调用共享提交,同 session 校验", async () => {
  5. const registry = new V4InteractionRegistry();
  6. let complete!: () => void;
  7. const operation = new Promise<void>((resolve) => {
  8. complete = resolve;
  9. });
  10. const fullAccess = vi.fn(() => operation);
  11. const reply = vi.fn();
  12. registry.register("p", reply, { sessionId: "s", kind: "other", fullAccess });
  13. await expect(registry.resolvePermissionMode("p", "other", "yolo")).rejects.toThrow(
  14. "not supported",
  15. );
  16. const first = registry.resolvePermissionMode("p", "s", "yolo");
  17. const duplicate = registry.resolvePermissionMode("p", "s", "guarded");
  18. expect(registry.resolve("p", { optionId: "deny" })).toBe(false);
  19. expect(reply).not.toHaveBeenCalled();
  20. complete();
  21. expect(await Promise.all([first, duplicate])).toEqual([true, false]);
  22. expect(fullAccess).toHaveBeenCalledExactlyOnceWith("yolo");
  23. expect(reply).toHaveBeenCalledExactlyOnceWith({ optionId: "allowOnce" });
  24. expect(await registry.resolvePermissionMode("p", "s", "yolo")).toBe(false);
  25. });
  26. it("保存失败保留审批,可重试;普通问答不允许伪造能力", async () => {
  27. const registry = new V4InteractionRegistry();
  28. const fullAccess = vi
  29. .fn()
  30. .mockRejectedValueOnce(new Error("disk full"))
  31. .mockResolvedValue(undefined);
  32. const reply = vi.fn();
  33. registry.register("p", reply, { sessionId: "s", kind: "other", fullAccess });
  34. await expect(registry.resolvePermissionMode("p", "s", "yolo")).rejects.toThrow("disk full");
  35. expect(reply).not.toHaveBeenCalled();
  36. expect(registry.has("p")).toBe(true);
  37. await registry.resolvePermissionMode("p", "s", "yolo");
  38. registry.register("question", vi.fn(), { sessionId: "s", kind: "other" });
  39. await expect(registry.resolvePermissionMode("question", "s", "yolo")).rejects.toThrow(
  40. "not supported",
  41. );
  42. });
  43. });