Files
rea/tests/boundary/providers/hopper/linuxPrivateDisplay.test.ts
T
morlutoGitHubDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
e91c8c39c2 refactor(test): colocate pure-domain tests and reduce test debt (#471)
* refactor(test): colocate pure-domain tests and reduce test debt

Move focused immutable domain/contracts/adapter tests from
tests/composition/** beside their production owners under src/** as
colocated *.test.ts/*.fixture.ts, and relocate persistence-shaped
composition tests under tests/boundary/filesystem/**. Update import
paths, the Vitest project topology (shared worker context for pure
domain/contracts and recording-port service projects), the test
migration ledger, testing docs, and AGENTS.md to match.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* docs: remove test migration ledger

The cutover is complete and the ledger is no longer referenced anywhere
in source, scripts, or the docs gate. Drop the standalone artifact.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* docs: clean up stale migration artifacts

Remove empty directories left by the test migration and replace the
"final cutover" language in docs/testing.md with standing PR guidance
that no longer references the completed migration.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: repair CI failures from schema, knip, and stale generated artifacts

Three root causes fixed:

1. catalog-schemas.mjs: valuesAt only traversed anyOf from Zod's
   toJSONSchema, not oneOf. The webScreenshotDiffSchema change to a
   discriminated union produces oneOf, so the product catalog generator
   could not derive the web_screenshot_diff schema version. Add oneOf
   traversal.

2. conformancePackage.ts: the test rewrite removed the only imports of
   ScenarioManifest, ReplayPlan, ShimPlan, and ExpectedEvidence, making
   them unused exports that knip flags. Remove the dead type aliases.

3. Regenerate stale artifacts (product catalog, MCP tool catalog, skill
   metadata, conformance manifest) that drifted from the source changes.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-08-10 14:57:44 +08:00

243 lines
7.4 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import type {
HopperStartupDiagnostic,
HopperStartupFailureCode,
} from "../../../../src/domain/hopperStartupFailure.js";
import {
runLinuxPrivateDisplayProbe,
selectLinuxPrivateDisplayStrategy,
type LinuxPrivateDisplayProbeProcessResult,
type LinuxPrivateDisplayProbeRunner,
type LinuxPrivateDisplayRunnableStrategy,
} from "../../../../src/hopper/LinuxPrivateDisplayProbe.js";
import {
LINUX_PRIVATE_DISPLAY_DIAGNOSTIC_PREFIX,
parseLinuxPrivateDisplayDiagnostic,
} from "../../../../src/hopper/LinuxPrivateDisplayDiagnostic.js";
const helperPath = fileURLToPath(
new URL("../../../../scripts/hopper-demo-x11.py", import.meta.url),
);
const hangingHelperPath = fileURLToPath(
new URL("../../../fixtures/x11ProbeHang.py", import.meta.url),
);
type DiagnosticOverrides = Partial<
Omit<HopperStartupDiagnostic, "failure_code" | "status">
> &
(
| { readonly status?: "ready"; readonly failure_code?: null }
| {
readonly status: "error";
readonly failure_code: HopperStartupFailureCode;
}
);
const diagnostic = (overrides: DiagnosticOverrides = {}) => {
const common = {
schema_version: 1,
component: "hopper_private_display",
operation: "probe",
reason: "ready",
socket_directory: "/tmp/.X11-unix",
socket_directory_mode: "1777",
mount_read_only: false,
effective_socket_directory_mode: "1777",
effective_mount_read_only: false,
wsl: false,
strategy: "direct",
fallback_reason: null,
xvfb_stderr_bytes: 0,
xvfb_stderr_truncated: false,
} as const;
return overrides.status === "error"
? ({ ...common, ...overrides } satisfies HopperStartupDiagnostic)
: ({
...common,
...overrides,
status: "ready",
failure_code: null,
} satisfies HopperStartupDiagnostic);
};
const processResult = (
value: HopperStartupDiagnostic,
overrides: Partial<LinuxPrivateDisplayProbeProcessResult> = {},
): LinuxPrivateDisplayProbeProcessResult => ({
outcome: "exited",
exitCode: value.status === "ready" ? 0 : failureExit(value.failure_code),
stderr: `${LINUX_PRIVATE_DISPLAY_DIAGNOSTIC_PREFIX}${JSON.stringify(value)}\n`,
stderrBytes: 0,
stderrTruncated: false,
cleanupIncomplete: false,
...overrides,
});
const failureExit = (code: HopperStartupFailureCode): number =>
code === "x11_socket_directory_unusable"
? 80
: code === "runtime_dependency_unavailable"
? 79
: 70;
describe("Linux private display selection", () => {
it("prefers a successful direct Xvfb probe", async () => {
const calls: LinuxPrivateDisplayRunnableStrategy[] = [];
const runProbe: LinuxPrivateDisplayProbeRunner = (strategy) => {
calls.push(strategy);
return Promise.resolve(processResult(diagnostic({ strategy })));
};
await expect(
selectLinuxPrivateDisplayStrategy({ helperPath, runProbe }),
).resolves.toMatchObject({ ok: true, strategy: "direct" });
expect(calls).toEqual(["direct"]);
});
it("uses a private mount namespace only for the exact socket conflict", async () => {
const calls: LinuxPrivateDisplayRunnableStrategy[] = [];
const runProbe: LinuxPrivateDisplayProbeRunner = (strategy) => {
calls.push(strategy);
return Promise.resolve(
strategy === "direct"
? processResult(
diagnostic({
strategy,
status: "error",
failure_code: "x11_socket_directory_unusable",
reason: "socket_directory_read_only",
socket_directory_mode: "0777",
mount_read_only: true,
effective_socket_directory_mode: "0777",
effective_mount_read_only: true,
wsl: true,
}),
)
: processResult(diagnostic({ strategy, wsl: true })),
);
};
const selected = await selectLinuxPrivateDisplayStrategy({
helperPath,
runProbe,
});
expect(selected).toMatchObject({
ok: true,
strategy: "user-mount-namespace",
diagnostic: {
socket_directory_mode: "0777",
mount_read_only: true,
effective_socket_directory_mode: "1777",
effective_mount_read_only: false,
wsl: true,
},
});
expect(calls).toEqual(["direct", "user-mount-namespace"]);
});
it.each([
["missing_xvfb", "runtime_dependency_unavailable", 79],
["address_collision", "private_display_unavailable", 70],
] as const)(
"does not mask deterministic direct failure %s with a namespace probe",
async (reason, failureCode, exitCode) => {
const calls: LinuxPrivateDisplayRunnableStrategy[] = [];
const runProbe: LinuxPrivateDisplayProbeRunner = (strategy) => {
calls.push(strategy);
return Promise.resolve(
processResult(
diagnostic({
strategy,
status: "error",
failure_code: failureCode,
reason,
}),
{ exitCode },
),
);
};
await expect(
selectLinuxPrivateDisplayStrategy({ helperPath, runProbe }),
).resolves.toMatchObject({ ok: false, exitCode });
expect(calls).toEqual(["direct"]);
},
);
it.each([
[
"malformed",
`${LINUX_PRIVATE_DISPLAY_DIAGNOSTIC_PREFIX}not-json`,
false,
"diagnostic_malformed",
],
[
"oversized",
`${LINUX_PRIVATE_DISPLAY_DIAGNOSTIC_PREFIX}${"x".repeat(70_000)}`,
true,
"diagnostic_truncated",
],
] as const)(
"rejects %s helper diagnostics",
async (_name, stderr, stderrTruncated, reason) => {
const runProbe: LinuxPrivateDisplayProbeRunner = () =>
Promise.resolve({
...processResult(diagnostic()),
exitCode: 70,
stderr,
stderrTruncated,
});
await expect(
selectLinuxPrivateDisplayStrategy({ helperPath, runProbe }),
).resolves.toMatchObject({
ok: false,
diagnostic: { reason },
});
},
);
it("reports missing helper dependencies without exposing raw stderr", () => {
for (const [option] of [
["--xauth", "missing_xauth"],
["--xvfb", "missing_xvfb"],
] as const) {
const result = spawnSync(
"/usr/bin/python3",
[helperPath, "--probe", option, "/rea/missing-executable"],
{ encoding: "utf8" },
);
expect(result.status).toBe(79);
const parsed = parseLinuxPrivateDisplayDiagnostic(result.stderr, false);
expect(parsed).toMatchObject({
ok: true,
value: {
status: "error",
failure_code: "runtime_dependency_unavailable",
},
});
if (parsed.ok)
expect(parsed.value.reason).toMatch(/^missing_(xauth|xvfb)$/u);
expect(result.stderr).not.toContain("Traceback");
}
});
it("kills the owned probe group when its absolute deadline expires", async () => {
const result = await selectLinuxPrivateDisplayStrategy({
helperPath: hangingHelperPath,
timeoutMs: 75,
runProbe: runLinuxPrivateDisplayProbe,
});
expect(result).toMatchObject({
ok: false,
diagnostic: { reason: "probe_timeout" },
});
const processes = spawnSync("/bin/ps", ["-eo", "args="], {
encoding: "utf8",
}).stdout;
expect(processes).not.toContain(hangingHelperPath);
});
});