Repository navigation
Expand file tree
/
Copy pathServerEnvironment.test.ts
More file actions
294 lines (275 loc) · 12.6 KB
/
Copy pathServerEnvironment.test.ts
File metadata and controls
294 lines (275 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { ORCHESTRATION_PROTOCOL_VERSION } from "@t3tools/contracts";
import { expect, it } from "@effect/vitest";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
import {
PUBLISH_AGENT_ACTIVITY_SECRET,
RELAY_ENVIRONMENT_CREDENTIAL_SECRET,
RELAY_URL_SECRET,
} from "../cloud/config.ts";
import * as ServerConfig from "../config.ts";
import * as ServerEnvironment from "./ServerEnvironment.ts";
const layerServerEnvironment = (baseDir: string) =>
ServerEnvironment.layer.pipe(
Layer.provide(ServerSecretStore.layer),
Layer.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
);
const layerEmptySecretStore = Layer.succeed(
ServerSecretStore.ServerSecretStore,
ServerSecretStore.ServerSecretStore.of({
get: () => Effect.succeedNone,
set: () => Effect.void,
create: () => Effect.void,
getOrCreateRandom: () => Effect.succeed(new Uint8Array()),
remove: () => Effect.void,
}),
);
const makeServerConfig = Effect.fn(function* (baseDir: string) {
const derivedPaths = yield* ServerConfig.deriveServerPaths(baseDir, undefined);
return {
...derivedPaths,
logLevel: "Error",
traceMinLevel: "Info",
traceTimingEnabled: true,
traceBatchWindowMs: 200,
traceMaxBytes: 10 * 1024 * 1024,
traceMaxFiles: 10,
otlpTracesUrl: undefined,
otlpMetricsUrl: undefined,
otlpLogsUrl: undefined,
otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
otelEnvironment: OtelEnvironment.none,
cwd: process.cwd(),
baseDir,
mode: "web",
autoBootstrapProjectFromCwd: false,
logWebSocketEvents: false,
tailscaleServeEnabled: false,
tailscaleServePort: 443,
port: 0,
host: undefined,
desktopBootstrapToken: undefined,
staticDir: undefined,
devUrl: undefined,
devAllowedOrigins: [],
noBrowser: false,
startupPresentation: "browser",
} satisfies ServerConfig.ServerConfig["Service"];
});
it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("ServerEnvironmentLive", (it) => {
it.effect("publishes proven install ownership only for manually updated servers", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped();
const prefix = `${baseDir}/node`;
const entry = `${prefix}/lib/node_modules/t3/dist/bin.mjs`;
yield* fs.makeDirectory(`${prefix}/lib/node_modules/t3/dist`, { recursive: true });
yield* fs.makeDirectory(`${prefix}/bin`, { recursive: true });
yield* fs.writeFileString(entry, "");
yield* fs.writeFileString(
`${prefix}/lib/node_modules/t3/package.json`,
'{"name":"t3","version":"0.0.45","bin":{"t3":"./dist/bin.mjs"}}',
);
yield* fs.symlink(entry, `${prefix}/bin/t3`);
const config = yield* makeServerConfig(baseDir);
yield* fs.makeDirectory(config.stateDir, { recursive: true });
for (const mode of ["web", "desktop"] as const) {
const descriptor = yield* Effect.gen(function* () {
const environment = yield* ServerEnvironment.ServerEnvironment;
return yield* environment.getDescriptor;
}).pipe(
Effect.provide(
ServerEnvironment.layer.pipe(
Layer.provide(layerEmptySecretStore),
Layer.provide(ServerConfig.layer({ ...config, mode })),
),
),
Effect.provideService(HostProcess.Arguments, ["node", entry]),
Effect.provideService(HostProcess.IsExecutable, false),
Effect.provideService(HostProcess.Platform, "linux"),
Effect.provideService(HostProcess.Environment, {}),
);
expect(descriptor.capabilities.serverInstallation).toEqual(
mode === "web" ? { kind: "npm-global", prefix } : undefined,
);
expect(descriptor.capabilities.serverSelfUpdate).toBe(
mode === "web" ? undefined : "desktop-managed",
);
expect(descriptor.capabilities).toMatchObject({
settledWorktreeCleanup: true,
storageCleanupRun: true,
projectWorktreeCleanup: true,
worktreesDirectory: true,
threadAutoSettlementScope: true,
});
}
}),
);
it.effect.each([
{ platform: "darwin", architecture: "arm64", supported: true },
{ platform: "linux", architecture: "x64", supported: true },
{ platform: "linux", architecture: "x64", libc: "musl", supported: false },
{ platform: "win32", architecture: "arm64", supported: false },
{ platform: "linux", architecture: "arm", supported: false },
] as const)("advertises voice transcription on $platform-$architecture: $supported", (host) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-server-environment-speech-test-",
});
const descriptor = yield* Effect.gen(function* () {
const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
return yield* serverEnvironment.getDescriptor;
}).pipe(
Effect.provide(layerServerEnvironment(baseDir)),
Effect.provideService(HostProcess.Platform, host.platform),
Effect.provideService(HostProcess.Architecture, host.architecture),
Effect.provideService(HostProcess.LinuxLibc, "libc" in host ? host.libc : "gnu"),
);
expect(descriptor.capabilities.voiceTranscription).toBe(host.supported);
}),
);
it.effect.each([
{ name: "missing", content: undefined },
{ name: "empty", content: "" },
{ name: "whitespace-only", content: " \t\n" },
])("concurrent initializers recover a $name environment id file", ({ content }) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const random = yield* SecureRandom.SecureRandom;
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-server-environment-concurrent-test-",
});
const serverConfig = yield* makeServerConfig(baseDir);
yield* fileSystem.makeDirectory(serverConfig.stateDir, { recursive: true });
if (content !== undefined) {
yield* fileSystem.writeFileString(serverConfig.environmentIdPath, content);
}
const bothGenerated = yield* Deferred.make<void>();
const bothReadEmpty = yield* Deferred.make<void>();
const firstInitialized = yield* Deferred.make<void>();
let remaining = 2;
let emptyReads = 0;
const readIdentity = Effect.gen(function* () {
const identity = yield* ServerEnvironment.ServerEnvironmentIdentity;
return yield* identity.getEnvironmentId;
}).pipe(
Effect.tap(() => Deferred.succeed(firstInitialized, undefined)),
Effect.provide(Layer.fresh(ServerEnvironment.layerIdentity)),
Effect.provideService(ServerConfig.ServerConfig, serverConfig),
Effect.provideService(FileSystem.FileSystem, {
...fileSystem,
readFileString: (path) =>
fileSystem.readFileString(path).pipe(
Effect.tap(
Effect.fn(function* (value) {
if (path !== serverConfig.environmentIdPath || remaining > 0 || value.trim()) {
return;
}
if (++emptyReads === 2) {
yield* Deferred.succeed(bothReadEmpty, undefined);
yield* Deferred.await(firstInitialized);
} else {
yield* Deferred.await(bothReadEmpty);
}
}),
),
),
}),
Effect.provideService(SecureRandom.SecureRandom, {
...random,
uuidV4: (() =>
Effect.gen(function* () {
const id = yield* random.uuidV4();
if (--remaining === 0) {
yield* Deferred.succeed(bothGenerated, undefined);
}
yield* Deferred.await(bothGenerated);
return id;
})) as SecureRandom.SecureRandom["Service"]["uuidV4"],
}),
);
const [first, second] = yield* Effect.all([readIdentity, readIdentity], {
concurrency: "unbounded",
});
const persisted = yield* fileSystem.readFileString(serverConfig.environmentIdPath);
expect(first).toBe(second);
expect(persisted.trim()).toBe(first);
}),
);
it.effect("persists the environment id across service restarts", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-server-environment-test-",
});
const first = yield* Effect.gen(function* () {
const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
return yield* serverEnvironment.getDescriptor;
}).pipe(Effect.provide(layerServerEnvironment(baseDir)));
const second = yield* Effect.gen(function* () {
const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
return yield* serverEnvironment.getDescriptor;
}).pipe(Effect.provide(layerServerEnvironment(baseDir)));
expect(first.environmentId).toBe(second.environmentId);
expect(first.orchestrationProtocolVersion).toBe(ORCHESTRATION_PROTOCOL_VERSION);
expect(second.capabilities.repositoryIdentity).toBe(true);
expect(second.capabilities.connectionProbe).toBe(true);
expect(second.capabilities.attachmentUploads).toBe(true);
expect(second.capabilities.fileAttachments).toEqual({ maxUploadBytes: 50 * 1024 * 1024 });
expect(second.capabilities.pullRequests).toBe(true);
expect(second.capabilities.requiredWorktreeBootstrap).toBe(true);
expect(second.capabilities.usagePriceOverrides).toBe(true);
expect(second.capabilities.threadActiveReorder).toBe(true);
expect(second.capabilities.threadTitleRegeneration).toBe(true);
expect(second.capabilities.threadPullRequests).toBe(true);
expect(second.capabilities.threadPullRequestLinking).toBe(true);
expect(second.capabilities.serverResolvedCommandContext).toBe(true);
expect(second.capabilities.projectProviderInstanceOverrides).toBe(true);
expect(second.capabilities.agentActivityPublishing).toBe(false);
}),
);
it.effect("reports agent activity publishing from the current secret state", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-server-environment-publish-test-",
});
const layerTest = Layer.mergeAll(
ServerEnvironment.layer.pipe(Layer.provide(ServerSecretStore.layer)),
ServerSecretStore.layer,
).pipe(Layer.provide(ServerConfig.layerTest(process.cwd(), baseDir)));
yield* Effect.gen(function* () {
const secrets = yield* ServerSecretStore.ServerSecretStore;
const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
const encode = (value: string) => new TextEncoder().encode(value);
const unlinked = yield* serverEnvironment.getDescriptor;
expect(unlinked.capabilities.agentActivityPublishing).toBe(false);
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, encode("true"));
const withoutLink = yield* serverEnvironment.getDescriptor;
expect(withoutLink.capabilities.agentActivityPublishing).toBe(false);
yield* secrets.set(RELAY_URL_SECRET, encode(""));
yield* secrets.set(RELAY_ENVIRONMENT_CREDENTIAL_SECRET, encode("credential"));
const emptyUrl = yield* serverEnvironment.getDescriptor;
expect(emptyUrl.capabilities.agentActivityPublishing).toBe(false);
yield* secrets.set(RELAY_URL_SECRET, encode("https://relay.example"));
const linked = yield* serverEnvironment.getDescriptor;
expect(linked.capabilities.agentActivityPublishing).toBe(true);
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, encode("false"));
const disabled = yield* serverEnvironment.getDescriptor;
expect(disabled.capabilities.agentActivityPublishing).toBe(false);
}).pipe(Effect.provide(layerTest));
}),
);
});