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

ResourceMonitorBinary.test.ts

109 lines4.7 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import { afterEach, assert, describe, it, vi } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6​
7import * as ServerConfig from "../config.ts";
8import * as ResourceMonitorBinary from "./ResourceMonitorBinary.ts";
9​
10// The override checks POSIX exec bits on a real file under a linux platform
11// mock; NTFS never reports those bits, so the check cannot be satisfied there.
12const windowsHost = HostProcess.Platform.defaultValue() === "win32";
13​
14describe("ResourceMonitorBinary", () => {
15 afterEach(() => {
16 vi.restoreAllMocks();
17 });
18​
19 it.effect.skipIf(windowsHost)("resolves an executable override on an unsupported platform", () =>
20 Effect.gen(function* () {
21 const fileSystem = yield* FileSystem.FileSystem;
22 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
23 prefix: "t3-resource-monitor-binary-",
24 });
25 const binaryPath = `${baseDir}/custom-resource-monitor`;
26 yield* fileSystem.writeFileString(binaryPath, "binary");
27 yield* fileSystem.chmod(binaryPath, 0o755);
28​
29 const service = yield* ResourceMonitorBinary.make().pipe(
30 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
31 Effect.provideService(HostProcess.Platform, "freebsd"),
32 Effect.provideService(HostProcess.Architecture, "ia32"),
33 Effect.provideService(HostProcess.Environment, {
34 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
35 }),
36 );
37​
38 assert.equal(yield* service.resolve, binaryPath);
39 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
40 );
41​
42 it.effect("rejects a non-executable POSIX override", () =>
43 Effect.gen(function* () {
44 const fileSystem = yield* FileSystem.FileSystem;
45 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
46 prefix: "t3-resource-monitor-binary-",
47 });
48 const binaryPath = `${baseDir}/t3-resource-monitor`;
49 yield* fileSystem.writeFileString(binaryPath, "binary");
50 yield* fileSystem.chmod(binaryPath, 0o644);
51​
52 const service = yield* ResourceMonitorBinary.make().pipe(
53 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
54 Effect.provideService(HostProcess.Platform, "linux"),
55 Effect.provideService(HostProcess.Architecture, "x64"),
56 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "gnu"),
57 Effect.provideService(HostProcess.Environment, {
58 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
59 }),
60 );
61 const error = yield* Effect.flip(service.resolve);
62​
63 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryNotExecutable);
64 assert.equal(error.path, binaryPath);
65 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
66 );
67​
68 // regression: #16838 a directory resolved as the resource monitor binary
69 it.effect("rejects a directory override", () =>
70 Effect.gen(function* () {
71 const fileSystem = yield* FileSystem.FileSystem;
72 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
73 prefix: "t3-resource-monitor-binary-",
74 });
75​
76 const service = yield* ResourceMonitorBinary.make().pipe(
77 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
78 Effect.provideService(HostProcess.Platform, "win32"),
79 Effect.provideService(HostProcess.Architecture, "arm64"),
80 Effect.provideService(HostProcess.Environment, {
81 T3CODE_RESOURCE_MONITOR_PATH: baseDir,
82 }),
83 );
84 const error = yield* Effect.flip(service.resolve);
85​
86 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryNotExecutable);
87 assert.equal(error.path, baseDir);
88 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
89 );
90​
91 it.effect("rejects bundled glibc binaries on musl Linux hosts", () =>
92 Effect.gen(function* () {
93 const fileSystem = yield* FileSystem.FileSystem;
94 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
95 prefix: "t3-resource-monitor-binary-",
96 });
97 const service = yield* ResourceMonitorBinary.make().pipe(
98 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
99 Effect.provideService(HostProcess.Platform, "linux"),
100 Effect.provideService(HostProcess.Architecture, "x64"),
101 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "musl"),
102 Effect.provideService(HostProcess.Environment, {}),
103 );
104 const error = yield* Effect.flip(service.resolve);
105​
106 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryUnsupported);
107 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
108 );
109});