fix: 收紧 WebProbe 浏览器启动与清理

This commit is contained in:
Codex
2026-07-13 10:26:48 +02:00
parent 11b1f3d647
commit cd2863e030
5 changed files with 79 additions and 35 deletions
@@ -5,7 +5,7 @@ import { test } from "bun:test";
import type { CommandResult } from "./command";
import { rootPath } from "./config";
import { hwlabRuntimeLaneSpecForNode } from "./hwlab-node-lanes";
import { evaluateWebProbeMemoryGuard, runWebProbeMemoryGuard, webProbeBrowserEvidenceEnvAssignments, webProbeGuardedLaunchShell, webProbeMemoryStartGuardConfigRef } from "./hwlab-node-web-probe-memory-guard";
import { evaluateWebProbeMemoryGuard, runWebProbeMemoryGuard, webProbeBrowserEvidenceEnvAssignments, webProbeGuardedLaunchShell, webProbeMemoryStartGuardConfigRef, webProbeScreenshotFullPage } from "./hwlab-node-web-probe-memory-guard";
import { nodeWebProbeScriptRunnerSource } from "./hwlab-node-web-probe-runner-source";
const spec = hwlabRuntimeLaneSpecForNode("v03", "NC01");
@@ -226,11 +226,16 @@ test("generated runner bounds network evidence and defaults screenshots to viewp
const parserSource = exportedFunctionSource(readFileSync(rootPath("scripts/src/hwlab-node/web-probe.ts"), "utf8"), "parseNodeWebProbeOptions");
assert.match(parserSource, /fullPage: args\.includes\("--full-page"\) && !args\.includes\("--no-full-page"\)/u);
const screenshotSource = readFileSync(rootPath("scripts/src/hwlab-node/web-probe-observe.ts"), "utf8");
assert.match(screenshotSource, /webProbeScreenshotFullPage\(spec, options\.fullPage\)/u);
assert.match(screenshotSource, /UNIDESK_WEB_PROBE_SCREENSHOT_FULL_PAGE === "1"/u);
assert.doesNotMatch(screenshotSource, /UNIDESK_WEB_PROBE_SCREENSHOT_FULL_PAGE !== "0"/u);
const sentinelSource = readFileSync(rootPath("scripts/src/hwlab-node-web-sentinel-p5.ts"), "utf8");
assert.match(sentinelSource, /webProbeScreenshotFullPage\(state\.spec, options\.fullPage\)/u);
assert.match(sentinelSource, /UNIDESK_SENTINEL_DASHBOARD_FULL_PAGE === "1"/u);
assert.doesNotMatch(sentinelSource, /UNIDESK_SENTINEL_DASHBOARD_FULL_PAGE !== "0"/u);
assert.equal(webProbeScreenshotFullPage(spec, false), false);
assert.equal(webProbeScreenshotFullPage({ ...spec, webProbe: { ...spec.webProbe!, resourcePolicy: { ...spec.webProbe!.resourcePolicy!, browserEvidence: { ...spec.webProbe!.resourcePolicy!.browserEvidence!, screenshotMode: "full-page" } } } }, false), true);
assert.equal(webProbeScreenshotFullPage(spec, true), true);
});
test("final launch guard rechecks MemAvailable under the configured lock", () => {
@@ -240,9 +245,17 @@ test("final launch guard rechecks MemAvailable under the configured lock", () =>
assert.match(sync, /MemAvailable:/u);
assert.match(sync, /mem_available_bytes.*-le '4294967296'/u);
assert.match(sync, /setsid sh -c/u);
assert.match(sync, /kill -TERM -- "-\$browser_job_pid"/u);
assert.match(sync, /kill -KILL -- "-\$browser_job_pid"/u);
assert.match(async, /browserCreated === true/u);
assert.match(async, /data\.workerPid, data\.jobPid, data\.pid/u);
assert.match(async, /\/proc\/.*\/cmdline/u);
assert.match(sync, /exec 9>&-/u);
assert.equal(sync.includes("playwright"), false);
assert.equal(sync.includes("fs.readlinkSync('/proc/' + name + '/exe')"), true);
assert.equal(sync.includes("trap 'browser_exit=$?; trap - EXIT TERM INT HUP; cleanup_browser_group"), true);
assert.equal(sync.includes('kill -TERM -- "-$browser_job_pid"'), true);
assert.equal(sync.includes('kill -0 -- "-$browser_job_pid"'), true);
assert.equal(sync.includes('kill -KILL -- "-$browser_job_pid"'), true);
assert.equal(async.includes("browserCreated === true"), false);
assert.equal(async.includes("data.workerPid, data.jobPid, data.pid"), true);
assert.equal(async.includes("[data.browserPid, data.workerPid"), false);
assert.equal(async.includes("reportedBrowserPid === null || row.pid === reportedBrowserPid"), true);
assert.equal(async.includes("then flock -u 9; exit 43; fi"), true);
assert.equal(async.includes("fs.readlinkSync('/proc/' + name + '/exe')"), true);
});