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