Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

86 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { readFileSync, readdirSync } from "node:fs";
3import { DatabaseSync } from "node:sqlite";
4import { test } from "node:test";
5
6const source = readFileSync(new URL("../services/repos/src/public_storage.rs", import.meta.url), "utf8");
7const sql = (name) => source.match(new RegExp(`const ${name}: &str = "([\\s\\S]*?)";`))[1];
8function database() {
9 const db = new DatabaseSync(":memory:");
10 const dir = new URL("../services/repos/migrations/", import.meta.url);
11 for (const name of readdirSync(dir)
12 .filter((n) => n.endsWith(".sql"))
13 .sort())
14 db.exec(readFileSync(new URL(name, dir), "utf8"));
15 return db;
16}
17function repo(db, id, bytes, workspace = "team", privateRepo = 0) {
18 db.prepare("INSERT INTO repos(id,namespace,name,owner_id,stored_bytes,is_private,created_at) VALUES (?,?,?,'owner',?,?,'2026-10-14T00:00:00Z')").run(
19 id,
20 workspace,
21 id,
22 bytes,
23 privateRepo,
24 );
25}
26const reserve = (db, id, bytes, cap = 5_000_000_000) => db.prepare(sql("RESERVE")).get({ 1: id, 2: bytes, 3: cap });
27
28test("overlapping pushes cannot reserve the same final capacity", () => {
29 const db = database();
30 repo(db, "first", 4_900_000_000);
31 repo(db, "second", 0);
32 assert.ok(reserve(db, "first", 80_000_000));
33 assert.equal(reserve(db, "second", 80_000_000), undefined);
34 assert.ok(reserve(db, "second", 20_000_000));
35 assert.equal(reserve(db, "first", 1), undefined);
36 assert.equal(db.prepare("SELECT SUM(stored_bytes + storage_reserved_bytes) n FROM repos").get().n, 5_000_000_000);
37 assert.equal(db.prepare("SELECT SUM(stored_bytes) n FROM repos").get().n, 4_900_000_000, "reservations are not measured storage billed to customers");
38 db.close();
39});
40
41test("deleted retained data and interrupted write reservations still consume capacity", () => {
42 const db = database();
43 repo(db, "retained", 4_950_000_000);
44 repo(db, "active", 0);
45 db.exec("UPDATE repos SET deleted_at = '2026-10-14T00:00:00Z' WHERE id = 'retained'");
46 assert.ok(reserve(db, "active", 50_000_000));
47 assert.equal(reserve(db, "active", 1), undefined);
48 db.exec("DELETE FROM repos WHERE id = 'retained'");
49 assert.ok(reserve(db, "active", 1));
50 db.close();
51});
52
53test("concurrent visibility changes cannot bypass public capacity", () => {
54 const db = database();
55 repo(db, "public", 4_000_000_000);
56 repo(db, "private_a", 750_000_000, "team", 1);
57 repo(db, "private_b", 750_000_000, "team", 1);
58 assert.ok(db.prepare(sql("MAKE_PUBLIC")).get({ 1: "private_a", 2: 5_000_000_000 }));
59 assert.equal(db.prepare(sql("MAKE_PUBLIC")).get({ 1: "private_b", 2: 5_000_000_000 }), undefined);
60 assert.ok(reserve(db, "private_b", 100_000_000));
61 assert.equal(db.prepare("SELECT is_private FROM repos WHERE id='private_b'").get().is_private, 1);
62 db.close();
63});
64
65test("transfer admission uses destination capacity at the moment of mutation", () => {
66 const db = database();
67 repo(db, "existing", 4_500_000_000, "destination");
68 repo(db, "moving", 600_000_000);
69 assert.equal(db.prepare(sql("TRANSFER")).get({ 1: "moving", 2: "destination", 3: "team", 4: 5_000_000_000 }), undefined);
70 assert.equal(db.prepare("SELECT namespace FROM repos WHERE id='moving'").get().namespace, "team");
71 db.exec("DELETE FROM repos WHERE id='existing'");
72 assert.ok(db.prepare(sql("TRANSFER")).get({ 1: "moving", 2: "destination", 3: "team", 4: 5_000_000_000 }));
73 db.close();
74});
75
76test("native copy freezes new source writes until its storage snapshot is taken", () => {
77 const db = database();
78 repo(db, "source", 100);
79 repo(db, "fork", 0);
80 db.exec("INSERT INTO storage_copy_locks VALUES ('source','fork','2026-10-14T00:00:00Z')");
81 assert.equal(reserve(db, "source", 20), undefined);
82 assert.ok(reserve(db, "fork", 100));
83 db.exec("DELETE FROM storage_copy_locks WHERE repo_id='source'");
84 assert.ok(reserve(db, "source", 20));
85 db.close();
86});