|
| 1 | +import { expect, it } from "@effect/vitest"; |
| 2 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as FileSystem from "effect/FileSystem"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 7 | + |
| 8 | +import * as ProcessRunner from "../processRunner.ts"; |
| 9 | +import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts"; |
| 10 | + |
| 11 | +const resolveLinuxLabel = (input: { |
| 12 | + readonly kernelRelease: string; |
| 13 | + readonly env: NodeJS.ProcessEnv; |
| 14 | +}) => |
| 15 | + resolveServerEnvironmentLabel({ cwdBaseName: "project" }).pipe( |
| 16 | + Effect.provideService( |
| 17 | + ProcessRunner.ProcessRunner, |
| 18 | + ProcessRunner.ProcessRunner.of({ |
| 19 | + run: () => |
| 20 | + Effect.succeed({ |
| 21 | + stdout: "", |
| 22 | + stderr: "", |
| 23 | + code: ChildProcessSpawner.ExitCode(1), |
| 24 | + timedOut: false, |
| 25 | + stdoutTruncated: false, |
| 26 | + stderrTruncated: false, |
| 27 | + stdoutInvalidUtf8: false, |
| 28 | + stderrInvalidUtf8: false, |
| 29 | + }), |
| 30 | + }), |
| 31 | + ), |
| 32 | + Effect.provide( |
| 33 | + Layer.mergeAll( |
| 34 | + FileSystem.layerNoop({ |
| 35 | + exists: () => Effect.succeed(false), |
| 36 | + readFileString: (path) => |
| 37 | + path === "/proc/sys/kernel/osrelease" |
| 38 | + ? Effect.succeed(input.kernelRelease) |
| 39 | + : Effect.die(`unexpected read: ${path}`), |
| 40 | + }), |
| 41 | + Layer.succeed(HostProcess.Platform, "linux"), |
| 42 | + Layer.succeed(HostProcess.Hostname, "DESKTOP-PC"), |
| 43 | + Layer.succeed(HostProcess.Environment, input.env), |
| 44 | + ), |
| 45 | + ), |
| 46 | + ); |
| 47 | + |
| 48 | +it.effect("labels a WSL server with its host and distro", () => |
| 49 | + Effect.gen(function* () { |
| 50 | + const label = yield* resolveLinuxLabel({ |
| 51 | + kernelRelease: "6.6.87.2-microsoft-standard-WSL2\n", |
| 52 | + env: { WSL_DISTRO_NAME: "Ubuntu" }, |
| 53 | + }); |
| 54 | + expect(label).toBe("DESKTOP-PC WSL (Ubuntu)"); |
| 55 | + }), |
| 56 | +); |
| 57 | + |
| 58 | +it.effect("labels a WSL server without a distro name by its host", () => |
| 59 | + Effect.gen(function* () { |
| 60 | + const label = yield* resolveLinuxLabel({ |
| 61 | + kernelRelease: "6.6.87.2-microsoft-standard-WSL2\n", |
| 62 | + env: {}, |
| 63 | + }); |
| 64 | + expect(label).toBe("DESKTOP-PC WSL"); |
| 65 | + }), |
| 66 | +); |
| 67 | + |
| 68 | +it.effect("keeps the hostname for non-WSL Linux", () => |
| 69 | + Effect.gen(function* () { |
| 70 | + const label = yield* resolveLinuxLabel({ |
| 71 | + kernelRelease: "6.8.0-45-generic\n", |
| 72 | + env: { WSL_DISTRO_NAME: "Ubuntu" }, |
| 73 | + }); |
| 74 | + expect(label).toBe("DESKTOP-PC"); |
| 75 | + }), |
| 76 | +); |
0 commit comments