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

ServerEnvironment.test.ts

133 lines4.9 KB

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 noBrowser: false,
47 startupPresentation: "browser",
48 } satisfies ServerConfig.ServerConfig["Service"];
49});
50​
51it.layer(NodeServices.layer)("ServerEnvironmentLive", (it) => {
52 it.effect("persists the environment id across service restarts", () =>
53 Effect.gen(function* () {
54 const fileSystem = yield* FileSystem.FileSystem;
55 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
56 prefix: "t3-server-environment-test-",
57 });
58​
59 const first = yield* Effect.gen(function* () {
60 const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
61 return yield* serverEnvironment.getDescriptor;
62 }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir)));
63 const second = yield* Effect.gen(function* () {
64 const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
65 return yield* serverEnvironment.getDescriptor;
66 }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir)));
67​
68 expect(first.environmentId).toBe(second.environmentId);
69 expect(second.capabilities.repositoryIdentity).toBe(true);
70 expect(second.capabilities.connectionProbe).toBe(true);
71 }),
72 );
73​
74 it.effect("structures persisted environment id filesystem failures", () =>
75 Effect.gen(function* () {
76 const fileSystem = yield* FileSystem.FileSystem;
77 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
78 prefix: "t3-server-environment-error-test-",
79 });
80 const serverConfig = yield* makeServerConfig(baseDir);
81 const environmentIdPath = serverConfig.environmentIdPath;
82 const methodByOperation = {
83 check: "exists",
84 read: "readFileString",
85 write: "writeFileString",
86 } as const;
87​
88 for (const operation of ["check", "read", "write"] as const) {
89 const writeAttempts: string[] = [];
90 const cause = PlatformError.systemError({
91 _tag: "PermissionDenied",
92 module: "FileSystem",
93 method: methodByOperation[operation],
94 description: "permission denied",
95 pathOrDescriptor: environmentIdPath,
96 });
97 const failingFileSystemLayer = FileSystem.layerNoop({
98 exists: () =>
99 operation === "check" ? Effect.fail(cause) : Effect.succeed(operation === "read"),
100 readFileString: () => Effect.fail(cause),
101 writeFileString: (path) => {
102 writeAttempts.push(path);
103 return Effect.fail(cause);
104 },
105 });
106​
107 const error = yield* Effect.gen(function* () {
108 const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
109 return yield* serverEnvironment.getDescriptor;
110 }).pipe(
111 Effect.provide(
112 ServerEnvironment.layer.pipe(
113 Layer.provide(Layer.merge(ServerConfig.layer(serverConfig), failingFileSystemLayer)),
114 ),
115 ),
116 Effect.flip,
117 );
118​
119 expect(isServerEnvironmentIdPersistenceError(error)).toBe(true);
120 if (!isServerEnvironmentIdPersistenceError(error)) {
121 throw error;
122 }
123 expect(error.operation).toBe(operation);
124 expect(error.environmentIdPath).toBe(environmentIdPath);
125 expect(error.cause).toBe(cause);
126 expect(error.message).toBe(
127 `Server environment ID ${operation} failed at '${environmentIdPath}'.`,
128 );
129 expect(writeAttempts).toEqual(operation === "write" ? [environmentIdPath] : []);
130 }
131 }),
132 );
133});