refactor(core): simplify filesystem mutation protocol (#31059)

This commit is contained in:
Kit Langton 2026-06-05 23:08:23 -04:00 committed by GitHub
commit ceccde7e84
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 356 additions and 623 deletions

View file

@ -36,17 +36,13 @@ describe("LocationMutation", () => {
Effect.gen(function* () {
const targetPath = path.join(directory, "hello.txt")
yield* Effect.promise(() => fs.writeFile(targetPath, "hello"))
const plan = yield* (yield* LocationMutation.Service).resolve({ path: "hello.txt" })
const target = yield* (yield* LocationMutation.Service).resolve({ path: "hello.txt" })
expect(plan.target).toMatchObject({
expect(target).toMatchObject({
canonical: yield* Effect.promise(() => fs.realpath(targetPath)),
exists: true,
resource: "hello.txt",
})
expect(plan.target.externalDirectory).toBeUndefined()
expect(yield* (yield* LocationMutation.Service).revalidate(plan)).toMatchObject({
canonical: plan.target.canonical,
})
expect(target.externalDirectory).toBeUndefined()
}).pipe(provide(directory)),
),
)
@ -55,18 +51,13 @@ describe("LocationMutation", () => {
withTmp((directory) =>
Effect.gen(function* () {
yield* Effect.promise(() => fs.mkdir(path.join(directory, "src")))
const plan = yield* (yield* LocationMutation.Service).resolve({ path: path.join("src", "new.txt") })
const target = yield* (yield* LocationMutation.Service).resolve({ path: path.join("src", "new.txt") })
const root = yield* Effect.promise(() => fs.realpath(directory))
expect(plan.target).toMatchObject({
expect(target).toMatchObject({
canonical: path.join(root, "src", "new.txt"),
exists: false,
resource: "src/new.txt",
})
expect(plan.authority.canonical).toBe(path.join(root, "src"))
expect(yield* (yield* LocationMutation.Service).revalidate(plan)).toMatchObject({
canonical: plan.target.canonical,
})
}).pipe(provide(directory)),
),
)
@ -98,16 +89,33 @@ describe("LocationMutation", () => {
}),
)
it.live("follows an in-location symlink using ordinary filesystem semantics", () =>
withTmp((directory) =>
Effect.gen(function* () {
if (process.platform === "win32") return
yield* Effect.promise(async () => {
await fs.mkdir(path.join(directory, "actual"))
await fs.symlink(path.join(directory, "actual"), path.join(directory, "linked"))
})
expect(yield* (yield* LocationMutation.Service).resolve({ path: "linked/new.txt" })).toMatchObject({
canonical: path.join(yield* Effect.promise(() => fs.realpath(directory)), "actual", "new.txt"),
resource: "actual/new.txt",
})
}).pipe(provide(directory)),
),
)
it.live("accepts an explicit absolute in-location target without external approval", () =>
withTmp((directory) =>
Effect.gen(function* () {
const targetPath = path.join(directory, "new.txt")
const plan = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
expect(plan.target).toMatchObject({
const target = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
expect(target).toMatchObject({
canonical: path.join(yield* Effect.promise(() => fs.realpath(directory)), "new.txt"),
resource: "new.txt",
})
expect(plan.target.externalDirectory).toBeUndefined()
expect(target.externalDirectory).toBeUndefined()
}).pipe(provide(directory)),
),
)
@ -117,13 +125,13 @@ describe("LocationMutation", () => {
withTmp((outside) =>
Effect.gen(function* () {
const targetPath = path.join(outside, "new.txt")
const plan = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
const target = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
const root = yield* Effect.promise(() => fs.realpath(outside))
expect(plan.target).toMatchObject({
expect(target).toMatchObject({
canonical: path.join(root, "new.txt"),
resource: path.join(root, "new.txt").replaceAll("\\", "/"),
})
expect(plan.target.externalDirectory).toMatchObject({
expect(target.externalDirectory).toMatchObject({
directory: root,
resource: path.join(root, "*").replaceAll("\\", "/"),
})
@ -138,11 +146,10 @@ describe("LocationMutation", () => {
Effect.gen(function* () {
const targetPath = path.join(outside, "existing.txt")
yield* Effect.promise(() => fs.writeFile(targetPath, "existing"))
const plan = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
const target = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
const root = yield* Effect.promise(() => fs.realpath(outside))
expect(plan.target).toMatchObject({ canonical: path.join(root, "existing.txt"), exists: true })
expect(plan.authority.canonical).toBe(path.join(root, "existing.txt"))
expect(plan.target.externalDirectory?.directory).toBe(root)
expect(target).toMatchObject({ canonical: path.join(root, "existing.txt") })
expect(target.externalDirectory?.directory).toBe(root)
}).pipe(provide(directory)),
),
),
@ -153,10 +160,9 @@ describe("LocationMutation", () => {
withTmp((outside) =>
Effect.gen(function* () {
const targetPath = path.join(outside, "new", "nested", "file.txt")
const plan = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
const target = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
const root = yield* Effect.promise(() => fs.realpath(outside))
expect(plan.authority.canonical).toBe(root)
expect(plan.target.externalDirectory).toMatchObject({
expect(target.externalDirectory).toMatchObject({
directory: root,
resource: path.join(root, "*").replaceAll("\\", "/"),
})
@ -165,66 +171,6 @@ describe("LocationMutation", () => {
),
)
it.live("rejects a symlink-ancestor swap during post-approval revalidation", () =>
withTmp((directory) =>
withTmp((outside) =>
Effect.gen(function* () {
if (process.platform === "win32") return
const parent = path.join(directory, "parent")
yield* Effect.promise(() => fs.mkdir(parent))
const service = yield* LocationMutation.Service
const plan = yield* service.resolve({ path: path.join("parent", "new.txt") })
yield* Effect.promise(async () => {
await fs.rmdir(parent)
await fs.symlink(outside, parent)
})
const error = yield* Effect.flip(service.revalidate(plan))
expect(error).toMatchObject({ _tag: "LocationMutation.RevalidationError" })
}).pipe(provide(directory)),
),
),
)
it.live("rejects an existing target identity swap during post-approval revalidation", () =>
withTmp((directory) =>
Effect.gen(function* () {
const targetPath = path.join(directory, "existing.txt")
yield* Effect.promise(() => fs.writeFile(targetPath, "first"))
const service = yield* LocationMutation.Service
const plan = yield* service.resolve({ path: "existing.txt" })
yield* Effect.promise(async () => {
const replacementPath = path.join(directory, "replacement.txt")
await fs.writeFile(replacementPath, "second")
await fs.rm(targetPath)
await fs.rename(replacementPath, targetPath)
})
const error = yield* Effect.flip(service.revalidate(plan))
expect(error).toMatchObject({
_tag: "LocationMutation.RevalidationError",
reason: "mutation authority changed",
})
}).pipe(provide(directory)),
),
)
it.live("rejects a nearer prospective ancestor introduced after approval", () =>
withTmp((directory) =>
Effect.gen(function* () {
const service = yield* LocationMutation.Service
const plan = yield* service.resolve({ path: path.join("new", "nested", "file.txt") })
yield* Effect.promise(() => fs.mkdir(path.join(directory, "new")))
const error = yield* Effect.flip(service.revalidate(plan))
expect(error).toMatchObject({
_tag: "LocationMutation.RevalidationError",
reason: "mutation authority changed",
})
}).pipe(provide(directory)),
),
)
test("keeps project references outside the mutation input API", () => {
expect(Object.keys(LocationMutation.ResolveInput.fields)).toEqual(["path", "kind"])
expect(Schema.decodeUnknownSync(LocationMutation.ResolveInput)({ path: "README.md", reference: "docs" })).toEqual({