100 lines
2.9 KiB
TypeScript
100 lines
2.9 KiB
TypeScript
// @vitest-environment node
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { settleIntegrationQueries, settleIntegrationQueriesWithProvenance } from "./settle-integrations";
|
|
|
|
const integrations = [
|
|
{ id: "first", name: "First", kind: "mock" },
|
|
{ id: "second", name: "Second", kind: "mock" },
|
|
];
|
|
|
|
describe("settleIntegrationQueries", () => {
|
|
it("retains successful integration results when another integration fails", async () => {
|
|
const results = await settleIntegrationQueries(
|
|
integrations,
|
|
vi.fn(async (integration) => {
|
|
if (integration.id === "first") throw new Error("offline");
|
|
return integration.name;
|
|
}),
|
|
{ throwOnAllFailure: true },
|
|
);
|
|
|
|
expect(results).toEqual(["Second"]);
|
|
});
|
|
|
|
it("retains fallback and successful results for a partial failure", async () => {
|
|
const results = await settleIntegrationQueries(
|
|
integrations,
|
|
vi.fn(async (integration) => {
|
|
if (integration.id === "first") throw new Error("offline");
|
|
return integration.name;
|
|
}),
|
|
{
|
|
throwOnAllFailure: true,
|
|
fallback: (integration) => `${integration.name} unavailable`,
|
|
},
|
|
);
|
|
|
|
expect(results).toEqual(["First unavailable", "Second"]);
|
|
});
|
|
|
|
it("throws when every integration fails and throwOnAllFailure is enabled", async () => {
|
|
await expect(
|
|
settleIntegrationQueries(
|
|
integrations,
|
|
vi.fn(async () => {
|
|
throw new Error("offline");
|
|
}),
|
|
{ throwOnAllFailure: true },
|
|
),
|
|
).rejects.toThrow("offline");
|
|
});
|
|
|
|
it("throws on total failure even when fallbacks are configured", async () => {
|
|
await expect(
|
|
settleIntegrationQueries(
|
|
integrations,
|
|
vi.fn(async () => {
|
|
throw new Error("offline");
|
|
}),
|
|
{
|
|
throwOnAllFailure: true,
|
|
fallback: (integration) => `${integration.name} unavailable`,
|
|
},
|
|
),
|
|
).rejects.toThrow("offline");
|
|
});
|
|
|
|
it("does not treat an empty integration list as a failure", async () => {
|
|
await expect(
|
|
settleIntegrationQueries(
|
|
[],
|
|
vi.fn(async () => "unused"),
|
|
{ throwOnAllFailure: true },
|
|
),
|
|
).resolves.toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("settleIntegrationQueriesWithProvenance", () => {
|
|
it("counts failed queries even when fallbacks preserve the result count", async () => {
|
|
const { results, ...provenance } = await settleIntegrationQueriesWithProvenance(
|
|
integrations,
|
|
vi.fn(async (integration) => {
|
|
if (integration.id === "first") throw new Error("offline");
|
|
return { integrationId: integration.id, isStale: true };
|
|
}),
|
|
{
|
|
fallback: (integration) => ({ integrationId: integration.id, isStale: false }),
|
|
},
|
|
);
|
|
|
|
expect(results).toHaveLength(2);
|
|
expect(provenance).toEqual({
|
|
failedIntegrationCount: 1,
|
|
staleIntegrationCount: 1,
|
|
});
|
|
});
|
|
});
|