Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion client/e2e/capabilities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function makeRepo(withMap: boolean): string {
const files: Record<string, string> = {
"src/editor.ts": Array.from({ length: 40 }, (_, i) => `export const e${i} = ${i};`).join("\n") + "\n",
"src/editor.test.ts": `import { e1 } from "./editor";\ntest("saves", () => expect(e1).toBe(1));\n`,
"src/export.ts": Array.from({ length: 20 }, (_, i) => `export const x${i} = ${i};`).join("\n") + "\n",
"src/export.ts": Array.from({ length: 200 }, (_, i) => `export const x${i} = ${i};`).join("\n") + "\n",
"src/legacy.ts": "export const old = 1;\n",
};
if (withMap) files["docs/capabilities/notes.yaml"] = AREA;
Expand Down Expand Up @@ -73,9 +73,68 @@ test("a repo with a map opens on its capabilities; one without keeps the treemap
await expect(page).toHaveURL(/cap=note-export/);
await page.screenshot({ path: path.join(SHOTS, "capabilities.png") });

// The rail at rest ranks the weak capability first; the weak filter and search keep it lit.
await page.keyboard.press("Escape");
await expect(page.locator("[data-cap-weakest]")).toContainText("Export a note");
await page.locator('[data-cap-weak="none"]').click();
await expect(page.locator('[data-capability="note-editor"]')).toHaveClass(/cap-dim/);
await expect(page.locator('[data-capability="note-export"]')).not.toHaveClass(/cap-dim/);
await page.locator('[data-cap-weak="none"]').click();
await page.keyboard.press("/");
await page.keyboard.type("editor.ts");
await expect(page.locator('[data-capability="note-export"]')).toHaveClass(/cap-dim/);
await page.keyboard.press("Enter");
await expect(page.locator('[data-cap-detail="note-editor"]')).toBeVisible();

// An area with no code of its own is a search result too.
await page.locator("[data-cap-search] input").fill("Write and read notes");
await expect(page.locator("[data-cap-matches]")).toHaveText("1");
await page.keyboard.press("Enter");
await expect(page.locator('[data-cap-detail="notes"]')).toBeVisible();
await page.locator("[data-cap-search] input").fill("");

// An unclaimed folder tile opens that folder's files.
await page.locator('[data-cap-folder="src"]').click();
await expect(page.locator('[data-cap-detail="unclaimed:src"]')).toContainText("legacy.ts");

// Back follows the URL: the previous selection comes back in the rail.
await page.locator('[data-capability="note-export"]').click();
await page.locator('[data-view-option="treemap"]').click();
await page.goBack();
await expect(page.locator('[data-cap-detail="note-export"]')).toBeVisible();

// Picking Treemap on a mapped repo is kept in the URL, so a reload (or a shared link) stays there.
await page.locator('[data-view-option="treemap"]').click();
await expect(page).toHaveURL(/view=treemap/);
await page.reload();
await expect(page.locator('[data-view-option="treemap"]')).toHaveAttribute("aria-checked", "true", { timeout: 60_000 });
await page.locator('[data-view-option="capabilities"]').click();
await expect(page.locator('[data-view="capabilities"]')).toHaveAttribute("data-cap-status", "checked");

const plain = makeRepo(false);
await page.goto(`/?${new URLSearchParams({ repo: plain })}`);
await expect(page.locator('[data-view-option="treemap"]')).toHaveAttribute("aria-checked", "true", { timeout: 60_000 });
await expect(page.locator('[data-view-option="capabilities"]')).toHaveCount(0);
expect(errors).toEqual([]);
});

