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

relayClient.test.ts

678 lines24.8 KB

1import { sha256 } from "@noble/hashes/sha2";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { describe, expect, it } from "@effect/vitest";
4import * as ConfigProvider from "effect/ConfigProvider";
5import * as Deferred from "effect/Deferred";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as Hex from "effect/encoding/Hex";
9import * as FileSystem from "effect/FileSystem";
10import * as Layer from "effect/Layer";
11import * as PlatformError from "effect/PlatformError";
12import * as Sink from "effect/Sink";
13import * as Stream from "effect/Stream";
14import { HttpClient, HttpClientResponse } from "effect/http";
15import { ChildProcess, ChildProcessSpawner } from "effect/process";
16import * as HostProcess from "./HostProcess.ts";
17​
18import * as RelayClient from "./relayClient.ts";
19​
20// The suite runs the linux code path against the real filesystem, checking
21// POSIX exec bits that NTFS never reports; the win32 branch skips that check.
22const windowsHost = HostProcess.Platform.defaultValue() === "win32";
23​
24const layerHostRuntime = (env: Record<string, string> = {}, platform: NodeJS.Platform = "linux") =>
25 Layer.mergeAll(
26 Layer.succeed(HostProcess.Platform, platform),
27 Layer.succeed(HostProcess.Architecture, "x64"),
28 Layer.succeed(HostProcess.Environment, env),
29 ConfigProvider.layer(ConfigProvider.fromEnv({ env })),
30 );
31​
32function makeHandle(exitCode = 0, output = "") {
33 const stdout = output ? Stream.make(new TextEncoder().encode(output)) : Stream.empty;
34 return ChildProcessSpawner.makeHandle({
35 pid: ChildProcessSpawner.ProcessId(100),
36 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
37 isRunning: Effect.succeed(false),
38 kill: () => Effect.void,
39 unref: Effect.succeed(Effect.void),
40 stdin: Sink.drain,
41 stdout,
42 stderr: Stream.empty,
43 all: stdout,
44 getInputFd: () => Sink.drain,
45 getOutputFd: () => Stream.empty,
46 });
48​
49const layerHttpClient = (bytes: Uint8Array) =>
50 Layer.succeed(
51 HttpClient.HttpClient,
52 HttpClient.make((request) =>
53 Effect.succeed(
54 HttpClientResponse.fromWeb(request, new Response(bytes.buffer as ArrayBuffer)),
55 ),
56 ),
57 );
58​
59const layerSpawner = (commands: Array<string>, versions: Record<string, string> = {}) =>
60 Layer.succeed(
61 ChildProcessSpawner.ChildProcessSpawner,
62 ChildProcessSpawner.make((command) =>
63 Effect.sync(() => {
64 if (!ChildProcess.isStandardCommand(command)) {
65 commands.push("piped-command");
66 return makeHandle();
67 }
68 commands.push(command.command);
69 if (command.args[0] === "version") {
70 const version = versions[command.command] ?? RelayClient.CLOUDFLARED_VERSION;
71 return makeHandle(0, `cloudflared version ${version} (built 2026-01-01-00:00 UTC)\n`);
72 }
73 return makeHandle();
74 }),
75 ),
76 );
77​
78const writeExecutable = (filePath: string, contents = "cloudflared") =>
79 Effect.gen(function* () {
80 const fileSystem = yield* FileSystem.FileSystem;
81 yield* fileSystem.makeDirectory(filePath.slice(0, filePath.lastIndexOf("/")), {
82 recursive: true,
83 });
84 yield* fileSystem.writeFileString(filePath, contents);
85 yield* fileSystem.chmod(filePath, 0o755);
86 });
87​
88type RelayClientTestServices =
89 | Layer.Success<ReturnType<typeof layerHostRuntime> | typeof NodeServices.layer>
90 | HttpClient.HttpClient
91 | ChildProcessSpawner.ChildProcessSpawner;
92​
93const managedPathFor = (baseDir: string, version: string) =>
94 `${baseDir}/tools/cloudflared/${version}/linux-x64/cloudflared`;
95​
96const renameError = (code: string, path: string) =>
97 PlatformError.systemError({
98 _tag: code === "EBUSY" ? "Busy" : code === "EACCES" ? "PermissionDenied" : "Unknown",
99 module: "FileSystem",
100 method: "rename",
101 pathOrDescriptor: path,
102 cause: Object.assign(new Error(code), { code }),
103 });
104​
105type InstallRename = "staging" | "activation";
106​
107const makeLockedRenames = (
108 fileSystem: FileSystem.FileSystem,
109 input: { readonly rename: InstallRename; readonly failures: number; readonly code: string },
110) =>
111 Effect.gen(function* () {
112 const firstAttempt = yield* Deferred.make<void>();
113 const renames = { attempts: 0 };
114 const locked = FileSystem.make({
115 ...fileSystem,
116 rename: (from, to) =>
117 Effect.suspend(() => {
118 if ((to.endsWith(".tmp") ? "staging" : "activation") !== input.rename) {
119 return fileSystem.rename(from, to);
120 }
121 renames.attempts += 1;
122 return renames.attempts <= input.failures
123 ? Deferred.succeed(firstAttempt, undefined).pipe(
124 Effect.andThen(Effect.fail(renameError(input.code, to))),
125 )
126 : fileSystem.rename(from, to);
127 }),
128 });
129 return { locked, renames, firstAttempt };
130 });
131​
132const testBinary = new TextEncoder().encode("test-cloudflared-binary");
133const testReleaseAsset = {
134 url: "https://example.test/cloudflared",
135 sha256: Hex.encode(sha256(testBinary)),
136 archive: "binary",
137} as const;
138​
139const layerInstallRuntime = (platform: NodeJS.Platform) =>
140 Layer.mergeAll(
141 NodeServices.layer,
142 layerHttpClient(testBinary),
143 layerSpawner([]),
144 layerHostRuntime({ PATH: "" }, platform),
145 );
146​
147describe("RelayClient", () => {
148 it.effect("cancels a contended install without removing the other installer's lock", () =>
149 Effect.gen(function* () {
150 const fileSystem = yield* FileSystem.FileSystem;
151 const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cloudflared-test-" });
152 const directory = `${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/linux-x64`;
153 const lockPath = `${directory}/cloudflared.lock`;
154 yield* fileSystem.makeDirectory(directory, { recursive: true });
155 yield* fileSystem.writeFileString(lockPath, "other-installer");
156 const contended = yield* Deferred.make<void>();
157 const manager = yield* RelayClient.makeCloudflaredRelayClient({ baseDir }).pipe(
158 Effect.provideService(FileSystem.FileSystem, {
159 ...fileSystem,
160 writeFileString: (path, contents, options) =>
161 fileSystem
162 .writeFileString(path, contents, options)
163 .pipe(
164 Effect.tapError(() =>
165 path === lockPath ? Deferred.succeed(contended, undefined) : Effect.void,
166 ),
167 ),
168 }),
169 );
170 const installing = yield* manager.install.pipe(Effect.forkChild);
171 yield* Deferred.await(contended);
172 yield* Fiber.interrupt(installing);
173 expect(yield* fileSystem.readFileString(lockPath)).toBe("other-installer");
174 }).pipe(
175 Effect.scoped,
176 Effect.provide(
177 Layer.mergeAll(
178 NodeServices.layer,
179 layerHttpClient(new Uint8Array()),
180 layerSpawner([]),
181 layerHostRuntime({ PATH: "" }),
182 ),
183 ),
184 ),
185 );
186​
187 it.effect("releases the install lock when a download is cancelled", () =>
188 Effect.gen(function* () {
189 const fileSystem = yield* FileSystem.FileSystem;
190 const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cloudflared-test-" });
191 const lockPath = `${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/linux-x64/cloudflared.lock`;
192 const downloading = yield* Deferred.make<void>();
193 const manager = yield* RelayClient.makeCloudflaredRelayClient({ baseDir });
194 const installing = yield* manager
195 .installWithProgress((event) =>
196 event.type === "progress" && event.stage === "downloading"
197 ? Deferred.succeed(downloading, undefined).pipe(Effect.andThen(Effect.never))
198 : Effect.void,
199 )
200 .pipe(Effect.forkChild);
201 yield* Deferred.await(downloading);
202 expect(yield* fileSystem.exists(lockPath)).toBe(true);
203 yield* Fiber.interrupt(installing);
204 expect(yield* fileSystem.exists(lockPath)).toBe(false);
205 }).pipe(
206 Effect.scoped,
207 Effect.provide(
208 Layer.mergeAll(
209 NodeServices.layer,
210 layerHttpClient(new Uint8Array()),
211 layerSpawner([]),
212 layerHostRuntime({ PATH: "" }),
213 ),
214 ),
215 ),
216 );
217​
218 it.effect.skipIf(windowsHost)("releases a lock acquired while installation is cancelled", () =>
219 Effect.gen(function* () {
220 const fileSystem = yield* FileSystem.FileSystem;
221 const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cloudflared-test-" });
222 const lockPath = `${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/linux-x64/cloudflared.lock`;
223 const acquired = yield* Deferred.make<void>();
224 const completeWrite = yield* Deferred.make<void>();
225 let pauseWrite = true;
226 const manager = yield* RelayClient.makeCloudflaredRelayClient({
227 baseDir,
228 releaseAsset: {
229 url: "https://example.test/cloudflared",
230 sha256: Hex.encode(sha256(new TextEncoder().encode("test-binary"))),
231 archive: "binary",
232 },
233 }).pipe(
234 Effect.provideService(FileSystem.FileSystem, {
235 ...fileSystem,
236 writeFileString: (path, contents, options) =>
237 fileSystem
238 .writeFileString(path, contents, options)
239 .pipe(
240 Effect.tap(() =>
241 path === lockPath && pauseWrite
242 ? Deferred.succeed(acquired, undefined).pipe(
243 Effect.andThen(Deferred.await(completeWrite)),
244 )
245 : Effect.void,
246 ),
247 ),
248 }),
249 );
250​
251 const installing = yield* manager.install.pipe(Effect.forkChild);
252 yield* Deferred.await(acquired);
253 const cancelling = yield* Fiber.interrupt(installing).pipe(
254 Effect.forkChild({ startImmediately: true }),
255 );
256 yield* Deferred.succeed(completeWrite, undefined);
257 yield* Fiber.join(cancelling);
258 expect(yield* fileSystem.exists(lockPath)).toBe(false);
259​
260 pauseWrite = false;
261 expect(yield* manager.install).toMatchObject({ status: "available" });
262 expect(yield* fileSystem.exists(lockPath)).toBe(false);
263 }).pipe(
264 Effect.scoped,
265 Effect.provide(
266 Layer.mergeAll(
267 NodeServices.layer,
268 layerHttpClient(new TextEncoder().encode("test-binary")),
269 layerSpawner([]),
270 layerHostRuntime({ PATH: "" }),
271 ),
272 ),
273 ),
274 );
275​
276 it.effect.skipIf(windowsHost)(
277 "downloads, verifies, validates, and atomically installs the managed executable",
278 () =>
279 Effect.gen(function* () {
280 const fileSystem = yield* FileSystem.FileSystem;
281 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
282 prefix: "t3-cloudflared-test-",
283 });
284 const bytes = new TextEncoder().encode("test-cloudflared-binary");
285 const manager = yield* RelayClient.makeCloudflaredRelayClient({
286 baseDir,
287 releaseAsset: {
288 url: "https://example.test/cloudflared",
289 sha256: Hex.encode(sha256(bytes)),
290 archive: "binary",
291 },
292 });
293​
294 const progress: Array<string> = [];
295 const installed = yield* manager.installWithProgress((event) =>
296 Effect.sync(() => {
297 if (event.type === "progress") {
298 progress.push(event.stage);
299 }
300 }),
301 );
302 const managedPath = `${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/linux-x64/cloudflared`;
303 expect(installed).toEqual({
304 status: "available",
305 executablePath: managedPath,
306 source: "managed",
307 version: RelayClient.CLOUDFLARED_VERSION,
308 });
309 expect(new TextDecoder().decode(yield* fileSystem.readFile(managedPath))).toBe(
310 "test-cloudflared-binary",
311 );
312 expect(progress).toEqual([
313 "checking",
314 "waiting_for_lock",
315 "downloading",
316 "verifying",
317 "installing",
318 "validating",
319 "activating",
320 ]);
321 expect(yield* manager.resolve).toEqual(installed);
322 }).pipe(
323 Effect.scoped,
324 Effect.provide(
325 Layer.mergeAll(
326 NodeServices.layer,
327 layerHttpClient(new TextEncoder().encode("test-cloudflared-binary")),
328 layerSpawner([]),
329 layerHostRuntime(),
330 ),
331 ),
332 ),
333 );
334​
335 it.effect("rejects downloads whose checksum does not match the pinned manifest", () =>
336 Effect.gen(function* () {
337 const fileSystem = yield* FileSystem.FileSystem;
338 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
339 prefix: "t3-cloudflared-test-",
340 });
341 const manager = yield* RelayClient.makeCloudflaredRelayClient({
342 baseDir,
343 releaseAsset: {
344 url: "https://example.test/cloudflared",
345 sha256: Hex.encode(sha256(new TextEncoder().encode("expected"))),
346 archive: "binary",
347 },
348 });
349​
350 const error = yield* manager.install.pipe(Effect.flip);
351 expect(error).toBeInstanceOf(RelayClient.RelayClientInstallError);
352 expect(error.reason).toBe("invalid_checksum");
353 expect(
354 yield* fileSystem.exists(
355 `${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/linux-x64/cloudflared.lock`,
356 ),
357 ).toBe(false);
358 }).pipe(
359 Effect.scoped,
360 Effect.provide(
361 Layer.mergeAll(
362 NodeServices.layer,
363 layerHttpClient(new TextEncoder().encode("tampered")),
364 layerSpawner([]),
365 layerHostRuntime(),
366 ),
367 ),
368 ),
369 );
370​
371 it.effect.skipIf(windowsHost)("serializes concurrent installs within one runtime", () => {
372 const commands: Array<string> = [];
373 const bytes = new TextEncoder().encode("test-cloudflared-binary");
374 return Effect.gen(function* () {
375 const fileSystem = yield* FileSystem.FileSystem;
376 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
377 prefix: "t3-cloudflared-test-",
378 });
379 const manager = yield* RelayClient.makeCloudflaredRelayClient({
380 baseDir,
381 releaseAsset: {
382 url: "https://example.test/cloudflared",
383 sha256: Hex.encode(sha256(bytes)),
384 archive: "binary",
385 },
386 });
387​
388 const [first, second] = yield* Effect.all([manager.install, manager.install], {
389 concurrency: "unbounded",
390 });
391 expect(second).toEqual(first);
392 expect(commands).toHaveLength(2);
393 }).pipe(
394 Effect.scoped,
395 Effect.provide(
396 Layer.mergeAll(
397 NodeServices.layer,
398 layerHttpClient(bytes),
399 layerSpawner(commands),
400 layerHostRuntime(),
401 ),
402 ),
403 );
404 });
405​
406 it.effect("observes hydrated PATH changes after the manager has been constructed", () => {
407 const platform = windowsHost ? "win32" : "linux";
408 const executableName = windowsHost ? "cloudflared.exe" : "cloudflared";
409 const configEnv = { PATH: "" };
410 const hostEnv = { PATH: "" };
411 return Effect.gen(function* () {
412 const fileSystem = yield* FileSystem.FileSystem;
413 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
414 prefix: "t3-cloudflared-test-",
415 });
416 const binDir = `${baseDir}/bin`;
417 const executablePath = `${binDir}/${executableName}`;
418 const resolvedExecutablePath = windowsHost
419 ? executablePath.replaceAll("/", "\\")
420 : executablePath;
421 const manager = yield* RelayClient.makeCloudflaredRelayClient({
422 baseDir,
423 });
424​
425 const resolveWithStaleConfig = manager.resolve.pipe(
426 Effect.provideService(
427 ConfigProvider.ConfigProvider,
428 ConfigProvider.fromEnv({ env: configEnv }),
429 ),
430 Effect.provideService(HostProcess.Environment, hostEnv),
431 );
432​
433 expect(yield* resolveWithStaleConfig).toEqual({
434 status: "missing",
435 version: RelayClient.CLOUDFLARED_VERSION,
436 });
437​
438 yield* fileSystem.makeDirectory(binDir);
439 yield* fileSystem.writeFileString(executablePath, "cloudflared");
440 if (!windowsHost) yield* fileSystem.chmod(executablePath, 0o755);
441 hostEnv.PATH = binDir;
442​
443 expect(yield* resolveWithStaleConfig).toEqual({
444 status: "available",
445 executablePath: resolvedExecutablePath,
446 source: "path",
447 version: RelayClient.CLOUDFLARED_VERSION,
448 });
449​
450 const resolveWithOverride = manager.resolve.pipe(
451 Effect.provideService(
452 ConfigProvider.ConfigProvider,
453 ConfigProvider.fromEnv({
454 env: { PATH: "", T3CODE_CLOUDFLARED_PATH: executablePath },
455 }),
456 ),
457 Effect.provideService(HostProcess.Environment, { PATH: "" }),
458 );
459 expect(yield* resolveWithOverride).toEqual({
460 status: "available",
461 executablePath,
462 source: "override",
463 version: RelayClient.CLOUDFLARED_VERSION,
464 });
465 }).pipe(
466 Effect.scoped,
467 Effect.provide(
468 Layer.mergeAll(
469 NodeServices.layer,
470 layerHttpClient(new Uint8Array()),
471 layerSpawner([]),
472 layerHostRuntime(configEnv, platform),
473 ),
474 ),
475 );
476 });
477​
478 describe("version selection", () => {
479 const run = <A, E>(
480 body: (baseDir: string) => Effect.Effect<A, E, RelayClientTestServices>,
481 options: { readonly versions?: Record<string, string> } = {},
482 ) =>
483 Effect.gen(function* () {
484 const fileSystem = yield* FileSystem.FileSystem;
485 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
486 prefix: "t3-cloudflared-test-",
487 });
488 return yield* body(baseDir);
489 }).pipe(
490 Effect.scoped,
491 Effect.provide(
492 Layer.mergeAll(
493 NodeServices.layer,
494 layerHttpClient(new Uint8Array()),
495 layerSpawner([], options.versions),
496 layerHostRuntime({ PATH: "" }),
497 ),
498 ),
499 );
500​
501 it.effect.skipIf(windowsHost)("skips a PATH binary older than the minimum version", () => {
502 const versions: Record<string, string> = {};
503 return run(
504 (baseDir) =>
505 Effect.gen(function* () {
506 const oldPath = `${baseDir}/old/cloudflared`;
507 const newPath = `${baseDir}/new/cloudflared`;
508 yield* writeExecutable(oldPath);
509 yield* writeExecutable(newPath);
510 versions[oldPath] = "2023.8.2";
511 versions[newPath] = RelayClient.CLOUDFLARED_MIN_VERSION;
512 const manager = yield* RelayClient.makeCloudflaredRelayClient({ baseDir });
513 const env = { PATH: `${baseDir}/old:${baseDir}/new` };
514 expect(
515 yield* manager.resolve.pipe(
516 Effect.provideService(
517 ConfigProvider.ConfigProvider,
518 ConfigProvider.fromEnv({ env }),
519 ),
520 Effect.provideService(HostProcess.Environment, env),
521 ),
522 ).toEqual({
523 status: "available",
524 executablePath: newPath,
525 source: "path",
526 version: RelayClient.CLOUDFLARED_MIN_VERSION,
527 });
528 }),
529 { versions },
530 );
531 });
532​
533 it.effect.skipIf(windowsHost)("reports an outdated override as missing", () => {
534 const versions: Record<string, string> = {};
535 return run(
536 (baseDir) =>
537 Effect.gen(function* () {
538 const overridePath = `${baseDir}/override/cloudflared`;
539 yield* writeExecutable(overridePath);
540 versions[overridePath] = "2025.6.0";
541 const manager = yield* RelayClient.makeCloudflaredRelayClient({ baseDir });
542 const env = { PATH: "", T3CODE_CLOUDFLARED_PATH: overridePath };
543 const withEnv = Effect.provideService(
544 ConfigProvider.ConfigProvider,
545 ConfigProvider.fromEnv({ env }),
546 );
547 expect(yield* manager.resolve.pipe(withEnv)).toEqual({
548 status: "missing",
549 version: RelayClient.CLOUDFLARED_VERSION,
550 });
551 const error = yield* manager.install.pipe(withEnv, Effect.flip);
552 expect(error.reason).toBe("override_missing");
553 }),
554 { versions },
555 );
556 });
557​
558 it.effect.skipIf(windowsHost)(
559 "prefers the pinned release, then the newest older managed release, then PATH",
560 () => {
561 const versions: Record<string, string> = {};
562 return run(
563 (baseDir) =>
564 Effect.gen(function* () {
565 const fileSystem = yield* FileSystem.FileSystem;
566 const pathBinary = `${baseDir}/bin/cloudflared`;
567 const older = managedPathFor(baseDir, "2025.9.0");
568 const newer = managedPathFor(baseDir, "2026.1.0");
569 const pinned = managedPathFor(baseDir, RelayClient.CLOUDFLARED_VERSION);
570 const newerServer = managedPathFor(baseDir, "2099.1.0");
571 versions[newerServer] = "2099.1.0";
572 yield* writeExecutable(newerServer);
573 versions[pathBinary] = "2026.9.3";
574 versions[older] = "2025.9.0";
575 versions[newer] = "2026.1.0";
576 yield* writeExecutable(pathBinary);
577 yield* writeExecutable(older);
578 yield* writeExecutable(newer);
579 const manager = yield* RelayClient.makeCloudflaredRelayClient({ baseDir });
580 const resolveWith = manager.resolve.pipe(
581 Effect.provideService(
582 ConfigProvider.ConfigProvider,
583 ConfigProvider.fromEnv({ env: { PATH: `${baseDir}/bin` } }),
584 ),
585 Effect.provideService(HostProcess.Environment, { PATH: `${baseDir}/bin` }),
586 );
587​
588 expect(yield* resolveWith).toMatchObject({ source: "managed", version: "2026.1.0" });
589​
590 yield* writeExecutable(pinned);
591 expect(yield* resolveWith).toMatchObject({
592 executablePath: pinned,
593 source: "managed",
594 version: RelayClient.CLOUDFLARED_VERSION,
595 });
596​
597 yield* manager.pruneManagedVersions;
598 expect(
599 (yield* fileSystem.readDirectory(`${baseDir}/tools/cloudflared`)).sort(),
600 ).toEqual(["2026.1.0", RelayClient.CLOUDFLARED_VERSION, "2099.1.0"]);
601​
602 yield* fileSystem.remove(pinned);
603 expect(yield* resolveWith).toMatchObject({ source: "managed", version: "2026.1.0" });
604 yield* fileSystem.remove(`${baseDir}/tools/cloudflared/2026.1.0`, {
605 recursive: true,
606 });
607 expect(yield* resolveWith).toMatchObject({ source: "path", version: "2026.9.3" });
608 }),
609 { versions },
610 );
611 },
612 );
613​
614 it.effect.skipIf(windowsHost)(
615 "keeps a self-updated managed binary as a fallback but still installs the pin",
616 () => {
617 const versions: Record<string, string> = {};
618 return run(
619 (baseDir) =>
620 Effect.gen(function* () {
621 const pinned = managedPathFor(baseDir, RelayClient.CLOUDFLARED_VERSION);
622 versions[pinned] = "2026.9.3";
623 yield* writeExecutable(pinned);
624 const manager = yield* RelayClient.makeCloudflaredRelayClient({ baseDir });
625 const resolved = yield* manager.resolve;
626 expect(resolved).toEqual({
627 status: "available",
628 executablePath: pinned,
629 source: "managed",
630 version: "2026.9.3",
631 });
632 expect(
633 resolved.status === "available" && RelayClient.isPinnedManagedRelayClient(resolved),
634 ).toBe(false);
635 }),
636 { versions },
637 );
638 },
639 );
640 });
641​
642 it("orders and parses cloudflared versions", () => {
643 expect(
644 RelayClient.parseCloudflaredVersionOutput(
645 "cloudflared version 2026.5.2 (built 2026-05-20-10:00 UTC)",
646 ),
647 ).toBe("2026.5.2");
648 expect(RelayClient.parseCloudflaredVersionOutput("Incorrect Usage")).toBeNull();
649 expect(RelayClient.compareCloudflaredVersions("2025.10.0", "2025.6.1")).toBeGreaterThan(0);
650 expect(RelayClient.compareCloudflaredVersions("2023.8.2", "2025.6.1")).toBeLessThan(0);
651 });
652​
653 it.effect.each([
654 ["linux", "EBUSY"],
655 ["win32", "EXDEV"],
656 ] as const)("fails staging at once on %s for %s", ([platform, code]) =>
657 Effect.gen(function* () {
658 const fileSystem = yield* FileSystem.FileSystem;
659 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
660 prefix: "t3-cloudflared-test-",
661 });
662 const { locked, renames } = yield* makeLockedRenames(fileSystem, {
663 rename: "staging",
664 failures: Infinity,
665 code,
666 });
667 const manager = yield* RelayClient.makeCloudflaredRelayClient({
668 baseDir,
669 releaseAsset: testReleaseAsset,
670 }).pipe(Effect.provideService(FileSystem.FileSystem, locked));
671​
672 const error = yield* manager.install.pipe(Effect.flip);
673​
674 expect(error.message).toBe("Could not stage the relay client.");
675 expect(renames.attempts).toBe(1);
676 }).pipe(Effect.scoped, Effect.provide(layerInstallRuntime(platform))),
677 );
678});