permission-full-access.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. import { SQLITE_MIGRATIONS } from "../src/storage/session-store/migrations.js";
  6. function fixture(mode: "guarded" | "yolo" = "yolo") {
  7. const db = new DatabaseSync(":memory:");
  8. db.exec("PRAGMA foreign_keys = ON");
  9. for (const id of ["0001_base_session_store", "0016_session_input_ledger"])
  10. db.exec(SQLITE_MIGRATIONS.find((migration) => migration.id === id)!.sql);
  11. for (const id of ["s", "other"])
  12. db.prepare(
  13. "insert into session (id, project_id, slug, directory, title, version, time_created, time_updated) values (?, 'project', 'test', '/workspace', 'test', '1', 0, 0)",
  14. ).run(id);
  15. const original = {
  16. text: "hello",
  17. intent: { mode: "edit", planEnabled: true },
  18. conversationInputIntent: {
  19. mode: "edit",
  20. planEnabled: true,
  21. attachments: ["a"],
  22. order: { admissionSeq: 7 },
  23. },
  24. };
  25. for (const [id, session, status] of [
  26. ["a", "s", "admitted"],
  27. ["other", "other", "admitted"],
  28. ["done", "s", "promoted"],
  29. ]) {
  30. db.prepare(
  31. "insert into session_input (id, session_id, payload, status, kind, delivery, admitted_sequence, time_created, time_updated) values (?, ?, ?, ?, 'sendText', 'queue', 1, 0, 0)",
  32. ).run(id!, session!, JSON.stringify(original), status!);
  33. }
  34. const entry = (id: string, data: unknown) => ({
  35. id,
  36. sessionID: "s" as SessionId,
  37. type: "runtime/execution_state",
  38. touchSession: false,
  39. time: { created: 1, updated: 1 },
  40. data,
  41. });
  42. const input = {
  43. sessionID: "s" as SessionId,
  44. queueItemIds: ["a"],
  45. execution: entry("execution", { mode, planEnabled: true }),
  46. receipt: {
  47. ...entry("receipt", { interactionId: "p" }),
  48. type: "runtime/permission_full_access",
  49. },
  50. };
  51. const read = (id: string) =>
  52. JSON.parse(
  53. db.prepare("select payload from session_input where id = ?").get(id)!.payload as string,
  54. );
  55. return { db, input, read, original };
  56. }
  57. describe("完全访问 SQLite 原子提交", () => {
  58. it("真实外键拒绝缺少 session 父行的授权且不留下部分记录", async () => {
  59. const { db, input } = fixture();
  60. const sessionID = "missing-draft" as SessionId;
  61. try {
  62. await expect(
  63. commitPermissionFullAccess(db, {
  64. ...input,
  65. sessionID,
  66. queueItemIds: [],
  67. execution: { ...input.execution, sessionID },
  68. receipt: { ...input.receipt, sessionID },
  69. }),
  70. ).rejects.toThrow("FOREIGN KEY constraint failed");
  71. expect(db.prepare("select count(*) as n from session_entry").get()!.n).toBe(0);
  72. } finally {
  73. db.close();
  74. }
  75. });
  76. it.each(["guarded", "yolo"] as const)("只将固定队列改为 %s", async (mode) => {
  77. const { db, input, read, original } = fixture(mode);
  78. try {
  79. await commitPermissionFullAccess(db, input);
  80. expect(read("a")).toEqual({
  81. ...original,
  82. intent: { ...original.intent, mode },
  83. conversationInputIntent: { ...original.conversationInputIntent, mode },
  84. });
  85. expect(read("other")).toEqual(original);
  86. expect(read("done")).toEqual(original);
  87. await commitPermissionFullAccess(db, { ...input, queueItemIds: ["other"] });
  88. expect(read("other")).toEqual(original);
  89. } finally {
  90. db.close();
  91. }
  92. });
  93. it("最后的 receipt 保存失败时回滚 execution 和队列", async () => {
  94. const { db, input, read, original } = fixture();
  95. try {
  96. db.exec(
  97. "CREATE TRIGGER fail_receipt BEFORE INSERT ON session_entry WHEN NEW.id = 'receipt' BEGIN SELECT RAISE(ABORT, 'disk full'); END;",
  98. );
  99. await expect(commitPermissionFullAccess(db, input)).rejects.toThrow("disk full");
  100. expect(read("a")).toEqual(original);
  101. expect(db.prepare("select count(*) as n from session_entry").get()!.n).toBe(0);
  102. } finally {
  103. db.close();
  104. }
  105. });
  106. it("过期/跨任务队列项拒绝整个提交", async () => {
  107. const { db, input, read, original } = fixture();
  108. try {
  109. await expect(
  110. commitPermissionFullAccess(db, { ...input, queueItemIds: ["a", "other"] }),
  111. ).rejects.toThrow("unavailable");
  112. expect(read("a")).toEqual(original);
  113. const controller = new AbortController();
  114. controller.abort();
  115. await expect(
  116. commitPermissionFullAccess(db, { ...input, signal: controller.signal }),
  117. ).rejects.toThrow();
  118. } finally {
  119. db.close();
  120. }
  121. });
  122. });