permission-full-access.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { DatabaseSync } from "node:sqlite";
  2. import { describe, expect, it } from "vitest";
  3. import { commitPermissionFullAccess } from "../src/storage/session-store/repositories/permission-full-access.js";
  4. import type { SessionId } from "@zcode/contracts";
  5. function fixture() {
  6. const db = new DatabaseSync(":memory:");
  7. db.exec(`
  8. CREATE TABLE session_entry (id TEXT PRIMARY KEY, session_id TEXT, type TEXT, time_created INTEGER, time_updated INTEGER, data TEXT);
  9. CREATE TABLE session_input (id TEXT PRIMARY KEY, session_id TEXT, payload TEXT, status TEXT, time_updated INTEGER);
  10. `);
  11. const original = {
  12. text: "hello",
  13. intent: { mode: "edit", planEnabled: true },
  14. conversationInputIntent: {
  15. mode: "edit",
  16. planEnabled: true,
  17. attachments: ["a"],
  18. order: { admissionSeq: 7 },
  19. },
  20. };
  21. for (const [id, session, status] of [
  22. ["a", "s", "admitted"],
  23. ["other", "other", "admitted"],
  24. ["done", "s", "promoted"],
  25. ]) {
  26. db.prepare("insert into session_input values (?, ?, ?, ?, 0)").run(
  27. id!,
  28. session!,
  29. JSON.stringify(original),
  30. status!,
  31. );
  32. }
  33. const entry = (id: string, data: unknown) => ({
  34. id,
  35. sessionID: "s" as SessionId,
  36. type: "runtime/execution_state",
  37. touchSession: false,
  38. time: { created: 1, updated: 1 },
  39. data,
  40. });
  41. const input = {
  42. sessionID: "s" as SessionId,
  43. queueItemIds: ["a"],
  44. execution: entry("execution", { mode: "yolo", planEnabled: true }),
  45. receipt: {
  46. ...entry("receipt", { interactionId: "p" }),
  47. type: "runtime/permission_full_access",
  48. },
  49. };
  50. const read = (id: string) =>
  51. JSON.parse(
  52. db.prepare("select payload from session_input where id = ?").get(id)!.payload as string,
  53. );
  54. return { db, input, read, original };
  55. }
  56. describe("完全访问 SQLite 原子提交", () => {
  57. it("仅指定任务和队列权限改变,重复 receipt 不扩大目标", async () => {
  58. const { db, input, read, original } = fixture();
  59. try {
  60. await commitPermissionFullAccess(db, input);
  61. expect(read("a")).toEqual({
  62. ...original,
  63. intent: { ...original.intent, mode: "yolo" },
  64. conversationInputIntent: { ...original.conversationInputIntent, mode: "yolo" },
  65. });
  66. expect(read("other")).toEqual(original);
  67. expect(read("done")).toEqual(original);
  68. await commitPermissionFullAccess(db, { ...input, queueItemIds: ["other"] });
  69. expect(read("other")).toEqual(original);
  70. } finally {
  71. db.close();
  72. }
  73. });
  74. it("最后的 receipt 保存失败时回滚 execution 和队列", async () => {
  75. const { db, input, read, original } = fixture();
  76. try {
  77. db.exec(
  78. "CREATE TRIGGER fail_receipt BEFORE INSERT ON session_entry WHEN NEW.id = 'receipt' BEGIN SELECT RAISE(ABORT, 'disk full'); END;",
  79. );
  80. await expect(commitPermissionFullAccess(db, input)).rejects.toThrow("disk full");
  81. expect(read("a")).toEqual(original);
  82. expect(db.prepare("select count(*) as n from session_entry").get()!.n).toBe(0);
  83. } finally {
  84. db.close();
  85. }
  86. });
  87. it("过期/跨任务队列项拒绝整个提交", async () => {
  88. const { db, input, read, original } = fixture();
  89. try {
  90. await expect(
  91. commitPermissionFullAccess(db, { ...input, queueItemIds: ["a", "other"] }),
  92. ).rejects.toThrow("unavailable");
  93. expect(read("a")).toEqual(original);
  94. const controller = new AbortController();
  95. controller.abort();
  96. await expect(
  97. commitPermissionFullAccess(db, { ...input, signal: controller.signal }),
  98. ).rejects.toThrow();
  99. } finally {
  100. db.close();
  101. }
  102. });
  103. });