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 startupPresentation: "browser",
40 } satisfies ServerConfigShape;
41});
42
43it.layer(NodeServices.layer)("ServerEnvironmentLive", (it) => {
44 it.effect("persists the environment id across service restarts", () =>
45 Effect.gen(function* () {
46 const fileSystem = yield* FileSystem.FileSystem;
47 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
48 prefix: "t3-server-environment-test-",
49 });
50
51 const first = yield* Effect.gen(function* () {
52 const serverEnvironment = yield* ServerEnvironment;
53 return yield* serverEnvironment.getDescriptor;
54 }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir)));
55 const second = yield* Effect.gen(function* () {
56 const serverEnvironment = yield* ServerEnvironment;
57 return yield* serverEnvironment.getDescriptor;
58 }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir)));
59
60 expect(first.environmentId).toBe(second.environmentId);
61 expect(second.capabilities.repositoryIdentity).toBe(true);
62 }),
63 );
64
65 it.effect("fails instead of overwriting a persisted id when reading the file errors", () =>
66 Effect.gen(function* () {
67 const fileSystem = yield* FileSystem.FileSystem;
68 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
69 prefix: "t3-server-environment-read-error-test-",
70 });
71 const serverConfig = yield* makeServerConfig(baseDir);
72 const environmentIdPath = serverConfig.environmentIdPath;
73 yield* fileSystem.makeDirectory(nodePath.dirname(environmentIdPath), { recursive: true });
74 yield* fileSystem.writeFileString(environmentIdPath, "persisted-environment-id\n");
75 const writeAttempts: string[] = [];
76 const failingFileSystemLayer = FileSystem.layerNoop({
77 exists: (path) => Effect.succeed(path === environmentIdPath),
78 readFileString: (path) =>
79 path === environmentIdPath
80 ? Effect.fail(
81 PlatformError.systemError({
82 _tag: "PermissionDenied",
83 module: "FileSystem",
84 method: "readFileString",
85 description: "permission denied",
86 pathOrDescriptor: path,
87 }),
88 )
89 : Effect.fail(
90 PlatformError.systemError({
91 _tag: "NotFound",
92 module: "FileSystem",
93 method: "readFileString",
94 description: "not found",
95 pathOrDescriptor: path,
96 }),
97 ),
98 writeFileString: (path) => {
99 writeAttempts.push(path);
100 return Effect.void;
101 },
102 });
103
104 const exit = yield* Effect.gen(function* () {
105 const serverEnvironment = yield* ServerEnvironment;
106 return yield* serverEnvironment.getDescriptor;
107 }).pipe(
108 Effect.provide(
109 ServerEnvironmentLive.pipe(
110 Layer.provide(
111 Layer.merge(Layer.succeed(ServerConfig, serverConfig), failingFileSystemLayer),
112 ),
113 ),
114 ),
115 Effect.exit,
116 );
117
118 expect(Exit.isFailure(exit)).toBe(true);
119 expect(writeAttempts).toEqual([]);
120 expect(yield* fileSystem.readFileString(environmentIdPath)).toBe(
121 "persisted-environment-id\n",
122 );
123 }),
124 );
125});