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

ServerEnvironment.test.ts

135 lines5.0 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 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 expect(second.capabilities.threadTitleRegeneration).toBe(true);
73 }),
74 );
75​
76 it.effect("structures persisted environment id filesystem failures", () =>
77 Effect.gen(function* () {
78 const fileSystem = yield* FileSystem.FileSystem;
79 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
80 prefix: "t3-server-environment-error-test-",
81 });
82 const serverConfig = yield* makeServerConfig(baseDir);
83 const environmentIdPath = serverConfig.environmentIdPath;
84 const methodByOperation = {
85 check: "exists",
86 read: "readFileString",
87 write: "writeFileString",
88 } as const;
89​
90 for (const operation of ["check", "read", "write"] as const) {
91 const writeAttempts: string[] = [];
92 const cause = PlatformError.systemError({
93 _tag: "PermissionDenied",
94 module: "FileSystem",
95 method: methodByOperation[operation],
96 description: "permission denied",
97 pathOrDescriptor: environmentIdPath,
98 });
99 const failingFileSystemLayer = FileSystem.layerNoop({
100 exists: () =>
101 operation === "check" ? Effect.fail(cause) : Effect.succeed(operation === "read"),
102 readFileString: () => Effect.fail(cause),
103 writeFileString: (path) => {
104 writeAttempts.push(path);
105 return Effect.fail(cause);
106 },
107 });
108​
109 const error = yield* Effect.gen(function* () {
110 const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
111 return yield* serverEnvironment.getDescriptor;
112 }).pipe(
113 Effect.provide(
114 ServerEnvironment.layer.pipe(
115 Layer.provide(Layer.merge(ServerConfig.layer(serverConfig), failingFileSystemLayer)),
116 ),
117 ),
118 Effect.flip,
119 );
120​
121 expect(isServerEnvironmentIdPersistenceError(error)).toBe(true);
122 if (!isServerEnvironmentIdPersistenceError(error)) {
123 throw error;
124 }
125 expect(error.operation).toBe(operation);
126 expect(error.environmentIdPath).toBe(environmentIdPath);
127 expect(error.cause).toBe(cause);
128 expect(error.message).toBe(
129 `Server environment ID ${operation} failed at '${environmentIdPath}'.`,
130 );
131 expect(writeAttempts).toEqual(operation === "write" ? [environmentIdPath] : []);
132 }
133 }),
134 );
135});