test(tool): migrate external directory tests to Effect runner (#27122)
This commit is contained in:
@@ -1,14 +1,16 @@
|
|||||||
import { describe, expect, test } from "bun:test"
|
import { describe, expect } from "bun:test"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { Effect } from "effect"
|
import { Effect } from "effect"
|
||||||
|
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
||||||
import type { Tool } from "@/tool/tool"
|
import type { Tool } from "@/tool/tool"
|
||||||
import { Instance } from "../../src/project/instance"
|
import { assertExternalDirectoryEffect } from "../../src/tool/external-directory"
|
||||||
import { WithInstance } from "../../src/project/with-instance"
|
|
||||||
import { assertExternalDirectory } from "../../src/tool/external-directory"
|
|
||||||
import { Filesystem } from "@/util/filesystem"
|
import { Filesystem } from "@/util/filesystem"
|
||||||
import { tmpdir } from "../fixture/fixture"
|
import { provideInstance, TestInstance, tmpdirScoped } from "../fixture/fixture"
|
||||||
import type { Permission } from "../../src/permission"
|
import type { Permission } from "../../src/permission"
|
||||||
import { SessionID, MessageID } from "../../src/session/schema"
|
import { SessionID, MessageID } from "../../src/session/schema"
|
||||||
|
import { testEffect } from "../lib/effect"
|
||||||
|
|
||||||
|
const it = testEffect(CrossSpawnSpawner.defaultLayer)
|
||||||
|
|
||||||
const baseCtx: Omit<Tool.Context, "ask"> = {
|
const baseCtx: Omit<Tool.Context, "ask"> = {
|
||||||
sessionID: SessionID.make("ses_test"),
|
sessionID: SessionID.make("ses_test"),
|
||||||
@@ -36,135 +38,116 @@ function makeCtx() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("tool.assertExternalDirectory", () => {
|
describe("tool.assertExternalDirectory", () => {
|
||||||
test("no-ops for empty target", async () => {
|
it.live("no-ops for empty target", () =>
|
||||||
const { requests, ctx } = makeCtx()
|
Effect.gen(function* () {
|
||||||
|
const { requests, ctx } = makeCtx()
|
||||||
|
|
||||||
await WithInstance.provide({
|
yield* assertExternalDirectoryEffect(ctx)
|
||||||
directory: "/tmp",
|
|
||||||
fn: async () => {
|
|
||||||
await assertExternalDirectory(ctx)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(requests.length).toBe(0)
|
expect(requests.length).toBe(0)
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("no-ops for paths inside Instance.directory", async () => {
|
it.live("no-ops for paths inside Instance.directory", () =>
|
||||||
const { requests, ctx } = makeCtx()
|
provideInstance("/tmp/project")(
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const { requests, ctx } = makeCtx()
|
||||||
|
|
||||||
await WithInstance.provide({
|
yield* assertExternalDirectoryEffect(ctx, path.join("/tmp/project", "file.txt"))
|
||||||
directory: "/tmp/project",
|
|
||||||
fn: async () => {
|
|
||||||
await assertExternalDirectory(ctx, path.join("/tmp/project", "file.txt"))
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(requests.length).toBe(0)
|
expect(requests.length).toBe(0)
|
||||||
})
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
test("asks with a single canonical glob", async () => {
|
it.live("asks with a single canonical glob", () =>
|
||||||
const { requests, ctx } = makeCtx()
|
Effect.gen(function* () {
|
||||||
|
const { requests, ctx } = makeCtx()
|
||||||
|
|
||||||
const directory = "/tmp/project"
|
const directory = "/tmp/project"
|
||||||
const target = "/tmp/outside/file.txt"
|
const target = "/tmp/outside/file.txt"
|
||||||
const expected = glob(path.join(path.dirname(target), "*"))
|
const expected = glob(path.join(path.dirname(target), "*"))
|
||||||
|
|
||||||
await WithInstance.provide({
|
yield* provideInstance(directory)(assertExternalDirectoryEffect(ctx, target))
|
||||||
directory,
|
|
||||||
fn: async () => {
|
|
||||||
await assertExternalDirectory(ctx, target)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const req = requests.find((r) => r.permission === "external_directory")
|
const req = requests.find((r) => r.permission === "external_directory")
|
||||||
expect(req).toBeDefined()
|
expect(req).toBeDefined()
|
||||||
expect(req!.patterns).toEqual([expected])
|
expect(req!.patterns).toEqual([expected])
|
||||||
expect(req!.always).toEqual([expected])
|
expect(req!.always).toEqual([expected])
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("uses target directory when kind=directory", async () => {
|
it.live("uses target directory when kind=directory", () =>
|
||||||
const { requests, ctx } = makeCtx()
|
Effect.gen(function* () {
|
||||||
|
const { requests, ctx } = makeCtx()
|
||||||
|
|
||||||
const directory = "/tmp/project"
|
const directory = "/tmp/project"
|
||||||
const target = "/tmp/outside"
|
const target = "/tmp/outside"
|
||||||
const expected = glob(path.join(target, "*"))
|
const expected = glob(path.join(target, "*"))
|
||||||
|
|
||||||
await WithInstance.provide({
|
yield* provideInstance(directory)(assertExternalDirectoryEffect(ctx, target, { kind: "directory" }))
|
||||||
directory,
|
|
||||||
fn: async () => {
|
|
||||||
await assertExternalDirectory(ctx, target, { kind: "directory" })
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const req = requests.find((r) => r.permission === "external_directory")
|
const req = requests.find((r) => r.permission === "external_directory")
|
||||||
expect(req).toBeDefined()
|
expect(req).toBeDefined()
|
||||||
expect(req!.patterns).toEqual([expected])
|
expect(req!.patterns).toEqual([expected])
|
||||||
expect(req!.always).toEqual([expected])
|
expect(req!.always).toEqual([expected])
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("skips prompting when bypass=true", async () => {
|
it.live("skips prompting when bypass=true", () =>
|
||||||
const { requests, ctx } = makeCtx()
|
provideInstance("/tmp/project")(
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const { requests, ctx } = makeCtx()
|
||||||
|
|
||||||
await WithInstance.provide({
|
yield* assertExternalDirectoryEffect(ctx, "/tmp/outside/file.txt", { bypass: true })
|
||||||
directory: "/tmp/project",
|
|
||||||
fn: async () => {
|
|
||||||
await assertExternalDirectory(ctx, "/tmp/outside/file.txt", { bypass: true })
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(requests.length).toBe(0)
|
expect(requests.length).toBe(0)
|
||||||
})
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
test("normalizes Windows path variants to one glob", async () => {
|
it.instance("normalizes Windows path variants to one glob", () =>
|
||||||
const { requests, ctx } = makeCtx()
|
Effect.gen(function* () {
|
||||||
|
const { requests, ctx } = makeCtx()
|
||||||
|
|
||||||
await using outerTmp = await tmpdir({
|
const outerTmp = yield* tmpdirScoped()
|
||||||
init: async (dir) => {
|
yield* Effect.promise(() => Bun.write(path.join(outerTmp, "outside.txt"), "x"))
|
||||||
await Bun.write(path.join(dir, "outside.txt"), "x")
|
|
||||||
},
|
|
||||||
})
|
|
||||||
await using tmp = await tmpdir({ git: true })
|
|
||||||
|
|
||||||
const target = path.join(outerTmp.path, "outside.txt")
|
const target = path.join(outerTmp, "outside.txt")
|
||||||
const alt = target
|
const alt = target
|
||||||
.replace(/^[A-Za-z]:/, "")
|
.replace(/^[A-Za-z]:/, "")
|
||||||
.replaceAll("\\", "/")
|
.replaceAll("\\", "/")
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
|
|
||||||
await WithInstance.provide({
|
yield* assertExternalDirectoryEffect(ctx, alt)
|
||||||
directory: tmp.path,
|
|
||||||
fn: async () => {
|
|
||||||
await assertExternalDirectory(ctx, alt)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const req = requests.find((r) => r.permission === "external_directory")
|
const req = requests.find((r) => r.permission === "external_directory")
|
||||||
const expected = glob(path.join(outerTmp.path, "*"))
|
const expected = glob(path.join(outerTmp, "*"))
|
||||||
expect(req).toBeDefined()
|
expect(req).toBeDefined()
|
||||||
expect(req!.patterns).toEqual([expected])
|
expect(req!.patterns).toEqual([expected])
|
||||||
expect(req!.always).toEqual([expected])
|
expect(req!.always).toEqual([expected])
|
||||||
})
|
}),
|
||||||
|
{ git: true },
|
||||||
|
)
|
||||||
|
|
||||||
test("uses drive root glob for root files", async () => {
|
it.instance("uses drive root glob for root files", () =>
|
||||||
const { requests, ctx } = makeCtx()
|
Effect.gen(function* () {
|
||||||
|
const { requests, ctx } = makeCtx()
|
||||||
|
|
||||||
await using tmp = await tmpdir({ git: true })
|
const tmp = yield* TestInstance
|
||||||
const root = path.parse(tmp.path).root
|
const root = path.parse(tmp.directory).root
|
||||||
const target = path.join(root, "boot.ini")
|
const target = path.join(root, "boot.ini")
|
||||||
|
|
||||||
await WithInstance.provide({
|
yield* assertExternalDirectoryEffect(ctx, target)
|
||||||
directory: tmp.path,
|
|
||||||
fn: async () => {
|
|
||||||
await assertExternalDirectory(ctx, target)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const req = requests.find((r) => r.permission === "external_directory")
|
const req = requests.find((r) => r.permission === "external_directory")
|
||||||
const expected = path.join(root, "*")
|
const expected = path.join(root, "*")
|
||||||
expect(req).toBeDefined()
|
expect(req).toBeDefined()
|
||||||
expect(req!.patterns).toEqual([expected])
|
expect(req!.patterns).toEqual([expected])
|
||||||
expect(req!.always).toEqual([expected])
|
expect(req!.always).toEqual([expected])
|
||||||
})
|
}),
|
||||||
|
{ git: true },
|
||||||
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user