| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { RELOADED_KEY, clientNavigated, reloadFixes, reloadedBefore } from "./stale-build.ts"; |
| 5 | |
| 6 | test("a client navigation that hits an older build reloads the page", () => { |
| 7 | const seen = { clientNavigation: true, caughtByCatchAll: false }; |
| 8 | assert.equal(reloadFixes({ ...seen, error: new Error("Unable to discover routes due to manifest version mismatch.") }), true); |
| 9 | assert.equal(reloadFixes({ ...seen, error: new TypeError("Failed to fetch dynamically imported module: https://g1t.sh/assets/pull-abc.js") }), true); |
| 10 | assert.equal(reloadFixes({ ...seen, error: new TypeError("Importing a module script failed.") }), true); |
| 11 | }); |
| 12 | |
| 13 | test("an address only the new build knows reloads, once it fell to the catch-all", () => { |
| 14 | assert.equal(reloadFixes({ error: {}, status: 404, clientNavigation: true, caughtByCatchAll: true }), true); |
| 15 | // A 404 a page threw itself (a missing issue) is a real one. |
| 16 | assert.equal(reloadFixes({ error: {}, status: 404, clientNavigation: true, caughtByCatchAll: false }), false); |
| 17 | }); |
| 18 | |
| 19 | test("a document load, or any other error, shows the error page", () => { |
| 20 | assert.equal(reloadFixes({ error: new Error("manifest version mismatch"), clientNavigation: false, caughtByCatchAll: false }), false); |
| 21 | assert.equal(reloadFixes({ error: {}, status: 404, clientNavigation: false, caughtByCatchAll: true }), false); |
| 22 | assert.equal(reloadFixes({ error: new Error("D1_ERROR: no such table"), clientNavigation: true, caughtByCatchAll: false }), false); |
| 23 | assert.equal(reloadFixes({ error: {}, status: 500, clientNavigation: true, caughtByCatchAll: false }), false); |
| 24 | }); |
| 25 | |
| 26 | test("the location a tab's document loaded at is no client navigation, whatever its key", () => { |
| 27 | // ScrollRestoration's inline script gives the first entry a random key. |
| 28 | assert.equal(clientNavigated("kvhli4udgd", "kvhli4udgd"), false); |
| 29 | assert.equal(clientNavigated("default", "default"), false); |
| 30 | assert.equal(clientNavigated("a1b2c3", "kvhli4udgd"), true); |
| 31 | // The server never navigated. |
| 32 | assert.equal(clientNavigated("default", undefined), false); |
| 33 | assert.equal(clientNavigated("a1b2c3", undefined), false); |
| 34 | }); |
| 35 | |
| 36 | test("an address loaded again once is not loaded again", () => { |
| 37 | const storage = (value: string | null) => ({ getItem: (key: string) => (key === RELOADED_KEY ? value : null) }); |
| 38 | assert.equal(reloadedBefore("/flagon-io/-/docs", storage("/flagon-io/-/docs")), true); |
| 39 | assert.equal(reloadedBefore("/flagon-io/-/docs", storage("/flagon-io/g1t/nope")), false); |
| 40 | assert.equal(reloadedBefore("/flagon-io/-/docs", storage(null)), false); |
| 41 | // No storage, or storage that throws: nothing remembered. |
| 42 | assert.equal(reloadedBefore("/flagon-io/-/docs", undefined), false); |
| 43 | assert.equal(reloadedBefore("/flagon-io/-/docs", { getItem: () => { throw new Error("denied"); } }), false); |
| 44 | }); |