|
|
@@ -0,0 +1,293 @@ |
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import { afterAll, beforeAll, expect, spyOn, test } from "bun:test"; |
| 7 |
import { mkdtemp, rm } from "node:fs/promises"; |
| 8 |
import { tmpdir } from "node:os"; |
| 9 |
import { join } from "node:path"; |
| 10 |
import { createApp, type App } from "../src/app.ts"; |
| 11 |
import { addToCrew, createAgent } from "../src/actions/crew.ts"; |
| 12 |
import { assignIssue, openIssue } from "../src/actions/issues.ts"; |
| 13 |
import { commitToBranch, createRepo, updateRepoSettings } from "../src/actions/repos.ts"; |
| 14 |
import { addDependency } from "../src/core/dependencies.ts"; |
| 15 |
import { getIssueById, type Issue } from "../src/core/issues.ts"; |
| 16 |
import { getRepoById, type Repo } from "../src/core/repos.ts"; |
| 17 |
import { createRun } from "../src/core/runs.ts"; |
| 18 |
import { createUser, getUserByName, type User } from "../src/core/users.ts"; |
| 19 |
import { changeCapacity } from "../src/actions/capacity.ts"; |
| 20 |
import { smallShare } from "../src/core/capacity.ts"; |
| 21 |
import { createLabel, listLabels, setIssueLabels } from "../src/core/issues.ts"; |
| 22 |
import { tool as capacityTool } from "../src/http/mcp/tools/capacity.ts"; |
| 23 |
import { autonomyTick, driveRepo } from "../src/crew/autonomy.ts"; |
| 24 |
import { isEpic, isSmall, sizeIssues, smallCap, smallWords } from "../src/crew/small_share.ts"; |
| 25 |
|
| 26 |
let dataDir: string; |
| 27 |
let app: App; |
| 28 |
let alice: User; |
| 29 |
let repo: Repo; |
| 30 |
|
| 31 |
const open = (title: string): Issue => { |
| 32 |
const made = openIssue(app, alice, getRepoById(app.db, repo.id)!, { title }); |
| 33 |
if (!made.ok) throw new Error(String(made.error)); |
| 34 |
return made.value; |
| 35 |
}; |
| 36 |
|
| 37 |
beforeAll(async () => { |
| 38 |
dataDir = await mkdtemp(join(tmpdir(), "forge-small-share-")); |
| 39 |
app = createApp({ dataDir, port: 0 }); |
| 40 |
const a = await createUser(app.db, { |
| 41 |
origin: "seed", trusted: true, username: "alice", email: "[email protected]", |
| 42 |
password: "a-good-passphrase", adminIfFirst: true, |
| 43 |
}); |
| 44 |
if (!a.ok) throw new Error(a.error); |
| 45 |
alice = a.value; |
| 46 |
const made = await createRepo(app, alice, { name: "notes", visibility: "private" }); |
| 47 |
if (!made.ok) throw new Error(String(made.error)); |
| 48 |
repo = made.value; |
| 49 |
const first = await commitToBranch(app, alice, repo, { |
| 50 |
message: "Start", |
| 51 |
files: [{ path: "README.md", content: "# notes\n" }], |
| 52 |
}); |
| 53 |
if (!first.ok) throw new Error(JSON.stringify(first.error)); |
| 54 |
for (const name of ["mason", "scout", "quill", "smith", "atlas"]) { |
| 55 |
const agent = await createAgent(app, alice, { username: name }); |
| 56 |
if (!agent.ok) throw new Error(String(agent.error)); |
| 57 |
expect(addToCrew(app, alice, repo, name).ok).toBe(true); |
| 58 |
} |
| 59 |
const on = await updateRepoSettings(app, alice, repo, { |
| 60 |
autonomy: "autonomous", |
| 61 |
autonomyDirection: "Keep the notes tidy.", |
| 62 |
autonomyCeilingUsd: 10, |
| 63 |
autonomySlots: 5, |
| 64 |
autonomyBy: alice.id, |
| 65 |
}); |
| 66 |
expect(on.ok).toBe(true); |
| 67 |
}); |
| 68 |
|
| 69 |
afterAll(async () => { |
| 70 |
app.close(); |
| 71 |
await rm(dataDir, { recursive: true, force: true }); |
| 72 |
}); |
| 73 |
|
| 74 |
test("small is read from the title or the labels, and the cap rounds up", () => { |
| 75 |
expect(isSmall("A test pins the sort order", [])).toBe(true); |
| 76 |
expect(isSmall("Sort notes", ["Tests-only"])).toBe(true); |
| 77 |
expect(isSmall("Sort notes by date", [])).toBe(false); |
| 78 |
expect(isSmall("Test: the sort order", [])).toBe(true); |
| 79 |
expect(isSmall("Sort notes", ["small"])).toBe(true); |
| 80 |
expect(isSmall("Sort notes", ["small-addition"])).toBe(true); |
| 81 |
|
| 82 |
expect(isSmall("Small business page", [])).toBe(false); |
| 83 |
expect(isSmall("Test sort order", [])).toBe(false); |
| 84 |
expect(isSmall("Tests for the sort", [])).toBe(false); |
| 85 |
expect(isSmall("Make it tests-only friendly", [])).toBe(false); |
| 86 |
expect(isEpic(["epic"])).toBe(true); |
| 87 |
expect(isEpic(["Epic"])).toBe(true); |
| 88 |
expect(isEpic([])).toBe(false); |
| 89 |
expect(smallCap(5, 30)).toBe(2); |
| 90 |
expect(smallCap(1, 30)).toBe(1); |
| 91 |
expect(smallCap(5, 0)).toBe(0); |
| 92 |
expect(smallCap(5, 100)).toBe(5); |
| 93 |
expect(smallWords(1, 2)).toBe("1/2"); |
| 94 |
}); |
| 95 |
|
| 96 |
test("with 5 slots and only small issues ready, at most 2 start; an epic's issue starts before a small one", async () => { |
| 97 |
const mason = getUserByName(app.db, "mason")!; |
| 98 |
const scout = getUserByName(app.db, "scout")!; |
| 99 |
|
| 100 |
for (const [title, who] of [["A test pins the sort", mason], ["A test pins the filter", scout]] as const) { |
| 101 |
const issue = open(title); |
| 102 |
const assigned = assignIssue(app, alice, getRepoById(app.db, repo.id)!, issue, who); |
| 103 |
expect(assigned.ok).toBe(true); |
| 104 |
createRun(app.db, { repoId: repo.id, kind: "agent", actorId: who.id, issueId: issue.id, reason: "assigned" }); |
| 105 |
} |
| 106 |
const third = open("A test pins the export"); |
| 107 |
|
| 108 |
|
| 109 |
expect(getIssueById(app.db, third.id)!.assigneeId).toBeNull(); |
| 110 |
|
| 111 |
|
| 112 |
const epic = open("Search the notes"); |
| 113 |
label(repo, epic, "epic"); |
| 114 |
const under = open("Index the notes"); |
| 115 |
expect(addDependency(app.db, epic, under, alice.id).ok).toBe(true); |
| 116 |
expect(await driveRepo(app, getRepoById(app.db, repo.id)!)).toBe("picked"); |
| 117 |
expect(getIssueById(app.db, under.id)!.assigneeId).not.toBeNull(); |
| 118 |
expect(getIssueById(app.db, third.id)!.assigneeId).toBeNull(); |
| 119 |
}); |
| 120 |
|
| 121 |
|
| 122 |
async function fresh(name: string): Promise<Repo> { |
| 123 |
const made = await createRepo(app, alice, { name, visibility: "private" }); |
| 124 |
if (!made.ok) throw new Error(String(made.error)); |
| 125 |
const first = await commitToBranch(app, alice, made.value, { |
| 126 |
message: "Start", |
| 127 |
files: [{ path: "README.md", content: `# ${name}\n` }], |
| 128 |
}); |
| 129 |
if (!first.ok) throw new Error(JSON.stringify(first.error)); |
| 130 |
for (const who of ["mason", "scout", "quill", "smith", "atlas"]) expect(addToCrew(app, alice, made.value, who).ok).toBe(true); |
| 131 |
const on = await updateRepoSettings(app, alice, made.value, { |
| 132 |
autonomy: "autonomous", |
| 133 |
autonomyDirection: "Keep it tidy.", |
| 134 |
autonomyCeilingUsd: 10, |
| 135 |
autonomySlots: 5, |
| 136 |
autonomyBy: alice.id, |
| 137 |
}); |
| 138 |
expect(on.ok).toBe(true); |
| 139 |
return getRepoById(app.db, made.value.id)!; |
| 140 |
} |
| 141 |
|
| 142 |
const openIn = (r: Repo, title: string): Issue => { |
| 143 |
const made = openIssue(app, alice, r, { title }); |
| 144 |
if (!made.ok) throw new Error(String(made.error)); |
| 145 |
return made.value; |
| 146 |
}; |
| 147 |
|
| 148 |
const label = (r: Repo, issue: Issue, name: string): void => { |
| 149 |
let found = listLabels(app.db, r.id).find((l) => l.name === name); |
| 150 |
if (found === undefined) { |
| 151 |
const made = createLabel(app.db, r.id, name, "aabbcc"); |
| 152 |
if (!made.ok) throw new Error(made.error); |
| 153 |
found = made.value; |
| 154 |
} |
| 155 |
setIssueLabels(app.db, issue.id, [found.id]); |
| 156 |
}; |
| 157 |
|
| 158 |
test("the share, not another reason, holds the small issues: 0 starts none, 100 starts one", async () => { |
| 159 |
const r = await fresh("share-edges"); |
| 160 |
const small = openIn(r, "A test pins the sort"); |
| 161 |
expect(changeCapacity(app, alice, "slots.smallShare", "0").ok).toBe(true); |
| 162 |
try { |
| 163 |
expect(await driveRepo(app, getRepoById(app.db, r.id)!)).not.toBe("picked"); |
| 164 |
expect(getIssueById(app.db, small.id)!.assigneeId).toBeNull(); |
| 165 |
expect(changeCapacity(app, alice, "slots.smallShare", "100").ok).toBe(true); |
| 166 |
expect(await driveRepo(app, getRepoById(app.db, r.id)!)).toBe("picked"); |
| 167 |
expect(getIssueById(app.db, small.id)!.assigneeId).not.toBeNull(); |
| 168 |
} finally { |
| 169 |
changeCapacity(app, alice, "slots.smallShare", null); |
| 170 |
} |
| 171 |
}); |
| 172 |
|
| 173 |
test("the cap is full of running small issues, and lifting it starts the next", async () => { |
| 174 |
const r = await fresh("cap-cause"); |
| 175 |
for (const who of ["mason", "scout"]) { |
| 176 |
const user = getUserByName(app.db, who)!; |
| 177 |
const issue = openIn(r, `A test pins the ${who}`); |
| 178 |
expect(assignIssue(app, alice, getRepoById(app.db, r.id)!, issue, user).ok).toBe(true); |
| 179 |
createRun(app.db, { repoId: r.id, kind: "agent", actorId: user.id, issueId: issue.id, reason: "assigned" }); |
| 180 |
} |
| 181 |
const third = openIn(r, "A test pins the export"); |
| 182 |
expect(await driveRepo(app, getRepoById(app.db, r.id)!)).not.toBe("picked"); |
| 183 |
expect(getIssueById(app.db, third.id)!.assigneeId).toBeNull(); |
| 184 |
expect(changeCapacity(app, alice, "slots.smallShare", "100").ok).toBe(true); |
| 185 |
try { |
| 186 |
expect(await driveRepo(app, getRepoById(app.db, r.id)!)).toBe("picked"); |
| 187 |
expect(getIssueById(app.db, third.id)!.assigneeId).not.toBeNull(); |
| 188 |
} finally { |
| 189 |
changeCapacity(app, alice, "slots.smallShare", null); |
| 190 |
} |
| 191 |
}); |
| 192 |
|
| 193 |
test("an issue under a P0 pillar starts before an older plain one", async () => { |
| 194 |
const r = await fresh("pillar-first"); |
| 195 |
const umbrella = openIn(r, "P0 Trust: keep the notes safe"); |
| 196 |
const plain = openIn(r, "Rename the export button"); |
| 197 |
const served = openIn(r, "Escape the note titles"); |
| 198 |
expect(addDependency(app.db, umbrella, served, alice.id).ok).toBe(true); |
| 199 |
expect(await driveRepo(app, getRepoById(app.db, r.id)!)).toBe("picked"); |
| 200 |
expect(getIssueById(app.db, served.id)!.assigneeId).not.toBeNull(); |
| 201 |
expect(getIssueById(app.db, plain.id)!.assigneeId).toBeNull(); |
| 202 |
}); |
| 203 |
|
| 204 |
test("an issue under an epic by label starts first, and a tests-only label counts as small", async () => { |
| 205 |
const r = await fresh("label-paths"); |
| 206 |
const epic = openIn(r, "Search for the notes"); |
| 207 |
label(r, epic, "epic"); |
| 208 |
const under = openIn(r, "Index the notes"); |
| 209 |
expect(addDependency(app.db, epic, under, alice.id).ok).toBe(true); |
| 210 |
const plain = openIn(r, "Rename the export button"); |
| 211 |
expect(await driveRepo(app, getRepoById(app.db, r.id)!)).toBe("picked"); |
| 212 |
expect(getIssueById(app.db, under.id)!.assigneeId).not.toBeNull(); |
| 213 |
expect(getIssueById(app.db, plain.id)!.assigneeId).toBeNull(); |
| 214 |
|
| 215 |
}); |
| 216 |
|
| 217 |
test("an issue titled Epic… with no label is not an epic: what waits for it is not roadmap", () => { |
| 218 |
const r = getRepoById(app.db, repo.id)!; |
| 219 |
const epic = open("Epic: roadmap by title only"); |
| 220 |
const under = open("Index by title"); |
| 221 |
expect(addDependency(app.db, epic, under, alice.id).ok).toBe(true); |
| 222 |
expect(sizeIssues(app.db, r, [under]).roadmap.has(under.id)).toBe(false); |
| 223 |
label(repo, epic, "epic"); |
| 224 |
expect(sizeIssues(app.db, r, [under]).roadmap.has(under.id)).toBe(true); |
| 225 |
}); |
| 226 |
|
| 227 |
|
| 228 |
async function labelledCapFull(name: string): Promise<{ r: Repo; ready: Issue }> { |
| 229 |
const r = await fresh(name); |
| 230 |
for (const who of ["mason", "scout"]) { |
| 231 |
const user = getUserByName(app.db, who)!; |
| 232 |
const issue = openIn(r, `Pin the ${who} order`); |
| 233 |
label(r, issue, "tests-only"); |
| 234 |
expect(assignIssue(app, alice, getRepoById(app.db, r.id)!, issue, user).ok).toBe(true); |
| 235 |
createRun(app.db, { repoId: r.id, kind: "agent", actorId: user.id, issueId: issue.id, reason: "assigned" }); |
| 236 |
} |
| 237 |
const ready = openIn(r, "Pin the export order"); |
| 238 |
label(r, ready, "tests-only"); |
| 239 |
return { r, ready }; |
| 240 |
} |
| 241 |
|
| 242 |
test("small only by label: running ones fill the cap and a ready one waits, until the share is 100", async () => { |
| 243 |
const { r, ready } = await labelledCapFull("label-cap"); |
| 244 |
expect(await driveRepo(app, getRepoById(app.db, r.id)!)).not.toBe("picked"); |
| 245 |
expect(getIssueById(app.db, ready.id)!.assigneeId).toBeNull(); |
| 246 |
expect(changeCapacity(app, alice, "slots.smallShare", "100").ok).toBe(true); |
| 247 |
try { |
| 248 |
expect(await driveRepo(app, getRepoById(app.db, r.id)!)).toBe("picked"); |
| 249 |
expect(getIssueById(app.db, ready.id)!.assigneeId).not.toBeNull(); |
| 250 |
} finally { |
| 251 |
changeCapacity(app, alice, "slots.smallShare", null); |
| 252 |
} |
| 253 |
}); |
| 254 |
|
| 255 |
test("a plain ready issue still starts when labelled small ones fill the cap", async () => { |
| 256 |
const { r, ready } = await labelledCapFull("label-cap-plain"); |
| 257 |
const plain = openIn(r, "Rename the export button"); |
| 258 |
expect(await driveRepo(app, getRepoById(app.db, r.id)!)).toBe("picked"); |
| 259 |
expect(getIssueById(app.db, plain.id)!.assigneeId).not.toBeNull(); |
| 260 |
expect(getIssueById(app.db, ready.id)!.assigneeId).toBeNull(); |
| 261 |
}); |
| 262 |
|
| 263 |
test("the tick line carries the small share as running/cap", async () => { |
| 264 |
const r = await fresh("tick-line"); |
| 265 |
const small = openIn(r, "A test pins the tick"); |
| 266 |
expect(assignIssue(app, alice, getRepoById(app.db, r.id)!, small, getUserByName(app.db, "mason")!).ok).toBe(true); |
| 267 |
createRun(app.db, { repoId: r.id, kind: "agent", actorId: getUserByName(app.db, "mason")!.id, issueId: small.id, reason: "assigned" }); |
| 268 |
const lines: string[] = []; |
| 269 |
const spy = spyOn(console, "info").mockImplementation((line: unknown) => { |
| 270 |
lines.push(String(line)); |
| 271 |
}); |
| 272 |
try { |
| 273 |
await autonomyTick(app, new Date(), async () => 0); |
| 274 |
} finally { |
| 275 |
spy.mockRestore(); |
| 276 |
} |
| 277 |
const mine = lines.filter((l) => l.includes("[autonomy] tick repo=alice/tick-line")); |
| 278 |
expect(mine.length).toBe(1); |
| 279 |
expect(mine[0]).toContain(" small=1/2"); |
| 280 |
}); |
| 281 |
|
| 282 |
test("slots.smallShare is a capacity setting an agent can change through the MCP tool", () => { |
| 283 |
const enumerated = (capacityTool.inputSchema as { properties: { key: { enum: string[] } } }).properties.key.enum; |
| 284 |
expect(enumerated).toContain("slots.smallShare"); |
| 285 |
expect(smallShare(app.db)).toBe(30); |
| 286 |
expect(changeCapacity(app, alice, "slots.smallShare", "50").ok).toBe(true); |
| 287 |
expect(smallShare(app.db)).toBe(50); |
| 288 |
const bad = changeCapacity(app, alice, "slots.smallShare", "101"); |
| 289 |
expect(bad.ok).toBe(false); |
| 290 |
expect(smallShare(app.db)).toBe(50); |
| 291 |
expect(changeCapacity(app, alice, "slots.smallShare", null).ok).toBe(true); |
| 292 |
expect(smallShare(app.db)).toBe(30); |
| 293 |
}); |
Says the tick line reads
small=1/2and /pipeline names the share; neither matches the code. Fix the words or the code.