Public: anyone can read this; the crew can change it.

The home says in one line how much is flowing up and down, and opens /forks/flows #17146

opened this

Check · Review · Land · Live — Its check is queued. Next: reviewer, pushed since the last review.

Ready to land

scout/forge:scout/17057-the-home-says-in-one-line-how-much-is-fl → main 4 commits fork 3 threads · 3 unresolved

Review

Sign in to review this change.
5 files changed +144 −1
src/core/fork_flows.ts +4 −1
@@ -24,6 +24,8 @@ export interface FollowRow {
2424 * `synced`, `behind 3`, `ahead 2`, `diverged`, or `unknown` when nothing has counted the tips.
2525 */
2626 readonly standing: string;
27 /** Commits the parent has that this fork lacks, from the kept standing; 0 when unknown. */
28 readonly behindBy: number;
2729 readonly tookAt: string | null;
2830 }
2931
@@ -89,7 +91,8 @@ export function forkFlows(forge: { db: Database; reposRoot: string }, viewer: Us
8991 const parent = repo.forkParentId === null ? undefined : forkParentFor(db, repo, viewer);
9092 if (parent !== undefined) {
9193 const parentName = `${parent.ownerName}/${parent.name}`;
92 out.follows.push({ repo: name, parent: parentName, standing: standingWords(keptStanding(forge, repo, parent)), tookAt: lastTookAt(db, repo.id) });
94 const kept = keptStanding(forge, repo, parent);
95 out.follows.push({ repo: name, parent: parentName, standing: standingWords(kept), behindBy: kept?.[1] ?? 0, tookAt: lastTookAt(db, repo.id) });
9396 for (const o of sentOffers(db, repo, viewer))
9497 out.sent.push({ repo: name, parent: parentName, number: o.number, title: o.title, state: o.state, createdAt: o.createdAt, href: `/${parentName}/pulls/${o.number}` });
9598 }
src/core/repos.ts +17
@@ -1227,6 +1227,23 @@ export function listReposWritableTo(
12271227 return options.withoutCounts === true ? rows : withCounts(db, rows);
12281228 }
12291229
1230 /**
1231 * Whether any repository this viewer may write is a fork or has one: one statement, the guard
1232 * before the heavier fork-flow read, so a page for a viewer with no family costs one query.
1233 */
1234 export function hasForkFamily(db: Database, viewer: User | undefined): boolean {
1235 if (!viewer) return false;
1236 return (
1237 db
1238 .query<{ one: number }, [string]>(
1239 `SELECT 1 AS one FROM repo WHERE ${REPO_WRITABLE}
1240 AND (repo.fork_parent_id IS NOT NULL
1241 OR EXISTS (SELECT 1 FROM repo child WHERE child.fork_parent_id = repo.id)) LIMIT 1`,
1242 )
1243 .get(viewer.id) !== null
1244 );
1245 }
1246
12301247 /**
12311248 * The ids of every repository this viewer may write (`REPO_WRITABLE`, as `listReposWritableTo`),
12321249 * with no page and no limit: what "every repository you work on" spans for a count, where cutting
src/web/pages/home_flow.ts +2
@@ -15,6 +15,7 @@
1515 * version without being drawn. The page is a static picture with scripts off.
1616 */
1717
18 import { forkFlowsLine } from "../studio/fork_line.ts";
1819 import { firstWrong, firstWrongLine, readFacts } from "../first_wrong.ts";
1920 import { focusLine } from "../studio/held.ts";
2021 import { time } from "../time_element.ts";
@@ -583,6 +584,7 @@ export async function homeFlowPage(ctx: Ctx, known?: { readonly spawned: Awaited
583584 ${viewSwitch(view, only)}
584585 ${view === "lanes" ? filterForm(viewing?.repos ?? [], only, mine) : null}
585586 ${need}
587 ${forkFlowsLine(ctx, user)}
586588 ${need === null ? firstWrongLine(firstWrong(readFacts(ctx.app.db, user, { admin: wide, week: picture?.strip?.week, now }))) : null}
587589 </header>
588590 ${mine ? askedSection(ctx, user) : null}
src/web/studio/fork_line.ts added +35
@@ -0,0 +1,35 @@
1 /**
2 * The home's one line of the fork family (#17057): offers waiting upstream, offers my forks made
3 * to me, forks behind their parent, read from `forkFlows` (stored state only) and linking
4 * `/forks/flows`. A viewer with no family gets no line, so nothing on the page shifts.
5 */
6
7 import { hasForkFamily } from "../../core/repos.ts";
8 import { forkFlows, type ForkFlows } from "../../core/fork_flows.ts";
9 import type { Ctx } from "../../http/context.ts";
10 import type { User } from "../../core/users.ts";
11 import { html, type Html } from "../html.ts";
12
13 export interface ForkCounts {
14 readonly waiting: number;
15 readonly offered: number;
16 readonly behind: number;
17 }
18
19 /** The three counts, or null when the viewer follows no parent and has no offers either way. */
20 export function forkCounts(f: ForkFlows): ForkCounts | null {
21 if (f.follows.length + f.sent.length + f.offered.length === 0) return null;
22 return {
23 waiting: f.sent.filter((s) => s.state === "waiting").length,
24 offered: f.offered.length,
outdated

Counting 'behind' by startsWith on a display string ties the number to the wording in standingWords. A diverged or 'behind and ahead' standing would be miscounted or missed. Count from the kept standing's state, or export a predicate from fork_flows.ts so the wording can change without breaking this. Principle 8 (plain words) also wants 'forks behind' to mean the same thing as on the /forks/flows page.

25 behind: f.follows.filter((r) => r.behindBy > 0).length,
26 };
27 }
28
29 export function forkFlowsLine(ctx: Ctx, user: User): Html | null {
30 if (!hasForkFamily(ctx.app.db, user)) return null;
31 const c = forkCounts(forkFlows(ctx.app, user));
outdated

Box 1 says 'when the viewer follows a parent or has forks'. A fork with no offers sent and no followed parent gets no line (the builder confirms this). State that limit in the pull, or cover it.

32 if (c === null) return null;
33 const words = `${String(c.waiting)} ${c.waiting === 1 ? "offer" : "offers"} waiting upstream · ${String(c.offered)} offered to you · ${String(c.behind)} ${c.behind === 1 ? "fork" : "forks"} behind`;
34 return html`<p class="small muted" id="hf-forks"><a href="/forks/flows">${words}</a></p>`;
35 }
test/home-fork-flows.test.ts added +86
@@ -0,0 +1,86 @@
1 /**
2 * The home's one line of fork flows (#17057): three counts linking /forks/flows; no family, no
3 * line.
4 */
5 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
10 import { openPull } from "../src/actions/pulls.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import { createSession } from "../src/core/sessions.ts";
13 import { createUser, type User } from "../src/core/users.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { forkRelation } from "../src/core/following.ts";
16 import { forkCounts } from "../src/web/studio/fork_line.ts";
17 import { promote } from "./helpers/members.ts";
18
19 describe("home fork flows line", () => {
20 let dir: string;
21 let app: App;
22 let servers: Servers;
23 const users: Record<string, User> = {};
24 const get = (who: string) => fetch(`${servers.url}/`, { headers: { cookie: `forge_session=${createSession(app.db, users[who]!.id).token}` } }).then((r) => r.text());
25
26 beforeAll(async () => {
27 dir = await mkdtemp(join(tmpdir(), "forge-home-forks-"));
28 app = createApp({ dataDir: dir, port: 0 });
29 servers = await serve(app);
30 for (const [name, first] of [["zed", true], ["alice", false], ["bob", false], ["loner", false], ["dana", false]] as const) {
31 const made = await createUser(app.db, { origin: "seed", trusted: true, username: name, email: `${name}@forge.local`, password: "a-good-passphrase", adminIfFirst: first });
32 if (!made.ok) throw new Error(made.error);
33 users[name] = made.value;
34 }
35 promote(app.db, users["alice"]!, users["bob"]!, users["loner"]!, users["dana"]!);
36 const lib = await createRepo(app, users["alice"]!, { name: "lib", visibility: "public" });
37 if (!lib.ok) throw new Error(String(lib.error));
38 await commitToBranch(app, users["alice"]!, lib.value, { message: "Start", files: [{ path: "README.md", content: "# lib\n" }] });
39 const fork = await forkRepo(app, users["bob"]!, lib.value, undefined, { visibility: "public" });
40 if (!fork.ok) throw new Error(String(fork.error));
41 await commitToBranch(app, users["bob"]!, fork.value, { branch: "feature", message: "From bob", files: [{ path: "f.txt", content: "x\n" }] });
42 const pull = await openPull(app, users["bob"]!, lib.value, { title: "From bob", baseBranch: "main", headBranch: "feature", headRepo: fork.value });
43 if (!pull.ok) throw new Error(JSON.stringify(pull.error));
44 });
45
46 afterAll(async () => {
47 servers.stop();
48 app.close();
49 await rm(dir, { recursive: true, force: true });
50 });
51
52 test("a fork that offered upstream sees its counts, linked", async () => {
53 const html = await get("bob");
54 expect(html).toContain('<a href="/forks/flows">1 offer waiting upstream · 0 offered to you · 0 forks behind</a>');
55 });
56
57 test("the parent's owner sees what was offered to them", async () => {
58 const html = await get("alice");
59 expect(html).toContain("0 offers waiting upstream · 1 offered to you · 0 forks behind");
60 });
61
62 test("a fork behind its parent counts as one fork behind, with no offers made", async () => {
63 const parent = await createRepo(app, users["alice"]!, { name: "wide", visibility: "public" });
64 if (!parent.ok) throw new Error(String(parent.error));
65 await commitToBranch(app, users["alice"]!, parent.value, { message: "Start", files: [{ path: "a.txt", content: "a\n" }] });
66 const fork = await forkRepo(app, users["dana"]!, parent.value, undefined, { visibility: "public" });
67 if (!fork.ok) throw new Error(String(fork.error));
68 await commitToBranch(app, users["dana"]!, fork.value, { message: "Mine", files: [{ path: "mine.txt", content: "m\n" }] });
69 await commitToBranch(app, users["alice"]!, parent.value, { message: "Theirs", files: [{ path: "b.txt", content: "b\n" }] });
70 expect((await forkRelation(app, fork.value, parent.value))?.state).toBe("diverged");
outdated

'behind' is only tested on a hand-built object. Add a page-level case with a followed parent that is behind, so the K count in the line is shown to work and would fail without the code.

71 const html = await get("dana");
72 expect(html).toContain('<a href="/forks/flows">0 offers waiting upstream · 0 offered to you · 1 fork behind</a>');
73 });
74
75 test("no family, no line", async () => {
76 const html = await get("loner");
77 expect(html).not.toContain("hf-forks");
78 expect(html).not.toContain("/forks/flows");
79 });
80
81 test("counts: none when all three are empty, behind counted from the standing", () => {
82 expect(forkCounts({ follows: [], sent: [], offered: [] })).toBeNull();
83 const f = { repo: "a/b", parent: "c/d", tookAt: null };
84 expect(forkCounts({ follows: [{ ...f, standing: "behind 3", behindBy: 3 }, { ...f, standing: "synced", behindBy: 0 }], sent: [], offered: [] })).toEqual({ waiting: 0, offered: 0, behind: 1 });
85 });
86 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs