329 lines
13 KiB
TypeScript
329 lines
13 KiB
TypeScript
import fs from "fs/promises"
|
|
import path from "path"
|
|
import { fileURLToPath } from "url"
|
|
import { describe, expect, test } from "bun:test"
|
|
import { Effect, Layer } from "effect"
|
|
import { FileMutation } from "@opencode-ai/core/file-mutation"
|
|
import { FSUtil } from "@opencode-ai/core/fs-util"
|
|
import { Location } from "@opencode-ai/core/location"
|
|
import { LocationMutation } from "@opencode-ai/core/location-mutation"
|
|
import { PermissionV2 } from "@opencode-ai/core/permission"
|
|
import { AbsolutePath } from "@opencode-ai/core/schema"
|
|
import { SessionV2 } from "@opencode-ai/core/session"
|
|
import { ToolRegistry } from "@opencode-ai/core/tool/registry"
|
|
import { WriteTool } from "@opencode-ai/core/tool/write"
|
|
import { location } from "./fixture/location"
|
|
import { tmpdir } from "./fixture/tmpdir"
|
|
import { testEffect } from "./lib/effect"
|
|
|
|
const sessionID = SessionV2.ID.make("ses_write_tool_test")
|
|
const assertions: PermissionV2.AssertInput[] = []
|
|
const writes: string[] = []
|
|
let denyAction: string | undefined
|
|
let afterAssertion = (_input: PermissionV2.AssertInput): Effect.Effect<void> => Effect.void
|
|
|
|
const permission = Layer.succeed(
|
|
PermissionV2.Service,
|
|
PermissionV2.Service.of({
|
|
assert: (input) =>
|
|
Effect.sync(() => assertions.push(input)).pipe(
|
|
Effect.andThen(
|
|
input.action === denyAction
|
|
? Effect.fail(new PermissionV2.DeniedError({ rules: [] }))
|
|
: afterAssertion(input),
|
|
),
|
|
),
|
|
ask: () => Effect.die("unused"),
|
|
reply: () => Effect.die("unused"),
|
|
get: () => Effect.die("unused"),
|
|
forSession: () => Effect.die("unused"),
|
|
list: () => Effect.die("unused"),
|
|
}),
|
|
)
|
|
|
|
const reset = () => {
|
|
assertions.length = 0
|
|
writes.length = 0
|
|
denyAction = undefined
|
|
afterAssertion = () => Effect.void
|
|
}
|
|
|
|
const filesystem = Layer.effect(
|
|
FSUtil.Service,
|
|
Effect.gen(function* () {
|
|
const fs = yield* FSUtil.Service
|
|
return FSUtil.Service.of({
|
|
...fs,
|
|
writeWithDirs: (target, content, mode) =>
|
|
Effect.sync(() => writes.push(target)).pipe(Effect.andThen(fs.writeWithDirs(target, content, mode))),
|
|
})
|
|
}),
|
|
).pipe(Layer.provide(FSUtil.defaultLayer))
|
|
|
|
const withTool = <A, E, R>(directory: string, body: (registry: ToolRegistry.Interface) => Effect.Effect<A, E, R>) => {
|
|
const activeLocation = Layer.succeed(
|
|
Location.Service,
|
|
Location.Service.of(location({ directory: AbsolutePath.make(directory) })),
|
|
)
|
|
const planning = LocationMutation.layer.pipe(Layer.provide(filesystem), Layer.provide(activeLocation))
|
|
const commits = FileMutation.layer.pipe(Layer.provide(filesystem), Layer.provide(planning))
|
|
const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
|
|
const write = WriteTool.layer.pipe(Layer.provide(registry), Layer.provide(planning), Layer.provide(commits))
|
|
return Effect.gen(function* () {
|
|
return yield* body(yield* ToolRegistry.Service)
|
|
}).pipe(Effect.provide(Layer.mergeAll(registry, planning, commits, write)))
|
|
}
|
|
|
|
const call = (input: typeof WriteTool.Parameters.Type, id = "call-write") => ({
|
|
sessionID,
|
|
call: { type: "tool-call" as const, id, name: "write", input },
|
|
})
|
|
|
|
const it = testEffect(Layer.empty)
|
|
|
|
describe("WriteTool", () => {
|
|
it.live("registers and creates a relative file through FileMutation once", () =>
|
|
Effect.acquireUseRelease(
|
|
Effect.promise(() => tmpdir()),
|
|
(tmp) => {
|
|
reset()
|
|
return withTool(tmp.path, (registry) =>
|
|
Effect.gen(function* () {
|
|
expect((yield* registry.definitions()).map((tool) => tool.name)).toEqual(["write"])
|
|
const settled = yield* registry.settle(call({ path: "src/new.txt", content: "created" }))
|
|
expect(settled).toEqual({
|
|
result: { type: "text", value: "Created file successfully: src/new.txt" },
|
|
output: {
|
|
structured: {
|
|
operation: "write",
|
|
target: path.join(yield* Effect.promise(() => fs.realpath(tmp.path)), "src", "new.txt"),
|
|
resource: "src/new.txt",
|
|
existed: false,
|
|
},
|
|
content: [{ type: "text", text: "Created file successfully: src/new.txt" }],
|
|
},
|
|
})
|
|
expect(yield* Effect.promise(() => fs.readFile(path.join(tmp.path, "src", "new.txt"), "utf8"))).toBe(
|
|
"created",
|
|
)
|
|
expect(assertions).toEqual([{ sessionID, action: "edit", resources: ["src/new.txt"], save: ["*"] }])
|
|
expect(writes).toEqual([path.join(yield* Effect.promise(() => fs.realpath(tmp.path)), "src", "new.txt")])
|
|
}),
|
|
)
|
|
},
|
|
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
|
|
),
|
|
)
|
|
|
|
it.live("overwrites a relative existing file and reports that it wrote the file", () =>
|
|
Effect.acquireUseRelease(
|
|
Effect.promise(() => tmpdir()),
|
|
(tmp) => {
|
|
reset()
|
|
return Effect.promise(() => fs.writeFile(path.join(tmp.path, "existing.txt"), "before")).pipe(
|
|
Effect.andThen(
|
|
withTool(tmp.path, (registry) => registry.settle(call({ path: "existing.txt", content: "after" }))),
|
|
),
|
|
Effect.andThen((settled) =>
|
|
Effect.gen(function* () {
|
|
expect(settled.result).toEqual({ type: "text", value: "Wrote file successfully: existing.txt" })
|
|
expect(settled.output?.structured).toMatchObject({ resource: "existing.txt", existed: true })
|
|
expect(yield* Effect.promise(() => fs.readFile(path.join(tmp.path, "existing.txt"), "utf8"))).toBe(
|
|
"after",
|
|
)
|
|
expect(writes).toHaveLength(1)
|
|
}),
|
|
),
|
|
)
|
|
},
|
|
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
|
|
),
|
|
)
|
|
|
|
it.live("preserves exactly one BOM when overwriting existing files", () =>
|
|
Effect.acquireUseRelease(
|
|
Effect.promise(() => tmpdir()),
|
|
(tmp) => {
|
|
reset()
|
|
const preserved = path.join(tmp.path, "preserved.txt")
|
|
const deduplicated = path.join(tmp.path, "deduplicated.txt")
|
|
return Effect.promise(() =>
|
|
Promise.all([fs.writeFile(preserved, "\uFEFFbefore"), fs.writeFile(deduplicated, "\uFEFFbefore")]),
|
|
).pipe(
|
|
Effect.andThen(
|
|
withTool(tmp.path, (registry) =>
|
|
Effect.gen(function* () {
|
|
yield* registry.settle(call({ path: "preserved.txt", content: "after" }, "call-preserved"))
|
|
yield* registry.settle(call({ path: "deduplicated.txt", content: "\uFEFFafter" }, "call-deduplicated"))
|
|
|
|
expect(yield* Effect.promise(() => fs.readFile(preserved, "utf8"))).toBe("\uFEFFafter")
|
|
expect(yield* Effect.promise(() => fs.readFile(deduplicated, "utf8"))).toBe("\uFEFFafter")
|
|
}),
|
|
),
|
|
),
|
|
)
|
|
},
|
|
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
|
|
),
|
|
)
|
|
|
|
it.live("accepts an absolute file path inside the active Location", () =>
|
|
Effect.acquireUseRelease(
|
|
Effect.promise(() => tmpdir()),
|
|
(tmp) => {
|
|
reset()
|
|
const target = path.join(tmp.path, "absolute.txt")
|
|
return withTool(tmp.path, (registry) => registry.execute(call({ path: target, content: "inside" }))).pipe(
|
|
Effect.andThen((result) =>
|
|
Effect.gen(function* () {
|
|
expect(result).toEqual({ type: "text", value: "Created file successfully: absolute.txt" })
|
|
expect(assertions.map((input) => input.action)).toEqual(["edit"])
|
|
expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("inside")
|
|
}),
|
|
),
|
|
)
|
|
},
|
|
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
|
|
),
|
|
)
|
|
|
|
it.live("approves an explicit external absolute path before edit", () =>
|
|
Effect.acquireUseRelease(
|
|
Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
|
|
([active, outside]) => {
|
|
reset()
|
|
const target = path.join(outside.path, "external.txt")
|
|
return withTool(active.path, (registry) => registry.settle(call({ path: target, content: "external" }))).pipe(
|
|
Effect.andThen((settled) =>
|
|
Effect.gen(function* () {
|
|
const canonicalTarget = path.join(yield* Effect.promise(() => fs.realpath(outside.path)), "external.txt")
|
|
expect(assertions.map((input) => input.action)).toEqual(["external_directory", "edit"])
|
|
expect(assertions[0]).toMatchObject({
|
|
resources: [
|
|
path.join(yield* Effect.promise(() => fs.realpath(outside.path)), "*").replaceAll("\\", "/"),
|
|
],
|
|
})
|
|
expect(assertions[1]).toMatchObject({ resources: [canonicalTarget.replaceAll("\\", "/")], save: ["*"] })
|
|
expect(settled.output?.structured).toMatchObject({
|
|
target: canonicalTarget,
|
|
resource: canonicalTarget.replaceAll("\\", "/"),
|
|
existed: false,
|
|
})
|
|
expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("external")
|
|
expect(writes).toEqual([canonicalTarget])
|
|
}),
|
|
),
|
|
)
|
|
},
|
|
([active, outside]) =>
|
|
Effect.promise(() =>
|
|
Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
|
|
),
|
|
),
|
|
)
|
|
|
|
it.live("does not write when external_directory or edit approval is denied", () =>
|
|
Effect.acquireUseRelease(
|
|
Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
|
|
([active, outside]) =>
|
|
Effect.gen(function* () {
|
|
const external = path.join(outside.path, "denied.txt")
|
|
reset()
|
|
denyAction = "external_directory"
|
|
expect(
|
|
yield* withTool(active.path, (registry) => registry.execute(call({ path: external, content: "blocked" }))),
|
|
).toEqual({
|
|
type: "error",
|
|
value: `Unable to write ${external}`,
|
|
})
|
|
expect(assertions.map((input) => input.action)).toEqual(["external_directory"])
|
|
expect(writes).toEqual([])
|
|
|
|
reset()
|
|
denyAction = "edit"
|
|
expect(
|
|
yield* withTool(active.path, (registry) =>
|
|
registry.execute(call({ path: "denied.txt", content: "blocked" })),
|
|
),
|
|
).toEqual({
|
|
type: "error",
|
|
value: "Unable to write denied.txt",
|
|
})
|
|
expect(assertions.map((input) => input.action)).toEqual(["edit"])
|
|
expect(writes).toEqual([])
|
|
}),
|
|
([active, outside]) =>
|
|
Effect.promise(() =>
|
|
Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
|
|
),
|
|
),
|
|
)
|
|
|
|
if (process.platform !== "win32") {
|
|
it.live("delegates post-approval revalidation to FileMutation before writing", () =>
|
|
Effect.acquireUseRelease(
|
|
Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
|
|
([active, outside]) => {
|
|
reset()
|
|
const parent = path.join(active.path, "parent")
|
|
afterAssertion = (input) =>
|
|
input.action === "edit"
|
|
? Effect.promise(async () => {
|
|
await fs.rmdir(parent)
|
|
await fs.symlink(outside.path, parent)
|
|
})
|
|
: Effect.void
|
|
return Effect.promise(() => fs.mkdir(parent)).pipe(
|
|
Effect.andThen(
|
|
withTool(active.path, (registry) =>
|
|
registry.execute(call({ path: "parent/escape.txt", content: "blocked" })),
|
|
),
|
|
),
|
|
Effect.andThen((result) =>
|
|
Effect.gen(function* () {
|
|
expect(result).toEqual({ type: "error", value: "Unable to write parent/escape.txt" })
|
|
expect(assertions.map((input) => input.action)).toEqual(["edit"])
|
|
expect(writes).toEqual([])
|
|
expect(
|
|
yield* Effect.promise(() =>
|
|
fs.stat(path.join(outside.path, "escape.txt")).then(
|
|
() => true,
|
|
() => false,
|
|
),
|
|
),
|
|
).toBe(false)
|
|
}),
|
|
),
|
|
)
|
|
},
|
|
([active, outside]) =>
|
|
Effect.promise(() =>
|
|
Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
|
|
),
|
|
),
|
|
)
|
|
}
|
|
})
|
|
|
|
test("keeps the locked write schema, semantics docstring, and deferred UX TODOs visible", async () => {
|
|
const source = (await fs.readFile(new URL("../src/tool/write.ts", import.meta.url), "utf8")).replaceAll("\r\n", "\n")
|
|
const definition = await Effect.runPromise(
|
|
withTool(path.dirname(fileURLToPath(import.meta.url)), (registry) => registry.definitions()),
|
|
)
|
|
const schema = definition[0]?.inputSchema as { readonly properties?: Record<string, unknown> }
|
|
|
|
expect(Object.keys(schema.properties ?? {}).sort()).toEqual(["content", "path"])
|
|
expect(source).toContain(
|
|
"Named project references\n * are read-oriented and deliberately are not accepted by mutation tools.",
|
|
)
|
|
for (const todo of [
|
|
"Revisit whether model-facing mutation schemas should prefer absolute `filePath` naming for trained-in compatibility after evaluating model behavior.",
|
|
"Add formatter integration after V2 formatter runtime exists.",
|
|
"Publish watcher/file-edit events after V2 watcher integration exists.",
|
|
"Add snapshots / undo after design exists.",
|
|
"Add LSP notification and diagnostics after V2 LSP runtime exists.",
|
|
]) {
|
|
expect(source).toContain(`TODO: ${todo}`)
|
|
}
|
|
})
|