|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import z from "zod" |
| 3 | +import { Bus } from "../../src/bus" |
| 4 | +import { BusEvent } from "../../src/bus/bus-event" |
| 5 | +import { Instance } from "../../src/project/instance" |
| 6 | +import { tmpdir } from "../fixture/fixture" |
| 7 | + |
| 8 | +const TestEvent = BusEvent.define("__test_bus_pub_sub", z.object({ value: z.string() })) |
| 9 | +const OtherEvent = BusEvent.define("__test_bus_other_type", z.object({ n: z.number() })) |
| 10 | + |
| 11 | +describe("Bus: publish and subscribe", () => { |
| 12 | + test("subscriber receives published event", async () => { |
| 13 | + await using tmp = await tmpdir({ git: true }) |
| 14 | + await Instance.provide({ |
| 15 | + directory: tmp.path, |
| 16 | + fn: async () => { |
| 17 | + const received: any[] = [] |
| 18 | + const unsub = Bus.subscribe(TestEvent, (e) => received.push(e)) |
| 19 | + await Bus.publish(TestEvent, { value: "hello" }) |
| 20 | + expect(received).toHaveLength(1) |
| 21 | + expect(received[0].properties.value).toBe("hello") |
| 22 | + unsub() |
| 23 | + }, |
| 24 | + }) |
| 25 | + }) |
| 26 | + |
| 27 | + test("unsubscribe stops receiving events", async () => { |
| 28 | + await using tmp = await tmpdir({ git: true }) |
| 29 | + await Instance.provide({ |
| 30 | + directory: tmp.path, |
| 31 | + fn: async () => { |
| 32 | + const received: any[] = [] |
| 33 | + const unsub = Bus.subscribe(TestEvent, (e) => received.push(e)) |
| 34 | + await Bus.publish(TestEvent, { value: "first" }) |
| 35 | + unsub() |
| 36 | + await Bus.publish(TestEvent, { value: "second" }) |
| 37 | + expect(received).toHaveLength(1) |
| 38 | + }, |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + test("multiple subscribers receive the same event", async () => { |
| 43 | + await using tmp = await tmpdir({ git: true }) |
| 44 | + await Instance.provide({ |
| 45 | + directory: tmp.path, |
| 46 | + fn: async () => { |
| 47 | + const a: any[] = [] |
| 48 | + const b: any[] = [] |
| 49 | + const unsub1 = Bus.subscribe(TestEvent, (e) => a.push(e)) |
| 50 | + const unsub2 = Bus.subscribe(TestEvent, (e) => b.push(e)) |
| 51 | + await Bus.publish(TestEvent, { value: "shared" }) |
| 52 | + expect(a).toHaveLength(1) |
| 53 | + expect(b).toHaveLength(1) |
| 54 | + unsub1() |
| 55 | + unsub2() |
| 56 | + }, |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + test("subscriber only receives matching event type", async () => { |
| 61 | + await using tmp = await tmpdir({ git: true }) |
| 62 | + await Instance.provide({ |
| 63 | + directory: tmp.path, |
| 64 | + fn: async () => { |
| 65 | + const received: any[] = [] |
| 66 | + const unsub = Bus.subscribe(TestEvent, (e) => received.push(e)) |
| 67 | + await Bus.publish(OtherEvent, { n: 42 }) |
| 68 | + expect(received).toHaveLength(0) |
| 69 | + unsub() |
| 70 | + }, |
| 71 | + }) |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe("Bus: subscribeAll wildcard", () => { |
| 76 | + test("wildcard subscriber receives all event types", async () => { |
| 77 | + await using tmp = await tmpdir({ git: true }) |
| 78 | + await Instance.provide({ |
| 79 | + directory: tmp.path, |
| 80 | + fn: async () => { |
| 81 | + const received: any[] = [] |
| 82 | + const unsub = Bus.subscribeAll((e) => received.push(e)) |
| 83 | + await Bus.publish(TestEvent, { value: "a" }) |
| 84 | + await Bus.publish(OtherEvent, { n: 1 }) |
| 85 | + expect(received).toHaveLength(2) |
| 86 | + unsub() |
| 87 | + }, |
| 88 | + }) |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +describe("Bus: once", () => { |
| 93 | + test("once unsubscribes after callback returns 'done'", async () => { |
| 94 | + await using tmp = await tmpdir({ git: true }) |
| 95 | + await Instance.provide({ |
| 96 | + directory: tmp.path, |
| 97 | + fn: async () => { |
| 98 | + let count = 0 |
| 99 | + Bus.once(TestEvent, () => { |
| 100 | + count++ |
| 101 | + return "done" |
| 102 | + }) |
| 103 | + await Bus.publish(TestEvent, { value: "first" }) |
| 104 | + await Bus.publish(TestEvent, { value: "second" }) |
| 105 | + expect(count).toBe(1) |
| 106 | + }, |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + test("once continues if callback returns undefined", async () => { |
| 111 | + await using tmp = await tmpdir({ git: true }) |
| 112 | + await Instance.provide({ |
| 113 | + directory: tmp.path, |
| 114 | + fn: async () => { |
| 115 | + let count = 0 |
| 116 | + Bus.once(TestEvent, () => { |
| 117 | + count++ |
| 118 | + return count >= 2 ? "done" : undefined |
| 119 | + }) |
| 120 | + await Bus.publish(TestEvent, { value: "1" }) |
| 121 | + await Bus.publish(TestEvent, { value: "2" }) |
| 122 | + await Bus.publish(TestEvent, { value: "3" }) |
| 123 | + expect(count).toBe(2) |
| 124 | + }, |
| 125 | + }) |
| 126 | + }) |
| 127 | +}) |
0 commit comments