1import { assert, describe, it } from "@effect/vitest";
2import * as ConfigProvider from "effect/ConfigProvider";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as HostProcess from "@t3tools/shared/HostProcess";
6
7import { getDefaultBuildArch } from "./build-target-arch.ts";
8
9const compactEnv = (env: Readonly<Record<string, string | undefined>>): Record<string, string> =>
10 Object.fromEntries(
11 Object.entries(env).filter((entry): entry is [string, string] => entry[1] !== undefined),
12 );
13
14const withHostRuntime = (
15 platform: NodeJS.Platform,
16 arch: NodeJS.Architecture,
17 env: Readonly<Record<string, string | undefined>> = {},
18) =>
19 Effect.provide(
20 Layer.mergeAll(
21 Layer.succeed(HostProcess.Platform, platform),
22 Layer.succeed(HostProcess.Architecture, arch),
23 ConfigProvider.layer(ConfigProvider.fromEnv({ env: compactEnv(env) })),
24 ),
25 );
26
27describe("build-target-arch", () => {
28 it.effect("uses the resolved host arch when selecting the default Windows build arch", () =>
29 Effect.gen(function* () {
30 // This mirrors the packaging script's default-path behavior: the current
31 // process is x64, but the machine itself is ARM64, so the default build
32 // target should be win-arm64 rather than win-x64.
33 const arch = yield* getDefaultBuildArch("win", { archChoices: ["x64", "arm64"] }).pipe(
34 withHostRuntime("win32", "x64", {
35 PROCESSOR_ARCHITECTURE: "AMD64", // The currently running Node process is x64.
36 PROCESSOR_ARCHITEW6432: "ARM64", // The process is x64, but the actual Windows host is ARM64.
37 }),
38 );
39
40 assert.equal(arch, "arm64");
41 }),
42 );
43
44 it.effect("does not apply Windows host env heuristics for non-Windows targets", () =>
45 Effect.gen(function* () {
46 const arch = yield* getDefaultBuildArch("linux", { archChoices: ["x64", "arm64"] }).pipe(
47 withHostRuntime("linux", "x64", {
48 PROCESSOR_ARCHITECTURE: "AMD64",
49 PROCESSOR_ARCHITEW6432: "ARM64",
50 }),
51 );
52
53 assert.equal(arch, "x64");
54 }),
55 );
56});