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

ServerEnvironment.test.ts

132 lines4.8 KB

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