Skip to content

Commit cf7ab26

Browse files
test(file-lock): pin one lock key for withFileLock and safeWriteJson
Pairing test for review comment 4545130063: both doors must lock the canonical referent of a symlink alias. Removing the canonicalization from acquireFileLock makes both rows fail, so the test is the ask and not a mirror of the fix.
1 parent bf215bb commit cf7ab26

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Pairing test for the ask in review comment 4545130063 on pull 1913, "Use the same lock key as
2+
// `safeWriteJson`." withFileLock (the door a task-history deletion comes through) and safeWriteJson
3+
// share ONE advisory lock protocol inside acquireFileLock, so for a symlink alias both must lock the
4+
// canonical referent key. Take the canonicalization out of acquireFileLock and both callers fall back
5+
// to the caller's raw spelling: both rows below fail there, which is what makes this an answer to the
6+
// ask rather than a mirror of the fix.
7+
import * as fs from "fs/promises"
8+
import * as os from "os"
9+
import * as path from "path"
10+
import type { BigIntStats } from "fs"
11+
import * as lockfile from "proper-lockfile"
12+
import { safeWriteJson } from "../safeWriteJson"
13+
import { withFileLock } from "../fileLock"
14+
import { resolveLockKey } from "../../services/file-safety/safeWriteText"
15+
16+
vi.mock("proper-lockfile", () => ({
17+
lock: vi.fn(async () => async () => {}),
18+
unlock: vi.fn(async () => {}),
19+
}))
20+
21+
vi.mock("fs/promises", async () => {
22+
const actual = await vi.importActual<typeof import("fs/promises")>("fs/promises")
23+
return { ...actual, realpath: vi.fn(), lstat: vi.fn(), readlink: vi.fn() }
24+
})
25+
26+
const mockedRealpath = vi.mocked(fs.realpath)
27+
const mockedLstat = vi.mocked(fs.lstat)
28+
const mockedReadlink = vi.mocked(fs.readlink)
29+
const mockedLock = vi.mocked(lockfile.lock)
30+
31+
let dir = ""
32+
let referent = ""
33+
let alias = ""
34+
35+
beforeAll(async () => {
36+
dir = await fs.mkdtemp(path.join(os.tmpdir(), "lockkey-alignment-"))
37+
referent = path.join(dir, "task.json")
38+
alias = path.join(dir, "alias.json")
39+
await fs.writeFile(referent, "{}", "utf8")
40+
})
41+
42+
afterAll(async () => {
43+
if (dir) await fs.rm(dir, { recursive: true, force: true })
44+
})
45+
46+
beforeEach(() => {
47+
mockedRealpath.mockReset()
48+
mockedLstat.mockReset()
49+
mockedReadlink.mockReset()
50+
mockedLock.mockReset()
51+
// alias.json is a symlink to task.json; every other path resolves to itself.
52+
mockedRealpath.mockImplementation(async (target) => (String(target) === alias ? referent : String(target)))
53+
// fs/promises types lstat as returning BigIntStats and this double only ever needs isSymbolicLink().
54+
// AGENTS.md asks for a reason beside a cast a typed API cannot express: no row in this file reads a
55+
// bigint field, so no bigint field is faked.
56+
mockedLstat.mockImplementation(async () => ({ isSymbolicLink: () => false }) as BigIntStats)
57+
mockedReadlink.mockRejectedValue(Object.assign(new Error("not a link"), { code: "EINVAL" }))
58+
})
59+
60+
describe("lock-key alignment between withFileLock and safeWriteJson", () => {
61+
it("locks the canonical referent for an alias, whichever door the caller comes through", async () => {
62+
await withFileLock(alias, async () => undefined)
63+
const deletionKey = mockedLock.mock.calls[0][0]
64+
await safeWriteJson(alias, { pinned: 1 })
65+
const writeKeys = mockedLock.mock.calls.map((call) => call[0])
66+
const writeKey = writeKeys[writeKeys.length - 1]
67+
expect(deletionKey).toBe(referent)
68+
expect(writeKey).toBe(referent)
69+
expect(deletionKey).toBe(writeKey)
70+
expect(deletionKey).toBe(await resolveLockKey(alias))
71+
})
72+
73+
it("keeps one key for a plain path and hands the operation its own path", async () => {
74+
let seenByOperation = ""
75+
await withFileLock(referent, async (absoluteFilePath) => { seenByOperation = absoluteFilePath })
76+
const plainKey = mockedLock.mock.calls[0][0]
77+
await safeWriteJson(referent, { pinned: 2 })
78+
const plainWriteKeys = mockedLock.mock.calls.map((call) => call[0])
79+
expect(plainKey).toBe(plainWriteKeys[plainWriteKeys.length - 1])
80+
expect(plainKey).toBe(await resolveLockKey(referent))
81+
expect(seenByOperation).toBe(referent)
82+
})
83+
})

0 commit comments

Comments
 (0)