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
37 changes: 36 additions & 1 deletion pm-stack/aggregator/src/board-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// the twin agreement is by construction, and the tests in
// test/board-view-8521.test.ts pin the semantics.

import type { BoardItem, GhItem, ProjectPoolItem, RefKey } from "./model.ts";
import type { BoardItem, GhItem, ProjectPoolItem, RefKey, RoadmapRow } from "./model.ts";
import { itemMatchesView, type ViewFilters, type ViewItemLike } from "./views.ts";

/** The ViewItemLike projection of a canonical GitHub item: the
Expand Down Expand Up @@ -66,6 +66,41 @@ export function filterBoardForView(
return { board: keptBoard, items: keptItems };
}

/** The /roadmap view rule (#4013), shared verbatim with its JSON
* twin /api/roadmap.json (#8723): keep a row when the linkedLike rule
* matches it; the items map keeps every item a kept row links to
* (its metadata renders in roadmapCard's links/badges even when the
* item fails the filter itself -- Codex P2, PR #4013) plus every
* item that matches the view directly. The HTML handler has applied
* this rule since #4013; the JSON handler filtered items by the
* view alone, so a kept row could reference items absent from its
* own payload. Both handlers now compose through this function --
* the twin agreement is by construction. */
export function filterRoadmapForView(
rows: readonly RoadmapRow[],
items: Map<RefKey, GhItem>,
poolByRef: Map<RefKey, ProjectPoolItem>,
filters: ViewFilters,
): { rows: RoadmapRow[]; items: Map<RefKey, GhItem> } {
const keptRows = rows.filter((r) =>
linkedLike(
{ ref: r.subject, area: r.area, horizon: r.horizon, state: r.status },
r.links,
items,
poolByRef,
filters,
),
);
const keptRefs = new Set<string>();
for (const r of keptRows) for (const l of r.links) keptRefs.add(l.ref);
const keptItems = new Map(
[...items].filter(
([ref, it]) => keptRefs.has(ref) || itemMatchesView(ghItemLike(it, poolByRef.get(ref)), filters),
),
);
return { rows: keptRows, items: keptItems };
}

