Open History to see the last commit
1import { renderToStaticMarkup } from "react-dom/server";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4import { fakeFetch, resetFakeServer } from "@/api/fake-fetch";
5import { DEFAULT_NETWORK_CONFIG, getNetworkConfig, setNetworkConfig } from "@/api/network-config";
6import { clearRequestLog, requestLogStore } from "@/api/request-log";
7import { subscribeToAppActions } from "@/lib/app-actions";
8import type { AppActionEvent } from "@/lib/app-actions";
9
10import { LATENCY_PRESETS } from "./latency";
11import { RequestLog, setFailNextRequest, setLatency } from "./network-controls";
12
13beforeEach(() => {
14 resetFakeServer();
15 clearRequestLog();
16 setNetworkConfig(DEFAULT_NETWORK_CONFIG);
17});
18
19afterEach(() => {
20 vi.useRealTimers();
21});
22
23describe("network presets", () => {
24 it.each(LATENCY_PRESETS)("a %i ms preset makes the fake API take that long", async (ms) => {
25 vi.useFakeTimers();
26 setLatency(ms);
27 let settled = false;
28 const request = fakeFetch("/api/account").then(() => (settled = true));
29 expect(requestLogStore.get().at(-1)).toMatchObject({ state: "pending", latencyMs: ms });
30 if (ms > 0) {
31 await vi.advanceTimersByTimeAsync(ms - 1);
32 expect(settled).toBe(false);
33 }
34 await vi.advanceTimersByTimeAsync(1);
35 await request;
36 expect(requestLogStore.get().at(-1)).toMatchObject({ state: "done", durationMs: ms });
37 });
38
39 it("fail next makes the next request fail with a 500, once", async () => {
40 setLatency(0);
41 setFailNextRequest(true);
42 expect((await fakeFetch("/api/account")).status).toBe(500);
43 expect(getNetworkConfig().failNextRequest).toBe(false);
44 expect((await fakeFetch("/api/account")).status).toBe(200);
45 });
46
47 it("reports each change as an app action, so the guide can follow", () => {
48 const events: AppActionEvent[] = [];
49 const stop = subscribeToAppActions((event) => events.push(event));
50 setLatency(5000);
51 setFailNextRequest(true);
52 stop();
53 expect(events).toEqual([
54 { type: "network.latency-set", latencyMs: 5000, status: "applied" },
55 { type: "network.fail-next-set", failNext: true, status: "applied" },
56 ]);
57 });
58});
59
60describe("the request inspector", () => {
61 const render = () => renderToStaticMarkup(<RequestLog />);
62
63 it("says so when there are no requests yet", () => {
64 expect(render()).toContain("No requests yet");
65 });
66
67 it("shows each request from the log, newest first, pending until it settles", async () => {
68 vi.useFakeTimers();
69 setLatency(5000);
70 const save = fakeFetch("/api/folders/work", {
71 method: "PATCH",
72 headers: { "Content-Type": "application/json" },
73 body: JSON.stringify({ name: "Jobs" }),
74 });
75 const load = fakeFetch("/api/notes");
76
77 let html = render();
78 expect(html).not.toContain("No requests yet");
79 expect(html.indexOf("/api/notes")).toBeLessThan(html.indexOf("/api/folders/work"));
80 expect(html.match(/data-state="pending"/g)).toHaveLength(2);
81 expect(html).toContain("Pending");
82 expect(html).toContain("Latency 5 s");
83 // The bodies are there for when a row is expanded.
84 expect(html).toContain(""name": "Jobs"");
85
86 await vi.advanceTimersByTimeAsync(5000);
87 await Promise.all([save, load]);
88 html = render();
89 expect(html).not.toContain('data-state="pending"');
90 expect(html.match(/>200</g)).toHaveLength(2);
91 expect(html).toContain("5.0 s");
92 });
93
94 it("marks failed requests", async () => {
95 setLatency(0);
96 setFailNextRequest(true);
97 await fakeFetch("/api/account");
98 const html = render();
99 expect(html).toContain(">500<");
100 expect(html).toContain("Failed on purpose");
101 });
102});