fix(linux): start Hopper demo sessions headlessly

This commit is contained in:
morluto
2026-07-14 04:58:45 +08:00
parent 08d5b913b0
commit 75818d119e
18 changed files with 816 additions and 169 deletions
+23
View File
@@ -8,6 +8,8 @@ const host = (overrides: Partial<DoctorHost> = {}): DoctorHost => ({
linuxDistribution: () => Promise.resolve(undefined),
validTarget: (path) => Promise.resolve(path.includes("Hopper")),
executable: (path) => Promise.resolve(path.includes("Hopper")),
supportedLinuxHopper: () => Promise.resolve(true),
linuxDemoRuntimeReady: () => Promise.resolve(true),
brewHopperPath: () => Promise.resolve(undefined),
manualHopperPaths: () => Promise.resolve([]),
...overrides,
@@ -78,6 +80,27 @@ describe("doctor", () => {
expect(result.healthy).toBe(true);
expect(result.hopperPath).toBe(path);
});
it("reports missing Linux demo-session dependencies", async () => {
const result = await runDoctor(
undefined,
host({
platform: "linux",
macosVersion: () => Promise.resolve(undefined),
linuxDistribution: () =>
Promise.resolve({
id: "ubuntu",
versionId: "24.04",
packageFamily: "deb",
supported: true,
}),
linuxDemoRuntimeReady: () => Promise.resolve(false),
}),
);
expect(result.healthy).toBe(false);
expect(
result.checks.find(({ name }) => name === "hopper-demo-runtime"),
).toMatchObject({ ok: false, classification: "missing_dependency" });
});
it("detects a Homebrew cask installed outside /Applications", async () => {
const path =
"/opt/homebrew/Caskroom/hopper/Hopper Disassembler.app/Contents/MacOS/hopper";
+7
View File
@@ -37,6 +37,13 @@ describe("analysis error projection", () => {
);
});
it("projects a safe explanation for an unsupported Linux demo dialog", () => {
expect(projectAnalysisError(new HopperProcessError(78))).toMatchObject({
message:
"This Hopper demo build or dialog is not supported for unattended Linux analysis. Update REA or use a supported Hopper build.",
});
});
it("projects every typed failure without internal details", () => {
const secretCause = new Error("secret-token");
const byTag = {
+43
View File
@@ -59,6 +59,21 @@ class SilentLauncher implements BridgeLauncher {
}
}
class ExitingLauncher implements BridgeLauncher {
constructor(readonly code: number) {}
launch() {
return Promise.resolve(
ok({
process: spawn(process.execPath, ["-e", `process.exit(${this.code})`], {
stdio: ["ignore", "ignore", "pipe"],
}),
ownsProcessLifetime: true,
}),
);
}
}
const clients: HopperClient[] = [];
const startClient = async () => {
const client = new HopperClient({
@@ -183,6 +198,34 @@ describe("HopperClient", () => {
expect(Date.now() - startedAt).toBeLessThan(500);
});
it("reports an owned launcher exit during bridge startup", async () => {
const client = new HopperClient({
launcher: new ExitingLauncher(78),
startupTimeoutMs: 10_000,
});
clients.push(client);
const startedAt = Date.now();
const result = await client.start();
expect(result.ok).toBe(false);
if (!result.ok)
expect(result.error).toMatchObject({
_tag: "HopperProcessError",
exitCode: 78,
});
expect(Date.now() - startedAt).toBeLessThan(500);
});
it("allows a short-lived launcher to hand off bridge startup", async () => {
const client = new HopperClient({
launcher: new ExitingLauncher(0),
startupTimeoutMs: 100,
});
clients.push(client);
const result = await client.start();
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error._tag).toBe("HopperTimeoutError");
});
it("ignores more than 1,024 late responses without corrupting the session", async () => {
const client = await startClient();
const timedOut = await Promise.all(
+51 -41
View File
@@ -3,7 +3,7 @@ import { describe, expect, it } from "vitest";
import {
installLinuxHopper,
linuxPackageManagerCommand,
linuxPackageManagerCommands,
linuxSharedLibrariesAvailable,
parseLinuxDistribution,
type LinuxDistribution,
@@ -19,9 +19,7 @@ class RecordingLinuxHost implements LinuxHopperInstallHost {
packageFamily: "deb",
supported: true,
};
metadata: unknown = releasesFor(new Uint8Array([1, 2, 3]));
archive = new Uint8Array([1, 2, 3]);
metadataOk = true;
archiveOk = true;
installSucceeds = true;
launcherExists = true;
@@ -33,11 +31,6 @@ class RecordingLinuxHost implements LinuxHopperInstallHost {
Promise.resolve(this.distributionValue);
download = (url: string): Promise<LinuxHopperDownload> => {
this.downloads.push(url);
if (url.includes("files-api.php"))
return Promise.resolve({
ok: this.metadataOk,
bytes: new TextEncoder().encode(JSON.stringify(this.metadata)),
});
return Promise.resolve({ ok: this.archiveOk, bytes: this.archive });
};
createTemporaryDirectory = (): Promise<string> =>
@@ -103,23 +96,48 @@ describe("Linux Hopper installation", () => {
["arch", "pacman"],
] as const)("selects the %s native package manager", (family, executable) => {
expect(
linuxPackageManagerCommand(family, "/tmp/Hopper package", true),
).toMatchObject({
executable,
});
expect(
linuxPackageManagerCommand(family, "/tmp/Hopper package", false),
).toMatchObject({
executable: "pkexec",
args: [
linuxPackageManagerCommands(family, "/tmp/Hopper package", true),
).toContainEqual(
expect.objectContaining({
executable,
expect.any(String),
expect.any(String),
expect.any(String),
],
});
}),
);
expect(
linuxPackageManagerCommands(family, "/tmp/Hopper package", false),
).toContainEqual(
expect.objectContaining({
executable: "pkexec",
args: expect.arrayContaining([executable, "/tmp/Hopper package"]),
}),
);
});
it.each([
["deb", ["xvfb", "xauth", "python3", "libx11-6", "libxtst6"]],
[
"rpm",
[
"xorg-x11-server-Xvfb",
"xorg-x11-xauth",
"python3",
"libX11",
"libXtst",
],
],
["arch", ["xorg-server-xvfb", "xorg-xauth", "python", "libx11", "libxtst"]],
] as const)(
"installs the %s demo-session dependencies",
(family, packages) => {
expect(
linuxPackageManagerCommands(family, "/tmp/Hopper package", true),
).toContainEqual(
expect.objectContaining({
args: expect.arrayContaining([...packages]),
}),
);
},
);
it.each(["deb", "rpm", "arch"] as const)(
"downloads, verifies, installs, reads back, and cleans a %s package",
async (family) => {
@@ -129,7 +147,7 @@ describe("Linux Hopper installation", () => {
packageFamily: family,
supported: true,
};
const result = await installLinuxHopper(host);
const result = await installFixture(host);
expect(result).toEqual({
status: "installed",
launcherPath: "/opt/hopper/bin/Hopper",
@@ -143,9 +161,6 @@ describe("Linux Hopper installation", () => {
it.each([
["unsupported host", "unsupported_host"],
["malformed metadata", "release_metadata"],
["unexpected origin", "release_metadata"],
["metadata request failure", "release_metadata"],
["package request failure", "download"],
["checksum mismatch", "integrity"],
["package manager failure", "authorization_or_package_manager"],
@@ -158,21 +173,11 @@ describe("Linux Hopper installation", () => {
packageFamily: "deb",
supported: false,
};
if (scenario === "malformed metadata") host.metadata = { invalid: true };
if (scenario === "unexpected origin") {
const metadata = releasesFor(host.archive);
const ubuntu = metadata["Ubuntu / Mint"];
if (ubuntu === undefined)
throw new Error("fixture omitted Ubuntu release");
ubuntu.filename = "https://example.com/Hopper.deb";
host.metadata = metadata;
}
if (scenario === "metadata request failure") host.metadataOk = false;
if (scenario === "package request failure") host.archiveOk = false;
if (scenario === "checksum mismatch") host.archive = new Uint8Array([9]);
if (scenario === "package manager failure") host.installSucceeds = false;
if (scenario === "missing launcher") host.launcherExists = false;
expect(await installLinuxHopper(host)).toEqual({
expect(await installFixture(host)).toEqual({
status: "failed",
reason,
});
@@ -198,7 +203,7 @@ describe("Linux Hopper installation", () => {
function releasesFor(
bytes: Uint8Array,
): Record<
string,
LinuxPackageFamily,
{ filename: string; file_length: string; file_hash: string }
> {
const release = (extension: string) => ({
@@ -207,8 +212,13 @@ function releasesFor(
file_hash: createHash("sha1").update(bytes).digest("hex"),
});
return {
"Ubuntu / Mint": release("deb"),
Fedora: release("rpm"),
Arch: release("pkg.tar.xz"),
deb: release("deb"),
rpm: release("rpm"),
arch: release("pkg.tar.xz"),
};
}
const installFixture = (host: RecordingLinuxHost) =>
installLinuxHopper(host, {
releases: releasesFor(new Uint8Array([1, 2, 3])),
});
+110 -14
View File
@@ -23,6 +23,8 @@ class FakeSetupHost implements SetupHost {
hopperInstalls = 0;
configurations = 0;
doctorHealthy: boolean | undefined;
linuxDemoRuntimeMissing = false;
unsupportedHopperVersion = false;
constructor(platform: NodeJS.Platform = "darwin") {
this.platform = platform;
@@ -35,6 +37,7 @@ class FakeSetupHost implements SetupHost {
hopperPath = (): Promise<string | undefined> => Promise.resolve(this.hopper);
installHopper = (): Promise<string | undefined> => {
this.hopperInstalls += 1;
this.linuxDemoRuntimeMissing = false;
if (this.hopperInstallSucceeds) this.hopper = "/manual/Hopper";
return Promise.resolve(this.hopper);
};
@@ -54,12 +57,38 @@ class FakeSetupHost implements SetupHost {
healthy: boolean;
hopperPath?: string;
checks: readonly DoctorCheck[];
}> =>
Promise.resolve({
healthy: this.doctorHealthy ?? this.hopper !== undefined,
}> => {
const checks: DoctorCheck[] = [
...(this.linuxDemoRuntimeMissing
? ([
{
name: "hopper-demo-runtime",
ok: false,
classification: "missing_dependency",
},
] as const)
: []),
...(this.unsupportedHopperVersion
? ([
{
name: "hopper-version",
ok: false,
classification: "config_drift",
detail: this.hopper ?? "",
},
] as const)
: []),
];
return Promise.resolve({
healthy:
this.doctorHealthy ??
(this.hopper !== undefined &&
!this.linuxDemoRuntimeMissing &&
!this.unsupportedHopperVersion),
...(this.hopper === undefined ? {} : { hopperPath: this.hopper }),
checks: [],
checks,
});
};
}
const options = (approved: boolean, installHopper = false): SetupOptions => ({
@@ -86,7 +115,29 @@ describe("setup workflow", () => {
);
});
it("applies an accepted interactive plan including Hopper", async () => {
it("reports ready after an accepted Linux plan installs healthy Hopper", async () => {
const host = new FakeSetupHost("linux");
host.distribution = {
id: "ubuntu",
versionId: "24.04",
packageFamily: "deb",
supported: true,
};
const result = await runSetup(
{ ...options(false), structured: false },
host,
() => Promise.resolve(true),
);
expect(result.status).toBe("ready");
expect(result.appliedActions).toEqual([
"installed_hopper",
"installed_skill",
]);
expect(host.hopperInstalls).toBe(1);
expect(result.remediation).toBeUndefined();
});
it("requires first-run activation after installing Hopper on macOS", async () => {
const host = new FakeSetupHost();
const result = await runSetup(
{ ...options(false), structured: false },
@@ -94,14 +145,7 @@ describe("setup workflow", () => {
() => Promise.resolve(true),
);
expect(result.status).toBe("needs_human");
expect(result.appliedActions).toEqual([
"installed_hopper",
"installed_skill",
]);
expect(host.hopperInstalls).toBe(1);
expect(result.remediation).toBe(
"Open Hopper, complete its one-time activation, then rerun rea doctor --json.",
);
expect(result.remediation).toContain("choose its demo mode");
});
it("declines an interactive plan without mutation", async () => {
@@ -129,13 +173,21 @@ describe("setup workflow", () => {
});
it("installs Hopper when unattended authorization is explicit", async () => {
const host = new FakeSetupHost();
const host = new FakeSetupHost("linux");
host.distribution = {
id: "ubuntu",
versionId: "24.04",
packageFamily: "deb",
supported: true,
};
const result = await runSetup(options(true, true), host);
expect(result.appliedActions).toEqual([
"installed_hopper",
"installed_skill",
]);
expect(host.hopperInstalls).toBe(1);
expect(result.status).toBe("ready");
expect(result.remediation).toBeUndefined();
});
it("reuses existing Hopper and configures detected clients", async () => {
@@ -155,6 +207,50 @@ describe("setup workflow", () => {
expect(host.hopperInstalls).toBe(0);
});
it("installs missing Linux demo dependencies for existing Hopper", async () => {
const host = new FakeSetupHost("linux");
host.distribution = {
id: "ubuntu",
versionId: "24.04",
packageFamily: "deb",
supported: true,
};
host.hopper = "/opt/hopper/bin/Hopper";
host.linuxDemoRuntimeMissing = true;
const result = await runSetup(
{ ...options(false), structured: false },
host,
() => Promise.resolve(true),
);
expect(result.status).toBe("ready");
expect(host.hopperInstalls).toBe(1);
expect(result.plannedActions.map(({ kind }) => kind)).toContain(
"install_hopper",
);
});
it("does not reinstall Hopper for an unsupported configured path", async () => {
const host = new FakeSetupHost("linux");
host.distribution = {
id: "ubuntu",
versionId: "24.04",
packageFamily: "deb",
supported: true,
};
host.hopper = "/custom/Hopper";
host.unsupportedHopperVersion = true;
const result = await runSetup(
{ ...options(false), structured: false },
host,
() => Promise.resolve(true),
);
expect(result.status).toBe("needs_human");
expect(host.hopperInstalls).toBe(0);
expect(result.plannedActions.map(({ kind }) => kind)).not.toContain(
"install_hopper",
);
});
it("reports a failed Hopper installation without configuring clients", async () => {
const host = new FakeSetupHost();
host.hopperInstallSucceeds = false;