feat: add removable reference feature vertical slice
This commit is contained in:
@@ -3,10 +3,11 @@ import { setupServer } from "msw/node";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
import { createHttpClient } from "../../src/adapters/http/client.js";
|
||||
import { TEST_HTTP_CONTRACT } from "../helpers/http-contract-fixture.js";
|
||||
|
||||
let attempts = 0;
|
||||
const server = setupServer(
|
||||
http.get("https://api.test/api/sample/resources", () => {
|
||||
http.get("https://api.test/api/entities", () => {
|
||||
attempts += 1;
|
||||
if (attempts < 3) {
|
||||
return HttpResponse.json(
|
||||
@@ -34,16 +35,21 @@ const clock = {
|
||||
sleep: async () => {},
|
||||
};
|
||||
|
||||
/** @param {Parameters<typeof createHttpClient>[0]} options */
|
||||
function testClient(options) {
|
||||
return createHttpClient({ ...TEST_HTTP_CONTRACT, ...options });
|
||||
}
|
||||
|
||||
describe("shared HTTP client", () => {
|
||||
it("retries a safe request at most twice and returns validated data", async () => {
|
||||
const client = createHttpClient({
|
||||
const client = testClient({
|
||||
baseUrl: "https://api.test",
|
||||
clock,
|
||||
random: () => 0,
|
||||
});
|
||||
|
||||
await expect(
|
||||
client.execute("LIST_SAMPLE_RESOURCES", { routeId: "SAMPLE_RESOURCE_LIST" }),
|
||||
client.execute("LIST_ENTITIES", { routeId: "TEST_ROUTE" }),
|
||||
).resolves.toMatchObject({
|
||||
ok: true,
|
||||
value: [{ id: "resource-1", displayName: "Example" }],
|
||||
@@ -55,12 +61,12 @@ describe("shared HTTP client", () => {
|
||||
it("rejects a non-JSON response without exposing its body", async () => {
|
||||
server.use(
|
||||
http.get(
|
||||
"https://api.test/api/sample/resources",
|
||||
"https://api.test/api/entities",
|
||||
() => new HttpResponse("<secret>raw body</secret>", { status: 502 }),
|
||||
),
|
||||
);
|
||||
const client = createHttpClient({ baseUrl: "https://api.test", clock });
|
||||
const result = await client.execute("LIST_SAMPLE_RESOURCES");
|
||||
const client = testClient({ baseUrl: "https://api.test", clock });
|
||||
const result = await client.execute("LIST_ENTITIES");
|
||||
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
@@ -72,21 +78,21 @@ describe("shared HTTP client", () => {
|
||||
it("classifies malformed JSON and invalid payloads at the boundary", async () => {
|
||||
server.use(
|
||||
http.get(
|
||||
"https://api.test/api/sample/resources",
|
||||
"https://api.test/api/entities",
|
||||
() =>
|
||||
new HttpResponse("{", {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}),
|
||||
),
|
||||
);
|
||||
const client = createHttpClient({ baseUrl: "https://api.test", clock });
|
||||
await expect(client.execute("LIST_SAMPLE_RESOURCES")).resolves.toMatchObject({
|
||||
const client = testClient({ baseUrl: "https://api.test", clock });
|
||||
await expect(client.execute("LIST_ENTITIES")).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "MALFORMED_JSON" },
|
||||
});
|
||||
|
||||
server.use(
|
||||
http.get("https://api.test/api/sample/resources", () =>
|
||||
http.get("https://api.test/api/entities", () =>
|
||||
HttpResponse.json({
|
||||
success: true,
|
||||
data: [{ id: "resource-1", name: 42 }],
|
||||
@@ -94,7 +100,7 @@ describe("shared HTTP client", () => {
|
||||
}),
|
||||
),
|
||||
);
|
||||
await expect(client.execute("LIST_SAMPLE_RESOURCES")).resolves.toMatchObject({
|
||||
await expect(client.execute("LIST_ENTITIES")).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "SCHEMA_MISMATCH" },
|
||||
});
|
||||
@@ -102,7 +108,7 @@ describe("shared HTTP client", () => {
|
||||
|
||||
it("guards mapper exceptions as UNKNOWN_FAILURE", async () => {
|
||||
server.use(
|
||||
http.get("https://api.test/api/sample/resources", () =>
|
||||
http.get("https://api.test/api/entities", () =>
|
||||
HttpResponse.json({
|
||||
success: true,
|
||||
data: [{ id: "resource-1", name: "Example" }],
|
||||
@@ -110,7 +116,7 @@ describe("shared HTTP client", () => {
|
||||
}),
|
||||
),
|
||||
);
|
||||
const client = createHttpClient({
|
||||
const client = testClient({
|
||||
baseUrl: "https://api.test",
|
||||
clock,
|
||||
mapPayload: () => {
|
||||
@@ -118,7 +124,7 @@ describe("shared HTTP client", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const result = await client.execute("LIST_SAMPLE_RESOURCES");
|
||||
const result = await client.execute("LIST_ENTITIES");
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "UNKNOWN_FAILURE" },
|
||||
|
||||
Reference in New Issue
Block a user