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

ServerEnvironment.test.ts

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