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

Every kind of post in the feed has a depth link one step down, proven by a fixture of each kind #15760

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15743-every-kind-of-post-in-the-feed-has-a-dep → main 0 commits fork 3 threads · 3 unresolved

  • Merged. scout/15743-every-kind-of-post-in-the-feed-has-a-dep was merged into main as f9c3622.

Review

Sign in to review this change.
2 files changed +167 −1
src/feed/depth.ts +4 −1
@@ -1,5 +1,5 @@
11 /**
2 * The one step deeper from a post: its run log, pull diff or issue thread, as plain
2 * The one step deeper from a post: its run log, pull diff, issue thread or shipped commit, as plain
33 * data (a label and an address). A post names a depth only when its link is one;
44 * the web card renders it as a real anchor, no script, and the terminal feed
55 * prints it as a line (`depthLine`). There is no issue post kind: an issue shows as a
@@ -10,6 +10,7 @@ export type Depth = { label: string; href: string };
1010
1111 const RUN = /^(\/[^/]+\/[^/]+\/runs\/[A-Za-z0-9_-]+)(?:\?all=1#L\d+)?$/;
1212 const PULL = /^(\/[^/]+\/[^/]+\/pulls\/\d{1,9})(?:#[\w-]+)?$/;
13 const COMMIT = /^(\/[^/]+\/[^/]+\/commit\/[0-9a-f]{40})$/;
1314 const ISSUE = /^(\/[^/]+\/[^/]+\/issues\/\d{1,9})(?:\?all=1)?(?:#[\w-]+)?$/;
1415
1516 export function depthOf(item: { kind: string; app: string; link: string }): Depth | undefined {
@@ -22,6 +23,8 @@ export function depthOf(item: { kind: string; app: string; link: string }): Dept
2223 const issue = ISSUE.exec(item.link);
2324 if ((item.kind === "tell" || item.kind === "reply") && issue !== null && inApp(issue[1]!))
2425 return { label: "Thread", href: issue[1]! };
26 const commit = COMMIT.exec(item.link);
27 if (item.kind === "release" && commit !== null && inApp(commit[1]!)) return { label: "Commit", href: commit[1]! };
2528 return undefined;
2629 }
2730
test/feed-depth-kinds.test.ts added +163
@@ -0,0 +1,163 @@
1 /**
2 * Every kind of post the feed renders names its depth: one fixture per kind, and a kind with
3 * no depth link fails by name. Kinds that have no deeper page yet are listed, not invented.
4 */
5 import { afterAll, beforeAll, 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 { addComment, openIssue } from "../src/actions/issues.ts";
10 import { openPull } from "../src/actions/pulls.ts";
11 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
12 import { createApp, type App } from "../src/app.ts";
13 import { createRun } from "../src/core/runs.ts";
14 import { createSession } from "../src/core/sessions.ts";
15 import { createUser, type User } from "../src/core/users.ts";
16 import { depthOf } from "../src/feed/depth.ts";
17 import { readFeed } from "../src/feed/feed.ts";
18 import type { FeedKind } from "../src/feed/items.ts";
19 import type { FeedItem } from "../src/feed/rank.ts";
20 import { share } from "../src/feed/share.ts";
21 import { forgeHttp, type ForgeHttp } from "../src/http/server.ts";
22 import { quietLogs } from "./helpers/globals.ts";
23 import { promote } from "./helpers/members.ts";
24
25 quietLogs();
26
27 let dataDir: string;
28 let app: App;
29 let http: ForgeHttp;
30 let alice: User;
31 let bob: User;
32 /** One feed item per kind, as the feed's own builder made it from real events. */
33 const items = new Map<string, FeedItem>();
34 let privateCommit = "";
35
36 const itemOf = (feed: readonly FeedItem[], kind: string, what: RegExp): FeedItem => {
37 const hit = feed.find((i) => i.kind === kind && what.test(`${i.what} ${i.link}`));
38 if (hit === undefined) throw new Error(`the feed built no ${kind} post matching ${what}: ${JSON.stringify(feed.map((i) => [i.kind, i.link]))}`);
39 return hit;
40 };
41
42 beforeAll(async () => {
43 dataDir = await mkdtemp(join(tmpdir(), "forge-depth-kinds-"));
44 app = createApp({ dataDir, port: 0, host: "forge.test" });
45 http = forgeHttp(app);
46 const first = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
47 const second = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" });
48 if (!first?.ok || !second?.ok) throw new Error("users");
49 alice = first.value;
50 bob = second.value;
51 promote(app.db, bob);
52 const made = await createRepo(app, alice, { name: "lib", visibility: "public" });
53 if (!made.ok) throw new Error(String(made.error));
54 const repo = made.value;
55 const main = await commitToBranch(app, alice, repo, { message: "one", files: [{ path: "a.md", content: "a\n" }] });
56 if (!main.ok) throw new Error(JSON.stringify(main.error));
57 const side = await commitToBranch(app, alice, repo, { branch: "feat", base: "main", message: "feat", files: [{ path: "f.md", content: "f\n" }] });
outdated

Links are typed by hand here. Produce these items with the feed's builder from real events so the test fails if the feed's link shape drifts.

58 if (!side.ok) throw new Error(JSON.stringify(side.error));
59 const pull = await openPull(app, alice, repo, { title: "Feat", baseBranch: "main", headBranch: "feat" });
60 if (!pull.ok) throw new Error(String(pull.error));
61 const issue = openIssue(app, alice, repo, { title: "Something to do" });
62 if (!issue.ok) throw new Error(String(issue.error));
63 const other = openIssue(app, alice, repo, { title: "Another" });
64 if (!other.ok) throw new Error(String(other.error));
65 const comment = addComment(app, alice, repo, issue.value, "a word");
66 if (!comment.ok) throw new Error(String(comment.error));
67 const run = createRun(app.db, { repoId: repo.id, kind: "check" });
68 const emit = (kind: string, payload: Record<string, unknown>) =>
69 app.bus.append({ kind, repoId: repo.id, actorId: alice.id, payload });
70 emit("run.finished", { kind: "check", state: "ok", summary: "green", runId: run.id });
71 emit("run.finished", { kind: "agent", state: "ok", summary: "big", runId: run.id, costUsd: 3 });
72 emit("look.taken", { env: `pull-${pull.value.number}`, commitOid: main.value.commitOid, number: pull.value.number });
73 emit("app.serving", { version: "1.0.0", commitOid: main.value.commitOid, pulls: [{ title: "Feat" }] });
outdated

Circular: it checks that ALL_KINDS contains keys that are already the exhaustive Record. Drop the loop and keep the typed Record as the guard.

74 emit("issue.blocked", { number: issue.value.number, blockedBy: other.value.number });
75 emit("wallet.low", { capUsd: 10, left: 0.5 });
76
77 const feed = readFeed(app.db, alice, { limit: 100 }).items;
78 items.set("tell", itemOf(feed, "tell", /\/issues\/\d+$/));
79 items.set("reply", itemOf(feed, "reply", /\/issues\/\d+#/));
80 items.set("run", itemOf(feed, "run", /\/runs\//));
81 items.set("pull", itemOf(feed, "pull", /\/pulls\/\d+$/));
82 items.set("look", itemOf(feed, "look", /\/pulls\/\d+$/));
83 items.set("release", itemOf(feed, "release", /\/commit\/[0-9a-f]{40}$/));
84 items.set("cost", itemOf(feed, "cost", /costs?/i));
85 items.set("map", itemOf(feed, "map", /\/map$/));
86 items.set("map-wallet", itemOf(feed, "map", /\/wallet$/));
87
88 // A share: bob is sent alice's tell and reads it in his own feed.
89 const sent = await share(app, alice, items.get("tell")!.id, ["bob"]);
outdated

Feeding every no-depth kind a pull link proves nothing about their own links. Use each kind's real item link.

90 if (!sent.ok) throw new Error(sent.error);
91 items.set("share", itemOf(readFeed(app.db, bob, { limit: 100 }).items, tellKindOfShare, /issues/));
92
93 // A private repository's shipped commit, for the viewer who may not read it.
94 const vault = await createRepo(app, alice, { name: "vault", visibility: "private" });
95 if (!vault.ok) throw new Error(String(vault.error));
96 const sealed = await commitToBranch(app, alice, vault.value, { message: "secret", files: [{ path: "s.md", content: "s\n" }] });
97 if (!sealed.ok) throw new Error(JSON.stringify(sealed.error));
98 privateCommit = sealed.value.commitOid;
99 }, 60_000);
100 afterAll(async () => {
101 app.close();
102 await rm(dataDir, { recursive: true, force: true });
103 });
104
105 const tellKindOfShare = "tell";
106 const get = (path: string, cookie?: string) =>
107 http.handle(new Request(`http://forge.test${path}`, cookie === undefined ? {} : { headers: { cookie } }));
108
109 /** The typed record is the guard: a FeedKind added without a line here fails the type check. */
110 const DEPTH: Record<FeedKind, string | null> = {
111 tell: "Thread",
112 reply: "Thread",
113 run: "Run log",
114 pull: "Diff",
115 look: "Diff",
116 release: "Commit", // a ship
117 // No deeper page yet: they point at the app, its map, wallet, settings or a milestone.
118 ask: null,
119 decision: null,
120 map: null,
121 cost: null,
122 milestone: null,
123 choose: null,
124 };
125
126 for (const [name, label] of [["tell", "Thread"], ["reply", "Thread"], ["run", "Run log"], ["pull", "Diff"], ["look", "Diff"], ["release", "Commit"]] as const) {
127 test(`a ${name} post the feed builds links a depth page that answers 200`, async () => {
128 const item = items.get(name)!;
129 expect(DEPTH[item.kind], `kind "${item.kind}"`).toBe(label);
130 const d = depthOf(item);
131 expect(d, `kind "${name}" has no depth link (item link ${item.link})`).toBeDefined();
132 expect(d!.label).toBe(label);
133 const res = await get(d!.href);
134 expect(res.status, `${name}: ${d!.href}`).toBe(200);
135 });
136 }
137
138 test("a shared post keeps its original's kind and link, so it has the same depth", async () => {
139 const share = items.get("share")!;
140 expect(share.id).toStartWith("share:");
141 expect(depthOf(share)).toEqual(depthOf(items.get("tell")!));
142 expect((await get(depthOf(share)!.href)).status).toBe(200);
143 });
144
145 test("kinds with no depth page yet get none from their real links", () => {
146 for (const [name, item] of [["cost", items.get("cost")!], ["map", items.get("map")!], ["map", items.get("map-wallet")!]] as const) {
147 expect(DEPTH[item.kind], name).toBeNull();
148 expect(depthOf(item), `${name}: ${item.link}`).toBeUndefined();
149 }
150 });
151
152 test("a guest gets 404 for a private repository's commit, so a ship there shows no link to them", async () => {
153 expect((await get(`/alice/vault/commit/${privateCommit}`)).status).toBe(404);
154 const mine = createSession(app.db, alice.id).token;
155 expect((await get(`/alice/vault/commit/${privateCommit}`, `forge_session=${mine}`)).status).toBe(200);
156 const bobs = createSession(app.db, bob.id).token;
157 expect((await get(`/alice/vault/commit/${privateCommit}`, `forge_session=${bobs}`)).status).toBe(404);
158 });
159
160 test("a ship whose link is only the app has no depth, a shipped commit does", () => {
161 expect(depthOf({ kind: "release", app: "alice/lib", link: "/alice/lib" })).toBeUndefined();
162 expect(depthOf({ kind: "release", app: "alice/lib", link: items.get("release")!.link })?.label).toBe("Commit");
163 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs