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

ServerEnvironmentLabel.test.ts

104 lines3.6 KB

1import { assert, it } from "@effect/vitest";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import * as Effect from "effect/Effect";
4import * as Fiber from "effect/Fiber";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import * as TestClock from "effect/testing/TestClock";
10import { ChildProcessSpawner } from "effect/process";
11​
12import * as ProcessRunner from "../processRunner.ts";
13import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
14​
15const neverExits = ChildProcessSpawner.make(() =>
16 Effect.succeed(
17 ChildProcessSpawner.makeHandle({
18 pid: ChildProcessSpawner.ProcessId(1),
19 exitCode: Effect.never,
20 exitStatus: Effect.never,
21 isRunning: Effect.succeed(true),
22 kill: () => Effect.void,
23 unref: Effect.succeed(Effect.void),
24 stdin: Sink.drain,
25 stdout: Stream.never,
26 stderr: Stream.never,
27 all: Stream.empty,
28 getInputFd: () => Sink.drain,
29 getOutputFd: () => Stream.empty,
30 }),
31 ),
32);
33​
34it.effect("falls back to the hostname when hostnamectl never answers", () =>
35 Effect.gen(function* () {
36 // Without /etc/machine-info the label comes from hostnamectl, which waits on D-Bus.
37 const label = yield* resolveServerEnvironmentLabel({ cwdBaseName: "project" }).pipe(
38 Effect.provideService(HostProcess.Platform, "linux"),
39 Effect.provideService(HostProcess.Hostname, "devbox"),
40 Effect.provide(
41 Layer.mergeAll(
42 FileSystem.layerNoop({ exists: () => Effect.succeed(false) }),
43 ProcessRunner.layer.pipe(
44 Layer.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, neverExits)),
45 ),
46 ),
47 ),
48 Effect.forkChild,
49 );
50 yield* TestClock.adjust("5 seconds");
51 assert.equal(yield* Fiber.join(label), "devbox");
52 }),
53);
54​
55const exitsWithFailure = ProcessRunner.ProcessRunner.of({
56 run: () =>
57 Effect.succeed({
58 stdout: "",
59 stderr: "",
60 code: ChildProcessSpawner.ExitCode(1),
61 timedOut: false,
62 stdoutTruncated: false,
63 stderrTruncated: false,
64 stdoutInvalidUtf8: false,
65 stderrInvalidUtf8: false,
66 }),
67});
68​
69it.effect("qualifies the inherited Windows hostname only inside WSL", () =>
70 Effect.gen(function* () {
71 const cases = [
72 {
73 kernel: "6.6.87.2-microsoft-standard-WSL2",
74 machineInfo: null,
75 expected: "DESKTOP-PC WSL (Ubuntu)",
76 },
77 { kernel: "6.8.0-45-generic", machineInfo: null, expected: "DESKTOP-PC" },
78 {
79 kernel: "6.6.87.2-microsoft-standard-WSL2",
80 machineInfo: 'PRETTY_HOSTNAME="Work WSL"',
81 expected: "Work WSL",
82 },
83 ];
84 for (const { kernel, machineInfo, expected } of cases) {
85 const files: Record<string, string | null> = {
86 "/proc/sys/kernel/osrelease": `${kernel}\n`,
87 "/etc/machine-info": machineInfo,
88 };
89 const label = yield* resolveServerEnvironmentLabel({ cwdBaseName: "project" }).pipe(
90 Effect.provideService(HostProcess.Platform, "linux"),
91 Effect.provideService(HostProcess.Hostname, "DESKTOP-PC"),
92 Effect.provideService(HostProcess.Environment, { WSL_DISTRO_NAME: "Ubuntu" }),
93 Effect.provideService(ProcessRunner.ProcessRunner, exitsWithFailure),
94 Effect.provide(
95 FileSystem.layerNoop({
96 exists: (path) => Effect.succeed(files[path] != null),
97 readFileString: (path) => Effect.succeed(files[path] ?? ""),
98 }),
99 ),
100 );
101 assert.equal(label, expected, kernel);
102 }
103 }),
104);