mirror of
https://github.com/Heretek-AI/opencode.git
synced 2026-11-21 21:29:28 -10:00
+21

![opencode-agent[bot]](/assets/img/avatar_default.png)



![opencode-agent[bot]](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot]
Affan Ali
affanali2k3
Frank
opencode-agent[bot]
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <[email protected]> Co-authored-by: Kit Langton <[email protected]> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <[email protected]> Co-authored-by: affanali2k3 <[email protected]> Co-authored-by: Frank <[email protected]> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <[email protected]> Co-authored-by: Aiden Cline <[email protected]> Co-authored-by: Jay V <[email protected]> Co-authored-by: Dax Raad <[email protected]> Co-authored-by: Aarav Sareen <[email protected]> Co-authored-by: OpeOginni <[email protected]> Co-authored-by: Luke Parker <[email protected]> Co-authored-by: Ben Guthrie <[email protected]> Co-authored-by: Dax <[email protected]> Co-authored-by: Filip <[email protected]> Co-authored-by: Max Anderson <[email protected]> Co-authored-by: Brendan Allan <[email protected]> Co-authored-by: Jack <[email protected]> Co-authored-by: Shoubhit Dash <[email protected]> Co-authored-by: Dustin Deus <[email protected]> Co-authored-by: starptech <[email protected]> Co-authored-by: Aiden Cline <[email protected]> Co-authored-by: usrnk1 <[email protected]> Co-authored-by: Jay <[email protected]> Co-authored-by: runvip <[email protected]> Co-authored-by: opencode <[email protected]> Co-authored-by: Julian Coy <[email protected]> Co-authored-by: Vladimir Glafirov <[email protected]>
179 lines
7.1 KiB
TypeScript
179 lines
7.1 KiB
TypeScript
import fs from "fs/promises"
|
|
import path from "path"
|
|
import { describe, expect, test } from "bun:test"
|
|
import { Effect, Layer, Schema } from "effect"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { Location } from "@opencode-ai/core/location"
|
|
import { LocationMutation } from "@opencode-ai/core/location-mutation"
|
|
import { AbsolutePath } from "@opencode-ai/core/schema"
|
|
import { tmpdir } from "./fixture/tmpdir"
|
|
import { location } from "./fixture/location"
|
|
import { it } from "./lib/effect"
|
|
|
|
function provide(directory: string) {
|
|
return Effect.provide(
|
|
LayerNode.compile(LocationMutation.node, [
|
|
[
|
|
Location.node,
|
|
Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))),
|
|
],
|
|
]),
|
|
)
|
|
}
|
|
|
|
function withTmp<A, E, R>(f: (directory: string) => Effect.Effect<A, E, R>) {
|
|
return Effect.acquireRelease(
|
|
Effect.promise(() => tmpdir()),
|
|
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
|
|
).pipe(Effect.flatMap((tmp) => f(tmp.path)))
|
|
}
|
|
|
|
describe("LocationMutation", () => {
|
|
it.live("resolves an active relative existing file target", () =>
|
|
withTmp((directory) =>
|
|
Effect.gen(function* () {
|
|
const targetPath = path.join(directory, "hello.txt")
|
|
yield* Effect.promise(() => fs.writeFile(targetPath, "hello"))
|
|
const target = yield* (yield* LocationMutation.Service).resolve({ path: "hello.txt" })
|
|
|
|
expect(target).toMatchObject({
|
|
canonical: yield* Effect.promise(() => fs.realpath(targetPath)),
|
|
resource: "hello.txt",
|
|
})
|
|
expect(target.externalDirectory).toBeUndefined()
|
|
}).pipe(provide(directory)),
|
|
),
|
|
)
|
|
|
|
it.live("resolves an active relative prospective file target", () =>
|
|
withTmp((directory) =>
|
|
Effect.gen(function* () {
|
|
yield* Effect.promise(() => fs.mkdir(path.join(directory, "src")))
|
|
const target = yield* (yield* LocationMutation.Service).resolve({ path: path.join("src", "new.txt") })
|
|
const root = yield* Effect.promise(() => fs.realpath(directory))
|
|
|
|
expect(target).toMatchObject({
|
|
canonical: path.join(root, "src", "new.txt"),
|
|
resource: "src/new.txt",
|
|
})
|
|
}).pipe(provide(directory)),
|
|
),
|
|
)
|
|
|
|
it.live("rejects a relative lexical escape instead of promoting it to external authority", () =>
|
|
withTmp((directory) =>
|
|
Effect.gen(function* () {
|
|
const error = yield* Effect.flip((yield* LocationMutation.Service).resolve({ path: "../outside.txt" }))
|
|
expect(error).toMatchObject({ _tag: "LocationMutation.PathError", reason: "relative_escape" })
|
|
}).pipe(provide(directory)),
|
|
),
|
|
)
|
|
|
|
it.live("rejects a prospective target below an escaping symlink ancestor", () =>
|
|
withTmp((directory) => {
|
|
const outside = `${directory}-outside`
|
|
return Effect.gen(function* () {
|
|
if (process.platform === "win32") return
|
|
yield* Effect.promise(async () => {
|
|
await fs.mkdir(outside)
|
|
await fs.symlink(outside, path.join(directory, "escape"))
|
|
})
|
|
const error = yield* Effect.flip(
|
|
(yield* LocationMutation.Service).resolve({ path: path.join("escape", "new.txt") }),
|
|
)
|
|
expect(error).toMatchObject({ _tag: "LocationMutation.PathError", reason: "location_escape" })
|
|
yield* Effect.promise(() => fs.rm(outside, { recursive: true, force: true }))
|
|
}).pipe(provide(directory))
|
|
}),
|
|
)
|
|
|
|
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 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(target.externalDirectory).toBeUndefined()
|
|
}).pipe(provide(directory)),
|
|
),
|
|
)
|
|
|
|
it.live("requires external-directory authorization for an explicit external absolute target", () =>
|
|
withTmp((directory) =>
|
|
withTmp((outside) =>
|
|
Effect.gen(function* () {
|
|
const targetPath = path.join(outside, "new.txt")
|
|
const target = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
|
|
const root = yield* Effect.promise(() => fs.realpath(outside))
|
|
expect(target).toMatchObject({
|
|
canonical: path.join(root, "new.txt"),
|
|
resource: path.join(root, "new.txt").replaceAll("\\", "/"),
|
|
})
|
|
expect(target.externalDirectory).toMatchObject({
|
|
directory: root,
|
|
resource: path.join(root, "*").replaceAll("\\", "/"),
|
|
})
|
|
}).pipe(provide(directory)),
|
|
),
|
|
),
|
|
)
|
|
|
|
it.live("resolves an existing external file target", () =>
|
|
withTmp((directory) =>
|
|
withTmp((outside) =>
|
|
Effect.gen(function* () {
|
|
const targetPath = path.join(outside, "existing.txt")
|
|
yield* Effect.promise(() => fs.writeFile(targetPath, "existing"))
|
|
const target = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
|
|
const root = yield* Effect.promise(() => fs.realpath(outside))
|
|
expect(target).toMatchObject({ canonical: path.join(root, "existing.txt") })
|
|
expect(target.externalDirectory?.directory).toBe(root)
|
|
}).pipe(provide(directory)),
|
|
),
|
|
),
|
|
)
|
|
|
|
it.live("anchors prospective external descendants at their stable existing directory", () =>
|
|
withTmp((directory) =>
|
|
withTmp((outside) =>
|
|
Effect.gen(function* () {
|
|
const targetPath = path.join(outside, "new", "nested", "file.txt")
|
|
const target = yield* (yield* LocationMutation.Service).resolve({ path: targetPath })
|
|
const root = yield* Effect.promise(() => fs.realpath(outside))
|
|
expect(target.externalDirectory).toMatchObject({
|
|
directory: root,
|
|
resource: path.join(root, "*").replaceAll("\\", "/"),
|
|
})
|
|
}).pipe(provide(directory)),
|
|
),
|
|
),
|
|
)
|
|
|
|
test("ignores unknown mutation input fields", () => {
|
|
expect(Object.keys(LocationMutation.ResolveInput.fields)).toEqual(["path", "kind"])
|
|
expect(Schema.decodeUnknownSync(LocationMutation.ResolveInput)({ path: "README.md", reference: "docs" })).toEqual({
|
|
path: "README.md",
|
|
})
|
|
})
|
|
})
|