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

relayClient.test.ts

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