Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 37x 37x 37x 828x 828x 322x 892x 892x 892x 37x 892x 892x 469x 2345x 2345x | import { vi } from "vitest";
/**
* Stub every HTTP verb so a smoke-rendered page never reaches the network.
*
* Responses are deliberately *empty but well-formed*: the answer a fresh
* account really gets. That is the shape most likely to break pages which
* assume their data is present — which is exactly what a smoke test should
* be finding.
*
* "Well-formed" has to account for the two shapes the API actually returns,
* because they have opposite empty values:
*
* collection endpoint `GET /agents` → []
* single resource `GET /agents/{id}` → null (the router's own
* `T | null` return type)
*
* Answering `[]` to *everything* is what this stub used to do, and it is a
* lie with teeth: `[]` is truthy, so an item fetch answering `[]` sails past
* every `if (!resource)` guard a page has and then throws on the first
* property access — `board.widgets.length`, `model.provider.display_name`.
* The page looks broken while being correct, because no real response is
* shaped like that. Answering `null` to everything has the mirror problem:
* list pages get `null` where the API would have sent `[]`.
*/
export const axiosStub = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
patch: vi.fn(),
delete: vi.fn(),
};
/**
* A request whose path ends in an id is fetching one resource.
*
* Both id conventions in this app count. Most pages decode the base64 route
* param (`useResourceId`) and send a bare UUID, but some pass the route param
* through untouched, so the segment arrives still encoded. Treating only one
* as an id would leave the other class of page being answered `[]` — the
* exact lie this function exists to stop.
*
* Query strings and trailing slashes are stripped first. Sub-collections
* under an id — `/agents/{id}/history` — end in a word rather than an id and
* are correctly treated as collections.
*/
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const decodesToUuid = (segment: string): boolean => {
try {
return UUID.test(atob(segment));
} catch {
// Not valid base64 — a plain path segment like "catalogue".
return false;
}
};
export function isItemRequest(url: string): boolean {
const path = String(url).split("?")[0].replace(/\/+$/, "");
const last = path.split("/").pop() ?? "";
return UUID.test(last) || decodesToUuid(last);
}
/** The empty-but-well-formed body for `url`. */
export const emptyBody = (url: string): unknown[] | null =>
isItemRequest(url) ? null : [];
export const emptyResponse = (url = "") => ({
data: emptyBody(url),
status: 200,
statusText: "OK",
headers: {},
config: {},
});
export function resetApiStub(): void {
for (const spy of Object.values(axiosStub)) {
spy.mockReset();
// The URL is the first argument for every verb axios exposes here, so one
// implementation serves them all.
spy.mockImplementation(async (url: string) => emptyResponse(url));
}
}
|