Compare commits

..

1 Commits

Author SHA1 Message Date
James Long 0984208948 test(core): simplify file mutation layer wiring 2026-06-20 21:55:08 -04:00
4 changed files with 41 additions and 38 deletions
+3
View File
@@ -3,6 +3,7 @@ export * as FileMutation from "./file-mutation"
import { Context, Effect, Layer, Schema } from "effect"
import { dirname } from "path"
import { KeyedMutex } from "./effect/keyed-mutex"
import { LayerNode } from "./effect/layer-node"
import { FSUtil } from "./fs-util"
export interface Target {
@@ -171,6 +172,8 @@ export const layer = Layer.effect(
}),
)
export const node = LayerNode.make(layer, [FSUtil.node])
function splitBom(text: string) {
const stripped = text.replace(/^\uFEFF+/, "")
return { bom: stripped.length !== text.length, text: stripped }
+3
View File
@@ -2,6 +2,7 @@ export * as LocationMutation from "./location-mutation"
import path from "path"
import { Context, Effect, Layer, Schema } from "effect"
import { LayerNode } from "./effect/layer-node"
import { FSUtil } from "./fs-util"
import { Location } from "./location"
@@ -153,3 +154,5 @@ export const layer = Layer.effect(
)
export const locationLayer = layer
export const node = (location: LayerNode.Node<Location.Service>) => LayerNode.make(layer, [FSUtil.node, location])
+26 -20
View File
@@ -2,6 +2,7 @@ import fs from "fs/promises"
import path from "path"
import { describe, expect } from "bun:test"
import { Deferred, Effect, Fiber, Layer } from "effect"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { FileMutation } from "@opencode-ai/core/file-mutation"
import { FSUtil } from "@opencode-ai/core/fs-util"
import { Location } from "@opencode-ai/core/location"
@@ -11,14 +12,16 @@ import { location } from "./fixture/location"
import { tmpdir } from "./fixture/tmpdir"
import { it } from "./lib/effect"
function provide(directory: string, filesystem = FSUtil.defaultLayer) {
const activeLocation = Layer.succeed(
Location.Service,
Location.Service.of(location({ directory: AbsolutePath.make(directory) })),
function provide(directory: string, filesystem?: LayerNode.Replacement<FSUtil.Service>) {
const activeLocation = LayerNode.make(
Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))),
[],
)
return Effect.provide(
LayerNode.buildLayer(LayerNode.group([LocationMutation.node(activeLocation), FileMutation.node]), {
replacements: filesystem ? [filesystem] : [],
}),
)
const resolution = LocationMutation.layer.pipe(Layer.provide(filesystem), Layer.provide(activeLocation))
const mutation = FileMutation.layer.pipe(Layer.provide(filesystem))
return Effect.provide(Layer.mergeAll(resolution, mutation))
}
function withTmp<A, E, R>(f: (directory: string) => Effect.Effect<A, E, R>) {
@@ -347,17 +350,20 @@ describe("FileMutation", () => {
})
function instrumentWrites(run: <E>(write: Effect.Effect<void, E>, target: string) => Effect.Effect<void, E>) {
return Layer.effect(
FSUtil.Service,
Effect.gen(function* () {
const filesystem = yield* FSUtil.Service
return FSUtil.Service.of({
...filesystem,
writeWithDirs: (target, content, mode) => run(filesystem.writeWithDirs(target, content, mode), target),
writeFile: (target, content, options) => run(filesystem.writeFile(target, content, options), target),
writeFileString: (target, content, options) =>
run(filesystem.writeFileString(target, content, options), target),
})
}),
).pipe(Layer.provide(FSUtil.defaultLayer))
return LayerNode.replace(
FSUtil.node,
Layer.effect(
FSUtil.Service,
Effect.gen(function* () {
const filesystem = yield* FSUtil.Service
return FSUtil.Service.of({
...filesystem,
writeWithDirs: (target, content, mode) => run(filesystem.writeWithDirs(target, content, mode), target),
writeFile: (target, content, options) => run(filesystem.writeFile(target, content, options), target),
writeFileString: (target, content, options) =>
run(filesystem.writeFileString(target, content, options), target),
})
}),
).pipe(Layer.provide(LayerNode.buildLayer(FSUtil.node))),
)
}
+9 -18
View File
@@ -4,7 +4,6 @@ import fs from "fs/promises"
import path from "path"
import { ConfigProvider, Deferred, Duration, Effect, Fiber, Layer, Option, Stream } from "effect"
import { Config } from "@opencode-ai/core/config"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { EventV2 } from "@opencode-ai/core/event"
import { FSUtil } from "@opencode-ai/core/fs-util"
import { Watcher } from "@opencode-ai/core/filesystem/watcher"
@@ -19,8 +18,7 @@ const describeWatcher = Watcher.hasNativeBinding() && !process.env.CI ? describe
type WatcherEvent = { file: string; event: "add" | "change" | "unlink" }
const root = LayerNode.group([FSUtil.node, EventV2.node])
const it = testEffect(LayerNode.buildLayer(root))
const it = testEffect(Layer.mergeAll(FSUtil.defaultLayer, EventV2.defaultLayer))
const configLayer = Layer.succeed(
Config.Service,
@@ -28,7 +26,6 @@ const configLayer = Layer.succeed(
entries: () => Effect.succeed([]),
}),
)
const configNode = LayerNode.make(configLayer, [])
const flagsLayer = ConfigProvider.layer(
ConfigProvider.fromUnknown({
@@ -38,21 +35,15 @@ const flagsLayer = ConfigProvider.layer(
)
function provide(directory: string, vcs?: Location.Interface["vcs"]) {
const locationNode = LayerNode.make(
Layer.succeed(
Location.Service,
Location.Service.of(location({ directory: AbsolutePath.make(directory) }, { vcs })),
),
[],
const locationLayer = Layer.succeed(
Location.Service,
Location.Service.of(location({ directory: AbsolutePath.make(directory) }, { vcs })),
)
return Effect.provide(
LayerNode.buildLayer(
LayerNode.group([
LayerNode.make(Watcher.layer, [configNode, EventV2.node, FSUtil.node, Git.node, locationNode]),
root,
]),
{ replacements: [LayerNode.replace(configNode, configLayer)] },
).pipe(
Watcher.layer.pipe(
Layer.provide(configLayer),
Layer.provide(Git.defaultLayer),
Layer.provide(locationLayer),
Layer.provide(flagsLayer),
),
)
@@ -205,7 +196,7 @@ describeWatcher("Watcher", () => {
yield* noUpdate((event) => event.file === file, fs.writeFileString(file, "gone")).pipe(
Effect.provideService(EventV2.Service, events),
)
}).pipe(Effect.provide(LayerNode.buildLayer(root))),
}).pipe(Effect.provide(Layer.mergeAll(FSUtil.defaultLayer, EventV2.defaultLayer))),
)
it.live("ignores .git/index changes", () =>