Open History to see the last commit
1import * as nodePath from "node:path";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { expect, it } from "@effect/vitest";
4import { Effect, Exit, FileSystem, Layer, PlatformError } from "effect";
5
6import { deriveServerPaths, ServerConfig, type ServerConfigShape } from "../../config.ts";
7import { ServerEnvironment } from "../Services/ServerEnvironment.ts";
8import { ServerEnvironmentLive } from "./ServerEnvironment.ts";
9
10const makeServerEnvironmentLayer = (baseDir: string) =>
11 ServerEnvironmentLive.pipe(Layer.provide(ServerConfig.layerTest(process.cwd(), baseDir)));
12
13const makeServerConfig = Effect.fn(function* (baseDir: string) {
14 const derivedPaths = yield* deriveServerPaths(baseDir, undefined);
15
16 return {
17 ...derivedPaths,
18 logLevel: "Error",
19 traceMinLevel: "Info",
20 traceTimingEnabled: true,
21 traceBatchWindowMs: 200,
22 traceMaxBytes: 10 * 1024 * 1024,
23 traceMaxFiles: 10,
24 otlpTracesUrl: undefined,
25 otlpMetricsUrl: undefined,
26 otlpExportIntervalMs: 10_000,
27 otlpServiceName: "t3-server",
28 cwd: process.cwd(),
29 baseDir,
30 mode: "web",
31 autoBootstrapProjectFromCwd: false,
32 logWebSocketEvents: false,
33 port: 0,
34 host: undefined,
35 desktopBootstrapToken: undefined,
36 staticDir: undefined,
37 devUrl: undefined,
38 noBrowser: false,
39 } satisfies ServerConfigShape;
40});
41
42it.layer(NodeServices.layer)("ServerEnvironmentLive", (it) => {
43 it.effect("persists the environment id across service restarts", () =>
44 Effect.gen(function* () {
45 const fileSystem = yield* FileSystem.FileSystem;
46 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
47 prefix: "t3-server-environment-test-",
48 });
49
50 const first = yield* Effect.gen(function* () {
51 const serverEnvironment = yield* ServerEnvironment;
52 return yield* serverEnvironment.getDescriptor;
53 }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir)));
54 const second = yield* Effect.gen(function* () {
55 const serverEnvironment = yield* ServerEnvironment;
56 return yield* serverEnvironment.getDescriptor;
57 }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir)));
58
59 expect(first.environmentId).toBe(second.environmentId);
60 expect(second.capabilities.repositoryIdentity).toBe(true);
61 }),
62 );
63
64 it.effect("fails instead of overwriting a persisted id when reading the file errors", () =>
65 Effect.gen(function* () {
66 const fileSystem = yield* FileSystem.FileSystem;
67 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
68 prefix: "t3-server-environment-read-error-test-",
69 });
70 const serverConfig = yield* makeServerConfig(baseDir);
71 const environmentIdPath = serverConfig.environmentIdPath;
72 yield* fileSystem.makeDirectory(nodePath.dirname(environmentIdPath), { recursive: true });
73 yield* fileSystem.writeFileString(environmentIdPath, "persisted-environment-id\n");
74 const writeAttempts: string[] = [];
75 const failingFileSystemLayer = FileSystem.layerNoop({
76 exists: (path) => Effect.succeed(path === environmentIdPath),
77 readFileString: (path) =>
78 path === environmentIdPath
79 ? Effect.fail(
80 PlatformError.systemError({
81 _tag: "PermissionDenied",
82 module: "FileSystem",
83 method: "readFileString",
84 description: "permission denied",
85 pathOrDescriptor: path,
86 }),
87 )
88 : Effect.fail(
89 PlatformError.systemError({
90 _tag: "NotFound",
91 module: "FileSystem",
92 method: "readFileString",
93 description: "not found",
94 pathOrDescriptor: path,
95 }),
96 ),
97 writeFileString: (path) => {
98 writeAttempts.push(path);
99 return Effect.void;
100 },
101 });
102
103 const exit = yield* Effect.gen(function* () {
104 const serverEnvironment = yield* ServerEnvironment;
105 return yield* serverEnvironment.getDescriptor;
106 }).pipe(
107 Effect.provide(
108 ServerEnvironmentLive.pipe(
109 Layer.provide(
110 Layer.merge(Layer.succeed(ServerConfig, serverConfig), failingFileSystemLayer),
111 ),
112 ),
113 ),
114 Effect.exit,
115 );
116
117 expect(Exit.isFailure(exit)).toBe(true);
118 expect(writeAttempts).toEqual([]);
119 expect(yield* fileSystem.readFileString(environmentIdPath)).toBe(
120 "persisted-environment-id\n",
121 );
122 }),
123 );
124});