[tests] Release screenshots, tests first: the demo control plane draws every shot from invented data only (.test names, documentation addresses)
This commit is contained in:
+32
-15
@@ -3,7 +3,8 @@
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
// A stateful stand-in for control's API, shaped like the real answers (docs/api.md), so the
|
||||
// smoke tests can walk every §4 flow and count its clicks.
|
||||
// smoke tests can walk every §4 flow and count its clicks. Its builders and routes are also the
|
||||
// base of the screenshots' demo control plane (demo.ts, docs/release.md).
|
||||
|
||||
type Json = Record<string, unknown>;
|
||||
|
||||
@@ -22,7 +23,7 @@ export interface Mock {
|
||||
const now = () => new Date().toISOString();
|
||||
const later = (s: number) => new Date(Date.now() + s * 1000).toISOString();
|
||||
|
||||
function host(hostname: string, share: boolean) {
|
||||
export function host(hostname: string, share: boolean) {
|
||||
return {
|
||||
hostname,
|
||||
upstreams: ["http://10.0.0.2:3000"],
|
||||
@@ -37,7 +38,7 @@ function host(hostname: string, share: boolean) {
|
||||
};
|
||||
}
|
||||
|
||||
function apply(id: string, state: string, summary: string, hosts: string[]) {
|
||||
export function apply(id: string, state: string, summary: string, hosts: string[]) {
|
||||
return {
|
||||
id,
|
||||
change_id: id,
|
||||
@@ -58,12 +59,30 @@ function apply(id: string, state: string, summary: string, hosts: string[]) {
|
||||
};
|
||||
}
|
||||
|
||||
export function installMock(): Mock {
|
||||
const m: Mock = {
|
||||
/** One API call: method, the path after /api/v1, and its answer as [status, body]. */
|
||||
export type Route = [string, RegExp, (mm: RegExpMatchArray, body: Json) => [number, unknown]];
|
||||
|
||||
export function newMock(): Mock {
|
||||
return {
|
||||
calls: [],
|
||||
state: { engine: "pingora", switching: 0, intermediate: "due", renewal: false, requestPolls: 0, hostsAdded: [] },
|
||||
};
|
||||
}
|
||||
|
||||
/** The first route that matches answers, with server_time added as control does; else control's 404. */
|
||||
export function respond(routes: Route[], method: string, path: string, body: Json): { status: number; text: string | null } {
|
||||
for (const [mm, re, f] of routes) {
|
||||
const match = path.match(re);
|
||||
if (mm === method && match) {
|
||||
const [status, data] = f(match, body);
|
||||
const withTime = data && typeof data === "object" ? { ...(data as Json), server_time: now() } : data;
|
||||
return { status, text: status === 204 ? null : JSON.stringify(withTime) };
|
||||
}
|
||||
}
|
||||
return { status: 404, text: JSON.stringify({ error: { code: "request.not_found", message: "There is no such API call.", field: null, params: {}, retry_after_s: null, detail: null }, errors: [], request_id: "x" }) };
|
||||
}
|
||||
|
||||
export function mockRoutes(m: Mock): Route[] {
|
||||
const overview = (): Json => ({
|
||||
counts: { hosts: 2 + m.state.hostsAdded.length, healthy: 2, degraded: 0, down: 0, unknown: 0 },
|
||||
hosts: [host("wiki.home.test", true), host("photos.home.test", false), ...m.state.hostsAdded.map((h) => host(h, false))],
|
||||
@@ -108,7 +127,7 @@ export function installMock(): Mock {
|
||||
server_time: now(),
|
||||
});
|
||||
|
||||
const routes: [string, RegExp, (mm: RegExpMatchArray, body: Json) => [number, unknown]][] = [
|
||||
return [
|
||||
["GET", /^\/overview$/, () => [200, overview()]],
|
||||
["POST", /^\/hosts\/validate/, () => [200, { message: "This host can be applied." }]],
|
||||
["POST", /^\/hosts$/, (_, b) => {
|
||||
@@ -152,6 +171,11 @@ export function installMock(): Mock {
|
||||
["POST", /^\/tokens$/, (_, b) => [201, { id: "tk_1", name: b.name, token: "ian_tk1_secretvalue", expires_at: later(365 * 86400) }]],
|
||||
["GET", /^\/share-links$/, () => [200, { items: [], epoch: 0 }]],
|
||||
];
|
||||
}
|
||||
|
||||
export function installMock(): Mock {
|
||||
const m = newMock();
|
||||
const routes = mockRoutes(m);
|
||||
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = typeof input === "string" ? input : input instanceof URL ? input.pathname + input.search : input.url;
|
||||
@@ -159,15 +183,8 @@ export function installMock(): Mock {
|
||||
const method = (init?.method ?? "GET").toUpperCase();
|
||||
const body = init?.body ? JSON.parse(String(init.body)) : {};
|
||||
m.calls.push({ method, path, body });
|
||||
for (const [mm, re, f] of routes) {
|
||||
const match = path.match(re);
|
||||
if (mm === method && match) {
|
||||
const [status, data] = f(match, body);
|
||||
const withTime = data && typeof data === "object" ? { ...(data as Json), server_time: now() } : data;
|
||||
return new Response(status === 204 ? null : JSON.stringify(withTime), { status, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
}
|
||||
return new Response(JSON.stringify({ error: { code: "request.not_found", message: "There is no such API call.", field: null, params: {}, retry_after_s: null, detail: null }, errors: [], request_id: "x" }), { status: 404, headers: { "Content-Type": "application/json" } });
|
||||
const a = respond(routes, method, path, body);
|
||||
return new Response(a.text, { status: a.status, headers: { "Content-Type": "application/json" } });
|
||||
}) as typeof fetch;
|
||||
|
||||
return m;
|
||||
|
||||
Reference in New Issue
Block a user