Skip to content

Commit a33cbc1

Browse files
committed
test(permission): fail approval backups without chmod
1 parent 49f43de commit a33cbc1

1 file changed

Lines changed: 18 additions & 29 deletions

File tree

‎src/permission/approval-store-migration.test.ts‎

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2-
import {
3-
chmod,
4-
mkdir,
5-
mkdtemp,
6-
readFile,
7-
rm,
8-
writeFile,
9-
} from "node:fs/promises";
2+
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
103
import { tmpdir } from "node:os";
11-
import { dirname, join } from "node:path";
4+
import { join } from "node:path";
125
import { installFileLogSink } from "../logging/sink.js";
136
import { generateSessionId, sessionDir } from "../session/index.js";
147
import { loadSeededApprovals } from "../session/runtime-assembly.js";
@@ -276,7 +269,7 @@ describe("migratePersistedApprovalStores", () => {
276269
expect(await readJson(projectStorePath())).toEqual({ approvals: [] });
277270
});
278271

279-
test("EACCES writing the backup leaves live bytes unchanged", async () => {
272+
test("a backup write failure leaves live bytes unchanged", async () => {
280273
const path = sessionStorePath();
281274
const original = {
282275
approvals: [
@@ -286,16 +279,12 @@ describe("migratePersistedApprovalStores", () => {
286279
};
287280
await writeFile(path, JSON.stringify(original));
288281
const liveBytes = await readFile(path, "utf-8");
289-
const dir = dirname(path);
290-
await chmod(dir, 0o555);
291-
try {
292-
const result = await migratePersistedApprovalStores(cwd, sessionId, home);
293-
expect(result.purged).toBe(0);
294-
expect(result.backups).toEqual([]);
295-
expect(await readFile(path, "utf-8")).toBe(liveBytes);
296-
} finally {
297-
await chmod(dir, 0o755);
298-
}
282+
await mkdir(`${backupPath(path)}.${process.pid}.tmp`);
283+
284+
const result = await migratePersistedApprovalStores(cwd, sessionId, home);
285+
expect(result.purged).toBe(0);
286+
expect(result.backups).toEqual([]);
287+
expect(await readFile(path, "utf-8")).toBe(liveBytes);
299288
});
300289

301290
test("per-file migration failures surface once as a single summary warn", async () => {
@@ -315,12 +304,10 @@ describe("migratePersistedApprovalStores", () => {
315304
approvals: [{ tool: "update_plan", pattern: "plan *" }],
316305
}),
317306
);
318-
// Read-only store dirs make the backup step fail, so both stores skip
319-
// untouched while the global path (absent) stays a clean no-op.
320-
const sessionStoreDir = dirname(sessionStorePath());
321-
const projectStoreDir = join(cwd, ".corbits");
322-
await chmod(sessionStoreDir, 0o555);
323-
await chmod(projectStoreDir, 0o555);
307+
// Directory entries at the deterministic temp paths make both backup
308+
// writes fail while the global path (absent) stays a clean no-op.
309+
await mkdir(`${backupPath(sessionStorePath())}.${process.pid}.tmp`);
310+
await mkdir(`${backupPath(projectStorePath())}.${process.pid}.tmp`);
324311
try {
325312
const result = await migratePersistedApprovalStores(cwd, sessionId, home);
326313
expect(result.purged).toBe(0);
@@ -333,7 +320,11 @@ describe("migratePersistedApprovalStores", () => {
333320

334321
const lines = (await readFile(join(logDir, "corbits.log"), "utf8"))
335322
.split("\n")
336-
.filter((line) => line.includes("Skipped approval-store migration"));
323+
.filter(
324+
(line) =>
325+
line.includes("Skipped approval-store migration") &&
326+
(line.includes(cwd) || line.includes(home)),
327+
);
337328
// One summary warn for the whole migration, not one per failing store.
338329
expect(lines).toHaveLength(1);
339330
const summary = lines[0];
@@ -342,8 +333,6 @@ describe("migratePersistedApprovalStores", () => {
342333
expect(summary).toContain(sessionStorePath());
343334
expect(summary).toContain(projectStorePath());
344335
} finally {
345-
await chmod(sessionStoreDir, 0o755);
346-
await chmod(projectStoreDir, 0o755);
347336
await rm(logDir, { recursive: true, force: true });
348337
}
349338
});

0 commit comments

Comments
 (0)