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

ResourceMonitorBinary.test.ts

155 lines6.6 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";
6import * as Path from "effect/Path";
7​
8import * as ServerConfig from "../config.ts";
9import * as ResourceMonitorBinary from "./ResourceMonitorBinary.ts";
10​
11// The override checks POSIX exec bits on a real file under a linux platform
12// mock; NTFS never reports those bits, so the check cannot be satisfied there.
13const windowsHost = HostProcess.Platform.defaultValue() === "win32";
14​
15describe("ResourceMonitorBinary", () => {
16 afterEach(() => {
17 vi.restoreAllMocks();
18 });
19​
20 it.effect.skipIf(windowsHost)("resolves an executable override on an unsupported platform", () =>
21 Effect.gen(function* () {
22 const fileSystem = yield* FileSystem.FileSystem;
23 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
24 prefix: "t3-resource-monitor-binary-",
25 });
26 const binaryPath = `${baseDir}/custom-resource-monitor`;
27 yield* fileSystem.writeFileString(binaryPath, "binary");
28 yield* fileSystem.chmod(binaryPath, 0o755);
29​
30 const service = yield* ResourceMonitorBinary.make().pipe(
31 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
32 Effect.provideService(HostProcess.Platform, "freebsd"),
33 Effect.provideService(HostProcess.Architecture, "ia32"),
34 Effect.provideService(HostProcess.Environment, {
35 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
36 }),
37 );
38​
39 assert.equal(yield* service.resolve, binaryPath);
40 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
41 );
42​
43 it.effect("rejects a non-executable POSIX override", () =>
44 Effect.gen(function* () {
45 const fileSystem = yield* FileSystem.FileSystem;
46 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
47 prefix: "t3-resource-monitor-binary-",
48 });
49 const binaryPath = `${baseDir}/t3-resource-monitor`;
50 yield* fileSystem.writeFileString(binaryPath, "binary");
51 yield* fileSystem.chmod(binaryPath, 0o644);
52​
53 const service = yield* ResourceMonitorBinary.make().pipe(
54 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
55 Effect.provideService(HostProcess.Platform, "linux"),
56 Effect.provideService(HostProcess.Architecture, "x64"),
57 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "gnu"),
58 Effect.provideService(HostProcess.Environment, {
59 T3CODE_RESOURCE_MONITOR_PATH: binaryPath,
60 }),
61 );
62 const error = yield* Effect.flip(service.resolve);
63​
64 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryNotExecutable);
65 assert.equal(error.path, binaryPath);
66 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
67 );
68​
69 // regression: #16838 a directory resolved as the resource monitor binary
70 it.effect("rejects a directory override", () =>
71 Effect.gen(function* () {
72 const fileSystem = yield* FileSystem.FileSystem;
73 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
74 prefix: "t3-resource-monitor-binary-",
75 });
76​
77 const service = yield* ResourceMonitorBinary.make().pipe(
78 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
79 Effect.provideService(HostProcess.Platform, "win32"),
80 Effect.provideService(HostProcess.Architecture, "arm64"),
81 Effect.provideService(HostProcess.Environment, {
82 T3CODE_RESOURCE_MONITOR_PATH: baseDir,
83 }),
84 );
85 const error = yield* Effect.flip(service.resolve);
86​
87 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryNotExecutable);
88 assert.equal(error.path, baseDir);
89 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
90 );
91​
92 it.effect.each(["release", "debug"])(
93 "resolves a cargo %s binary from the built server layout",
94 (profile) =>
95 Effect.gen(function* () {
96 const fileSystem = yield* FileSystem.FileSystem;
97 const path = yield* Path.Path;
98 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
99 prefix: "t3-resource-monitor-binary-",
100 });
101 const projectDirectory = path.join(baseDir, "repo");
102 const distDirectory = path.join(projectDirectory, "apps", "server", "dist");
103 const binaryDirectory = path.join(
104 projectDirectory,
105 "native",
106 "resource-monitor",
107 "target",
108 profile,
109 );
110 const binaryPath = path.join(binaryDirectory, "t3-resource-monitor.exe");
111 yield* fileSystem.makeDirectory(distDirectory, { recursive: true });
112 yield* fileSystem.makeDirectory(binaryDirectory, { recursive: true });
113 yield* fileSystem.writeFileString(binaryPath, "binary");
114 yield* fileSystem.chmod(binaryPath, 0o755);
115​
116 const service = yield* ResourceMonitorBinary.make().pipe(
117 // The bundled module lives in dist rather than src/resourceTelemetry.
118 Effect.provideService(Path.Path, {
119 ...path,
120 resolve: (...segments) =>
121 path.resolve(
122 ...segments.map((segment) =>
123 segment === import.meta.dirname ? distDirectory : segment,
124 ),
125 ),
126 }),
127 Effect.provide(ServerConfig.layerTest(projectDirectory, baseDir)),
128 Effect.provideService(HostProcess.Platform, "win32"),
129 Effect.provideService(HostProcess.Architecture, "arm64"),
130 Effect.provideService(HostProcess.Environment, {}),
131 );
132​
133 assert.equal(yield* service.resolve, binaryPath);
134 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
135 );
136​
137 it.effect("rejects bundled glibc binaries on musl Linux hosts", () =>
138 Effect.gen(function* () {
139 const fileSystem = yield* FileSystem.FileSystem;
140 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
141 prefix: "t3-resource-monitor-binary-",
142 });
143 const service = yield* ResourceMonitorBinary.make().pipe(
144 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
145 Effect.provideService(HostProcess.Platform, "linux"),
146 Effect.provideService(HostProcess.Architecture, "x64"),
147 Effect.provideService(ResourceMonitorBinary.ResourceMonitorHostLinuxLibc, "musl"),
148 Effect.provideService(HostProcess.Environment, {}),
149 );
150 const error = yield* Effect.flip(service.resolve);
151​
152 assert.instanceOf(error, ResourceMonitorBinary.ResourceMonitorBinaryUnsupported);
153 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
154 );
155});