|
| 1 | +import { describe, expect } from "bun:test" |
| 2 | +import { Bus } from "@opencode-ai/core/bus" |
| 3 | +import { KV } from "@opencode-ai/core/kv" |
| 4 | +import { Group } from "@opencode-ai/core/persistent-pty" |
| 5 | +import { Pty } from "@opencode-ai/schema/pty" |
| 6 | +import { Session } from "@opencode-ai/schema/session" |
| 7 | +import { LayerNode } from "@opencode-ai/util/effect/layer-node" |
| 8 | +import { Effect, Fiber, Stream } from "effect" |
| 9 | +import { testEffect } from "./lib/effect" |
| 10 | + |
| 11 | +const it = testEffect(LayerNode.compile(LayerNode.group([Group.node, KV.node, Bus.node]))) |
| 12 | + |
| 13 | +describe("Group", () => { |
| 14 | + it.effect("persists each group in its own versioned KV entry", () => |
| 15 | + Effect.gen(function* () { |
| 16 | + const groups = yield* Group.Service |
| 17 | + const kv = yield* KV.Service |
| 18 | + const id = Group.ID.make("grp_session_one") |
| 19 | + const created = yield* groups.create(id, [ |
| 20 | + { type: "session", id: Session.ID.make("ses_one") }, |
| 21 | + { type: "terminal", id: Pty.ID.make("pty_one") }, |
| 22 | + { type: "terminal", id: Pty.ID.make("pty_one") }, |
| 23 | + ]) |
| 24 | + |
| 25 | + expect(yield* groups.get(id)).toEqual(created) |
| 26 | + expect(yield* kv.get(`group:v1:${id}`)).toEqual(created) |
| 27 | + expect(created.items).toHaveLength(2) |
| 28 | + |
| 29 | + yield* groups.removeItem(id, { type: "session", id: Session.ID.make("ses_one") }) |
| 30 | + yield* groups.addItem(id, { type: "terminal", id: Pty.ID.make("pty_two") }) |
| 31 | + expect((yield* groups.get(id))?.items).toEqual([ |
| 32 | + { type: "terminal", id: Pty.ID.make("pty_one") }, |
| 33 | + { type: "terminal", id: Pty.ID.make("pty_two") }, |
| 34 | + ]) |
| 35 | + |
| 36 | + yield* groups.remove(id) |
| 37 | + expect(yield* groups.get(id)).toBeUndefined() |
| 38 | + expect(yield* kv.get(`group:v1:${id}`)).toBeUndefined() |
| 39 | + }), |
| 40 | + ) |
| 41 | + |
| 42 | + it.effect("creates caller-owned group IDs idempotently", () => |
| 43 | + Effect.gen(function* () { |
| 44 | + const groups = yield* Group.Service |
| 45 | + const id = Group.ID.make("grp_session_one") |
| 46 | + const first = yield* groups.create(id, [{ type: "session", id: Session.ID.make("ses_one") }]) |
| 47 | + const second = yield* groups.create(id, [{ type: "session", id: Session.ID.make("ses_other") }]) |
| 48 | + |
| 49 | + expect(second).toEqual(first) |
| 50 | + expect(yield* groups.get(id)).toEqual(first) |
| 51 | + }), |
| 52 | + ) |
| 53 | + |
| 54 | + it.effect("serializes concurrent mutations within one group", () => |
| 55 | + Effect.gen(function* () { |
| 56 | + const groups = yield* Group.Service |
| 57 | + const id = Group.ID.make("grp_session_one") |
| 58 | + yield* groups.create(id) |
| 59 | + yield* Effect.all( |
| 60 | + Array.from({ length: 20 }, (_, index) => |
| 61 | + groups.addItem(id, { type: "terminal", id: Pty.ID.make(`pty_${index}`) }), |
| 62 | + ), |
| 63 | + { concurrency: "unbounded" }, |
| 64 | + ) |
| 65 | + |
| 66 | + expect((yield* groups.get(id))?.items).toHaveLength(20) |
| 67 | + }), |
| 68 | + ) |
| 69 | + |
| 70 | + it.effect("publishes every removed group item", () => |
| 71 | + Effect.gen(function* () { |
| 72 | + const groups = yield* Group.Service |
| 73 | + const bus = yield* Bus.Service |
| 74 | + const session = { type: "session" as const, id: Session.ID.make("ses_one") } |
| 75 | + const terminal = { type: "terminal" as const, id: Pty.ID.make("pty_one") } |
| 76 | + const group = yield* groups.create(Group.ID.make("grp_session_one"), [session, terminal]) |
| 77 | + const events = yield* bus |
| 78 | + .subscribe(Group.Event.ItemRemoved) |
| 79 | + .pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped) |
| 80 | + yield* Effect.yieldNow |
| 81 | + |
| 82 | + yield* groups.removeItem(group.id, terminal) |
| 83 | + yield* groups.remove(group.id) |
| 84 | + |
| 85 | + expect(Array.from(yield* Fiber.join(events)).map((event) => event.data)).toEqual([ |
| 86 | + { groupID: group.id, item: terminal }, |
| 87 | + { groupID: group.id, item: session }, |
| 88 | + ]) |
| 89 | + }), |
| 90 | + ) |
| 91 | + |
| 92 | + it.effect("publishes every added group item", () => |
| 93 | + Effect.gen(function* () { |
| 94 | + const groups = yield* Group.Service |
| 95 | + const bus = yield* Bus.Service |
| 96 | + const session = { type: "session" as const, id: Session.ID.make("ses_one") } |
| 97 | + const terminal = { type: "terminal" as const, id: Pty.ID.make("pty_one") } |
| 98 | + const group = yield* groups.create(Group.ID.make("grp_session_one"), [session]) |
| 99 | + const event = yield* bus.subscribe(Group.Event.ItemAdded).pipe(Stream.runHead, Effect.forkScoped) |
| 100 | + yield* Effect.yieldNow |
| 101 | + |
| 102 | + yield* groups.addItem(group.id, terminal) |
| 103 | + |
| 104 | + expect((yield* Fiber.join(event)).valueOrUndefined?.data).toEqual({ groupID: group.id, item: terminal }) |
| 105 | + }), |
| 106 | + ) |
| 107 | +}) |
0 commit comments