Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import { assertTrue } from "@effect/vitest/utils";
4import { Effect, Option } from "effect";
5
6import { ServerLifecycleEvents, ServerLifecycleEventsLive } from "./serverLifecycleEvents.ts";
7
8it.effect(
9 "publishes lifecycle events without subscribers and snapshots the latest welcome/ready",
10 () =>
11 Effect.gen(function* () {
12 const lifecycleEvents = yield* ServerLifecycleEvents;
13 const environment = {
14 environmentId: EnvironmentId.make("environment-test"),
15 label: "Test environment",
16 platform: { os: "darwin" as const, arch: "arm64" as const },
17 serverVersion: "0.0.0-test",
18 capabilities: { repositoryIdentity: true },
19 };
20
21 const welcome = yield* lifecycleEvents
22 .publish({
23 version: 1,
24 type: "welcome",
25 payload: {
26 environment,
27 cwd: "/tmp/project",
28 projectName: "project",
29 },
30 })
31 .pipe(Effect.timeoutOption("50 millis"));
32 assertTrue(Option.isSome(welcome));
33 assert.equal(welcome.value.sequence, 1);
34
35 const ready = yield* lifecycleEvents
36 .publish({
37 version: 1,
38 type: "ready",
39 payload: {
40 at: new Date().toISOString(),
41 environment,
42 },
43 })
44 .pipe(Effect.timeoutOption("50 millis"));
45 assertTrue(Option.isSome(ready));
46 assert.equal(ready.value.sequence, 2);
47
48 const snapshot = yield* lifecycleEvents.snapshot;
49 assert.equal(snapshot.sequence, 2);
50 assert.deepEqual(snapshot.events.map((event) => event.type).toSorted(), ["ready", "welcome"]);
51 }).pipe(Effect.provide(ServerLifecycleEventsLive)),
52);