Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

81 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import test from "node:test";
3
4import { PATIENCE_MS, REFRESH_PATIENCE_MS, installResilientFetch, isNetworkFailure, keepTrying, refreshesCurrent, retriable, retryDelay, watchBuildNavigation } from "./resilient-fetch.ts";
5import { reconnectDelay } from "./reconnect.ts";
6
7const ORIGIN = "https://g1t.sh";
8const url = (path: string) => new URL(path, ORIGIN);
9
10test("only an active navigation carries its build; fetchers, refreshes and writes never force a document switch", async () => {
11 const calls: Headers[] = [];
12 const previous = Object.getOwnPropertyDescriptor(globalThis, "window");
13 const browser = { location: new URL("https://g1t.sh/edit"), fetch: async (_input: unknown, init?: RequestInit) => { calls.push(new Headers(init?.headers)); return new Response(null); } };
14 Object.defineProperty(globalThis, "window", { value: browser, configurable: true });
15 let destination: { pathname: string; search: string } | undefined;
16 const stop = watchBuildNavigation(() => destination);
17 try {
18 installResilientFetch("old-build");
19 await browser.fetch("/notifications.data");
20 destination = { pathname: "/next", search: "?tab=one" };
21 await browser.fetch("/next.data?tab=one&_routes=root");
22 await browser.fetch("/unrelated.data");
23 await browser.fetch("/next.data?tab=one", { method: "POST" });
24 await browser.fetch("/edit.data");
25 await browser.fetch("https://external.example/next.data?tab=one");
26 assert.deepEqual(calls.map((headers) => headers.get("x-g1t-build")), [null, "old-build", null, null, null, null]);
27 } finally {
28 stop();
29 if (previous) Object.defineProperty(globalThis, "window", previous);
30 else Reflect.deleteProperty(globalThis, "window");
31 }
32});
33
34test("a request that got no answer, in each browser's words, is a network failure", () => {
35 assert.equal(isNetworkFailure(new TypeError("Failed to fetch")), true);
36 assert.equal(isNetworkFailure(new TypeError("NetworkError when attempting to fetch resource.")), true);
37 assert.equal(isNetworkFailure(new TypeError("Load failed")), true);
38});
39
40test("a stale build, an abort or a server's error is not", () => {
41 assert.equal(isNetworkFailure(new TypeError("Failed to fetch dynamically imported module: https://g1t.sh/assets/a.js")), false);
42 assert.equal(isNetworkFailure(new DOMException("The user aborted a request.", "AbortError")), false);
43 assert.equal(isNetworkFailure(new Error("Failed to fetch")), false);
44 assert.equal(isNetworkFailure({ status: 500 }), false);
45});
46
47test("only the router's own reads are sent again", () => {
48 assert.equal(retriable(url("/flagon-io/g1t/settings/repository.data?_routes=root"), "GET", ORIGIN), true);
49 assert.equal(retriable(url("/__manifest?p=/a"), "get", ORIGIN), true);
50 assert.equal(retriable(url("/flagon-io/g1t/settings/repository.data"), "POST", ORIGIN), false);
51 assert.equal(retriable(url("/-/notify"), "GET", ORIGIN), false);
52 assert.equal(retriable(new URL("https://otel.g1t.sh/x.data"), "GET", ORIGIN), false);
53});
54
55test("a read for the page already showing is a refresh; one for another page is a navigation", () => {
56 const here = { pathname: "/flagon-io/g1t/settings/repository", search: "" };
57 assert.equal(refreshesCurrent(url("/flagon-io/g1t/settings/repository.data?_routes=root,routes/repo/layout"), here), true);
58 assert.equal(refreshesCurrent(url("/flagon-io/g1t/issues.data"), here), false);
59 assert.equal(refreshesCurrent(url("/_root.data"), { pathname: "/", search: "" }), true);
60 assert.equal(refreshesCurrent(url("/a.data?tab=open&_routes=x"), { pathname: "/a", search: "?tab=open" }), true);
61 assert.equal(refreshesCurrent(url("/a.data?tab=closed"), { pathname: "/a", search: "?tab=open" }), false);
62});
63
64test("retries come quickly at first, then settle", () => {
65 assert.deepEqual([1, 2, 3, 4, 5, 9].map(retryDelay), [250, 500, 1000, 2000, 4000, 5000]);
66 assert.deepEqual([1, 2, 3, 4, 8].map(reconnectDelay), [3000, 6000, 12000, 24000, 30000]);
67});
68
69test("a refresh waits out the browser being offline, however long", () => {
70 assert.equal(keepTrying({ refresh: true, elapsedMs: 60 * 60_000, onlineMs: 0, erroring: false }, 5000), true);
71});
72
73test("a network that is up and never answers is no blip: a refresh gives up, and so does a click", () => {
74 assert.equal(keepTrying({ refresh: true, elapsedMs: 0, onlineMs: REFRESH_PATIENCE_MS - 5000, erroring: false }, 5000), true);
75 assert.equal(keepTrying({ refresh: true, elapsedMs: 0, onlineMs: REFRESH_PATIENCE_MS - 4000, erroring: false }, 5000), false);
76 assert.equal(keepTrying({ refresh: false, elapsedMs: PATIENCE_MS - 4000, onlineMs: 0, erroring: false }, 5000), false);
77});
78
79test("while the error page tries on its own, a read is tried once", () => {
80 assert.equal(keepTrying({ refresh: true, elapsedMs: 0, onlineMs: 0, erroring: true }, 250), false);
81});