126 lines
4.7 KiB
TypeScript
126 lines
4.7 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { platformErrorForOutcome } from "../../../src/adapters/http/platform-error-mapping.ts";
|
|
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
|
|
import {
|
|
mapTransportFailure,
|
|
PLATFORM_ERRORS,
|
|
} from "../../../src/platform/errors/platform-errors.ts";
|
|
import { isAppError } from "../../../src/platform/errors/app-error.ts";
|
|
import { RequestBudgetExceededError } from "../../../src/platform/traffic/request-budget.ts";
|
|
import { ConcurrencyQueueFullError } from "../../../src/platform/traffic/concurrency-budget.ts";
|
|
import { DeadlineExceededError } from "../../../src/platform/resilience/deadline.ts";
|
|
import { JsonBudgetExceededError } from "../../../src/platform/validation/bounded-json.ts";
|
|
import { TEST_LIST_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts";
|
|
import {
|
|
networkFailure,
|
|
scopeSnapshot,
|
|
testExecutionContext,
|
|
testExecutor,
|
|
type Transport,
|
|
} from "../../helpers/traffic-test-kit.ts";
|
|
|
|
const abortError = () => new DOMException("aborted", "AbortError");
|
|
|
|
describe("platform error normalization", () => {
|
|
it("maps an abort caused by interaction cancellation to request.cancelled", () => {
|
|
const error = mapTransportFailure(abortError(), { deadlineExpired: false });
|
|
|
|
expect(error).toMatchObject({
|
|
kind: "request.cancelled",
|
|
category: "transport",
|
|
retryability: "never",
|
|
});
|
|
});
|
|
|
|
it("maps an abort caused by the deadline to request.timeout", () => {
|
|
expect(mapTransportFailure(abortError(), { deadlineExpired: true }).kind).toBe(
|
|
"request.timeout",
|
|
);
|
|
expect(
|
|
mapTransportFailure(new DeadlineExceededError(10, 11), {
|
|
deadlineExpired: false,
|
|
}).kind,
|
|
).toBe("request.timeout");
|
|
});
|
|
|
|
it.each([
|
|
[new TypeError("Failed to fetch"), "network.unavailable"],
|
|
[new RequestBudgetExceededError(2), "traffic.budget-exceeded"],
|
|
[new ConcurrencyQueueFullError(0), "traffic.budget-exceeded"],
|
|
[new JsonBudgetExceededError("too-large", "x"), "response.too-large"],
|
|
[new SyntaxError("Unexpected token"), "response.invalid"],
|
|
["not even an error", "network.unavailable"],
|
|
] as const)("maps %s to %s", (error, kind) => {
|
|
expect(mapTransportFailure(error, { deadlineExpired: false }).kind).toBe(kind);
|
|
});
|
|
|
|
it("keeps every platform error a valid, stable envelope", () => {
|
|
for (const error of Object.values(PLATFORM_ERRORS)) {
|
|
expect(isAppError(error)).toBe(true);
|
|
}
|
|
expect(Object.values(PLATFORM_ERRORS).map((error) => error.kind)).toEqual([
|
|
"network.unavailable",
|
|
"request.timeout",
|
|
"request.cancelled",
|
|
"response.too-large",
|
|
"response.invalid",
|
|
"traffic.budget-exceeded",
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("V3 outcome normalization at the adapter boundary", () => {
|
|
const TEST_QUERY = TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<
|
|
unknown,
|
|
unknown,
|
|
unknown
|
|
>;
|
|
|
|
it("classifies a traffic-budget rejection as traffic.budget-exceeded", async () => {
|
|
const transport = vi.fn<Transport>(networkFailure);
|
|
const execution = testExecutionContext({ maxPhysicalRequests: 1, maxRetries: 0 });
|
|
const executor = testExecutor(transport);
|
|
const context = { routeId: "TEST_ROUTE", scope: scopeSnapshot(), execution };
|
|
await executor.execute(TEST_QUERY, { limit: 1 }, context);
|
|
const rejected = await executor.execute(TEST_QUERY, { limit: 1 }, context);
|
|
|
|
expect(platformErrorForOutcome(rejected)?.kind).toBe("traffic.budget-exceeded");
|
|
});
|
|
|
|
it.each([
|
|
[{ kind: "CANCELLED", effect: "NOT_STARTED" }, "request.cancelled"],
|
|
[
|
|
{ kind: "TRANSPORT_FAILURE", failure: { kind: "TIMEOUT", retryable: false }, effect: "NOT_STARTED" },
|
|
"request.timeout",
|
|
],
|
|
[
|
|
{ kind: "TRANSPORT_FAILURE", failure: { kind: "NETWORK_FAILURE", retryable: false }, effect: "NOT_STARTED" },
|
|
"network.unavailable",
|
|
],
|
|
[
|
|
{ kind: "CONTRACT_VIOLATION", violation: { kind: "RESPONSE_TOO_LARGE", operation: "RESPONSE" }, effect: "NOT_APPLICABLE" },
|
|
"response.too-large",
|
|
],
|
|
[
|
|
{ kind: "CONTRACT_VIOLATION", violation: { kind: "JSON_INVALID", operation: "RESPONSE" }, effect: "NOT_APPLICABLE" },
|
|
"response.invalid",
|
|
],
|
|
] as const)("maps outcome %# to %s", (outcome, kind) => {
|
|
expect(platformErrorForOutcome(outcome)?.kind).toBe(kind);
|
|
});
|
|
|
|
it("leaves owner-interpreted outcomes to their owner", () => {
|
|
expect(
|
|
platformErrorForOutcome({ kind: "UNAUTHENTICATED", effect: "NOT_APPLICABLE" }),
|
|
).toBeNull();
|
|
expect(
|
|
platformErrorForOutcome({
|
|
kind: "CONTRACT_VIOLATION",
|
|
violation: { kind: "SCOPE_FENCED", operation: "RESPONSE" },
|
|
effect: "NOT_APPLICABLE",
|
|
}),
|
|
).toBeNull();
|
|
});
|
|
});
|