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

ServerEnvironment.test.ts

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