Diffinery

homeflare/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

100 lines · 5.0 KBRaw History
1import { describe, expect, it } from "vitest";
2import { DatabaseSync } from "node:sqlite";
3import { egressFor, egressStart, type Egress } from "../../src/lib/egress.js";
4import { Actions } from "../../src/repo/actions.js";
5import { REPO_SCHEMA, migrateRepoSchema } from "../../src/repo/schema.js";
6import type { RepoDO } from "../../src/repo/RepoDO.js";
7import type { Caller } from "../../src/repo/types.js";
8import type { Env } from "../../src/env.js";
9import { testD1 } from "./stubs/d1.js";
10
11const at = "2026-10-01T00:00:00.000Z";
12
13/** Accounts as in quotas.test.ts: ann and her organization acme, root (a site administrator) and his organization ops. */
14function accounts() {
15 const DB = testD1();
16 const add = (id: string, kind: string, siteAdmin = 0) => DB.sqlite.prepare("INSERT INTO accounts (id, login, kind, status, site_admin, created_at) VALUES (?, ?, ?, 'active', ?, ?)").run(id, id, kind, siteAdmin, at);
17 add("ann", "user");
18 add("root", "user", 1);
19 add("acme", "org");
20 add("ops", "org");
21 DB.sqlite.prepare("INSERT INTO org_members (org_id, user_id, role, created_at) VALUES ('acme', 'ann', 'owner', ?), ('ops', 'root', 'owner', ?)").run(at, at);
22 return DB;
23}
24
25function sqlStorage() {
26 const db = new DatabaseSync(":memory:");
27 return {
28 exec(query: string, ...args: unknown[]) {
29 const params = args.map((a) => (a === undefined ? null : a)) as Array<string | number | null>;
30 let rows: unknown[] = [];
31 if (!params.length && query.trim().replace(/;\s*$/, "").includes(";")) db.exec(query);
32 else rows = db.prepare(query).all(...params);
33 return { toArray: () => rows, one: () => rows[0] };
34 },
35 };
36}
37
38/** A repository owned by `owner` whose hosted job launch and sandbox boot record the egress they're given. */
39function repository(owner: string, vars: Record<string, string>) {
40 const sql = sqlStorage();
41 sql.exec(REPO_SCHEMA);
42 migrateRepoSchema(sql as unknown as SqlStorage);
43 const started: { job?: Egress; sandbox?: Egress } = {};
44 const repo = {
45 sql,
46 meta: { repoId: "r", owner, ownerId: owner, name: "app", defaultBranch: "main" },
47 upsertCheck: () => {},
48 broadcast: () => {},
49 resolve: async () => "abc",
50 } as unknown as RepoDO;
51 const env = {
52 DB: accounts(),
53 PUBLIC_URL: "https://d.test",
54 ...vars,
55 RUNNER: { idFromName: (n: string) => n, get: () => ({ startJob: async (i: { egress?: Egress }) => void (started.job = i.egress) }) },
56 SANDBOX: { idFromName: (n: string) => n, get: () => ({ boot: async (...a: unknown[]) => void (started.sandbox = a[4] as Egress) }) },
57 } as unknown as Env;
58 const actions = new Actions(repo, env);
59 const t = new Date().toISOString();
60 sql.exec("INSERT INTO runs (id, run_number, workflow_path, workflow_name, event, ref, sha, actor, status, created_at, updated_at, event_payload) VALUES ('run1', 1, 'w.yml', 'w', 'push', 'refs/heads/main', 'abc', '{}', 'in_progress', ?, ?, '{}')", t, t);
61 sql.exec("INSERT INTO jobs (id, run_id, job_key, name, status) VALUES ('j1', 'run1', 'k', 'k', 'queued')");
62 const caller = { id: "ann", login: "ann", kind: "user", role: "write" } as Caller;
63 return {
64 started,
65 async run() {
66 await (actions as unknown as { launch(id: string): Promise<void> }).launch("j1");
67 await actions.createSandbox(caller);
68 return started;
69 },
70 };
71}
72
73describe("egress of hosted compute", () => {
74 it("is open unless EGRESS is web, and a site administrator's accounts stay open", async () => {
75 const DB = accounts();
76 const env = (vars: Record<string, string> = {}) => ({ DB, ...vars }) as unknown as Env;
77 for (const id of ["ann", "acme", "root", "ops"]) {
78 expect(await egressFor(env(), id)).toBe("open");
79 expect(await egressFor(env({ EGRESS: "open" }), id)).toBe("open");
80 expect(await egressFor(env({ EGRESS: "everything" }), id)).toBe("open");
81 }
82 expect(await egressFor(env({ EGRESS: "web" }), "ann")).toBe("web");
83 expect(await egressFor(env({ EGRESS: "web" }), "acme")).toBe("web");
84 expect(await egressFor(env({ EGRESS: "web" }), "root")).toBe("open");
85 expect(await egressFor(env({ EGRESS: "web" }), "ops")).toBe("open");
86 });
87
88 it("web starts a container without internet, HTTP and HTTPS to every host through the Worker", () => {
89 expect(egressStart("open")).toEqual({ enableInternet: true, interceptHttps: false, envVars: {} });
90 expect(egressStart("web")).toEqual({ enableInternet: false, interceptHttps: true, allowedHosts: ["*"], envVars: { DIFFINERY_EGRESS: "web" } });
91 });
92
93 it("the repository owner's policy reaches its hosted jobs and sandboxes", async () => {
94 expect(await repository("acme", {}).run()).toEqual({ job: "open", sandbox: "open" });
95 expect(await repository("acme", { EGRESS: "web" }).run()).toEqual({ job: "web", sandbox: "web" });
96 expect(await repository("ann", { EGRESS: "web" }).run()).toEqual({ job: "web", sandbox: "web" });
97 // Diffinery's own pipelines run in a site administrator's organization: they keep their egress.
98 expect(await repository("ops", { EGRESS: "web" }).run()).toEqual({ job: "open", sandbox: "open" });
99 });
100});