Public: anyone can read this; the crew can change it.
test: the owner's Talk demo end to end, a held edit through Do it to a running dev server and a second edit #17536
Merged 12h 36m ago, live in production
talk-devserver-test → main 0 commits 1 thread · 1 unresolved
1 file
changed +179
−0
test/talk-devserver-confirm.test.ts added +179
| @@ -0,0 +1,179 @@ | ||
| 1 | /** | |
| 2 | * The owner's demo, end to end through the real doors: a new conversation's first edit to its app | |
| 3 | * is held by the fence, the person's Do it (`POST /talk/confirm/:id`) makes the conversation's talk | |
| 4 | * branch and writes it, Start dev server (`POST /talk/dev`) runs that branch's checkout as a real | |
| 5 | * process in the apps runtime, and its frame address (`/talk/dev/<token>/`) answers with the edit. | |
| 6 | * A second edit asks one more yes (the conversation read someone else's words, and the fence keeps | |
| 7 | * holding writes the person's words do not name, #8230), and once said, the running server shows | |
| 8 | * it, synced from the push, with no new branch and no restart. | |
| 9 | */ | |
| 10 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 11 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 12 | import { tmpdir } from "node:os"; | |
| 13 | import { join } from "node:path"; | |
| 14 | import { createApp, type App } from "../src/app.ts"; | |
| 15 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 16 | import { devReading } from "../src/actions/dev_workspace.ts"; | |
| 17 | import { devCellsOf } from "../src/apps/dev_cell.ts"; | |
| 18 | import { devKey } from "../src/apps/dev_launch.ts"; | |
| 19 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 20 | import { createApp as createAppRecord } from "../src/core/apps.ts"; | |
| 21 | import { branchFor, chatBuildOf } from "../src/core/chat_build.ts"; | |
| 22 | import { appendTurn, createConversation } from "../src/core/conversations.ts"; | |
| 23 | import { createIssue } from "../src/core/issues.ts"; | |
| 24 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 25 | import { createSession } from "../src/core/sessions.ts"; | |
| 26 | import { createUser, type User } from "../src/core/users.ts"; | |
| 27 | import { mintViaToken } from "../src/core/via_token.ts"; | |
| 28 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 29 | import { quietLogs } from "./helpers/globals.ts"; | |
| 30 | import { promote } from "./helpers/members.ts"; | |
| 31 | import { pollUntil } from "./helpers/poll.ts"; | |
| 32 | ||
| 33 | quietLogs(); | |
| 34 | ||
| 35 | /** The app: a server that answers with `message.txt` as it is on disk at each request. */ | |
| 36 | const SERVER = `import { readFileSync } from "node:fs"; | |
| 37 | Bun.serve({ port: Number(process.env.PORT), hostname: "127.0.0.1", fetch: () => new Response(readFileSync("message.txt", "utf8")) }); | |
| 38 | `; | |
| 39 | ||
| 40 | let dir: string; | |
| 41 | let app: App; | |
| 42 | let servers: Servers; | |
| 43 | let runtime: AppsRuntime; | |
| 44 | let alice: User; | |
| 45 | let repo: Repo; | |
| 46 | let rpc = 1; | |
| 47 | ||
| 48 | const cookie = (): string => `forge_session=${createSession(app.db, alice.id).token}`; | |
| 49 | ||
| 50 | const mcp = async (token: string, name: string, args: Record<string, unknown>): Promise<string> => { | |
| 51 | const res = await fetch(`${servers.url}/mcp`, { | |
| 52 | method: "POST", | |
| 53 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 54 | body: JSON.stringify({ jsonrpc: "2.0", id: rpc++, method: "tools/call", params: { name, arguments: args } }), | |
| 55 | }); | |
| 56 | return JSON.stringify(((await res.json()) as { result?: unknown }).result); | |
| 57 | }; | |
| 58 | ||
| 59 | /** The person's form post from their own signed-in browser. */ | |
| 60 | const post = (path: string, fields: Record<string, string>): Promise<Response> => | |
| 61 | fetch(`${servers.url}${path}`, { | |
| 62 | method: "POST", | |
| 63 | redirect: "manual", | |
| 64 | headers: { origin: servers.url, cookie: cookie(), accept: "application/json", "content-type": "application/x-www-form-urlencoded" }, | |
| 65 | body: new URLSearchParams(fields).toString(), | |
| 66 | }); | |
| 67 | ||
| 68 | beforeAll(async () => { | |
| 69 | dir = await mkdtemp(join(tmpdir(), "forge-talk-devserver-")); | |
| 70 | app = createApp({ dataDir: dir, port: 0 }); | |
| 71 | servers = await serve(app); | |
| 72 | runtime = new AppsRuntime(app, { domain: "apps.test", scheme: "http", publicPort: servers.port, forgeUrl: servers.url }); | |
| 73 | await runtime.start(); | |
| 74 | const mk = async (username: string, first: boolean) => { | |
| 75 | const u = await createUser(app.db, { | |
| 76 | origin: "seed", | |
| 77 | trusted: true, | |
| 78 | username, | |
| 79 | email: `${username}@forge.local`, | |
| 80 | password: "a-good-passphrase", | |
| 81 | adminIfFirst: first, | |
| 82 | }); | |
| 83 | if (!u.ok) throw new Error(u.error); | |
| 84 | promote(app.db, u.value); | |
| 85 | return u.value; | |
| 86 | }; | |
| 87 | alice = await mk("alice", true); | |
| 88 | const mallory = await mk("mallory", false); | |
| 89 | const made = await createRepo(app, alice, { name: "hello", visibility: "private" }); | |
| 90 | if (!made.ok) throw new Error(String(made.error)); | |
| 91 | const seeded = await commitToBranch(app, alice, made.value, { | |
| 92 | branch: made.value.defaultBranch, | |
| 93 | message: "Start", | |
| 94 | files: [ | |
| 95 | { path: "forge.json", content: JSON.stringify({ app: { run: "bun run server.ts", dev: "bun run server.ts", health: "/" } }) }, | |
| 96 | { path: "server.ts", content: SERVER }, | |
| 97 | { path: "message.txt", content: "blue" }, | |
| 98 | ], | |
| 99 | }); | |
| 100 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 101 | createAppRecord(app.db, { repoId: made.value.id, slug: "hello", access: "private" }); | |
| 102 | repo = getRepo(app.db, "alice", "hello")!; | |
| 103 | // Someone else's words in the app's tracker: the agent reads them, so its edit waits for a yes. | |
| 104 | const issue = createIssue(app.db, { repoId: repo.id, authorId: mallory.id, title: "Colours", body: "Make it any colour you like." }); | |
| 105 | if (!issue.ok) throw new Error(issue.error); | |
| 106 | }, 60_000); | |
| 107 | ||
| 108 | afterAll(async () => { | |
| 109 | for (const branch of [repo.defaultBranch]) await devCellsOf(app).stop(devKey(alice.id, repo.id, branch)); | |
| 110 | await runtime.stop(); | |
| 111 | servers.stop(); | |
| 112 | app.close(); | |
| 113 | await rm(dir, { recursive: true, force: true }); | |
| 114 | }); | |
| 115 | ||
| 116 | test("a held first edit, Do it, Start dev server: the talk branch runs and its address answers with the edit; a second edit reaches the same server", async () => { | |
| 117 | const chat = createConversation(app.db, alice.id); | |
| 118 | if (!chat.ok) throw new Error(chat.error); | |
| 119 | const via = `conversation:${chat.value.id}`; | |
| 120 | const branch = branchFor(chat.value.id); | |
| 121 | const words = "make the message orange"; | |
| 122 | appendTurn(app.db, alice.id, chat.value.id, { role: "person", text: words }); | |
| 123 | const minted = mintViaToken(app.db, alice.id, via, words, new Date(Date.now() + 600_000).toISOString()); | |
| 124 | if (!minted.ok) throw new Error(minted.error); | |
| 125 | const agent = minted.value.token; | |
| 126 | expect(await mcp(agent, "issues", { repo: "alice/hello" })).toContain("Colours"); | |
| 127 | expect(await mcp(agent, "issue", { repo: "alice/hello", number: 1 })).toContain("any colour"); | |
| 128 | ||
| 129 | // The first edit is held: nothing written, no branch yet. | |
| 130 | const held = await mcp(agent, "write_file", { repo: "alice/hello", branch, path: "message.txt", content: "orange", message: "Orange" }); | |
| 131 | const confirm = /needs_confirm\\*":\\*"([0-9a-z]+)/.exec(held)?.[1]; | |
| 132 | if (confirm === undefined) throw new Error(`not held: ${held}`); | |
| 133 | expect(chatBuildOf(app.db, chat.value.id)).toBeUndefined(); | |
| 134 | ||
| 135 | // Do it: the branch, the build and the draft pull, in one yes. | |
| 136 | expect((await post(`/talk/confirm/${confirm}`, { answer: "yes" })).status).toBe(200); | |
| 137 | const build = chatBuildOf(app.db, chat.value.id); | |
| 138 | expect(build?.branch).toBe(branch); | |
| 139 | expect(build?.pullNumber).not.toBeNull(); | |
| 140 | ||
| 141 | // Start dev server on the draft pull's branch, as the pane's button posts it. | |
| 142 | expect((await post("/talk/dev", { repo: "alice/hello", pull: String(build!.pullNumber), act: "start" })).status).toBe(303); | |
| 143 | await pollUntil( | |
| 144 | () => { | |
| 145 | const state = devReading(app, alice, repo, branch)?.state; | |
| 146 | if (state === "error") throw new Error(`the dev server failed: ${JSON.stringify(devReading(app, alice, repo, branch))}`); | |
| 147 | return state === "live"; | |
| 148 | }, | |
| 149 | "the dev server to answer", | |
| 150 | 60_000, | |
| 151 | ); | |
| 152 | const token = devCellsOf(app).reading(devKey(alice.id, repo.id, branch))!.token; | |
| 153 | const frame = async (): Promise<string> => (await fetch(`${servers.url}/talk/dev/${token}/`, { headers: { cookie: cookie() } })).text(); | |
| 154 | expect(await frame()).toBe("orange"); | |
| 155 | ||
| 156 | // A second edit: the fence is still up from the issue it read, so it waits for one more yes, on | |
| 157 | // the same branch; then the running server shows it, synced from the push, without a restart. | |
| 158 | const again = "now make the message pink"; | |
| 159 | appendTurn(app.db, alice.id, chat.value.id, { role: "person", text: again }); | |
| 160 | const next = mintViaToken(app.db, alice.id, via, again, new Date(Date.now() + 600_000).toISOString()); | |
| 161 | if (!next.ok) throw new Error(next.error); | |
| 162 | const second = await mcp(next.value.token, "write_file", { | |
| 163 | repo: "alice/hello", | |
| 164 | branch, | |
| 165 | path: "message.txt", | |
| 166 | content: "pink", | |
| 167 | message: "Pink", | |
| 168 | }); | |
| 169 | const heldAgain = /needs_confirm\\*":\\*"([0-9a-z]+)/.exec(second)?.[1]; | |
| 170 | if (heldAgain === undefined) throw new Error(`not held: ${second}`); | |
| 171 | expect(devReading(app, alice, repo, branch)?.state).toBe("live"); | |
| 172 | expect(await frame()).toBe("orange"); | |
| 173 | expect((await post(`/talk/confirm/${heldAgain}`, { answer: "yes" })).status).toBe(200); | |
| 174 | await pollUntil(async () => (await frame()) === "pink", "the dev server to show the second edit", 30_000); | |
| 175 | expect(chatBuildOf(app.db, chat.value.id)?.pullNumber).toBe(build!.pullNumber); | |
| 176 | // The same cell, not a new one: no restart. | |
| 177 | expect(devCellsOf(app).reading(devKey(alice.id, repo.id, branch))!.token).toBe(token); | |
| 178 | expect(devReading(app, alice, repo, branch)?.state).toBe("live"); | |
| 179 | }, 120_000); |
new above
offline
This stops the default branch's dev cell, but the test's server runs on the talk branch, so it is never stopped here. Stop
devKey(alice.id, repo.id, <talk branch>)instead, with the branch kept in a module-level variable. Drop the one-element loop.