Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as PlatformError from "effect/PlatformError";
7import * as Schema from "effect/Schema";
8
9import * as ServerConfig from "../config.ts";
10import * as ServerEnvironment from "./ServerEnvironment.ts";
11
12const isServerEnvironmentIdPersistenceError = Schema.is(
13 ServerEnvironment.ServerEnvironmentIdPersistenceError,
14);
15
16const makeServerEnvironmentLayer = (baseDir: string) =>
17 ServerEnvironment.layer.pipe(Layer.provide(ServerConfig.layerTest(process.cwd(), baseDir)));
18
19const makeServerConfig = Effect.fn(function* (baseDir: string) {
20 const derivedPaths = yield* ServerConfig.deriveServerPaths(baseDir, undefined);
21
22 return {
23 ...derivedPaths,
24 logLevel: "Error",
25 traceMinLevel: "Info",
26 traceTimingEnabled: true,
27 traceBatchWindowMs: 200,
28 traceMaxBytes: 10 * 1024 * 1024,
29 traceMaxFiles: 10,
30 otlpTracesUrl: undefined,
31 otlpMetricsUrl: undefined,
32 otlpExportIntervalMs: 10_000,
33 otlpServiceName: "t3-server",
34 cwd: process.cwd(),
35 baseDir,
36 mode: "web",
37 autoBootstrapProjectFromCwd: false,
38 logWebSocketEvents: false,
39 tailscaleServeEnabled: false,
40 tailscaleServePort: 443,
41 port: 0,
42 host: undefined,
43 desktopBootstrapToken: undefined,
44 staticDir: undefined,
45 devUrl: undefined,
46 devAllowedOrigins: [],
47 noBrowser: false,
48 startupPresentation: "browser",
49 } satisfies ServerConfig.ServerConfig["Service"];
50});
51
52it.layer(NodeServices.layer)("ServerEnvironmentLive", (it) => {
53 it.effect("persists the environment id across service restarts", () =>
54 Effect.gen(function* () {
55 const fileSystem = yield* FileSystem.FileSystem;
56 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
57 prefix: "t3-server-environment-test-",
58 });
59
60 const first = yield* Effect.gen(function* () {
61 const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
62 return yield* serverEnvironment.getDescriptor;
63 }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir)));
64 const second = yield* Effect.gen(function* () {
65 const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
66 return yield* serverEnvironment.getDescriptor;
67 }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir)));
68
69 expect(first.environmentId).toBe(second.environmentId);
70 expect(second.capabilities.repositoryIdentity).toBe(true);
71 expect(second.capabilities.connectionProbe).toBe(true);
72 }),
73 );
74
75 it.effect("structures persisted environment id filesystem failures", () =>
76 Effect.gen(function* () {
77 const fileSystem = yield* FileSystem.FileSystem;
78 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
79 prefix: "t3-server-environment-error-test-",
80 });
81 const serverConfig = yield* makeServerConfig(baseDir);
82 const environmentIdPath = serverConfig.environmentIdPath;
83 const methodByOperation = {
84 check: "exists",
85 read: "readFileString",
86 write: "writeFileString",
87 } as const;
88
89 for (const operation of ["check", "read", "write"] as const) {
90 const writeAttempts: string[] = [];
91 const cause = PlatformError.systemError({
92 _tag: "PermissionDenied",
93 module: "FileSystem",
94 method: methodByOperation[operation],
95 description: "permission denied",
96 pathOrDescriptor: environmentIdPath,
97 });
98 const failingFileSystemLayer = FileSystem.layerNoop({
99 exists: () =>
100 operation === "check" ? Effect.fail(cause) : Effect.succeed(operation === "read"),
101 readFileString: () => Effect.fail(cause),
102 writeFileString: (path) => {
103 writeAttempts.push(path);
104 return Effect.fail(cause);
105 },
106 });
107
108 const error = yield* Effect.gen(function* () {
109 const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
110 return yield* serverEnvironment.getDescriptor;
111 }).pipe(
112 Effect.provide(
113 ServerEnvironment.layer.pipe(
114 Layer.provide(Layer.merge(ServerConfig.layer(serverConfig), failingFileSystemLayer)),
115 ),
116 ),
117 Effect.flip,
118 );
119
120 expect(isServerEnvironmentIdPersistenceError(error)).toBe(true);
121 if (!isServerEnvironmentIdPersistenceError(error)) {
122 throw error;
123 }
124 expect(error.operation).toBe(operation);
125 expect(error.environmentIdPath).toBe(environmentIdPath);
126 expect(error.cause).toBe(cause);
127 expect(error.message).toBe(
128 `Server environment ID ${operation} failed at '${environmentIdPath}'.`,
129 );
130 expect(writeAttempts).toEqual(operation === "write" ? [environmentIdPath] : []);
131 }
132 }),
133 );
134});