Open History to see the last commit
1import {
2 EnvironmentId,
3 EventId,
4 ORCHESTRATION_V2_WS_METHODS,
5 type OrchestrationV2ThreadStreamItem,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "@effect/vitest";
8import * as DateTime from "effect/DateTime";
9import * as Effect from "effect/Effect";
10import * as Option from "effect/Option";
11import * as Queue from "effect/Queue";
12import * as Stream from "effect/Stream";
13import * as SubscriptionRef from "effect/SubscriptionRef";
14
15import {
16 AVAILABLE_CONNECTION_STATE,
17 PrimaryConnectionTarget,
18 type PreparedConnection,
19} from "../connection/model.ts";
20import * as EnvironmentSupervisor from "../connection/supervisor.ts";
21import * as Persistence from "../platform/persistence.ts";
22import * as RpcSession from "../rpc/session.ts";
23import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
24import { v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
25import { makeEnvironmentThreadState } from "./threads.ts";
26
27const TARGET = new PrimaryConnectionTarget({
28 environmentId: EnvironmentId.make("environment-1"),
29 label: "Test environment",
30 httpBaseUrl: "https://environment.example.test",
31 wsBaseUrl: "wss://environment.example.test",
32});
33
34describe("V2 thread synchronization", () => {
35 it.effect("applies a snapshot followed by committed projection events", () =>
36 Effect.gen(function* () {
37 const events = yield* Queue.unbounded<OrchestrationV2ThreadStreamItem>();
38 const client = {
39 [ORCHESTRATION_V2_WS_METHODS.subscribeThread]: () => Stream.fromQueue(events),
40 } as unknown as WsRpcProtocolClient;
41 const rpcSession: RpcSession.RpcSession = {
42 client,
43 initialConfig: Effect.never,
44 ready: Effect.void,
45 probe: Effect.void,
46 closed: Effect.never,
47 };
48 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
49 target: TARGET,
50 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
51 session: yield* SubscriptionRef.make(Option.some(rpcSession)),
52 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
53 connect: Effect.void,
54 disconnect: Effect.void,
55 retryNow: Effect.void,
56 });
57 const cache = Persistence.EnvironmentCacheStore.of({
58 loadShell: () => Effect.succeed(Option.none()),
59 saveShell: () => Effect.void,
60 loadThread: () => Effect.succeed(Option.none()),
61 saveThread: () => Effect.void,
62 removeThread: () => Effect.void,
63 clear: () => Effect.void,
64 });
65 const state = yield* makeEnvironmentThreadState(v2ThreadId).pipe(
66 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
67 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
68 );
69
70 yield* Queue.offer(events, {
71 kind: "snapshot",
72 snapshotSequence: 1,
73 projection: v2Projection,
74 });
75 yield* SubscriptionRef.changes(state).pipe(
76 Stream.filter((current) => current.status === "live"),
77 Stream.runHead,
78 );
79
80 const occurredAt = DateTime.makeUnsafe("2026-06-20T02:00:00.000Z");
81 yield* Queue.offer(events, {
82 kind: "event",
83 sequence: 2,
84 event: {
85 id: EventId.make("event-title"),
86 type: "thread.metadata-updated",
87 threadId: v2ThreadId,
88 occurredAt,
89 payload: { ...v2Projection.thread, title: "Updated", updatedAt: occurredAt },
90 },
91 });
92 yield* SubscriptionRef.changes(state).pipe(
93 Stream.filter((current) => Option.getOrNull(current.data)?.thread.title === "Updated"),
94 Stream.runHead,
95 );
96
97 expect(Option.getOrThrow((yield* SubscriptionRef.get(state)).data).thread.title).toBe(
98 "Updated",
99 );
100 }),
101 );
102});