Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import {
3 HostProcessArchitecture,
4 HostProcessEnvironment,
5 HostProcessPlatform,
6} from "@t3tools/shared/hostProcess";
7import { afterEach, assert, describe, expect, it, vi } from "@effect/vitest";
8import * as Effect from "effect/Effect";
9import * as FileSystem from "effect/FileSystem";
10
11import * as ServerConfig from "../config.ts";
12import * as ResourceMonitorBinary from "./ResourceMonitorBinary.ts";
13
14// The override checks POSIX exec bits on a real file under a linux platform
15// mock; NTFS never reports those bits, so the check cannot be satisfied there.
16const windowsHost = HostProcessPlatform.defaultValue() === "win32";
17
18describe("ResourceMonitorBinary", () => {
19 afterEach(() => {
20 vi.restoreAllMocks();
21 });
22
23 it.effect("skips Linux libc detection on Windows", () =>
24 Effect.gen(function* () {
25 const getReport = vi.spyOn(process.report, "getReport").mockImplementation(() => {
26 throw new Error("Linux libc detection must not run on Windows");
27 });
28 const fileSystem = yield* FileSystem.FileSystem;
29 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
30 prefix: "t3-resource-monitor-binary-",
31 });
32 const binaryPath = `${baseDir}/t3-resource-monitor.exe`;
33 yield* fileSystem.writeFileString(binaryPath, "binary");
34
35 const service = yield* ResourceMonitorBinary.make().pipe(
36 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
37 Effect.provideService(HostProcessPlatform, "win32"),
38 Effect.provideService(HostProcessArchitecture, "arm64"),
39 Effect.provideService(HostProcessEnvironment, {
40 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
41 }),
42 );
43
44 assert.equal(yield* service.resolve, binaryPath);
45 expect(getReport).not.toHaveBeenCalled();
46 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
47 );
48
49 it.effect.skipIf(windowsHost)("resolves an executable override", () =>
50 Effect.gen(function* () {
51 const fileSystem = yield* FileSystem.FileSystem;
52 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
53 prefix: "t3-resource-monitor-binary-",
54 });
55 const binaryPath = `${baseDir}/t3-resource-monitor`;
56 yield* fileSystem.writeFileString(binaryPath, "binary");
57 yield* fileSystem.chmod(binaryPath, 0o755);
58
59 const service = yield* ResourceMonitorBinary.make().pipe(
60 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
61 Effect.provideService(HostProcessPlatform, "linux"),
62 Effect.provideService(HostProcessArchitecture, "x64"),
63 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "musl"),
64 Effect.provideService(HostProcessEnvironment, {
65 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
66 }),
67 );
68
69 assert.equal(yield* service.resolve, binaryPath);
70 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
71 );
72
73 it.effect.skipIf(windowsHost)("resolves an executable override on an unsupported platform", () =>
74 Effect.gen(function* () {
75 const fileSystem = yield* FileSystem.FileSystem;
76 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
77 prefix: "t3-resource-monitor-binary-",
78 });
79 const binaryPath = `${baseDir}/custom-resource-monitor`;
80 yield* fileSystem.writeFileString(binaryPath, "binary");
81 yield* fileSystem.chmod(binaryPath, 0o755);
82
83 const service = yield* ResourceMonitorBinary.make().pipe(
84 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
85 Effect.provideService(HostProcessPlatform, "freebsd"),
86 Effect.provideService(HostProcessArchitecture, "ia32"),
87 Effect.provideService(HostProcessEnvironment, {
88 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
89 }),
90 );
91
92 assert.equal(yield* service.resolve, binaryPath);
93 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
94 );
95
96 it.effect("rejects a non-executable POSIX override", () =>
97 Effect.gen(function* () {
98 const fileSystem = yield* FileSystem.FileSystem;
99 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
100 prefix: "t3-resource-monitor-binary-",
101 });
102 const binaryPath = `${baseDir}/t3-resource-monitor`;
103 yield* fileSystem.writeFileString(binaryPath, "binary");
104 yield* fileSystem.chmod(binaryPath, 0o644);
105
106 const service = yield* ResourceMonitorBinary.make().pipe(
107 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
108 Effect.provideService(HostProcessPlatform, "linux"),
109 Effect.provideService(HostProcessArchitecture, "x64"),
110 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "gnu"),
111 Effect.provideService(HostProcessEnvironment, {
112 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
113 }),
114 );
115 const error = yield* Effect.flip(service.resolve);
116
117 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryNotExecutable);
118 assert.equal(error.path, binaryPath);
119 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
120 );
121
122 it.effect("rejects unsupported platform and architecture pairs", () =>
123 Effect.gen(function* () {
124 const fileSystem = yield* FileSystem.FileSystem;
125 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
126 prefix: "t3-resource-monitor-binary-",
127 });
128 const service = yield* ResourceMonitorBinary.make().pipe(
129 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
130 Effect.provideService(HostProcessPlatform, "freebsd"),
131 Effect.provideService(HostProcessArchitecture, "ia32"),
132 Effect.provideService(HostProcessEnvironment, {}),
133 );
134 const error = yield* Effect.flip(service.resolve);
135
136 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryUnsupported);
137 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
138 );
139
140 it.effect("rejects bundled glibc binaries on musl Linux hosts", () =>
141 Effect.gen(function* () {
142 const fileSystem = yield* FileSystem.FileSystem;
143 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
144 prefix: "t3-resource-monitor-binary-",
145 });
146 const service = yield* ResourceMonitorBinary.make().pipe(
147 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
148 Effect.provideService(HostProcessPlatform, "linux"),
149 Effect.provideService(HostProcessArchitecture, "x64"),
150 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "musl"),
151 Effect.provideService(HostProcessEnvironment, {}),
152 );
153 const error = yield* Effect.flip(service.resolve);
154
155 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryUnsupported);
156 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
157 );
158});