/** The view-naming query params of a request (view id, viewer, vf.*
* filter params), re-encoded — the params a fragment URL must carry
* so a refresh re-resolves the same view instead of dropping it. */
Expand Down
36 changes: 7 additions & 29 deletions pm-stack/aggregator/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from "./comments.ts";
import type { SavedView, ViewFilters, ViewItemLike } from "./views.ts";
import { ViewsStore, filtersFromQuery, itemMatchesView, ownerKey } from "./views.ts";
import { filterBoardForView, fragBoardUrl, ghItemLike, linkedLike } from "./board-view.ts";
import { filterBoardForView, filterRoadmapForView, fragBoardUrl, linkedLike } from "./board-view.ts";
import { EventsStore, emitEvent, pageEvents } from "./events.ts";
import { FreshnessStore, type FreshnessInput } from "./freshness.ts";
import { createFreshnessIpcHandler } from "./freshness-ipc.ts";
Expand Down Expand Up @@ -1869,25 +1869,11 @@ async function handler(req: Request): Promise<Response> {
);
}
const poolByRef = new Map(pool.map((p) => [p.ref, p]));
const keptRows = rows.filter((r) =>
linkedLike(
{ ref: r.subject, area: r.area, horizon: r.horizon, state: r.status },
r.links,
items,
poolByRef,
resolved.filters,
),
);
// Codex P2, PR #4013: items linked from a KEPT row keep their
// metadata (links/badges in roadmapCard) even when they fail the
// item filter themselves — same rule the board handler applies.
const keptRefs = new Set<string>();
for (const r of keptRows) for (const l of r.links) keptRefs.add(l.ref);
const keptItems = new Map(
[...items].filter(
([ref, it]) => keptRefs.has(ref) || itemMatchesView(ghItemLike(it, poolByRef.get(ref)), resolved.filters),
),
);
// Shared with /api/roadmap.json via filterRoadmapForView (#8723).
const { rows: keptRows, items: keptItems } = filterRoadmapForView(rows, items, poolByRef, resolved.filters);
return html(
renderRoadmap(
keptRows,
Expand Down Expand Up @@ -2283,18 +2269,10 @@ async function handler(req: Request): Promise<Response> {
}
const pool = await source.projects().catch(() => [] as ProjectPoolItem[]);
const poolByRef = new Map(pool.map((p) => [p.ref, p]));
const keptRows = rows.filter((r) =>
linkedLike(
{ ref: r.subject, area: r.area, horizon: r.horizon, state: r.status },
r.links,
items,
poolByRef,
resolved.filters,
),
);
const keptItems = [...items.values()].filter((it) =>
itemMatchesView(ghItemLike(it, poolByRef.get(it.ref)), resolved.filters),
);
// #8723: the same rule as the HTML twin -- items linked from a
// kept row ship even when they fail the view themselves.
const { rows: keptRows, items: keptItemMap } = filterRoadmapForView(rows, items, poolByRef, resolved.filters);
const keptItems = [...keptItemMap.values()];
const annotatedKeptRows = await Promise.all(
keptRows.map(async (row) => ({ ...row, annotations: await roadmapAnnotationsStore.forObject(row.subject) })),
);
Expand Down
104 changes: 104 additions & 0 deletions pm-stack/aggregator/test/roadmap-view-8723.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { describe, expect, test } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { filterRoadmapForView } from "../src/board-view.ts";
import type { GhItem, ProjectPoolItem, RefKey, RoadmapRow } from "../src/model.ts";

// #8723: PR #4013 gave the HTML /roadmap handler the keptRefs rule --
// items linked from a KEPT row keep their metadata even when they
// fail the view filter themselves -- but /api/roadmap.json filtered
// items by the view alone, so a kept row in the JSON payload could
// reference items absent from its own items[]. Oracle: the HTML
// handler's rule itself (#4013, Codex P2). Both handlers now compose
// through filterRoadmapForView, so these tests pin the shared
// composition plus the handler wiring, following the #8521 pattern.

const item = (ref: RefKey, author = "someone", state: GhItem["state"] = "open"): GhItem => ({
ref,
kind: "issue",
url: `https://github.com/${ref.replace("#", "/issues/")}`,
title: `item ${ref}`,
state,
labels: [],
author,
updatedAt: new Date().toISOString(),
});

const row = (subject: string, area: string, links: string[]): RoadmapRow => ({
id: subject.length,
subject,
title: `row ${subject}`,
area,
status: "active",
horizon: "Now",
source: "grist",
notes: "",
links: links.map((ref) => ({ ref: ref as RefKey, relation: "tracks" as const })),
});

const noPool = new Map<RefKey, ProjectPoolItem>();

describe("filterRoadmapForView (#8723, the /roadmap rule shared with /api/roadmap.json)", () => {
test("an item linked from a kept row ships even when it fails the view itself", () => {
const rows = [
row("grist:kept", "Fleet", ["developing-today/code#1", "dezren39/darwin-nix#2"]),
row("grist:dropped", "Other", ["developing-today/code#3"]),
];
const items = new Map(
[
["developing-today/code#1", item("developing-today/code#1", "alice")],
["dezren39/darwin-nix#2", item("dezren39/darwin-nix#2", "bob")],
["developing-today/code#3", item("developing-today/code#3", "bob")],
["developing-today/code#4", item("developing-today/code#4", "alice")],
] as [RefKey, GhItem][],
);
// View keeps only owner alice's items: row "kept" matches via its
// link to #1; #2 fails the view (bob) but is linked from the kept
// row, so it ships -- the parity the JSON twin was missing. #3 is
// linked only from the dropped row; #4 matches directly.
const out = filterRoadmapForView(rows, items, noPool, { owner: "alice" });
expect(out.rows.map((r) => r.subject)).toEqual(["grist:kept"]);
expect([...out.items.keys()].sort()).toEqual([
"developing-today/code#1",
"developing-today/code#4",
"dezren39/darwin-nix#2",
]);
});

test("a row kept by its own fields keeps all of its linked items", () => {
const rows = [row("grist:area", "Fleet", ["dezren39/darwin-nix#9"])];
const items = new Map([["dezren39/darwin-nix#9", item("dezren39/darwin-nix#9", "bob")]] as [RefKey, GhItem][]);
const out = filterRoadmapForView(rows, items, noPool, { area: "Fleet" });
expect(out.rows).toHaveLength(1);
expect([...out.items.keys()]).toEqual(["dezren39/darwin-nix#9"]);
});

test("an empty filter set keeps every row and item (the composition's identity case)", () => {
const rows = [row("grist:a", "Fleet", ["developing-today/code#1"]), row("grist:b", "Other", [])];
const items = new Map([["developing-today/code#1", item("developing-today/code#1")]] as [RefKey, GhItem][]);
const out = filterRoadmapForView(rows, items, noPool, {});
expect(out.rows).toHaveLength(2);
expect([...out.items.keys()]).toEqual(["developing-today/code#1"]);
});
});

describe("roadmap handler wiring (#8723 regression pin)", () => {
const server = readFileSync(join(import.meta.dir, "../src/server.ts"), "utf8");
const htmlBlock = server.slice(server.indexOf('if (path === "/roadmap")'), server.indexOf('if (path === "/api/flow-analytics.json")'));
const jsonBlock = server.slice(server.indexOf('if (path === "/api/roadmap.json")'), server.indexOf('if (path === "/api/quota.json")'));

test("the HTML handler filters through the shared composition", () => {
expect(htmlBlock).toContain("filterRoadmapForView");
});

test("the JSON twin filters through the same shared composition, so the twins cannot drift", () => {
expect(jsonBlock).toContain("filterRoadmapForView");
expect(jsonBlock).not.toContain("itemMatchesView(ghItemLike(it");
});

test("the unfiltered JSON branch stays untouched by view filtering", () => {
const unfiltered = jsonBlock.slice(0, jsonBlock.indexOf("const pool = await source.projects()"));
expect(unfiltered).toContain("items: [...items.values()]");
expect(unfiltered).not.toContain("filterRoadmapForView");
});
});
Loading