Files
reverse-engineering-anything/src/domain/analysisErrorCore.cancellation.test.ts
T
morluto b655645608 refactor(errors): give caller aborts a typed reason at the adapters (#10633)
* refactor(errors): give caller aborts a typed reason at the adapters

Squashes 908813551f, 87858de4dc (fix(mcp): preserve cancellation reasons in handlers) and fa151dc3f6 (test(server): race handler-entry gates against the triggering tool call) onto main without #10566's pre-squash commits.

* refactor(errors): give owner-initiated aborts a typed reason outside the browser adapters (#10692)

* refactor(errors): give owner-initiated aborts a typed reason outside the browser adapters

* refactor(verify): keep the abort-reason scan receiver-independent

Playwright's route.abort takes a network error code; the one call site now names its default code instead of the guard exempting a variable name.

* refactor(browser): give owner-initiated CDP aborts typed reasons (#10706)

* refactor(browser): give owner-initiated CDP aborts typed reasons

* fix(browser): give the page-side request fetch page-native abort reasons

The request callback runs in the page through page.evaluate and cannot
reach REA's modules. Define its deadline and cleanup reasons as
DOMExceptions inside the callback, and make the test fake evaluate the
callback from its source so module imports are unavailable, as in a page.

* test(process): expect the reader close's typed owner reason

Closing the Darwin run-token reader aborts its compilation with OwnerCancellation.ownerClosed since the owner-abort change in this PR; the boundary test still expected the default DOMException.

* fix(server): report HTTP session replacement and shutdown to pending tools as an owner close
2026-11-15 17:28:05 +08:00

93 lines
3.4 KiB
TypeScript

import { expect, it } from "vitest";
import {
AnalysisCancelledError,
CallerAbort,
cancelledBy,
carriedReason,
isAborted,
OwnerCancellation,
} from "./analysisErrorCore.js";
import { projectAnalysisError } from "./analysisErrorProjection.js";
it("takes the cause from each kind of required cancellation reason", () => {
const callerReason = { caller: "stop" };
const controller = new AbortController();
controller.abort(callerReason);
const fromSignal = cancelledBy("probe", controller.signal);
const fromEarlier = cancelledBy("outer", fromSignal);
const fromCarried = cancelledBy("carried", carriedReason(callerReason));
const fromOwner = cancelledBy("closed", OwnerCancellation.ownerClosed);
expect(fromSignal).toBeInstanceOf(AnalysisCancelledError);
expect(fromSignal.operation).toBe("probe");
expect(fromSignal.cause).toBe(callerReason);
expect(fromEarlier.cause).toBe(callerReason);
expect(fromCarried.cause).toBe(callerReason);
expect(fromOwner.cause).toBe(OwnerCancellation.ownerClosed);
expect(OwnerCancellation.ownerClosed.kind).toBe("owner-closed");
});
it("keeps the cancellation projection unchanged by the reason it carries", () => {
const controller = new AbortController();
controller.abort(new Error("caller detail"));
const cleanup = { reason: "retained", resources: ["/tmp/owned"] };
const fromSignal = cancelledBy("probe", controller.signal, {
userMessage: "Stopped.",
cleanup,
});
const fromOwner = cancelledBy("probe", OwnerCancellation.ownerClosed, {
userMessage: "Stopped.",
cleanup,
});
expect(projectAnalysisError(fromOwner)).toEqual(
projectAnalysisError(fromSignal),
);
expect(JSON.stringify(projectAnalysisError(fromOwner))).not.toContain(
"OwnerCancellation",
);
});
it("narrows only an aborted signal for cancelledBy", () => {
const controller = new AbortController();
const optional: AbortSignal | undefined = controller.signal;
expect(isAborted(undefined)).toBe(false);
expect(isAborted(optional)).toBe(false);
controller.abort("caller");
if (!isAborted(optional)) throw new Error("Expected an aborted signal");
expect(cancelledBy("probe", optional).cause).toBe("caller");
});
it("reads the reason of a signal that is not this realm's AbortSignal", () => {
const reason = { caller: "foreign" };
const foreign = { aborted: true, reason } as unknown as AbortSignal;
expect(cancelledBy("probe", foreign).cause).toBe(reason);
});
it("does not accept a cause beside the required reason", () => {
const controller = new AbortController();
controller.abort("caller");
// @ts-expect-error The reason argument owns the cause.
const error = cancelledBy("probe", controller.signal, { cause: "other" });
expect(error.cause).toBe("caller");
});
it("keeps a caller abort the platform AbortError shape that names its adapter", () => {
const interrupt = CallerAbort.forProcessSignal("SIGINT");
const cancelled = new CallerAbort("request-cancelled", "user stopped");
for (const reason of [interrupt, cancelled]) {
expect(reason).toBeInstanceOf(DOMException);
expect(reason).toBeInstanceOf(Error);
expect(reason.name).toBe("AbortError");
}
expect(interrupt.kind).toBe("interrupt");
expect(CallerAbort.forProcessSignal("SIGTERM").kind).toBe("terminate");
expect(cancelled.clientReason).toBe("user stopped");
expect(OwnerCancellation.startupOwnerClosed).not.toBe(
OwnerCancellation.ownerClosed,
);
});