|
1 | 1 | // Proxy capture SQLite store tests cover persisted capture reads and writes. |
2 | 2 | import fs from "node:fs"; |
3 | 3 | import path from "node:path"; |
| 4 | +import { constants } from "node:sqlite"; |
4 | 5 | import { afterEach, describe, expect, it, vi } from "vitest"; |
5 | 6 | import { cleanupTempDirs, makeTempDir } from "../../test/helpers/temp-dir.js"; |
6 | 7 | import { resolveSqliteDatabaseFilePaths } from "../infra/sqlite-files.js"; |
@@ -161,6 +162,66 @@ describe("DebugProxyCaptureStore", () => { |
161 | 162 | expect(lease.store.isClosed).toBe(true); |
162 | 163 | }); |
163 | 164 |
|
| 165 | + it.each(["deleteSessions", "purgeAll"] as const)( |
| 166 | + "rolls back path-based %s when session deletion fails", |
| 167 | + (operation) => { |
| 168 | + const root = makeTempDir(cleanupDirs, "openclaw-proxy-capture-rollback-"); |
| 169 | + const dbPath = path.join(root, "capture.sqlite"); |
| 170 | + const blobDir = path.join(root, "blobs"); |
| 171 | + const lease = acquireDebugProxyCaptureStore(dbPath, blobDir); |
| 172 | + const sessionId = "path-based-rollback-session"; |
| 173 | + |
| 174 | + try { |
| 175 | + lease.store.upsertSession({ |
| 176 | + id: sessionId, |
| 177 | + startedAt: 1, |
| 178 | + mode: "sdk", |
| 179 | + sourceScope: "openclaw", |
| 180 | + sourceProcess: "plugin", |
| 181 | + dbPath, |
| 182 | + blobDir, |
| 183 | + }); |
| 184 | + const blob = lease.store.persistPayload(Buffer.from("rollback payload"), "text/plain"); |
| 185 | + lease.store.recordEvent({ |
| 186 | + sessionId, |
| 187 | + ts: 2, |
| 188 | + sourceScope: "openclaw", |
| 189 | + sourceProcess: "plugin", |
| 190 | + protocol: "https", |
| 191 | + direction: "outbound", |
| 192 | + kind: "request", |
| 193 | + flowId: "path-based-rollback-flow", |
| 194 | + dataBlobId: blob.blobId, |
| 195 | + dataSha256: blob.sha256, |
| 196 | + }); |
| 197 | + |
| 198 | + const cleanup = () => |
| 199 | + operation === "deleteSessions" |
| 200 | + ? lease.store.deleteSessions([sessionId]) |
| 201 | + : lease.store.purgeAll(); |
| 202 | + lease.store.db.setAuthorizer((action, table) => |
| 203 | + action === constants.SQLITE_DELETE && table === "capture_sessions" |
| 204 | + ? constants.SQLITE_DENY |
| 205 | + : constants.SQLITE_OK, |
| 206 | + ); |
| 207 | + |
| 208 | + expect(cleanup).toThrow(/not authorized/u); |
| 209 | + lease.store.db.setAuthorizer(null); |
| 210 | + expect(lease.store.listSessions()).toHaveLength(1); |
| 211 | + expect(lease.store.getSessionEvents(sessionId)).toHaveLength(1); |
| 212 | + expect(fs.existsSync(blob.path)).toBe(true); |
| 213 | + |
| 214 | + expect(cleanup()).toEqual({ sessions: 1, events: 1, blobs: 1 }); |
| 215 | + expect(lease.store.listSessions()).toEqual([]); |
| 216 | + expect(lease.store.getSessionEvents(sessionId)).toEqual([]); |
| 217 | + expect(fs.existsSync(blob.path)).toBe(false); |
| 218 | + } finally { |
| 219 | + lease.store.db.setAuthorizer(null); |
| 220 | + lease.release(); |
| 221 | + } |
| 222 | + }, |
| 223 | + ); |
| 224 | + |
164 | 225 | it("uses rollback journaling for captures on NFS-backed volumes", () => { |
165 | 226 | vi.spyOn(fs, "statfsSync").mockReturnValue({ |
166 | 227 | type: 0x6969, |
|
0 commit comments