Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 150 to 151

Compare

+216 −0 in 1 place
new file, 216 lines
1+// The Worker's way into issues: a key acts only as its own agent, a reader's key only reads, an ask goes only to
2+// the project's agents, and a write's news reaches the project object. Each IssueProject is an IssuesObject over
3+// node:sqlite, reached through fake namespaces shaped like Cloudflare's.
4+
5+import assert from "node:assert/strict";
6+import { DatabaseSync } from "node:sqlite";
7+import { describe, it } from "node:test";
8+import {
9+ type IssueNews,
10+ type IssuesAuth,
11+ type IssuesEnv,
12+ IssuesObject,
13+ issuesFetch,
14+ type ProjectHooks,
15+} from "../../src/issues/serve.ts";
16+import { SCHEMA } from "../../src/issues/store.ts";
18+function storage() {
19+ const db = new DatabaseSync(":memory:");
20+ return {
21+ sql: {
22+ exec: (q: string, ...b: unknown[]) => {
23+ if (/^\s*CREATE\b/i.test(q)) {
24+ db.exec(q);
25+ return { toArray: () => [] };
26+ }
27+ const rows = db.prepare(q).all(...(b as (string | number | null)[]));
28+ return { toArray: () => rows };
29+ },
30+ },
31+ transactionSync: <T>(action: () => T): T => {
32+ db.exec("SAVEPOINT t");
33+ try {
34+ const out = action();
35+ db.exec("RELEASE t");
36+ return out;
37+ } catch (e) {
38+ db.exec("ROLLBACK TO t");
39+ db.exec("RELEASE t");
40+ throw e;
41+ }
42+ },
43+ };
44+}
46+/** A namespace like Cloudflare's: idFromName gives the name back, get gives one object per name. */
47+function namespace<T extends object>(make: (name: string) => T) {
48+ const made = new Map<string, T>();
49+ return {
50+ idFromName: (name: string) => name,
51+ get: (id: never) => {
52+ const name = id as unknown as string;
53+ if (!made.has(name)) made.set(name, make(name));
54+ return made.get(name)!;
55+ },
56+ };
57+}
59+function world(hooks?: ProjectHooks) {
60+ const env: IssuesEnv = { ISSUES_DO: namespace(() => new IssuesObject(storage(), env)) };
61+ if (hooks) env.PROJECT = namespace(() => hooks);
62+ const waits: Promise<unknown>[] = [];
63+ const ctx = { waitUntil: (p: Promise<unknown>) => void waits.push(p) };
64+ const call = async (auth: IssuesAuth, method: string, rest: string, body?: unknown, project = "web") => {
65+ const r = await issuesFetch(
66+ new Request(`https://abso.example/issues/p/${project}${rest}`, {
67+ method,
68+ headers: { Authorization: "Bearer secret" },
69+ ...(body !== undefined ? { body: JSON.stringify(body) } : {}),
70+ }),
71+ env,
72+ ctx,
73+ { project, rest: rest.replace(/\?.*$/, ""), auth },
74+ );
75+ await Promise.all(waits.splice(0));
76+ return { status: r.status, json: (await r.json()) as Record<string, any> };
77+ };
78+ return { env, call };
79+}
81+const token: IssuesAuth = { kind: "token" };
82+const leadKey: IssuesAuth = { kind: "key", agent: "lead", role: "writer" };
83+const readerKey: IssuesAuth = { kind: "key", agent: "watcher", role: "reader" };
85+describe("issuesFetch: who may do what", () => {
86+ it("the token acts as any agent; a key only as its own", async () => {
87+ const { call } = world();
88+ assert.equal((await call(token, "POST", "/a/fay/issue", { title: "Sync is slow" })).status, 200);
89+ assert.equal((await call(leadKey, "POST", "/a/lead/issue", { title: "Logo" })).status, 200);
90+ const other = await call(leadKey, "POST", "/a/fay/issue/1/comment", { text: "as fay" });
91+ assert.equal(other.status, 403);
92+ assert.match(other.json.error, /this key is lead's/);
93+ assert.equal((await call(leadKey, "GET", "/a/fay/waiting")).status, 403, "nor reads another's own list");
94+ assert.equal((await call(leadKey, "GET", "/issues")).json.issues.length, 2);
95+ });
97+ it("a reader's key reads and never writes", async () => {
98+ const { call } = world();
99+ await call(token, "POST", "/a/fay/issue", { title: "Sync is slow" });
100+ assert.equal((await call(readerKey, "GET", "/issue/1")).status, 200);
101+ assert.equal((await call(readerKey, "GET", "/search?q=sync")).json.hits.length, 1);
102+ const refused = await call(readerKey, "POST", "/a/watcher/issue/1/comment", { text: "hi" });
103+ assert.equal(refused.status, 403);
104+ assert.match(refused.json.error, /only reads/);
105+ });
107+ it("refuses a bad project or agent name, and an empty rest", async () => {
108+ const { call } = world();
109+ assert.equal((await call(token, "GET", "/issues", undefined, "no/such")).status, 404);
110+ assert.equal((await call(token, "POST", "/a/person:u1/issue", { title: "x" })).status, 400);
111+ assert.equal((await call(token, "GET", "")).status, 404);
112+ });
114+ it("each project has its own issues", async () => {
115+ const { call } = world();
116+ await call(token, "POST", "/a/fay/issue", { title: "Web's" });
117+ assert.equal((await call(token, "GET", "/issues", undefined, "other")).json.issues.length, 0);
118+ });
120+ it("attachments are 501, and a write naming one is refused before anything is written", async () => {
121+ const { call } = world();
122+ assert.equal((await call(token, "POST", "/a/fay/attachments")).status, 501);
123+ const named = await call(token, "POST", "/a/fay/issue", { title: "x", attachments: ["a0123456789ab"] });
124+ assert.equal(named.status, 501);
125+ assert.equal((await call(token, "GET", "/issues")).json.issues.length, 0);
126+ });
127+});
129+describe("issuesFetch: the project object", () => {
130+ it("an ask goes only to the project's agents", async () => {
131+ const { call } = world({ agents: async () => ["lead", "fay", "cloud"] });
132+ const refused = await call(token, "POST", "/a/lead/ask", { of: "Cloud Lead", question: "Which region?" });
133+ assert.equal(refused.status, 400);
134+ assert.match(refused.json.error, /isn't an agent in web, so nobody would hear the ask; did you mean cloud\?/);
135+ assert.equal((await call(token, "POST", "/a/lead/ask", { of: "cloud", question: "Which region?" })).status, 200);
136+ });
138+ it("without agents (or without PROJECT) the ask check is skipped", async () => {
139+ for (const hooks of [{}, undefined]) {
140+ const { call } = world(hooks);
141+ assert.equal((await call(token, "POST", "/a/lead/ask", { of: "anyone", question: "Q" })).status, 200);
142+ }
143+ });
145+ it("a write's news goes to the project from its writer; reads and refusals send none", async () => {
146+ const told: { project: string; items: IssueNews[] }[] = [];
147+ const { call } = world({
148+ news: async (project, items) => {
149+ told.push({ project, items });
150+ return [];
151+ },
152+ });
153+ await call(token, "POST", "/a/lead/issue", { title: "Sync is slow" });
154+ assert.equal(told.length, 0, "an issue for nobody in particular tells nobody");
155+ const asked = await call(token, "POST", "/a/lead/ask", { issue: 1, of: "fay", question: "Which way?" });
156+ assert.equal(told.length, 1);
157+ assert.equal(told[0]!.project, "web");
158+ assert.deepEqual(told[0]!.items, [
159+ {
160+ from: "lead",
161+ issue: 1,
162+ text: `lead asks fay a question on issue 1 ("Sync is slow"): "Which way?" Answer: abso answer ${asked.json.id} "..."`,
163+ to: ["fay"],
164+ },
165+ ]);
166+ await call(token, "GET", "/issue/1");
167+ await call(token, "POST", "/a/lead/answer/1", { words: "not mine to answer" });
168+ assert.equal(told.length, 1);
169+ });
171+ it("news with no news method on the project is dropped, and the write stands", async () => {
172+ const { call } = world({ agents: async () => ["lead", "fay"] });
173+ const asked = await call(token, "POST", "/a/lead/ask", { of: "fay", question: "Which way?" });
174+ assert.equal(asked.status, 200);
175+ assert.equal((await call(token, "GET", "/a/fay/waiting")).json.asks.length, 1);
176+ });
178+ it("an agent reads its news and marks it read", async () => {
179+ const { call } = world();
180+ await call(token, "POST", "/a/fay/issue", { title: "Sync is slow" });
181+ await call(token, "POST", "/a/lead/ask", { issue: 1, of: "fay", question: "Which way?" });
182+ await call(token, "POST", "/a/fay/answer/1", { words: "live" });
183+ const news = await call(leadKey, "GET", "/a/lead/news");
184+ assert.deepEqual(
185+ news.json.news.map((n: { kind: string; text: string; read: boolean }) => [n.kind, n.text, n.read]),
186+ [["answer", "live", false]],
187+ );
188+ const marked = await call(leadKey, "POST", "/a/lead/news/read", {});
189+ assert.equal(marked.status, 200);
190+ assert.equal((await call(leadKey, "GET", "/a/lead/news")).json.news[0].read, true);
191+ });
192+});
194+describe("IssuesObject", () => {
195+ it("runs every migration once, and again runs none", () => {
196+ const s = storage();
197+ const env: IssuesEnv = { ISSUES_DO: namespace(() => ({})) };
198+ assert.equal(new IssuesObject(s, env).migrations(), SCHEMA.length);
199+ assert.equal(new IssuesObject(s, env).migrations(), SCHEMA.length);
200+ });
202+ it("names who wrote each text: the opener, then whoever last edited it", async () => {
203+ const { call } = world();
204+ await call(token, "POST", "/a/fay/issue", { title: "Sync", description: "slow" });
205+ await call(token, "POST", "/a/lead/issue/1/edit", { title: "Sync is slow" });
206+ await call(token, "POST", "/a/lead/issue/1/comment", { text: "seen it" });
207+ const page = (await call(token, "GET", "/issue/1")).json;
208+ assert.deepEqual(page.issue.authorship, {
209+ title: { kind: "agent", actor: "lead", provenance: "native", authorField: "edits.by" },
210+ description: { kind: "agent", actor: "fay", provenance: "native", authorField: "issues.opened_by" },
211+ });
212+ assert.equal(page.comments[0].authorship.actor, "lead");
213+ const hit = (await call(token, "GET", "/search?q=slow")).json.hits[0];
214+ assert.equal(hit.issue.authorship.title.actor, "lead");
215+ });
216+});
MCP
compare on this site's /mcp
CLI
abso diff 150 151 server/test/issues/serve.test.ts --json
JSON
/christopher/abso-core/compare/150..151?file=server%2Ftest%2Fissues%2Fserve.test.ts&format=json