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

The feed is the git primitives: one journey test walks a tell, a reply, a like and a share and finds each as its git object #12481

Merged opened this

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

Merged

scout/forge:scout/12474-the-feed-is-the-git-primitives-one-journ → main 3 commits fork 2 threads · 2 unresolved

  • Merged. scout/12474-the-feed-is-the-git-primitives-one-journ was merged into main as 77532cd.

Review

Sign in to review this change.
2 files changed +277 −0
docs/features/feed-like-ledger.md +2
@@ -20,3 +20,5 @@ A like is a row of `feed_signal` (who, item, act, when), the same for a person a
2020 ## Left out
2121
2222 Pruning old ledger rows, and hiding likers from other readers.
23
24 `test/feed-journey.test.ts` walks a tell, a reply, a like and a share to their git objects, and checks that a private repository's posts reach an outsider through no door (JSON timeline, `readFeed`, the `/feed` page, the reply door, like and ledger, the terminal's data path, MCP `feed`). Run it with `bun test test/feed-journey.test.ts`.

This paragraph sits under "Left out" but describes what is covered. Move it under its own heading (e.g. "Tested by").

test/feed-journey.test.ts added +275
@@ -0,0 +1,275 @@
1 /**
2 * The feed is the git primitives (docs/VISION.md, order 1): one journey walks
3 * a tell, a reply, a like and a share and finds each as its object — an issue,
4 * a comment on it, a row in the decision ledger, a boosted item for a follower —
5 * and a run or a pull as a post one step from its page. A private repository's
6 * posts reach an outsider through none of the doors.
7 */
8 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
9 import { mkdtemp, rm } from "node:fs/promises";
10 import { tmpdir } from "node:os";
11 import { join } from "node:path";
12 import { createApp, type App } from "../src/app.ts";
13 import { createRepo } from "../src/actions/repos.ts";
14 import { getIssue, listComments } from "../src/core/issues.ts";
15 import { pin } from "../src/core/pins.ts";
16 import type { Repo } from "../src/core/repos.ts";
17 import { createToken } from "../src/core/tokens.ts";
18 import { createUser, type User } from "../src/core/users.ts";
19 import { readFeed } from "../src/feed/feed.ts";
20 import { readInteraction } from "../src/feed/interact.ts";
21 import { likeLedger } from "../src/feed/likes.ts";
22 import { replyTarget } from "../src/feed/reply.ts";
23 import { readTimeline } from "../src/feed/timeline.ts";
24 import { tool as feedTool } from "../src/http/mcp/tools/feed.ts";
25 import { share } from "../src/feed/share.ts";
26 import { serve, type Servers } from "../src/http/server.ts";
27 import { quietLogs } from "./helpers/globals.ts";
28 import { promote } from "./helpers/members.ts";
29
30 quietLogs();
31
32 interface Post {
33 id: string;
34 kind: string;
35 app: string;
36 link: string;
37 what: string;
38 reason?: string;
39 likes?: number;
40 liked?: boolean;
41 }
42
43 describe("the feed journey", () => {
44 let dataDir: string;
45 let app: App;
46 let servers: Servers;
47 let alice: User;
48 let bob: User;
49 let carol: User;
50 let tok: Record<string, string> = {};
51 let open: Repo;
52 let secret: Repo;
53 let issueNumber = 0;
54 let postId = "";
55 let crew: User;
56
57 const api = (who: string, path: string, init: RequestInit = {}) =>
58 fetch(`${servers.url}${path}`, {
59 ...init,
60 headers: { authorization: `Bearer ${tok[who]}`, "content-type": "application/json" },
61 });
62 const timeline = async (who: string): Promise<Post[]> =>
63 ((await (await api(who, "/api/v1/feed?limit=50")).json()) as { items: Post[] }).items;
64 const ledger = (item: string) =>
65 app.db.query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM feed_signal WHERE item_id = ?1").get(item)!.n;
66
67 beforeAll(async () => {
68 dataDir = await mkdtemp(join(tmpdir(), "forge-feed-journey-"));
69 app = createApp({ dataDir, port: 0 });
70 servers = await serve(app);
71 const mk = async (username: string, first = false) => {
72 const r = await createUser(app.db, {
73 origin: "seed",
74 trusted: true,
75 username,
76 allowReserved: true,
77 email: `${username}@forge.local`,
78 password: "a-good-passphrase",
79 adminIfFirst: first,
80 });
81 if (!r.ok) throw new Error(r.error);
82 promote(app.db, r.value);
83 return r.value;
84 };
85 await mk("root", true);
86 alice = await mk("alice");
87 bob = await mk("bob");
88 carol = await mk("carol");
89 // A crew member is an agent: stored as a user row with is_agent set.
90 app.db.run(
91 "INSERT INTO user (id, username, email, password_hash, is_agent) VALUES ('agent-scout', 'scout', '[email protected]', '', 1)",
92 );
93 crew = { ...alice, id: "agent-scout", username: "scout" };
94 for (const u of [alice, bob, carol]) tok[u.username] = createToken(app.db, u.id, "t", "rw").token;
95 const o = await createRepo(app, alice, { name: "open", visibility: "public" });
96 const s = await createRepo(app, alice, { name: "secret", visibility: "private" });
97 if (!o.ok || !s.ok) throw new Error("repos");
98 open = o.value;
99 secret = s.value;
100 }, 30_000);
101
102 afterAll(async () => {
103 servers.stop();
104 app.close();
105 await rm(dataDir, { recursive: true, force: true });
106 });
107
108 test("a tell is a post in the timeline and an issue on its repository", async () => {
109 const res = await api("alice", "/api/v1/feed/tells", {
110 method: "POST",
111 body: JSON.stringify({ app: "alice/open", text: "Add a dark mode" }),
112 });
113 expect(res.status).toBe(201);
114 issueNumber = ((await res.json()) as { issue: number }).issue;
115 expect(issueNumber).toBeGreaterThan(0);
116 expect(getIssue(app.db, open.id, issueNumber)?.title).toBe("Add a dark mode");
117 const post = (await timeline("alice")).find((p) => p.kind === "tell" && p.app === "alice/open");
118 expect(post).toBeDefined();
119 expect(post!.link).toBe(`/alice/open/issues/${issueNumber}`);
120 expect(post!.what).toContain("Add a dark mode");
121 postId = post!.id;
122 expect(postId).not.toBe("");
123 });
124
125 test("a reply is a comment on that issue", async () => {
126 expect(issueNumber).toBeGreaterThan(0);
127 const issue = getIssue(app.db, open.id, issueNumber)!;
128 const res = await fetch(`${servers.url}/alice/open/issues/${issueNumber}/comments`, {
129 method: "POST",
130 redirect: "manual",
131 headers: {
132 authorization: `Bearer ${tok["bob"]}`,
133 origin: servers.url,
134 "content-type": "application/x-www-form-urlencoded",
135 },
136 body: new URLSearchParams({ body: "Yes please.", back: "feed" }),
137 });
138 expect([200, 303]).toContain(res.status);
139 expect(listComments(app.db, issue.id).map((c) => c.body)).toEqual(["Yes please."]);
140 });
141
142 test("a like is one ledger row; a second like writes nothing, an unlike is a new row", async () => {
143 const like = (kind: string) =>
144 api("bob", "/api/v1/feed/interactions", { method: "POST", body: JSON.stringify({ item: postId, kind }) });
145 expect(postId).not.toBe("");
146 expect(ledger(postId)).toBe(0);
147 expect(await (await like("liked")).json()).toEqual({ likes: 1, liked: true });
148 expect(ledger(postId)).toBe(1);
149 expect(await (await like("liked")).json()).toEqual({ likes: 1, liked: true });
150 expect(ledger(postId)).toBe(1);
151 const seen = readFeed(app.db, bob).items.find((i) => i.id === postId)!;
152 expect(seen.likes).toBe(1);
153 expect(seen.liked).toBe(true);
154 expect(await (await like("unliked")).json()).toEqual({ likes: 0, liked: false });
155 expect(ledger(postId)).toBe(2);
156 await like("liked");
157 });
158
159 test("a share of a public post appears for a follower, and only once", async () => {
160 expect(postId).not.toBe("");
161 pin(app.db, carol.id, open.id);
162 const r = await share(app, alice, postId, ["carol"]);
163 expect(r.ok && r.value.sent).toEqual([{ to: "carol", outcome: "delivered" }]);
164 const hit = readFeed(app.db, carol).items.find((i) => i.reason?.startsWith("shared by alice: "));
165 expect(hit?.link).toBe(`/alice/open/issues/${issueNumber}`);
166 const again = await share(app, alice, postId, ["carol"]);
167 expect(again.ok && again.value.sent[0]?.outcome).toBe("duplicate");
168 });
169
170 test("a crew member's run and pull are posts that link one step deeper", async () => {
171 // The actor is a real agent row, as the crew's runs are.
172 app.bus.append({
173 kind: "run.finished",
174 repoId: open.id,
175 actorId: crew.id,
176 payload: { kind: "deploy", state: "ok", summary: "up", runId: "r-journey" },
177 });
178 app.bus.append({
179 kind: "pull.opened",
180 repoId: open.id,
181 actorId: crew.id,
182 payload: { number: 7 },
183 });
184 const items = await timeline("bob");
185 expect(items.find((p) => p.kind === "run")?.link).toBe("/alice/open/runs/r-journey");
186 expect(items.find((p) => p.kind === "pull")?.link).toBe("/alice/open/pulls/7");
outdated

Add the terminal, web page and reply doors here; "every door" currently means the JSON API plus readFeed.

187 });
188
189 test("a private repository's posts are absent for an outsider on every door", async () => {
190 const t = await api("alice", "/api/v1/feed/tells", {
191 method: "POST",
192 body: JSON.stringify({ app: "alice/secret", text: "Rotate the keys" }),
193 });
194 expect(t.status).toBe(201);
195 const secretIssue = ((await t.json()) as { issue: number }).issue;
196 app.bus.append({ kind: "pull.opened", repoId: secret.id, actorId: crew.id, payload: { number: 9 } });
197 app.bus.append({
198 kind: "run.finished",
199 repoId: secret.id,
200 actorId: alice.id,
201 payload: { kind: "deploy", state: "ok", summary: "boom", runId: "r-secret" },
202 });
203 const mine = await timeline("alice");
204 const hidden = mine.filter((p) => p.app === "alice/secret");
205 expect(hidden.length).toBeGreaterThan(0);
206 expect(hidden.some((p) => p.kind === "tell")).toBe(true);
207 expect(hidden.some((p) => p.kind === "run")).toBe(true);
208 expect(hidden.some((p) => p.kind === "pull")).toBe(true);
209
210 // Timeline and ranked feed.
211 expect((await timeline("bob")).some((p) => p.app === "alice/secret")).toBe(false);
212 expect(readFeed(app.db, bob).items.some((i) => i.app === "alice/secret")).toBe(false);
213 // Like, ledger read and tell: refused as though absent.
214 for (const p of hidden) {
215 const liked = await api("bob", "/api/v1/feed/interactions", {
216 method: "POST",
217 body: JSON.stringify({ item: p.id, kind: "liked" }),
218 });
219 expect(liked.status).toBe(404);
220 expect(ledger(p.id)).toBe(0);
221 const read = await api("bob", `/api/v1/feed/likes?item=${encodeURIComponent(p.id)}`);
222 expect(read.status).toBe(404);
223 const shared = await share(app, bob, p.id, ["carol"]);
224 expect(shared.ok).toBe(false);
225 }
226 const told = await api("bob", "/api/v1/feed/tells", {
227 method: "POST",
228 body: JSON.stringify({ app: "alice/secret", text: "let me in" }),
229 });
230 expect(told.status).toBe(404);
231 // The web feed page, rendered for the outsider, names nothing of it.
232 const page = await fetch(`${servers.url}/feed`, { headers: { authorization: `Bearer ${tok["bob"]}` } });
233 expect(page.status).toBe(200);
234 const html = await page.text();
235 expect(html).not.toContain("alice/secret");
236 expect(html).not.toContain("Rotate the keys");
237 expect(html).toContain("alice/open"); // the page did render the public posts
238 // The reply door: a comment on the private tell's issue is refused and writes nothing.
239 const reply = await fetch(`${servers.url}/alice/secret/issues/${secretIssue}/comments`, {
240 method: "POST",
241 redirect: "manual",
242 headers: {
243 authorization: `Bearer ${tok["bob"]}`,
244 origin: servers.url,
245 "content-type": "application/x-www-form-urlencoded",
246 },
247 body: new URLSearchParams({ body: "let me comment", back: "feed" }),
248 });
249 expect(reply.status).toBe(404);
250 expect(listComments(app.db, getIssue(app.db, secret.id, secretIssue)!.id)).toEqual([]);
251 // The terminal's data path: its timeline, reply target, like and ledger.
252 expect(readTimeline(app.db, bob, { limit: 50 }).items.some((i) => i.app === "alice/secret")).toBe(false);
253 for (const p of hidden) {
254 expect(replyTarget(app.db, bob, p)).toBeUndefined();
255 expect(readInteraction(app.db, bob, { item: p.id, kind: "liked", app: p.app }, ["liked"]).ok).toBe(false);
256 expect(likeLedger(app.db, p.id).rows.length).toBe(0);
257 }
258 expect(replyTarget(app.db, alice, hidden.find((p) => p.kind === "tell")!)).toBeDefined();
259 // MCP `feed`: both the ranked and the log read.
260 const mcpCtx = { app } as unknown as Parameters<typeof feedTool.run>[0];
261 for (const args of [{ limit: 50 }, { limit: 50, before: "now" }]) {
262 const got = JSON.stringify(await feedTool.run(mcpCtx, bob, args));
263 expect(got).not.toContain("alice/secret");
264 expect(got).not.toContain("Rotate the keys");
265 }
266 // A follower who is not the owner (carol) gets nothing from any timeline read.
267 expect(readTimeline(app.db, carol, { limit: 50 }).items.some((i) => i.app === "alice/secret")).toBe(false);
268 expect((await timeline("carol")).some((p) => p.app === "alice/secret")).toBe(false);
269 // A share by the owner never widens: a reader without access gets nothing.
270 const owned = hidden[0]!;
271 const out = await share(app, alice, owned.id, ["carol"]);
272 expect(out.ok && out.value.sent).toEqual([{ to: "carol", outcome: "unreachable" }]);
273 expect(readFeed(app.db, carol).items.some((i) => i.app === "alice/secret")).toBe(false);
274 });
275 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs