import { describe, expect, it } from "vitest";
// Every push that opens a new view over the current one remembers the list item it was opened from
// (`lib/history.rememberNavPosition`), so Back hands focus back to that item (E11 S5). A push that
// forgets strands keyboard focus on
again with no test noticing — the flow only shows in a real
// browser, after a round trip. So the source is the check: each `history.pushState(` in the app has a
// `rememberNavPosition(` right before it or inside its arguments. The overlay stack's own push is the
// one exception — an overlay entry opens no view, and its list stays mounted underneath.
//
// The assertion pins the SET of offending sites and checks the scan found pushes at all: a guard whose
// glob stops matching is green over nothing (E10 review round 4).
const files = import.meta.glob(["../**/*.ts", "../**/*.tsx", "!../**/*.test.*"], {
query: "?raw",
import: "default",
eager: true,
}) as Record;
const EXEMPT: Record = {
"lib/history.ts#_ov": "the overlay stack: an overlay entry opens no view over the page",
};
// Lines before a push, and lines of the push call itself, that may carry the remember call.
const BEFORE = 4;
const WITHIN = 4;
function offenders(): { hits: string[]; pushes: number } {
const hits: string[] = [];
let pushes = 0;
for (const [path, code] of Object.entries(files)) {
// Glob keys are relative to this file: `./x.ts` is lib, `../components/x.tsx` is a sibling folder.
const rel = path.startsWith("./") ? `lib/${path.slice(2)}` : path.replace(/^\.\.\//, "");
const lines = code.split("\n");
lines.forEach((line, i) => {
if (!line.includes("history.pushState(")) return;
pushes++;
const around = lines.slice(Math.max(0, i - BEFORE), i + WITHIN).join("\n");
if (around.includes("rememberNavPosition(")) return;
const exempt = Object.keys(EXEMPT).find(
(id) => id.startsWith(`${rel}#`) && around.includes(id.split("#")[1]!),
);
if (!exempt) hits.push(`${rel}:${i + 1}`);
});
}
return { hits: hits.sort(), pushes };
}
describe("every view push remembers the item it was opened from", () => {
it("finds no pushState without rememberNavPosition", () => {
const { hits, pushes } = offenders();
expect(pushes).toBeGreaterThan(4);
expect(hits).toEqual([]);
});
});