81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
composeContractContributions,
|
|
ContractContributionError,
|
|
type InstalledContractContribution,
|
|
type InstalledHttpContract,
|
|
} from "../../../src/contracts/external-contract-runtime.ts";
|
|
import type { TrafficBudget } from "../../../src/platform/traffic/traffic-budget.ts";
|
|
import {
|
|
TEST_CONTRACT_CONTRIBUTION,
|
|
TEST_LIST_HTTP_CONTRACT,
|
|
} from "../../helpers/external-contract-fixture.ts";
|
|
|
|
function compositionReason(run: () => unknown): string {
|
|
try {
|
|
run();
|
|
} catch (error) {
|
|
return error instanceof ContractContributionError ? error.reason : String(error);
|
|
}
|
|
throw new Error("composition unexpectedly succeeded");
|
|
}
|
|
|
|
const FINITE: TrafficBudget = Object.freeze({
|
|
maxPhysicalRequests: 3,
|
|
maxRetries: 2,
|
|
maxConcurrentRequests: 2,
|
|
maxQueuedRequests: 2,
|
|
});
|
|
|
|
function withTraffic(traffic: unknown): InstalledContractContribution {
|
|
const list = {
|
|
...TEST_LIST_HTTP_CONTRACT,
|
|
frontend: { ...TEST_LIST_HTTP_CONTRACT.frontend, traffic },
|
|
} as unknown as InstalledHttpContract<unknown, unknown, unknown>;
|
|
return {
|
|
...TEST_CONTRACT_CONTRIBUTION,
|
|
http: [list],
|
|
};
|
|
}
|
|
|
|
describe("operation traffic policy", () => {
|
|
it("requires finite positive traffic limits for an operation", () => {
|
|
expect(
|
|
compositionReason(() =>
|
|
composeContractContributions([
|
|
withTraffic({ ...FINITE, maxPhysicalRequests: Number.POSITIVE_INFINITY }),
|
|
]),
|
|
),
|
|
).toMatch(/traffic.*maxPhysicalRequests/i);
|
|
});
|
|
|
|
it.each([
|
|
{ ...FINITE, maxRetries: -1 },
|
|
{ ...FINITE, maxConcurrentRequests: 0 },
|
|
{ maxPhysicalRequests: 3, maxRetries: 1, maxConcurrentRequests: 1 },
|
|
{ ...FINITE, extra: 1 },
|
|
])("rejects a malformed traffic policy %#", (traffic) => {
|
|
expect(
|
|
compositionReason(() => composeContractContributions([withTraffic(traffic)])),
|
|
).toMatch(/traffic|field/i);
|
|
});
|
|
|
|
it("installs an immutable snapshot of a finite traffic policy", () => {
|
|
const source = { ...FINITE };
|
|
const composed = composeContractContributions([withTraffic(source)]);
|
|
source.maxPhysicalRequests = 60;
|
|
|
|
const installed = composed.httpByOperationId.get("TEST_LIST_ENTITIES");
|
|
expect(installed?.frontend.traffic).toEqual(FINITE);
|
|
expect(Object.isFrozen(installed?.frontend.traffic)).toBe(true);
|
|
});
|
|
|
|
it("keeps operations without a traffic policy valid", () => {
|
|
const composed = composeContractContributions([TEST_CONTRACT_CONTRIBUTION]);
|
|
expect(
|
|
composed.httpByOperationId.get("TEST_LIST_ENTITIES")?.frontend.traffic,
|
|
).toBeUndefined();
|
|
});
|
|
});
|