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

12345678910111213141516171819202122232425262728293031323334353637383940
  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.resolveFullAccess("p", "other")).rejects.toThrow("not supported");
  14. const first = registry.resolveFullAccess("p", "s");
  15. const duplicate = registry.resolveFullAccess("p", "s");
  16. expect(registry.resolve("p", { optionId: "deny" })).toBe(false);
  17. expect(reply).not.toHaveBeenCalled();
  18. complete();
  19. await Promise.all([first, duplicate]);
  20. expect(fullAccess).toHaveBeenCalledOnce();
  21. expect(reply).toHaveBeenCalledExactlyOnceWith({ optionId: "allowOnce" });
  22. expect(await registry.resolveFullAccess("p", "s")).toBe(false);
  23. });
  24. it("保存失败保留审批,可重试;普通问答不允许伪造能力", async () => {
  25. const registry = new V4InteractionRegistry();
  26. const fullAccess = vi
  27. .fn()
  28. .mockRejectedValueOnce(new Error("disk full"))
  29. .mockResolvedValue(undefined);
  30. const reply = vi.fn();
  31. registry.register("p", reply, { sessionId: "s", kind: "other", fullAccess });
  32. await expect(registry.resolveFullAccess("p", "s")).rejects.toThrow("disk full");
  33. expect(reply).not.toHaveBeenCalled();
  34. expect(registry.has("p")).toBe(true);
  35. await registry.resolveFullAccess("p", "s");
  36. registry.register("question", vi.fn(), { sessionId: "s", kind: "other" });
  37. await expect(registry.resolveFullAccess("question", "s")).rejects.toThrow("not supported");
  38. });
  39. });