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 { assert, describe, it } from "@effect/vitest";
8import * as Effect from "effect/Effect";
9import * as FileSystem from "effect/FileSystem";
10
11import { ServerConfig } from "../config.ts";
12import * as ResourceMonitorBinary from "./ResourceMonitorBinary.ts";
13
14describe("ResourceMonitorBinary", () => {
15 it.effect("resolves an executable override", () =>
16 Effect.gen(function* () {
17 const fileSystem = yield* FileSystem.FileSystem;
18 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
19 prefix: "t3-resource-monitor-binary-",
20 });
21 const binaryPath = `${baseDir}/t3-resource-monitor`;
22 yield* fileSystem.writeFileString(binaryPath, "binary");
23 yield* fileSystem.chmod(binaryPath, 0o755);
24
25 const service = yield* ResourceMonitorBinary.make().pipe(
26 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
27 Effect.provideService(HostProcessPlatform, "linux"),
28 Effect.provideService(HostProcessArchitecture, "x64"),
29 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "musl"),
30 Effect.provideService(HostProcessEnvironment, {
31 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
32 }),
33 );
34
35 assert.equal(yield* service.resolve, binaryPath);
36 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
37 );
38
39 it.effect("resolves an executable override on an unsupported platform", () =>
40 Effect.gen(function* () {
41 const fileSystem = yield* FileSystem.FileSystem;
42 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
43 prefix: "t3-resource-monitor-binary-",
44 });
45 const binaryPath = `${baseDir}/custom-resource-monitor`;
46 yield* fileSystem.writeFileString(binaryPath, "binary");
47 yield* fileSystem.chmod(binaryPath, 0o755);
48
49 const service = yield* ResourceMonitorBinary.make().pipe(
50 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
51 Effect.provideService(HostProcessPlatform, "freebsd"),
52 Effect.provideService(HostProcessArchitecture, "ia32"),
53 Effect.provideService(HostProcessEnvironment, {
54 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
55 }),
56 );
57
58 assert.equal(yield* service.resolve, binaryPath);
59 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
60 );
61
62 it.effect("rejects a non-executable POSIX override", () =>
63 Effect.gen(function* () {
64 const fileSystem = yield* FileSystem.FileSystem;
65 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
66 prefix: "t3-resource-monitor-binary-",
67 });
68 const binaryPath = `${baseDir}/t3-resource-monitor`;
69 yield* fileSystem.writeFileString(binaryPath, "binary");
70 yield* fileSystem.chmod(binaryPath, 0o644);
71
72 const service = yield* ResourceMonitorBinary.make().pipe(
73 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
74 Effect.provideService(HostProcessPlatform, "linux"),
75 Effect.provideService(HostProcessArchitecture, "x64"),
76 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "gnu"),
77 Effect.provideService(HostProcessEnvironment, {
78 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
79 }),
80 );
81 const error = yield* Effect.flip(service.resolve);
82
83 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryNotExecutable);
84 assert.equal(error.path, binaryPath);
85 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
86 );
87
88 it.effect("rejects unsupported platform and architecture pairs", () =>
89 Effect.gen(function* () {
90 const fileSystem = yield* FileSystem.FileSystem;
91 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
92 prefix: "t3-resource-monitor-binary-",
93 });
94 const service = yield* ResourceMonitorBinary.make().pipe(
95 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
96 Effect.provideService(HostProcessPlatform, "freebsd"),
97 Effect.provideService(HostProcessArchitecture, "ia32"),
98 Effect.provideService(HostProcessEnvironment, {}),
99 );
100 const error = yield* Effect.flip(service.resolve);
101
102 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryUnsupported);
103 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
104 );
105
106 it.effect("rejects bundled glibc binaries on musl Linux hosts", () =>
107 Effect.gen(function* () {
108 const fileSystem = yield* FileSystem.FileSystem;
109 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
110 prefix: "t3-resource-monitor-binary-",
111 });
112 const service = yield* ResourceMonitorBinary.make().pipe(
113 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
114 Effect.provideService(HostProcessPlatform, "linux"),
115 Effect.provideService(HostProcessArchitecture, "x64"),
116 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "musl"),
117 Effect.provideService(HostProcessEnvironment, {}),
118 );
119 const error = yield* Effect.flip(service.resolve);
120
121 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryUnsupported);
122 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
123 );
124});