testRuntimeReloadsRetiredBuild

tcmts.cli.test.e2e.prod.testRuntimeReloadsRetiredBuild · #q7t3ajk7 · term

the browser runtime reloads a page whose build was retired

import { browserRuntime } from "#codebase/tcmts/cli/web";
import assert from "node:assert/strict";

export const testRuntimeReloadsRetiredBuild = async () => {
  let reloads = 0;
  const g = globalThis as Record<string, unknown>;
  const saved = { fetch: g.fetch, location: g.location };
  g.location = { reload: () => reloads++ };
  g.fetch = async () => new Response(JSON.stringify({ name: "OutdatedError", message: "gone" }), { status: 410 });
  try {
    const mod = (await import("data:text/javascript," + encodeURIComponent(browserRuntime()))) as { remote: (h: string, n: string, c: boolean) => () => Promise<unknown> };
    const call = mod.remote("a".repeat(52), "greeting", true)();
    const settled = await Promise.race([call.then(() => "resolved", () => "rejected"), new Promise((r) => setTimeout(() => r("pending"), 100))]);
    assert.equal(reloads, 1, "the page reloads itself onto the current build");
    assert.equal(settled, "pending", "and the stale call never resolves with stale data");
  } finally {
    Object.assign(g, saved);
    delete g.__tcmReloading;
  }
}

uses