test("narrow views open the overview on demand, and a failed map request says so", async ({ page }) => {
const mapped = makeRepo(true);
await page.setViewportSize({ width: 1100, height: 800 });
await page.goto(`/?${new URLSearchParams({ repo: mapped, view: "capabilities" })}`);
await expect(page.locator('[data-view="capabilities"]')).toHaveAttribute("data-cap-status", "checked", { timeout: 60_000 });
// The explorer takes part of the width, so the view is under 980 px: the rail waits for a click.
await expect(page.locator("[data-cap-overview]")).toHaveCount(0);
await page.locator("[data-cap-rail-toggle]").click();
await expect(page.locator("[data-cap-weakest]")).toContainText("Export a note");
await expect(page.locator("[data-cap-errors]")).toContainText("note-export");

// A request that fails: an explicit Capabilities URL stays and names the failure; a plain URL shows the treemap.
await page.route("**/api/capabilities**", (route) => route.abort());
await page.goto(`/?${new URLSearchParams({ repo: mapped, view: "capabilities" })}`);
await expect(page.locator('[data-view="capabilities"]')).toHaveAttribute("data-cap-status", "unavailable", { timeout: 60_000 });
await expect(page.locator('[data-view="capabilities"]')).toContainText("could not be read");
await page.goto(`/?${new URLSearchParams({ repo: mapped })}`);
await expect(page.locator('[data-view-option="treemap"]')).toHaveAttribute("aria-checked", "true", { timeout: 60_000 });
await expect(page.locator('[data-view-option="capabilities"]')).toHaveCount(0);
});
22 changes: 11 additions & 11 deletions client/src/components/Treemap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,26 @@ export default function Treemap(props: TreemapProps) {
const showCapabilities = () => location().view === "capabilities";
// Until the map's status is known, a URL with no view could still land on Capabilities:
// hold the treemap canvas (under a second) instead of flashing it and switching away.
const viewAsked = new URLSearchParams(window.location.search).has("view");
const deciding = () => !viewAsked && !capMap() && capabilities.state !== "errored" && location().view === "treemap";
const deciding = () => !location().pinned && !capMap() && capabilities.state !== "errored" && location().view === "treemap";
const covered = () => showDependencyGraph() || showScatter() || showChanges() || showCapabilities() || deciding();
// The capabilities map (docs/capabilities/*.yaml), read again with each new scan of the root.
// Keyed on the root, not the filtered tree: an Explorer filter that matches nothing must not stall it.
const [capabilities] = createResource(
() => (props.data ? { root: props.rootPath, data: props.data } : null),
({ root }) => fetchCapabilities(root),
() => props.rootPath || null,
(root) => fetchCapabilities(root),
);
// Read the value only once it exists: a first read of a resource suspends the boundary above it.
const capMap = () => (capabilities.state === "ready" || capabilities.state === "refreshing" ? capabilities.latest : undefined);
const hasCapabilities = () => capMap()?.status === "checked";
// A repo with a map opens on it, unless the URL already names a view; one without stays on the treemap.
let landed = false;
let landedFor = "";
createEffect(() => {
const map = capMap();
if (!map || landed) return;
landed = true;
const asked = new URLSearchParams(window.location.search).has("view");
if (map.status === "checked" && !asked && location().view === "treemap") replace({ view: "capabilities" });
else if (map.status !== "checked" && location().view === "capabilities") replace({ view: "treemap" });
if (!map || landedFor === props.rootPath) return;
landedFor = props.rootPath;
if (map.status === "checked" && !location().pinned && location().view === "treemap") replace({ view: "capabilities" });
// Only a confirmed missing map leaves an explicit Capabilities URL; a failed request stays and says why.
else if (map.status === "no-map" && location().view === "capabilities") replace({ view: "treemap" });
});
const [menu, setMenu] = createSignal<NodeMenuTarget | null>(null);

Expand Down Expand Up @@ -272,7 +272,7 @@ export default function Treemap(props: TreemapProps) {
showMetricPopover={showMetricPopover}
setShowMetricPopover={setShowMetricPopover}
view={() => location().view}
onViewChange={(view) => go({ view })}
onViewChange={(view) => go({ view, pinned: view === "treemap" })}
hasCapabilities={hasCapabilities}
/>

Expand Down
8 changes: 5 additions & 3 deletions client/src/navigation/location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ describe("parseLocation / toSearch", () => {
expect(parseLocation(toSearch(picking))).toEqual(picking);
// Imports mode has no flow param, and the treemap ignores one.
expect(parseLocation(toSearch({ ...loc, flow: undefined }))).toEqual({ repo: "/r", at: "", view: "graph" });
expect(parseLocation("?view=treemap&flow=route:abc")).toEqual({ repo: "", at: "", view: "treemap" });
// An explicit view=treemap is a pinned choice (it outranks the capabilities landing view).
expect(parseLocation("?view=treemap&flow=route:abc")).toEqual({ repo: "", at: "", view: "treemap", pinned: true });
expect(toSearch(parseLocation("?view=treemap"))).toBe("?view=treemap");
});

it("round-trips a selected origin and destination, which never make a new place", () => {
Expand Down Expand Up @@ -56,7 +58,7 @@ describe("parseLocation / toSearch", () => {
it("reads the legacy depFocus alias as the graph view", () => {
expect(parseLocation("?depFocus=server%2Fapp")).toEqual({ repo: "", at: "server/app", view: "graph" });
expect(parseLocation("?depFocus=%2F")).toEqual({ repo: "", at: "", view: "graph" });
expect(parseLocation("?depFocus=a&at=b&view=treemap")).toEqual({ repo: "", at: "b", view: "treemap" });
expect(parseLocation("?depFocus=a&at=b&view=treemap")).toEqual({ repo: "", at: "b", view: "treemap", pinned: true });
});

it("keeps foreign params and drops the legacy one", () => {
Expand Down Expand Up @@ -209,7 +211,7 @@ describe("anchor mode", () => {
expect(search).not.toContain("flow=");
// A broken anchor is ignored; the treemap ignores both.
expect(parseLocation("?view=graph&anchor=nonsense")).toEqual({ repo: "", at: "", view: "graph" });
expect(parseLocation(`?view=treemap&anchor=${anchor}&dir=forward`)).toEqual({ repo: "", at: "", view: "treemap" });
expect(parseLocation(`?view=treemap&anchor=${anchor}&dir=forward`)).toEqual({ repo: "", at: "", view: "treemap", pinned: true });
// The direction and the anchor are part of the place: a new anchor is a new history entry, a new direction is not.
const a = { repo: "", at: "", view: "graph" as const, anchor };
expect(samePlace(a, { ...a, anchor: "src/b.tsx:1:1-1:2" })).toBe(false);
Expand Down
8 changes: 7 additions & 1 deletion client/src/navigation/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface Location {
repo: string;
at: string;
view: View;
/**
* The person picked the treemap themselves (`view=treemap` in the URL). A repo with a
* capabilities map otherwise lands on the Capabilities view; this keeps the choice in links.
*/
pinned?: boolean;
/**
* Graph only: folders and files opened in place (graph ids, which are `at`
* values). Kept across focus changes, so a mixed-scale cut survives a drill.
Expand Down Expand Up @@ -180,6 +185,7 @@ export function parseLocation(search: string): Location {
repo: params.get(REPO_PARAM) ?? "",
at: normalizeAt(rawAt),
view,
...(view === "treemap" && rawView === "treemap" ? { pinned: true } : {}),
...(open.length ? { open } : {}),
...(flow !== null ? { flow } : {}),
...(field ? { field } : {}),
Expand Down Expand Up @@ -223,7 +229,7 @@ export function toSearch(loc: Location, currentSearch = ""): string {
if (loc.view === "changes" && loc.changes) for (const [key, value] of new URLSearchParams(loc.changes)) params.set(key, value);
if (loc.repo) params.set(REPO_PARAM, loc.repo);
if (loc.at) params.set(AT_PARAM, loc.at);
if (loc.view !== "treemap") params.set(VIEW_PARAM, loc.view);
if (loc.view !== "treemap" || loc.pinned) params.set(VIEW_PARAM, loc.view);
if (loc.open?.length) params.set(OPEN_PARAM, loc.open.join(","));
if (loc.view === "graph" && loc.anchor) {
params.set(ANCHOR_PARAM, loc.anchor);
Expand Down
Loading
Loading