Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Logger from "effect/Logger";
7import * as Option from "effect/Option";
8import * as Path from "effect/Path";
9import * as References from "effect/References";
10import * as Schema from "effect/Schema";
11
12import * as ServerRuntimeState from "./serverRuntimeState.ts";
13
14const isServerRuntimeStateError = Schema.is(ServerRuntimeState.ServerRuntimeStateError);
15
16interface CapturedLog {
17 readonly message: unknown;
18 readonly annotations: Readonly<Record<string, unknown>>;
19}
20
21describe("serverRuntimeState", () => {
22 it.effect("persists and reads the runtime state", () =>
23 Effect.gen(function* () {
24 const fileSystem = yield* FileSystem.FileSystem;
25 const path = yield* Path.Path;
26 const root = yield* fileSystem.makeTempDirectoryScoped({
27 prefix: "t3-server-runtime-state-test-",
28 });
29 const statePath = path.join(root, "runtime", "server.json");
30 const state: ServerRuntimeState.PersistedServerRuntimeState = {
31 version: 1,
32 pid: 123,
33 host: "127.0.0.1",
34 port: 4_971,
35 origin: "http://127.0.0.1:4971",
36 devUrl: "http://localhost:5733/",
37 startedAt: "2026-06-20T00:00:00.000Z",
38 };
39
40 yield* ServerRuntimeState.persistServerRuntimeState({ path: statePath, state });
41 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
42
43 assert.deepEqual(Option.getOrThrow(restored), state);
44 }).pipe(Effect.provide(NodeServices.layer)),
45 );
46
47 it.effect("records the dev web URL when the server fronts a dev server", () =>
48 Effect.gen(function* () {
49 const state = yield* ServerRuntimeState.makePersistedServerRuntimeState({
50 config: { host: undefined, devUrl: new URL("http://localhost:5733") },
51 port: 13_773,
52 });
53
54 assert.equal(state.devUrl, "http://localhost:5733/");
55 assert.equal(state.origin, "http://127.0.0.1:13773");
56
57 const withoutDev = yield* ServerRuntimeState.makePersistedServerRuntimeState({
58 config: { host: undefined, devUrl: undefined },
59 port: 13_773,
60 });
61 assert.isFalse("devUrl" in withoutDev);
62 }),
63 );
64
65 it.effect("treats a missing runtime state file as absent", () =>
66 Effect.gen(function* () {
67 const fileSystem = yield* FileSystem.FileSystem;
68 const path = yield* Path.Path;
69 const root = yield* fileSystem.makeTempDirectoryScoped({
70 prefix: "t3-server-runtime-state-test-",
71 });
72
73 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(
74 path.join(root, "missing.json"),
75 );
76
77 assert.isTrue(Option.isNone(restored));
78 }).pipe(Effect.provide(NodeServices.layer)),
79 );
80
81 it.effect("preserves malformed state decode failures", () => {
82 const logs: CapturedLog[] = [];
83 const logger = Logger.make(({ fiber, message }) => {
84 logs.push({
85 message,
86 annotations: fiber.getRef(References.CurrentLogAnnotations),
87 });
88 });
89
90 return Effect.gen(function* () {
91 const fileSystem = yield* FileSystem.FileSystem;
92 const path = yield* Path.Path;
93 const root = yield* fileSystem.makeTempDirectoryScoped({
94 prefix: "t3-server-runtime-state-test-",
95 });
96 const statePath = path.join(root, "server.json");
97 yield* fileSystem.writeFileString(statePath, "{not json");
98
99 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
100
101 assert.isTrue(Option.isNone(restored));
102 assert.equal(logs[0]?.message, `Failed to decode server runtime state at ${statePath}.`);
103 const error = logs[0]?.annotations.cause;
104 assert.isTrue(isServerRuntimeStateError(error));
105 if (isServerRuntimeStateError(error)) {
106 assert.equal(error.operation, "decode");
107 assert.equal(error.statePath, statePath);
108 assert.equal(error.message, `Failed to decode server runtime state at ${statePath}.`);
109 assert.deepInclude(error.cause, { _tag: "SchemaError" });
110 }
111 }).pipe(
112 Effect.provide(
113 Layer.merge(NodeServices.layer, Logger.layer([logger], { mergeWithExisting: false })),
114 ),
115 );
116 });
117
118 it.effect("preserves runtime state read failures", () => {
119 const logs: CapturedLog[] = [];
120 const logger = Logger.make(({ fiber, message }) => {
121 logs.push({
122 message,
123 annotations: fiber.getRef(References.CurrentLogAnnotations),
124 });
125 });
126
127 return Effect.gen(function* () {
128 const fileSystem = yield* FileSystem.FileSystem;
129 const path = yield* Path.Path;
130 const root = yield* fileSystem.makeTempDirectoryScoped({
131 prefix: "t3-server-runtime-state-test-",
132 });
133 const statePath = path.join(root, "server.json");
134 yield* fileSystem.makeDirectory(statePath);
135
136 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
137
138 assert.isTrue(Option.isNone(restored));
139 assert.equal(logs[0]?.message, `Failed to read server runtime state at ${statePath}.`);
140 const error = logs[0]?.annotations.cause;
141 assert.isTrue(isServerRuntimeStateError(error));
142 if (isServerRuntimeStateError(error)) {
143 assert.equal(error.operation, "read");
144 assert.equal(error.statePath, statePath);
145 assert.equal(error.message, `Failed to read server runtime state at ${statePath}.`);
146 assert.deepInclude(error.cause, { _tag: "PlatformError" });
147 }
148 }).pipe(
149 Effect.provide(
150 Layer.merge(NodeServices.layer, Logger.layer([logger], { mergeWithExisting: false })),
151 ),
152 );
153 });
154
155 it.effect("preserves runtime state persistence failures", () =>
156 Effect.gen(function* () {
157 const fileSystem = yield* FileSystem.FileSystem;
158 const path = yield* Path.Path;
159 const root = yield* fileSystem.makeTempDirectoryScoped({
160 prefix: "t3-server-runtime-state-test-",
161 });
162 const blockedDirectory = path.join(root, "not-a-directory");
163 const statePath = path.join(blockedDirectory, "server.json");
164 yield* fileSystem.writeFileString(blockedDirectory, "blocked");
165
166 const error = yield* ServerRuntimeState.persistServerRuntimeState({
167 path: statePath,
168 state: {
169 version: 1,
170 pid: 123,
171 port: 4_971,
172 origin: "http://127.0.0.1:4971",
173 startedAt: "2026-06-20T00:00:00.000Z",
174 },
175 }).pipe(Effect.flip);
176
177 assert.isTrue(isServerRuntimeStateError(error));
178 if (isServerRuntimeStateError(error)) {
179 assert.equal(error.operation, "persist");
180 assert.equal(error.statePath, statePath);
181 assert.equal(error.message, `Failed to persist server runtime state at ${statePath}.`);
182 assert.deepInclude(error.cause, { _tag: "PlatformError" });
183 }
184 }).pipe(Effect.provide(NodeServices.layer)),
185 );
186});