fix(core): preserve typed config test-layer errors (#45778)

This commit is contained in:
Kit Langton
2026-08-27 22:13:34 -04:00
committed by GitHub
Unverified
parent 2389111bf6
commit 88988b8c12
2 changed files with 50 additions and 1 deletions
+4 -1
View File
@@ -84,7 +84,10 @@ export const testLayer = (initial: Entry[] = []) =>
const entry = current[index]
if (!entry || entry.type !== "document")
return yield* Effect.fail(new UpdateError({ message: "No editable config document found" }))
const info = produce(entry.info, update)
const info = yield* Effect.try({
try: () => produce(entry.info, update),
catch: (cause) => new UpdateError({ message: "Config update failed", cause }),
})
yield* Ref.set(entries, current.with(index, new Document({ type: "document", path: entry.path, info })))
return info
}),
+46
View File
@@ -97,6 +97,20 @@ describe("Config", () => {
Effect.andThen(
Effect.gen(function* () {
const config = yield* Config.Service
const content = yield* Effect.promise(() => fs.readFile(globalFile, "utf8"))
const cause = new Error("Rejected config update")
const error = yield* config
.update((draft) => {
draft.shell = "discarded"
throw cause
})
.pipe(Effect.flip)
expect(error).toBeInstanceOf(Config.UpdateError)
expect(error.message).toBe("Config update failed")
expect(error.cause).toBe(cause)
expect(yield* Effect.promise(() => fs.readFile(globalFile, "utf8"))).toBe(content)
const updated = yield* config.update((draft) => {
draft.shell = "updated"
})
@@ -338,6 +352,38 @@ describe("Config", () => {
}).pipe(Effect.provide(Config.testLayer())),
)
it.effect("keeps test config unchanged after an update callback fails", () =>
Effect.gen(function* () {
const config = yield* Config.Service
const test = yield* Config.Test
const entry = new Document({
type: "document",
path: AbsolutePath.make(path.join(import.meta.dir, "opencode.json")),
info: new Info({ shell: "initial" }),
})
yield* test.setEntries([entry])
const cause = new Error("Rejected config update")
const error = yield* config
.update((draft) => {
draft.shell = "discarded"
throw cause
})
.pipe(Effect.flip)
expect(error).toBeInstanceOf(Config.UpdateError)
expect(error.message).toBe("Config update failed")
expect(error.cause).toBe(cause)
expect(yield* config.entries()).toEqual([entry])
expect(entry.info.shell).toBe("initial")
const updated = yield* config.update((draft) => {
draft.shell = "recovered"
})
expect(updated.shell).toBe("recovered")
expect(Config.latest(yield* test.entries(), "shell")).toBe("recovered")
}).pipe(Effect.provide(Config.testLayer())),
)
it.effect("returns the latest defined scalar from priority-ordered documents", () =>
Effect.sync(() => {
const entries = [