mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-01 18:10:29 +00:00
51 lines
1.8 KiB
TypeScript
51 lines
1.8 KiB
TypeScript
import { describe, expect } from "bun:test"
|
|
import { Config } from "@opencode/core/config"
|
|
import { Document, Info, type Entry } from "@opencode/schema/config"
|
|
import { Event } from "@opencode/schema/event"
|
|
import { Deferred, Effect, PubSub, Ref, Stream } from "effect"
|
|
import { ConfigEntryObserver } from "../../src/config/plugin/entry-observer"
|
|
import { it } from "../lib/effect"
|
|
import { host } from "../plugin/host"
|
|
|
|
describe("ConfigEntryObserver", () => {
|
|
it.effect("closes the startup race and reloads later updates", () =>
|
|
Effect.gen(function* () {
|
|
const current = yield* Ref.make([document("first")])
|
|
const updates = yield* PubSub.unbounded<ReturnType<typeof updated>>()
|
|
const reloaded = yield* Deferred.make<void>()
|
|
const config = Config.Service.of({
|
|
entries: () => Ref.get(current),
|
|
changes: () => Stream.empty,
|
|
})
|
|
const event = host({
|
|
event: {
|
|
subscribe: () =>
|
|
Stream.unwrap(Ref.set(current, [document("raced")]).pipe(Effect.as(Stream.fromPubSub(updates)))),
|
|
},
|
|
}).event
|
|
|
|
const loaded = yield* ConfigEntryObserver.observe(
|
|
config,
|
|
event,
|
|
Deferred.succeed(reloaded, undefined).pipe(Effect.asVoid),
|
|
)
|
|
|
|
expect(Config.latest(loaded.entries, "shell")).toBe("raced")
|
|
expect(yield* Deferred.isDone(reloaded)).toBe(false)
|
|
|
|
yield* Ref.set(current, [document("later")])
|
|
yield* PubSub.publish(updates, updated())
|
|
yield* Deferred.await(reloaded)
|
|
|
|
expect(Config.latest(loaded.entries, "shell")).toBe("later")
|
|
}),
|
|
)
|
|
})
|
|
|
|
function document(shell: string): Entry {
|
|
return new Document({ type: "document", info: new Info({ shell }) })
|
|
}
|
|
|
|
function updated() {
|
|
return { id: Event.ID.create(), created: Date.now(), type: "config.updated" as const, data: {} }
|
|
}
|