test(lifecycle): move browser launch, secret write and inspector admission gates onto the harness (#11397)

V8InspectorAdmission's local gate() copy is removed; BuildSecretFiles'
hoisted write seam holds a PhaseGate.
This commit is contained in:
morluto authored and GitHub committed 2026-11-16 03:34:59 +08:00
1 parent 28332d43ca
commit d89e6725b5
3 files changed
+27 -56

No files matched your search

+6 -13
View File
@@ -1,4 +1,5 @@
import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js";
import { phaseGate } from "../../tests/support/lifecycleHarness.js";
import {
AnalysisCleanupLeaseGroup,
AnalysisCleanupLease,
@@ -1662,10 +1663,6 @@ it("keeps the session's exact cleanup lease pending after response timeout until
});
it("retains late opening failure cleanup after cancellation until the exact profile lease recovers", async () => {
let finishOpening: () => void = () => {};
const terminal = new Promise<void>((resolve) => {
finishOpening = resolve;
});
let profileStarted: () => void = () => {};
const profileAttempt = new Promise<void>((resolve) => {
profileStarted = resolve;
@@ -1679,18 +1676,14 @@ it("retains late opening failure cleanup after cancellation until the exact prof
reason: "late opening profile cleanup",
});
new AnalysisCleanupLeaseGroup(failure, [lease]);
let launchStarted: () => void = () => {};
const started = new Promise<void>((resolve) => {
launchStarted = resolve;
});
const launch = phaseGate();
mocks.openBrowser.mockImplementation(
async (
_scenario: unknown,
_env: unknown,
options: { retainCleanup?: (lease: AnalysisCleanupLease) => void },
) => {
launchStarted();
await terminal;
await launch.hold();
options.retainCleanup?.(lease);
throw failure;
},
@@ -1704,12 +1697,12 @@ it("retains late opening failure cleanup after cancellation until the exact prof
const pending = provider.captureScenario(scenario, {
signal: caller.signal,
});
await awaitAcknowledgement(started, pending, "browser launch start");
await launch.reached(pending, "browser launch start");
caller.abort(reason);
const result = await pending;
if (result.ok) throw new Error("Expected cancelled opening");
expect(result.error.cleanupIncomplete).toBe(true);
finishOpening();
launch.release();
await profileAttempt;
await expect(provider.close()).rejects.toMatchObject({
cleanupIncomplete: true,
@@ -1728,7 +1721,7 @@ it("retains late opening failure cleanup after cancellation until the exact prof
} finally {
caller.abort(reason);
denied = false;
finishOpening();
launch.release();
await provider.close();
}
});
+12 -21
View File
@@ -6,16 +6,17 @@ import { expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import { DesktopBuildSecrets } from "../application/desktop/DesktopBuildSecrets.js";
import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js";
import {
phaseGate,
type PhaseGate,
} from "../../tests/support/lifecycleHarness.js";
// A made-up value, never a real credential.
const FIRST = "e-Plum7-Kite-Throwaway-42";
/** The next secret file opened to be written waits, once its bytes are due, until `release` settles. */
/** The next secret file opened to be written holds this gate once its bytes are due. */
const hold = vi.hoisted(() => ({
next: undefined as
| { readonly entered: () => void; readonly release: Promise<void> }
| undefined,
next: undefined as PhaseGate | undefined,
}));
vi.mock("node:fs/promises", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:fs/promises")>();
@@ -32,8 +33,7 @@ vi.mock("node:fs/promises", async (importOriginal) => {
hold.next = undefined;
const write = file.writeFile.bind(file);
file.writeFile = async (...written: Parameters<typeof write>) => {
held.entered();
await held.release;
await held.hold();
await write(...written);
};
}
@@ -46,22 +46,13 @@ it("never gives one export's handle to another whose bytes differ while the firs
const directory = join(await createTestTempDirectory("secrets-"), "s");
await mkdir(directory, { mode: 0o700 });
const secrets = new DesktopBuildSecrets(directory);
let entered = () => {};
const writing = new Promise<void>((resolve) => {
entered = resolve;
});
let release = () => {};
hold.next = {
entered,
release: new Promise<void>((resolve) => {
release = resolve;
}),
};
const firstWrite = phaseGate();
hold.next = firstWrite;
const first = secrets.keep("rea-app", Buffer.from(FIRST));
try {
await awaitAcknowledgement(writing, first, "first secret write");
await firstWrite.reached(first, "first secret write");
const second = await secrets.keep("rea-app", Buffer.alloc(0));
release();
firstWrite.release();
const kept = await first;
expect(kept).not.toBe(second);
expect(await secrets.resolve(second, "setting")).toEqual({
@@ -74,7 +65,7 @@ it("never gives one export's handle to another whose bytes differ while the firs
});
expect((await readdir(directory)).sort()).toEqual([kept, second].sort());
} finally {
release();
firstWrite.release();
await Promise.allSettled([first]);
await secrets.close();
}
+9 -22
View File
@@ -1,21 +1,14 @@
import { expect, it } from "vitest";
import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js";
import { phaseGate } from "../../tests/support/lifecycleHarness.js";
import { V8InspectorAdmission } from "./V8InspectorProvider.js";
const gate = (): { promise: Promise<void>; release: () => void } => {
let release = (): void => {};
const promise = new Promise<void>((resolve) => {
release = resolve;
});
return { promise, release };
};
it("does not make one inspector admission wait on another", async () => {
const first = new V8InspectorAdmission();
const second = new V8InspectorAdmission();
const held = gate();
const running = first.run("observe_javascript_runtime", () => held.promise);
const held = phaseGate();
const running = first.run("observe_javascript_runtime", () => held.hold());
const closing = first.close();
try {
let secondRan = false;
@@ -37,10 +30,9 @@ it("does not make one inspector admission wait on another", async () => {
it("holds a later operation until the same admission finishes closing", async () => {
const admission = new V8InspectorAdmission();
const held = gate();
const running = admission.run(
"observe_javascript_runtime",
() => held.promise,
const held = phaseGate();
const running = admission.run("observe_javascript_runtime", () =>
held.hold(),
);
const closing = admission.close();
let proceeded = false;
@@ -61,21 +53,16 @@ it("holds a later operation until the same admission finishes closing", async ()
it("closes one inspector file scope without waiting for another", async () => {
const first = new V8InspectorAdmission();
const second = new V8InspectorAdmission();
const held = gate();
let entered!: () => void;
const started = new Promise<void>((resolve) => {
entered = resolve;
});
const held = phaseGate();
const scope = first.files;
const original = scope.retryCleanup;
scope.retryCleanup = function (this: typeof scope) {
entered();
return held.promise.then(() => original.call(this));
return held.hold().then(() => original.call(this));
};
const closingFirst = first.close();
const closingSecond = second.close();
try {
await awaitAcknowledgement(started, closingFirst, "first file cleanup");
await held.reached(closingFirst, "first file cleanup");
await awaitAcknowledgement(
closingSecond.then(() => undefined),
closingFirst,