1import { EnvironmentId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import { assertTrue } from "@effect/vitest/utils";
4import * as Effect from "effect/Effect";
5import * as Option from "effect/Option";
6
7import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts";
8
9it.effect(
10 "publishes lifecycle events without subscribers and snapshots the latest welcome/ready",
11 () =>
12 Effect.gen(function* () {
13 const lifecycleEvents = yield* ServerLifecycleEvents.ServerLifecycleEvents;
14 const environment = {
15 environmentId: EnvironmentId.make("environment-test"),
16 label: "Test environment",
17 platform: { os: "darwin" as const, arch: "arm64" as const },
18 serverVersion: "0.0.0-test",
19 capabilities: { repositoryIdentity: true },
20 };
21
22 const welcome = yield* lifecycleEvents
23 .publish({
24 version: 1,
25 type: "welcome",
26 payload: {
27 environment,
28 cwd: "/tmp/project",
29 projectName: "project",
30 },
31 })
32 .pipe(Effect.timeoutOption("50 millis"));
33 assertTrue(Option.isSome(welcome));
34 assert.equal(welcome.value.sequence, 1);
35
36 const ready = yield* lifecycleEvents
37 .publish({
38 version: 1,
39 type: "ready",
40 payload: {
41 at: "2026-01-01T00:00:00.000Z",
42 environment,
43 },
44 })
45 .pipe(Effect.timeoutOption("50 millis"));
46 assertTrue(Option.isSome(ready));
47 assert.equal(ready.value.sequence, 2);
48
49 yield* lifecycleEvents.publish({
50 version: 1,
51 type: "legacyThreadMigration",
52 payload: {
53 status: "running",
54 totalThreadCount: 12,
55 },
56 });
57 yield* lifecycleEvents.publish({
58 version: 1,
59 type: "legacyThreadMigration",
60 payload: {
61 status: "complete",
62 totalThreadCount: 12,
63 },
64 });
65
66 const snapshot = yield* lifecycleEvents.snapshot;
67 assert.equal(snapshot.sequence, 4);
68 assert.deepEqual(snapshot.events.map((event) => event.type).toSorted(), [
69 "legacyThreadMigration",
70 "ready",
71 "welcome",
72 ]);
73 const migration = snapshot.events.find((event) => event.type === "legacyThreadMigration");
74 assert.equal(migration?.payload.status, "complete");
75 }).pipe(Effect.provide(ServerLifecycleEvents.layer)),
76);