Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

serverRuntimeState.test.ts

205 lines7.6 KB

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>>;
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("marks a service-supervised server so CLIs can tell it from a manual one", () =>
66 Effect.gen(function* () {
67 const managed = yield* ServerRuntimeState.makePersistedServerRuntimeState({
68 config: { host: undefined, devUrl: undefined },
69 port: 13_773,
70 serviceManaged: true,
71 });
72 const manual = yield* ServerRuntimeState.makePersistedServerRuntimeState({
73 config: { host: undefined, devUrl: undefined },
74 port: 13_773,
75 });
76​
77 assert.isTrue(managed.serviceManaged);
78 // Older readers decode the file without the field, so it is omitted
79 // rather than written as false.
80 assert.isFalse("serviceManaged" in manual);
81 }),
82 );
83​
84 it.effect("treats a missing runtime state file as absent", () =>
85 Effect.gen(function* () {
86 const fileSystem = yield* FileSystem.FileSystem;
87 const path = yield* Path.Path;
88 const root = yield* fileSystem.makeTempDirectoryScoped({
89 prefix: "t3-server-runtime-state-test-",
90 });
91​
92 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(
93 path.join(root, "missing.json"),
94 );
95​
96 assert.isTrue(Option.isNone(restored));
97 }).pipe(Effect.provide(NodeServices.layer)),
98 );
99​
100 it.effect("preserves malformed state decode failures", () => {
101 const logs: CapturedLog[] = [];
102 const logger = Logger.make(({ fiber, message }) => {
103 logs.push({
104 message,
105 annotations: fiber.getRef(References.CurrentLogAnnotations),
106 });
107 });
108​
109 return Effect.gen(function* () {
110 const fileSystem = yield* FileSystem.FileSystem;
111 const path = yield* Path.Path;
112 const root = yield* fileSystem.makeTempDirectoryScoped({
113 prefix: "t3-server-runtime-state-test-",
114 });
115 const statePath = path.join(root, "server.json");
116 yield* fileSystem.writeFileString(statePath, "{not json");
117​
118 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
119​
120 assert.isTrue(Option.isNone(restored));
121 assert.equal(logs[0]?.message, `Failed to decode server runtime state at ${statePath}.`);
122 const error = logs[0]?.annotations.cause;
123 assert.isTrue(isServerRuntimeStateError(error));
124 if (isServerRuntimeStateError(error)) {
125 assert.equal(error.operation, "decode");
126 assert.equal(error.statePath, statePath);
127 assert.equal(error.message, `Failed to decode server runtime state at ${statePath}.`);
128 assert.deepInclude(error.cause, { _tag: "SchemaError" });
129 }
130 }).pipe(
131 Effect.provide(
132 Layer.merge(NodeServices.layer, Logger.layer([logger], { mergeWithExisting: false })),
133 ),
134 );
135 });
136​
137 it.effect("preserves runtime state read failures", () => {
138 const logs: CapturedLog[] = [];
139 const logger = Logger.make(({ fiber, message }) => {
140 logs.push({
141 message,
142 annotations: fiber.getRef(References.CurrentLogAnnotations),
143 });
144 });
145​
146 return Effect.gen(function* () {
147 const fileSystem = yield* FileSystem.FileSystem;
148 const path = yield* Path.Path;
149 const root = yield* fileSystem.makeTempDirectoryScoped({
150 prefix: "t3-server-runtime-state-test-",
151 });
152 const statePath = path.join(root, "server.json");
153 yield* fileSystem.makeDirectory(statePath);
154​
155 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
156​
157 assert.isTrue(Option.isNone(restored));
158 assert.equal(logs[0]?.message, `Failed to read server runtime state at ${statePath}.`);
159 const error = logs[0]?.annotations.cause;
160 assert.isTrue(isServerRuntimeStateError(error));
161 if (isServerRuntimeStateError(error)) {
162 assert.equal(error.operation, "read");
163 assert.equal(error.statePath, statePath);
164 assert.equal(error.message, `Failed to read server runtime state at ${statePath}.`);
165 assert.deepInclude(error.cause, { _tag: "PlatformError" });
166 }
167 }).pipe(
168 Effect.provide(
169 Layer.merge(NodeServices.layer, Logger.layer([logger], { mergeWithExisting: false })),
170 ),
171 );
172 });
173​
174 it.effect("preserves runtime state persistence failures", () =>
175 Effect.gen(function* () {
176 const fileSystem = yield* FileSystem.FileSystem;
177 const path = yield* Path.Path;
178 const root = yield* fileSystem.makeTempDirectoryScoped({
179 prefix: "t3-server-runtime-state-test-",
180 });
181 const blockedDirectory = path.join(root, "not-a-directory");
182 const statePath = path.join(blockedDirectory, "server.json");
183 yield* fileSystem.writeFileString(blockedDirectory, "blocked");
184​
185 const error = yield* ServerRuntimeState.persistServerRuntimeState({
186 path: statePath,
187 state: {
188 version: 1,
189 pid: 123,
190 port: 4_971,
191 origin: "http://127.0.0.1:4971",
192 startedAt: "2026-06-20T00:00:00.000Z",
193 },
194 }).pipe(Effect.flip);
195​
196 assert.isTrue(isServerRuntimeStateError(error));
197 if (isServerRuntimeStateError(error)) {
198 assert.equal(error.operation, "persist");
199 assert.equal(error.statePath, statePath);
200 assert.equal(error.message, `Failed to persist server runtime state at ${statePath}.`);
201 assert.deepInclude(error.cause, { _tag: "PlatformError" });
202 }
203 }).pipe(Effect.provide(NodeServices.layer)),
204 );
205});