Skip to content

Commit 6430564

Browse files
committed
test(core): cover Effect Schema event definitions
Exercise the new SyncEvent and BusEvent Schema overloads end to end and use Schema.isSchema for the conversion check so the mixed-schema path stays explicit and covered.
1 parent 73d8099 commit 6430564

4 files changed

Lines changed: 67 additions & 6 deletions

File tree

packages/opencode/src/bus/bus-event.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@ export function define<Type extends string, P extends ZodType>(
2323
properties: P,
2424
): { type: Type; properties: P }
2525
export function define(type: string, properties: unknown) {
26-
const zodProperties = isEffectSchema(properties) ? zod(properties) : (properties as ZodType)
26+
const zodProperties = Schema.isSchema(properties) ? zod(properties) : (properties as ZodType)
2727
const result = { type, properties: zodProperties }
2828
registry.set(type, result as Definition)
2929
return result
3030
}
3131

32-
function isEffectSchema(value: unknown): value is Schema.Top {
33-
return typeof value === "object" && value !== null && "ast" in value
34-
}
35-
3632
export function payloads() {
3733
return registry
3834
.entries()

packages/opencode/src/sync/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function define<
117117
}
118118

119119
function toZodObject(value: ZodObject | Schema.Top): z.ZodObject {
120-
if (typeof value === "object" && value !== null && "ast" in value) {
120+
if (Schema.isSchema(value)) {
121121
return zod(value as Schema.Top) as unknown as z.ZodObject
122122
}
123123
return value as z.ZodObject

packages/opencode/test/bus/bus.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, describe, expect, test } from "bun:test"
2+
import { Schema } from "effect"
23
import z from "zod"
34
import { Bus } from "../../src/bus"
45
import { BusEvent } from "../../src/bus/bus-event"
@@ -10,6 +11,8 @@ const TestEvent = {
1011
Pong: BusEvent.define("test.pong", z.object({ message: z.string() })),
1112
}
1213

14+
const EffectTestEvent = BusEvent.define("test.effect-schema.ping", Schema.Struct({ value: Schema.Number }))
15+
1316
function withInstance(directory: string, fn: () => Promise<void>) {
1417
return Instance.provide({ directory, fn })
1518
}
@@ -76,6 +79,22 @@ describe("Bus", () => {
7679
await Bus.publish(TestEvent.Ping, { value: 1 })
7780
})
7881
})
82+
83+
test("accepts Effect Schema event definitions", async () => {
84+
await using tmp = await tmpdir()
85+
const received: number[] = []
86+
87+
await withInstance(tmp.path, async () => {
88+
Bus.subscribe(EffectTestEvent, (evt) => {
89+
received.push(evt.properties.value)
90+
})
91+
await Bun.sleep(10)
92+
await Bus.publish(EffectTestEvent, { value: 42 })
93+
await Bun.sleep(10)
94+
})
95+
96+
expect(received).toEqual([42])
97+
})
7998
})
8099

81100
describe("unsubscribe", () => {

packages/opencode/test/sync/index.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, test, expect, beforeEach, afterEach, afterAll } from "bun:test"
2+
import { Schema } from "effect"
23
import { tmpdir } from "../fixture/fixture"
34
import z from "zod"
45
import { Bus } from "../../src/bus"
@@ -128,6 +129,51 @@ describe("SyncEvent", () => {
128129
})
129130
}),
130131
)
132+
133+
test(
134+
"accepts Effect Schema event definitions",
135+
withInstance(async () => {
136+
SyncEvent.reset()
137+
try {
138+
const Created = SyncEvent.define({
139+
type: "item.effect.created",
140+
version: 1,
141+
aggregate: "id",
142+
schema: Schema.Struct({ id: Schema.String, name: Schema.String }),
143+
})
144+
145+
SyncEvent.init({
146+
projectors: [SyncEvent.project(Created, () => {})],
147+
})
148+
149+
const events: Array<{
150+
type: string
151+
properties: { id: string; name: string }
152+
}> = []
153+
const received = new Promise<void>((resolve) => {
154+
Bus.subscribeAll((event) => {
155+
events.push(event)
156+
resolve()
157+
})
158+
})
159+
160+
SyncEvent.run(Created, { id: "evt_1", name: "schema" })
161+
162+
await received
163+
expect(events).toEqual([
164+
{
165+
type: "item.effect.created",
166+
properties: {
167+
id: "evt_1",
168+
name: "schema",
169+
},
170+
},
171+
])
172+
} finally {
173+
setup()
174+
}
175+
}),
176+
)
131177
})
132178

133179
describe("replay", () => {

0 commit comments

Comments
 (0)