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

serverRuntimeState.test.ts

167 lines6.2 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: "v12-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 startedAt: "2026-06-20T00:00:00.000Z",
37 };
38​
39 yield* ServerRuntimeState.persistServerRuntimeState({ path: statePath, state });
40 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
41​
42 assert.deepEqual(Option.getOrThrow(restored), state);
43 }).pipe(Effect.provide(NodeServices.layer)),
44 );
45​
46 it.effect("treats a missing runtime state file as absent", () =>
47 Effect.gen(function* () {
48 const fileSystem = yield* FileSystem.FileSystem;
49 const path = yield* Path.Path;
50 const root = yield* fileSystem.makeTempDirectoryScoped({
51 prefix: "v12-server-runtime-state-test-",
52 });
53​
54 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(
55 path.join(root, "missing.json"),
56 );
57​
58 assert.isTrue(Option.isNone(restored));
59 }).pipe(Effect.provide(NodeServices.layer)),
60 );
61​
62 it.effect("preserves malformed state decode failures", () => {
63 const logs: CapturedLog[] = [];
64 const logger = Logger.make(({ fiber, message }) => {
65 logs.push({
66 message,
67 annotations: fiber.getRef(References.CurrentLogAnnotations),
68 });
69 });
70​
71 return Effect.gen(function* () {
72 const fileSystem = yield* FileSystem.FileSystem;
73 const path = yield* Path.Path;
74 const root = yield* fileSystem.makeTempDirectoryScoped({
75 prefix: "v12-server-runtime-state-test-",
76 });
77 const statePath = path.join(root, "server.json");
78 yield* fileSystem.writeFileString(statePath, "{not json");
79​
80 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
81​
82 assert.isTrue(Option.isNone(restored));
83 assert.equal(logs[0]?.message, `Failed to decode server runtime state at ${statePath}.`);
84 const error = logs[0]?.annotations.cause;
85 assert.isTrue(isServerRuntimeStateError(error));
86 if (isServerRuntimeStateError(error)) {
87 assert.equal(error.operation, "decode");
88 assert.equal(error.statePath, statePath);
89 assert.equal(error.message, `Failed to decode server runtime state at ${statePath}.`);
90 assert.deepInclude(error.cause, { _tag: "SchemaError" });
91 }
92 }).pipe(
93 Effect.provide(
94 Layer.merge(NodeServices.layer, Logger.layer([logger], { mergeWithExisting: false })),
95 ),
96 );
97 });
98​
99 it.effect("preserves runtime state read failures", () => {
100 const logs: CapturedLog[] = [];
101 const logger = Logger.make(({ fiber, message }) => {
102 logs.push({
103 message,
104 annotations: fiber.getRef(References.CurrentLogAnnotations),
105 });
106 });
107​
108 return Effect.gen(function* () {
109 const fileSystem = yield* FileSystem.FileSystem;
110 const path = yield* Path.Path;
111 const root = yield* fileSystem.makeTempDirectoryScoped({
112 prefix: "v12-server-runtime-state-test-",
113 });
114 const statePath = path.join(root, "server.json");
115 yield* fileSystem.makeDirectory(statePath);
116​
117 const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
118​
119 assert.isTrue(Option.isNone(restored));
120 assert.equal(logs[0]?.message, `Failed to read server runtime state at ${statePath}.`);
121 const error = logs[0]?.annotations.cause;
122 assert.isTrue(isServerRuntimeStateError(error));
123 if (isServerRuntimeStateError(error)) {
124 assert.equal(error.operation, "read");
125 assert.equal(error.statePath, statePath);
126 assert.equal(error.message, `Failed to read server runtime state at ${statePath}.`);
127 assert.deepInclude(error.cause, { _tag: "PlatformError" });
128 }
129 }).pipe(
130 Effect.provide(
131 Layer.merge(NodeServices.layer, Logger.layer([logger], { mergeWithExisting: false })),
132 ),
133 );
134 });
135​
136 it.effect("preserves runtime state persistence failures", () =>
137 Effect.gen(function* () {
138 const fileSystem = yield* FileSystem.FileSystem;
139 const path = yield* Path.Path;
140 const root = yield* fileSystem.makeTempDirectoryScoped({
141 prefix: "v12-server-runtime-state-test-",
142 });
143 const blockedDirectory = path.join(root, "not-a-directory");
144 const statePath = path.join(blockedDirectory, "server.json");
145 yield* fileSystem.writeFileString(blockedDirectory, "blocked");
146​
147 const error = yield* ServerRuntimeState.persistServerRuntimeState({
148 path: statePath,
149 state: {
150 version: 1,
151 pid: 123,
152 port: 4_971,
153 origin: "http://127.0.0.1:4971",
154 startedAt: "2026-06-20T00:00:00.000Z",
155 },
156 }).pipe(Effect.flip);
157​
158 assert.isTrue(isServerRuntimeStateError(error));
159 if (isServerRuntimeStateError(error)) {
160 assert.equal(error.operation, "persist");
161 assert.equal(error.statePath, statePath);
162 assert.equal(error.message, `Failed to persist server runtime state at ${statePath}.`);
163 assert.deepInclude(error.cause, { _tag: "PlatformError" });
164 }
165 }).pipe(Effect.provide(NodeServices.layer)),
166 );
167});