Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { standardCommand } from "./test-support/standard-command.ts";
import { assert, it } from "@effect/vitest";
import * as ConfigProvider from "effect/ConfigProvider";
import * as FileSystem from "effect/FileSystem";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import { ChildProcessSpawner } from "effect/unstable/process";
import {
BundleNotSelfContainedError,
createStageWorkspaceConfig,
createStagePatchedDependencies,
createBuildConfig,
DESKTOP_EXTRA_RESOURCES,
DESKTOP_PLUGIN_CATALOG_RESOURCE_SOURCE_DIR,
MAC_FILE_EXCLUSIONS,
UnsupportedDesktopBuildArchitectureError,
packWindowsServerAsar,
resolveMacStageDependencies,
resolveFffNativeDependencies,
resolveBuildOptions,
resolveResourceMonitorRustTargets,
resolveResourceMonitorCargoBuildArgs,
resolveWindowsServerAsarIgnoreGlobs,
resourceMonitorExecutableName,
validateWindowsPackagedPayload,
WindowsPrimaryNativeProbeError,
WindowsPackagedPayloadValidationError,
WINDOWS_PACKAGED_PAYLOAD_FILE_LIMIT,
WINDOWS_SERVER_ASAR_IGNORE_GLOBS,
} from "./build-desktop-artifact.ts";
import { HostProcessArchitecture, HostProcessPlatform } from "@akeru/shared/hostProcess";
import { mockProcess, makeWindowsPayloadFixture } from "./test-support/desktop-build.ts";
it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
it("carries only staged dependency patch metadata into staged desktop installs", () => {
assert.deepStrictEqual(
createStagePatchedDependencies(
{
"@expo/[email protected]": "patches/@expo%[email protected]",
"@ff-labs/[email protected]": "patches/@[email protected]",
"@pierre/[email protected]": "patches/@pierre%[email protected]",
"[email protected]": "patches/[email protected]",
"[email protected]": "patches/[email protected]",
},
{
"@ff-labs/fff-node": "0.9.4",
"@pierre/diffs": "1.1.20",
effect: "4.0.0-beta.73",
},
),
{
"@ff-labs/[email protected]": "patches/@[email protected]",
"@pierre/[email protected]": "patches/@pierre%[email protected]",
"[email protected]": "patches/[email protected]",
},
);
assert.deepStrictEqual(
createStagePatchedDependencies(
{
"@expo/[email protected]": "patches/@expo%[email protected]",
},
{ effect: "4.0.0-beta.73" },
),
{},
);
});
it("stages pnpm 11 allowBuilds and patchedDependencies in the workspace yaml", () => {
assert.deepStrictEqual(
createStageWorkspaceConfig({
platform: "linux",
arch: "x64",
allowBuilds: {
electron: true,
"node-pty": true,
"browser-tabs-lock": false,
},
patchedDependencies: {
"[email protected]": "patches/[email protected]",
},
overrides: {
effect: "4.0.0-beta.73",
},
}),
{
supportedArchitectures: {
os: ["linux"],
cpu: ["x64"],
libc: ["glibc"],
},
allowBuilds: {
electron: true,
"node-pty": true,
"browser-tabs-lock": false,
},
patchedDependencies: {
"[email protected]": "patches/[email protected]",
},
overrides: {
effect: "4.0.0-beta.73",
},
},
);
// Empty maps must not be written — pnpm would still require reviewed
// packages if allowBuilds is present but incomplete, and omitting empty
// patchedDependencies keeps the stage yaml minimal.
assert.deepStrictEqual(
createStageWorkspaceConfig({
platform: "mac",
arch: "arm64",
allowBuilds: {},
patchedDependencies: {},
overrides: {},
}),
{
supportedArchitectures: {
os: ["darwin"],
cpu: ["arm64"],
},
},
);
});
it("excludes Windows terminal binaries only from macOS packages", () => {
assert.deepStrictEqual(MAC_FILE_EXCLUSIONS, [
"!**/node_modules/node-pty/prebuilds/win32-*/**/*",
"!**/node_modules/node-pty/third_party/conpty/**/*",
]);
});
it("stages only server runtime externals in macOS packages", () => {
assert.deepStrictEqual(
resolveMacStageDependencies({
serverDependencies: {
"@anthropic-ai/claude-agent-sdk": "^0.3.170",
"@ff-labs/fff-node": "0.9.4",
"@opencode-ai/sdk": "^1.3.15",
"@pierre/diffs": "1.3.0",
execa: "9.6.1",
libsql: "0.5.29",
"msgpackr-extract": "3.0.4",
"node-pty": "1.1.0",
"playwright-core": "1.60.0",
},
desktopDependencies: {
"@example/desktop-runtime": "1.0.0",
effect: "4.0.0-beta.103",
},
arch: "arm64",
fffNodeVersion: "0.9.4",
}),
{
"@ff-labs/fff-node": "0.9.4",
execa: "9.6.1",
libsql: "0.5.29",
"msgpackr-extract": "3.0.4",
"node-pty": "1.1.0",
"playwright-core": "1.60.0",
"@example/desktop-runtime": "1.0.0",
effect: "4.0.0-beta.103",
"@ff-labs/fff-bin-darwin-arm64": "0.9.4",
},
);
});
it("excludes node-pty binaries for the other Windows architecture", () => {
assert.deepStrictEqual(resolveWindowsServerAsarIgnoreGlobs("x64"), [
...WINDOWS_SERVER_ASAR_IGNORE_GLOBS,
"**/node_modules/node-pty/prebuilds/win32-arm64",
"**/node_modules/node-pty/prebuilds/win32-arm64/**",
"**/node_modules/node-pty/third_party/conpty/*/win10-arm64",
"**/node_modules/node-pty/third_party/conpty/*/win10-arm64/**",
]);
assert.deepStrictEqual(resolveWindowsServerAsarIgnoreGlobs("arm64"), [
...WINDOWS_SERVER_ASAR_IGNORE_GLOBS,
"**/node_modules/node-pty/prebuilds/win32-x64",
"**/node_modules/node-pty/prebuilds/win32-x64/**",
"**/node_modules/node-pty/third_party/conpty/*/win10-x64",
"**/node_modules/node-pty/third_party/conpty/*/win10-x64/**",
]);
});
it.effect(
"keeps target and WSL native files while excluding the other Windows architecture",
() =>
Effect.scoped(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const tempDir = yield* fs.makeTempDirectoryScoped({
prefix: "t3-windows-architecture-test-",
});
const sourceDir = path.join(tempDir, "server");
const nativeFiles = [
"node_modules/node-pty/prebuilds/win32-x64/conpty/OpenConsole.exe",
"node_modules/node-pty/prebuilds/win32-arm64/conpty/OpenConsole.exe",
"node_modules/node-pty/prebuilds/linux-x64/pty.node",
"node_modules/node-pty/third_party/conpty/1.0.0/win10-x64/OpenConsole.exe",
"node_modules/node-pty/third_party/conpty/1.0.0/win10-arm64/OpenConsole.exe",
];
for (const nativeFile of nativeFiles) {
const nativePath = path.join(sourceDir, nativeFile);
yield* fs.makeDirectory(path.dirname(nativePath), { recursive: true });
yield* fs.writeFileString(nativePath, "native");
}
const asarPath = path.join(tempDir, "server.asar");
yield* packWindowsServerAsar({ sourceDir, asarPath, arch: "x64" });
const unpackedRoot = `${asarPath}.unpacked`;
assert.isTrue(
yield* fs.exists(
path.join(
unpackedRoot,
"node_modules/node-pty/prebuilds/win32-x64/conpty/OpenConsole.exe",
),
),
);
assert.isTrue(
yield* fs.exists(
path.join(unpackedRoot, "node_modules/node-pty/prebuilds/linux-x64/pty.node"),
),
);
assert.isFalse(
yield* fs.exists(
path.join(unpackedRoot, "node_modules/node-pty/prebuilds/win32-arm64"),
),
);
assert.isFalse(
yield* fs.exists(
path.join(unpackedRoot, "node_modules/node-pty/third_party/conpty/1.0.0/win10-arm64"),
),
);
}),
),
);
it.effect("validates every ASAR-unpacked native in the packaged Windows payload", () =>
Effect.scoped(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const fixture = yield* makeWindowsPayloadFixture({ copyUnpackedNatives: true });
const result = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
});
const secondAsarPath = path.join(path.dirname(fixture.generatedAsarPath), "second.asar");
yield* packWindowsServerAsar({
sourceDir: fixture.sourceDir,
asarPath: secondAsarPath,
arch: "x64",
});
const [firstAsar, secondAsar] = yield* Effect.all([
fs.readFile(fixture.generatedAsarPath),
fs.readFile(secondAsarPath),
]);
assert.equal(result.packagedAppDir, fixture.packagedAppDir);
assert.deepStrictEqual(result.unpackedFiles, ["node_modules/native/addon.node"]);
assert.isBelow(result.fileCount, WINDOWS_PACKAGED_PAYLOAD_FILE_LIMIT);
assert.deepStrictEqual(secondAsar, firstAsar);
}),
),
);
it.effect("excludes the plugin catalog from the Windows loose-file budget", () =>
Effect.scoped(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const fixture = yield* makeWindowsPayloadFixture({ copyUnpackedNatives: true });
const baseline = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
});
const pluginDir = path.join(fixture.packagedAppDir, "resources/plugins/entries/example");
yield* fs.makeDirectory(pluginDir, { recursive: true });
yield* Effect.forEach(
Array.from({ length: 100 }, (_, index) => index),
(index) => fs.writeFileString(path.join(pluginDir, `${String(index)}.json`), "{}"),
{ concurrency: "unbounded" },
);
const withCatalog = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
fileLimit: baseline.fileCount,
});
assert.equal(withCatalog.fileCount, baseline.fileCount);
}),
),
);
it.effect("probes fff through the packaged Windows primary instead of helper executables", () => {
const commands: Array<{
readonly command: string;
readonly args: ReadonlyArray<string>;
readonly options: {
readonly cwd?: string | undefined;
readonly env?: Readonly<Record<string, string | undefined>> | undefined;
};
}> = [];
const spawnerLayer = Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
commands.push(standardCommand(command));
return Effect.succeed(mockProcess(0));
}),
);
return Effect.scoped(
Effect.gen(function* () {
const path = yield* Path.Path;
const fixture = yield* makeWindowsPayloadFixture({ copyUnpackedNatives: true });
yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
});
const primaryProbe = commands.find(
(command) => command.options.env?.ELECTRON_RUN_AS_NODE === "1",
);
if (primaryProbe === undefined) return assert.fail("Windows primary probe was not spawned");
assert.equal(
primaryProbe.command,
path.join(fixture.packagedAppDir, fixture.appExecutableName),
);
assert.deepStrictEqual(primaryProbe.args.slice(0, 3), [
"--no-global-search-paths",
"--input-type=module",
"--eval",
]);
assert.include(primaryProbe.args[3], "FileFinder.create");
assert.equal(
primaryProbe.args[4],
path.join(
fixture.packagedAppDir,
"resources/server.asar/node_modules/@ff-labs/fff-node/dist/src/index.js",
),
);
assert.equal(primaryProbe.options.cwd, fixture.packagedAppDir);
assert.equal(primaryProbe.options.env?.NODE_PATH, "");
}),
).pipe(
Effect.provide(
Layer.mergeAll(
spawnerLayer,
Layer.succeed(HostProcessPlatform, "win32"),
Layer.succeed(HostProcessArchitecture, "x64"),
),
),
);
});
it.effect("skips the primary native probe for cross-architecture Windows payloads", () => {
const commands: Array<{
readonly command: string;
readonly options: {
readonly env?: Readonly<Record<string, string | undefined>> | undefined;
};
}> = [];
const spawnerLayer = Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
commands.push(standardCommand(command));
return Effect.succeed(mockProcess(0));
}),
);
return Effect.scoped(
Effect.gen(function* () {
const fixture = yield* makeWindowsPayloadFixture({ copyUnpackedNatives: true });
yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "arm64",
});
assert.isFalse(
commands.some(
(command) =>
command.command !== process.execPath &&
command.options.env?.ELECTRON_RUN_AS_NODE === "1",
),
);
assert.isTrue(
commands.some(
(command) =>
command.command === process.execPath && command.options.env?.NODE_PATH === "",
),
);
}),
).pipe(
Effect.provide(
Layer.mergeAll(
spawnerLayer,
Layer.succeed(HostProcessPlatform, "win32"),
Layer.succeed(HostProcessArchitecture, "x64"),
),
),
);
});
it.effect("rejects a cross-architecture Windows payload without its primary executable", () =>
Effect.scoped(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const fixture = yield* makeWindowsPayloadFixture({ copyUnpackedNatives: true });
const executablePath = path.join(fixture.packagedAppDir, fixture.appExecutableName);
yield* fs.remove(executablePath);
const error = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "arm64",
}).pipe(Effect.flip);
assert.instanceOf(error, WindowsPrimaryNativeProbeError);
assert.equal(error.executablePath, executablePath);
}),
).pipe(
Effect.provide(
Layer.mergeAll(
Layer.succeed(HostProcessPlatform, "win32"),
Layer.succeed(HostProcessArchitecture, "x64"),
),
),
),
);
it.effect("rejects a packaged sidecar whose ASAR-unpacked native is missing", () =>
Effect.scoped(
Effect.gen(function* () {
const fixture = yield* makeWindowsPayloadFixture({ copyUnpackedNatives: false });
const error = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
}).pipe(Effect.flip);
assert.instanceOf(error, WindowsPackagedPayloadValidationError);
assert.equal(error.reason, "unpacked-native-missing");
assert.deepStrictEqual(error.missingFiles, [
"server.asar.unpacked/node_modules/native/addon.node",
]);
}),
),
);
it.effect("rejects directories in place of packaged executable files", () =>
Effect.scoped(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const fixture = yield* makeWindowsPayloadFixture({ copyUnpackedNatives: true });
const nativePath = path.join(
fixture.packagedAppDir,
"resources/server.asar.unpacked/node_modules/native/addon.node",
);
yield* fs.remove(nativePath);
yield* fs.makeDirectory(nativePath);
const nativeError = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
}).pipe(Effect.flip);
assert.instanceOf(nativeError, WindowsPackagedPayloadValidationError);
assert.equal(nativeError.reason, "unpacked-native-missing");
assert.deepStrictEqual(nativeError.missingFiles, [
"server.asar.unpacked/node_modules/native/addon.node",
]);
yield* fs.remove(nativePath, { recursive: true });
yield* fs.writeFileString(nativePath, "native-binary");
const resourceMonitorPath = path.join(
fixture.packagedAppDir,
"resources/resource-monitor/t3-resource-monitor.exe",
);
yield* fs.remove(resourceMonitorPath);
yield* fs.makeDirectory(resourceMonitorPath);
const resourceMonitorError = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
}).pipe(Effect.flip);
assert.instanceOf(resourceMonitorError, WindowsPackagedPayloadValidationError);
assert.equal(resourceMonitorError.reason, "resource-monitor-missing");
assert.deepStrictEqual(resourceMonitorError.missingFiles, [
"resource-monitor/t3-resource-monitor.exe",
]);
}),
),
);
it.effect("rejects a Windows payload that regresses above the file-count budget", () =>
Effect.scoped(
Effect.gen(function* () {
const fixture = yield* makeWindowsPayloadFixture({ copyUnpackedNatives: true });
const error = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
fileLimit: 2,
}).pipe(Effect.flip);
assert.instanceOf(error, WindowsPackagedPayloadValidationError);
assert.equal(error.reason, "file-limit-exceeded");
assert.isAbove(error.fileCount ?? 0, 2);
}),
),
);
it.effect("rejects a sidecar whose extracted server bundle cannot resolve", () =>
Effect.scoped(
Effect.gen(function* () {
const fixture = yield* makeWindowsPayloadFixture({
copyUnpackedNatives: true,
serverEntrySource: 'import "t3code-deliberately-missing-package";\n',
});
const error = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
}).pipe(Effect.flip);
assert.instanceOf(error, BundleNotSelfContainedError);
assert.include(error.output, "t3code-deliberately-missing-package");
}),
),
);
it.effect("rejects a sidecar that omits a lazy runtime package", () =>
Effect.scoped(
Effect.gen(function* () {
const fixture = yield* makeWindowsPayloadFixture({
copyUnpackedNatives: true,
includeLazyRuntimePackage: false,
});
const error = yield* validateWindowsPackagedPayload({
stageDistDir: fixture.stageDistDir,
appExecutableName: fixture.appExecutableName,
targetArch: "x64",
}).pipe(Effect.flip);
assert.instanceOf(error, BundleNotSelfContainedError);
assert.include(error.output, "Cannot find package 'execa'");
}),
),
);
it.effect("keeps executable resource editing enabled for unsigned Windows builds", () =>
Effect.gen(function* () {
const config = yield* createBuildConfig(
"win",
"nsis",
"1.2.3",
false,
false,
undefined,
undefined,
);
const win = config.win;
assert.ok(win);
assert.equal(win.icon, "icon.ico");
assert.equal(win.signAndEditExecutable, true);
assert.notProperty(win, "azureSignOptions");
}).pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })))),
);
it("stages runtime resources outside app.asar", () => {
assert.deepStrictEqual(DESKTOP_EXTRA_RESOURCES, [
{
from: "apps/desktop/prod-resources/resource-monitor",
to: "resource-monitor",
},
{
from: DESKTOP_PLUGIN_CATALOG_RESOURCE_SOURCE_DIR,
to: "plugins",
},
]);
assert.deepStrictEqual(resolveResourceMonitorRustTargets("mac", "universal"), [
"aarch64-apple-darwin",
"x86_64-apple-darwin",
]);
assert.deepStrictEqual(resolveResourceMonitorRustTargets("linux", "x64"), [
"x86_64-unknown-linux-gnu",
]);
assert.deepStrictEqual(resolveResourceMonitorRustTargets("win", "arm64"), [
"aarch64-pc-windows-msvc",
]);
assert.equal(resourceMonitorExecutableName("mac"), "t3-resource-monitor");
assert.equal(resourceMonitorExecutableName("win"), "t3-resource-monitor.exe");
assert.deepStrictEqual(
resolveResourceMonitorCargoBuildArgs(
"/repo/native/resource-monitor/Cargo.toml",
"/repo/native/resource-monitor/target",
"aarch64-apple-darwin",
),
[
"build",
"--locked",
"--release",
"--manifest-path",
"/repo/native/resource-monitor/Cargo.toml",
"--target-dir",
"/repo/native/resource-monitor/target",
"--target",
"aarch64-apple-darwin",
],
);
});
it("promotes target fff binaries to direct staged dependencies", () => {
assert.deepStrictEqual(resolveFffNativeDependencies("mac", "arm64", "0.9.4"), {
"@ff-labs/fff-bin-darwin-arm64": "0.9.4",
});
assert.deepStrictEqual(resolveFffNativeDependencies("mac", "universal", "0.9.4"), {
"@ff-labs/fff-bin-darwin-arm64": "0.9.4",
"@ff-labs/fff-bin-darwin-x64": "0.9.4",
});
assert.deepStrictEqual(resolveFffNativeDependencies("win", "x64", "0.9.4"), {
"@ff-labs/fff-bin-win32-x64": "0.9.4",
});
assert.deepStrictEqual(resolveFffNativeDependencies("linux", "x64", "0.9.4"), {
"@ff-labs/fff-bin-linux-x64-gnu": "0.9.4",
"@ff-labs/fff-bin-linux-x64-musl": "0.9.4",
});
assert.deepStrictEqual(resolveFffNativeDependencies("linux", "arm64", "0.9.4"), {
"@ff-labs/fff-bin-linux-arm64-gnu": "0.9.4",
"@ff-labs/fff-bin-linux-arm64-musl": "0.9.4",
});
});
it.effect("rejects universal builds on Linux and Windows before staging binaries", () =>
Effect.gen(function* () {
for (const platform of ["linux", "win"] as const) {
const error = yield* Effect.flip(
resolveBuildOptions({
platform: Option.some(platform),
target: Option.none(),
arch: Option.some("universal"),
buildVersion: Option.none(),
outputDir: Option.none(),
skipBuild: Option.none(),
keepStage: Option.none(),
signed: Option.none(),
verbose: Option.none(),
mockUpdates: Option.none(),
mockUpdateServerPort: Option.none(),
wslPrebuild: Option.none(),
}),
);
assert.instanceOf(error, UnsupportedDesktopBuildArchitectureError);
assert.deepStrictEqual(error.supportedArchitectures, ["x64", "arm64"]);
}
}),
);
});
import * as NodeServices from "@effect/platform-node/NodeServices";