Open History to see the last commit
1import { afterEach, describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import { vi } from "vite-plus/test";
6
7import { ProcessRunner, ProcessSpawnError, type ProcessRunnerShape } from "../../processRunner.ts";
8import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
9import { ChildProcessSpawner } from "effect/unstable/process";
10
11const runMock = vi.fn<ProcessRunnerShape["run"]>();
12
13const ProcessRunnerTest = Layer.succeed(
14 ProcessRunner,
15 ProcessRunner.of({
16 run: (input) => runMock(input),
17 }),
18);
19const NoopFileSystemLayer = FileSystem.layerNoop({});
20const TestLayer = Layer.merge(NoopFileSystemLayer, ProcessRunnerTest);
21const LinuxMachineInfoLayer = Layer.merge(
22 ProcessRunnerTest,
23 FileSystem.layerNoop({
24 exists: (path) => Effect.succeed(path === "/etc/machine-info"),
25 readFileString: (path) =>
26 path === "/etc/machine-info"
27 ? Effect.succeed('PRETTY_HOSTNAME="Build Agent 01"\nICON_NAME="computer-vm"\n')
28 : Effect.succeed(""),
29 }),
30);
31
32afterEach(() => {
33 runMock.mockReset();
34});
35
36describe("resolveServerEnvironmentLabel", () => {
37 it.effect("uses hostname fallback regardless of launch mode", () =>
38 Effect.gen(function* () {
39 const result = yield* resolveServerEnvironmentLabel({
40 cwdBaseName: "t3code",
41 platform: "win32",
42 hostname: "macbook-pro",
43 }).pipe(Effect.provide(TestLayer));
44
45 expect(result).toBe("macbook-pro");
46 }),
47 );
48
49 it.effect("prefers the macOS ComputerName", () =>
50 Effect.gen(function* () {
51 runMock.mockReturnValueOnce(
52 Effect.succeed({
53 stdout: " Julius's MacBook Pro \n",
54 stderr: "",
55 code: ChildProcessSpawner.ExitCode(0),
56 timedOut: false,
57 stdoutTruncated: false,
58 stderrTruncated: false,
59 }),
60 );
61
62 const result = yield* resolveServerEnvironmentLabel({
63 cwdBaseName: "t3code",
64 platform: "darwin",
65 hostname: "macbook-pro",
66 }).pipe(Effect.provide(TestLayer));
67
68 expect(result).toBe("Julius's MacBook Pro");
69 expect(runMock).toHaveBeenCalledWith(
70 expect.objectContaining({
71 command: "scutil",
72 args: ["--get", "ComputerName"],
73 timeoutBehavior: "timedOutResult",
74 }),
75 );
76 }),
77 );
78
79 it.effect("prefers Linux PRETTY_HOSTNAME from machine-info", () =>
80 Effect.gen(function* () {
81 const result = yield* resolveServerEnvironmentLabel({
82 cwdBaseName: "t3code",
83 platform: "linux",
84 hostname: "buildbox",
85 }).pipe(Effect.provide(LinuxMachineInfoLayer));
86
87 expect(result).toBe("Build Agent 01");
88 expect(runMock).not.toHaveBeenCalled();
89 }),
90 );
91
92 it.effect("falls back to hostnamectl pretty hostname on Linux", () =>
93 Effect.gen(function* () {
94 runMock.mockReturnValueOnce(
95 Effect.succeed({
96 stdout: "CI Runner\n",
97 stderr: "",
98 code: ChildProcessSpawner.ExitCode(0),
99 timedOut: false,
100 stdoutTruncated: false,
101 stderrTruncated: false,
102 }),
103 );
104
105 const result = yield* resolveServerEnvironmentLabel({
106 cwdBaseName: "t3code",
107 platform: "linux",
108 hostname: "runner-01",
109 }).pipe(Effect.provide(TestLayer));
110
111 expect(result).toBe("CI Runner");
112 expect(runMock).toHaveBeenCalledWith(
113 expect.objectContaining({
114 command: "hostnamectl",
115 args: ["--pretty"],
116 timeoutBehavior: "timedOutResult",
117 }),
118 );
119 }),
120 );
121
122 it.effect("falls back to the hostname when friendly labels are unavailable", () =>
123 Effect.gen(function* () {
124 const result = yield* resolveServerEnvironmentLabel({
125 cwdBaseName: "t3code",
126 platform: "win32",
127 hostname: "JULIUS-LAPTOP",
128 }).pipe(Effect.provide(TestLayer));
129
130 expect(result).toBe("JULIUS-LAPTOP");
131 }),
132 );
133
134 it.effect("falls back to the hostname when the friendly-label command is missing", () =>
135 Effect.gen(function* () {
136 runMock.mockReturnValueOnce(
137 Effect.fail(
138 new ProcessSpawnError({
139 command: "scutil",
140 args: ["--get", "ComputerName"],
141 cause: new Error("spawn scutil ENOENT"),
142 }),
143 ),
144 );
145
146 const result = yield* resolveServerEnvironmentLabel({
147 cwdBaseName: "t3code",
148 platform: "darwin",
149 hostname: "macbook-pro",
150 }).pipe(Effect.provide(TestLayer));
151
152 expect(result).toBe("macbook-pro");
153 }),
154 );
155
156 it.effect("falls back to the cwd basename when the hostname is blank", () =>
157 Effect.gen(function* () {
158 runMock.mockReturnValueOnce(
159 Effect.succeed({
160 stdout: " ",
161 stderr: "",
162 code: ChildProcessSpawner.ExitCode(0),
163 timedOut: false,
164 stdoutTruncated: false,
165 stderrTruncated: false,
166 }),
167 );
168
169 const result = yield* resolveServerEnvironmentLabel({
170 cwdBaseName: "t3code",
171 platform: "linux",
172 hostname: " ",
173 }).pipe(Effect.provide(TestLayer));
174
175 expect(result).toBe("t3code");
176 }),
177 );
178});