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

The Friend test: someone sent an app opens its link and makes it theirs in one action, over the page and the MCP, in one journey test #12473

Merged opened this

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

Merged

scout/forge:scout/12465-the-friend-test-someone-sent-an-app-open → main 4 commits fork 2 threads · 2 unresolved

  • Merged. scout/12465-the-friend-test-someone-sent-an-app-open was merged into main as 407cf76.

Review

Sign in to review this change.
2 files changed +205 −0
src/web/studio/make_it_yours.ts +4
@@ -221,6 +221,10 @@ export async function makeItYoursReceipt(ctx: Ctx): Promise<Response> {
221221 title: p.existing && !justMade ? `${p.repo} was already yours` : `${p.repo} is yours`,
222222 body: html`<div class="narrow-wide">
223223 ${pageHead(justMade ? receiptWords({ ...p, existing: false }) : receiptWords(p))}
224 <ul class="list setting-list">
225 ${row({ title: "Copied", meta: html`${p.copied.join(", ")}` })}
226 ${row({ title: "Not copied", meta: html`${p.notCopied.join(", ")}` })}
227 </ul>
224228 <div class="form-actions">
225229 ${btn("Open mine", { href: `/${p.repo}`, variant: "primary", icon: "fork" })}
226230 ${when(
test/friend-journey.test.ts added +201
@@ -0,0 +1,201 @@
1 /**
2 * The Friend test (vision): someone sent an app opens its link and makes it
3 * theirs in one action. One journey over the page and the MCP: a signed-out
4 * visitor reads "Make it yours" and where it leads, signs up, makes it theirs
5 * with one POST and finds a private fork that follows the original; the
6 * receipt says what was copied and what was not. The MCP says the same words,
7 * a dry run changes nothing, a private origin is 404 on both doors, and the
8 * daily copy limit answers with its reason.
9 */
10 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
11 import { existsSync } from "node:fs";
12 import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
13 import { tmpdir } from "node:os";
14 import { join } from "node:path";
15 import { createApp, type App } from "../src/app.ts";
16 import { newApp } from "../src/actions/apps.ts";
17 import { COPIED, COPIES_PER_DAY, NOT_COPIED, receiptWords } from "../src/actions/make_it_yours.ts";
18 import { envDataDir } from "../src/apps/data.ts";
19 import { getAppByRepo, listApps } from "../src/core/apps.ts";
20 import { getRepo } from "../src/core/repos.ts";
21 import { createSession } from "../src/core/sessions.ts";
22 import { createToken } from "../src/core/tokens.ts";
23 import { createUser, getUserByName } from "../src/core/users.ts";
24 import { setLimit } from "../src/settings/limits.ts";
25 import { serve, type Servers } from "../src/http/server.ts";
26 import { quietLogs } from "./helpers/globals.ts";
27 import { mcpTool } from "./helpers/mcp.ts";
28 import { promote } from "./helpers/members.ts";
29 import { proveAll } from "./helpers/verified.ts";
30
31 quietLogs();
32
33 describe("the Friend test", () => {
34 let dataDir: string;
35 let app: App;
36 let servers: Servers;
37
38 /** Signs up over the API; a new account is a guest until the owner admits them (`member`). */
39 const signUp = async (username: string, member = true) => {
40 const res = await fetch(`${servers.url}/api/v1/users`, {
41 method: "POST",
42 headers: { "content-type": "application/json" },
43 body: JSON.stringify({ username, password: "a-long-passphrase" }),
44 });
45 expect(res.status).toBe(201);
46 proveAll(app); // the email link is its own suite (verify-email)
47 const user = getUserByName(app.db, username)!;
48 if (member) promote(app.db, user);
49 return {
50 user,
51 cookie: `forge_session=${createSession(app.db, user.id).token}`,
52 token: createToken(app.db, user.id, "rw", "rw").token,
53 };
54 };
55
56 beforeAll(async () => {
57 dataDir = await mkdtemp(join(tmpdir(), "forge-friend-"));
58 app = createApp({ dataDir, port: 0, mailer: async () => true });
59 servers = await serve(app);
60 setLimit(app.db, "signup.per_source", 100, "system");
61 const alice = await createUser(app.db, {
62 origin: "seed",
63 trusted: true,
64 username: "alice",
65 email: "[email protected]",
66 password: "a-good-passphrase",
67 adminIfFirst: true,
68 });
69 if (!alice.ok) throw new Error(alice.error);
70 for (const [name, visibility] of [["garden", "public"], ["vault", "private"]] as const) {
71 const made = await newApp(app, alice.value, { name, description: "A garden planner", visibility });
72 if (!made.ok) throw new Error(String(made.error));
73 }
74 const gardenId = getAppByRepo(app.db, getRepo(app.db, "alice", "garden")!.id)!.id;
75 await mkdir(envDataDir(dataDir, gardenId, "production"), { recursive: true });
76 await writeFile(join(envDataDir(dataDir, gardenId, "production"), "plants.db"), "tomatoes");
77 app.db.query(`INSERT INTO app_env (app_id, name, value_enc) VALUES (?1, 'API_KEY', 'x')`).run(
78 getAppByRepo(app.db, getRepo(app.db, "alice", "garden")!.id)!.id,
79 );
80 }, 60_000);
81
82 afterAll(async () => {
83 servers.stop();
84 app.close();
85 await rm(dataDir, { recursive: true, force: true });
86 });
87
88 test("a signed-out visitor sees Make it yours and where it leads; one POST later it is theirs, privately, following, with a receipt", async () => {
89 const seen = await (await fetch(`${servers.url}/alice/garden`)).text();
90 expect(seen).toContain("Make it yours");
91 expect(seen).toContain('href="/alice/garden/make-it-yours');
92 const lead = await fetch(`${servers.url}/alice/garden/make-it-yours?receipt=1`, { redirect: "manual" });
93 expect(lead.status).toBe(303);
94 expect(lead.headers.get("location")).toBe(
95 `/signin?next=${encodeURIComponent("/alice/garden/make-it-yours?receipt=1")}`,
96 );
97
98 // A stranger who only signed up is a guest: the page offers them help
99 // build, in its own words, and the MCP refuses in the guest's words.
100 const guest = await signUp("guest", false);
101 const guestPage = await (await fetch(`${servers.url}/alice/garden/make-it-yours`, { headers: { cookie: guest.cookie } })).text();
102 expect(guestPage).toContain("Help build this");
103 expect((await mcpTool(servers.url, guest.token, "make_it_yours", { repo: "alice/garden", dryRun: false })).error).toContain("not_member");
104 expect(getRepo(app.db, "guest", "garden")).toBeUndefined();
105
106 // Admitted by the owner (the forge in a link does this), the friend makes it theirs.
107 const fred = await signUp("fred");
108 const cookie = `${fred.cookie}; forge_mode=studio`;
109 const plan = await (await fetch(`${servers.url}/alice/garden/make-it-yours?receipt=1`, { headers: { cookie } })).text();
110 expect(plan).toContain("fred/garden");
outdated

Only app_env is checked. Seed the original's data and assert the fork has none.

111 expect(plan).toContain("its variables and secrets");
112 expect(getRepo(app.db, "fred", "garden")).toBeUndefined();
113
114 const posted = await fetch(`${servers.url}/alice/garden/make-it-yours`, {
115 method: "POST",
116 headers: { cookie, origin: servers.url, "content-type": "application/x-www-form-urlencoded" },
outdated

Add the COPIED and NOT_COPIED words here, so the receipt page itself says what was and was not copied.

117 body: "receipt=1",
118 redirect: "manual",
119 });
120 expect(posted.status).toBe(303);
121 expect(posted.headers.get("location")).toBe("/alice/garden/make-it-yours/receipt?made=1");
122
123 const copy = getRepo(app.db, "fred", "garden")!;
124 expect(copy.visibility).toBe("private");
125 expect(copy.forkParentId).toBe(getRepo(app.db, "alice", "garden")!.id);
126 expect(getAppByRepo(app.db, copy.id)).toBeDefined();
127 const copyAppId = getAppByRepo(app.db, copy.id)!.id;
128 expect(app.db.query(`SELECT COUNT(*) AS n FROM app_env WHERE app_id = ?1`).get(copyAppId)).toEqual({ n: 0 });
129 // The original's data is seeded; none of it reaches the copy.
130 expect(existsSync(join(envDataDir(dataDir, copyAppId, "production"), "plants.db"))).toBe(false);
131
132 const receipt = await (await fetch(`${servers.url}/alice/garden/make-it-yours/receipt?made=1`, { headers: { cookie } })).text();
133 expect(receipt).toContain(receiptWords({ repo: "fred/garden", from: "alice/garden", existing: false }).replace("'", "&#39;"));
134 expect(receipt).toContain('href="/fred/garden"');
135 for (const w of [...COPIED, ...NOT_COPIED]) expect(receipt).toContain(w.replace("'", "&#39;"));
136 expect(receipt).toContain("the app&#39;s data");
137 expect(receipt).toContain("its variables and secrets");
138 // Their own forge shows it, and it says what it follows.
139 const mine = await (await fetch(`${servers.url}/fred/garden`, { headers: { cookie } })).text();
140 expect(mine).toContain("alice/garden");
141 // Private: the signed-out door does not know it.
142 expect((await fetch(`${servers.url}/fred/garden`)).status).toBe(404);
143 });
144
145 test("the MCP tool says the same words; a dry run changes nothing", async () => {
146 const gina = await signUp("gina");
147 const reposBefore = listApps(app.db).length;
148 const plan = await mcpTool(servers.url, gina.token, "make_it_yours", { repo: "alice/garden" });
149 expect(plan.value).toMatchObject({ dryRun: true, from: "alice/garden", repo: "gina/garden", existing: false });
150 expect(plan.value.copied).toEqual([...COPIED]);
151 expect(plan.value.notCopied).toEqual([...NOT_COPIED]);
152 expect(plan.value.notCopied).toContain("the app's data");
153 expect(plan.value.notCopied).toContain("its variables and secrets");
154 expect(getRepo(app.db, "gina", "garden")).toBeUndefined();
155 expect(listApps(app.db).length).toBe(reposBefore);
156
157 const made = await mcpTool(servers.url, gina.token, "make_it_yours", { repo: "alice/garden", dryRun: false });
158 expect(made.value.says).toBe(receiptWords({ repo: "gina/garden", from: "alice/garden", existing: false }));
159 expect(made.value.url).toBe("/gina/garden");
160 const copy = getRepo(app.db, "gina", "garden")!;
161 expect(copy.visibility).toBe("private");
162 expect(copy.forkParentId).toBe(getRepo(app.db, "alice", "garden")!.id);
163 });
164
165 test("a private origin is 404 on both doors, even as a plan", async () => {
166 const hank = await signUp("hank");
167 const before = listApps(app.db).length;
168 for (const dryRun of [true, false]) {
169 const over = await mcpTool(servers.url, hank.token, "make_it_yours", { repo: "alice/vault", dryRun });
170 expect(over.error).toContain("no such app or repository");
171 }
172 expect((await fetch(`${servers.url}/alice/vault/make-it-yours`, { headers: { cookie: hank.cookie } })).status).toBe(404);
173 const post = await fetch(`${servers.url}/alice/vault/make-it-yours`, {
174 method: "POST",
175 headers: { cookie: hank.cookie, origin: servers.url },
176 redirect: "manual",
177 });
178 expect(post.status).toBe(404);
179 expect(getRepo(app.db, "hank", "vault")).toBeUndefined();
180 expect(listApps(app.db).length).toBe(before);
181 });
182
183 test("the daily copy limit answers with its reason, on both doors", async () => {
184 const iris = await signUp("iris");
185 const now = new Date().toISOString();
186 for (let i = 0; i < COPIES_PER_DAY; i++)
187 app.db.query(`INSERT INTO fork_made (owner_id, made_at) VALUES (?1, ?2)`).run(iris.user.id, now);
188 const over = await mcpTool(servers.url, iris.token, "make_it_yours", { repo: "alice/garden", dryRun: false });
189 expect(over.error).toContain(`at most ${COPIES_PER_DAY} copies a day`);
190 expect(over.error).toContain("another after");
191 const post = await fetch(`${servers.url}/alice/garden/make-it-yours`, {
192 method: "POST",
193 headers: { cookie: iris.cookie, origin: servers.url },
194 redirect: "manual",
195 });
196 expect(post.status).toBe(303);
197 const flash = await (await fetch(`${servers.url}${post.headers.get("location")}`, { headers: { cookie: `${iris.cookie}; ${(post.headers.get("set-cookie") ?? "").split(";")[0]}` } })).text();
198 expect(flash).toContain(`today&#39;s copies made (${COPIES_PER_DAY} a day)`);
199 expect(getRepo(app.db, "iris", "garden")).toBeUndefined();
200 });
201 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs