Skip to content

Commit e6021d6

Browse files
test(file-lock): pin one lock key for withFileLock and safeWriteJson
Answers the ask in review comment 4545130063 on pull 1913 on this head. On 316abe2 the two callers already computed the canonical key for themselves, so the pairing rows pass with or without the fix; the third row, which hands the protocol the alias directly, fails at the pre-fix blob and fails again if the canonicalization is taken out of acquireFileLock, so the spec pins the protocol rather than mirroring the fix.
1 parent 9f9c33d commit e6021d6

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)