CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | /**
* Tests for `src/lib/pr-slash-commands.ts`.
*
* Two layers (matching the project convention):
* 1. Pure helpers — `parseSlashCommand`, marker detection,
* `parseMergeStrategy`, `/help` rendering. Run unconditionally.
* 2. End-to-end with a real DB row + bare repo. Gated on
* `DATABASE_URL` via `HAS_DB` skipIf, matching ai-ci-healer.test.ts.
*
* All AI calls are stubbed via the `deps.anthropic` injection point.
* The merge call is stubbed via `deps.merge` to avoid touching real
* git refs (the executor's job is to format the result, not to re-test
* pr-merge.ts).
*/
import { describe, it, expect, beforeAll, afterAll } from "bun:test";
import { join } from "path";
import { mkdir, rm } from "fs/promises";
import {
parseSlashCommand,
executeSlashCommand,
detectSlashCmdComment,
stripSlashCmdMarker,
slashCmdMarker,
SLASH_COMMANDS,
__test,
} from "../lib/pr-slash-commands";
import { db } from "../db";
import {
pullRequests,
repositories,
users,
workflows,
} from "../db/schema";
import { initBareRepo, createOrUpdateFileOnBranch } from "../git/repository";
const HAS_DB = Boolean(process.env.DATABASE_URL);
const TEST_REPOS = join(
import.meta.dir,
"../../.test-repos-pr-slash-" + Date.now()
);
beforeAll(async () => {
process.env.GIT_REPOS_PATH = TEST_REPOS;
await rm(TEST_REPOS, { recursive: true, force: true });
await mkdir(TEST_REPOS, { recursive: true });
});
afterAll(async () => {
await rm(TEST_REPOS, { recursive: true, force: true });
});
// ---------------------------------------------------------------------------
// 1. Pure parser surface — runs without a DB
// ---------------------------------------------------------------------------
describe("parseSlashCommand", () => {
it("returns null for empty input", () => {
expect(parseSlashCommand("")).toBeNull();
expect(parseSlashCommand(" ")).toBeNull();
});
it("returns null for free-form text", () => {
expect(parseSlashCommand("hey team, take a look")).toBeNull();
expect(parseSlashCommand("hey /merge")).toBeNull(); // must start at col 0
});
it("returns null for Unix-path-like comments that start with /", () => {
expect(parseSlashCommand("/usr/local/bin/foo")).toBeNull();
expect(parseSlashCommand("/etc/passwd is interesting")).toBeNull();
});
it("recognises every command without args", () => {
for (const cmd of SLASH_COMMANDS) {
const parsed = parseSlashCommand(`/${cmd}`);
expect(parsed).not.toBeNull();
expect(parsed!.command).toBe(cmd);
expect(parsed!.args).toEqual([]);
}
});
it("parses /merge with a strategy arg", () => {
expect(parseSlashCommand("/merge squash")).toEqual({
command: "merge",
args: ["squash"],
raw: "merge squash",
});
expect(parseSlashCommand("/merge rebase")).toEqual({
command: "merge",
args: ["rebase"],
raw: "merge rebase",
});
expect(parseSlashCommand("/merge merge")).toEqual({
command: "merge",
args: ["merge"],
raw: "merge merge",
});
});
it("parses /cc with multiple @users", () => {
const p = parseSlashCommand("/cc @alice @bob @carol");
expect(p).not.toBeNull();
expect(p!.command).toBe("cc");
expect(p!.args).toEqual(["@alice", "@bob", "@carol"]);
});
it("only inspects the first non-blank line", () => {
const p = parseSlashCommand("/needs-work\nplease tighten the loop body");
expect(p).not.toBeNull();
expect(p!.command).toBe("needs-work");
expect(p!.args).toEqual([]);
});
it("normalises trailing punctuation", () => {
expect(parseSlashCommand("/help.")!.command).toBe("help");
expect(parseSlashCommand("/HELP")!.command).toBe("help");
});
it("ignores a leading space after the slash", () => {
expect(parseSlashCommand("/ merge")).toBeNull();
});
});
describe("detectSlashCmdComment / stripSlashCmdMarker", () => {
it("detects each command marker", () => {
for (const cmd of SLASH_COMMANDS) {
const body = `${slashCmdMarker(cmd)}\n\nbody text`;
expect(detectSlashCmdComment(body)).toBe(cmd);
expect(stripSlashCmdMarker(body)).toBe("body text");
}
});
it("returns null for normal comments", () => {
expect(detectSlashCmdComment("just a plain comment")).toBeNull();
expect(detectSlashCmdComment("<!-- something else -->")).toBeNull();
});
});
describe("__test.parseMergeStrategy", () => {
it("defaults to merge when the arg is missing or junk", () => {
expect(__test.parseMergeStrategy(undefined)).toBe("merge");
expect(__test.parseMergeStrategy("")).toBe("merge");
expect(__test.parseMergeStrategy("rocket")).toBe("merge");
});
it("accepts the three documented strategies", () => {
expect(__test.parseMergeStrategy("squash")).toBe("squash");
expect(__test.parseMergeStrategy("rebase")).toBe("rebase");
expect(__test.parseMergeStrategy("merge")).toBe("merge");
expect(__test.parseMergeStrategy("SQUASH")).toBe("squash");
});
});
// ---------------------------------------------------------------------------
// 2. Executor — /help runs without any deps
// ---------------------------------------------------------------------------
describe("executeSlashCommand — /help", () => {
it("renders the full command list with the marker", async () => {
const result = await executeSlashCommand({
command: "help",
args: [],
prId: "00000000-0000-0000-0000-000000000000",
userId: "00000000-0000-0000-0000-000000000000",
repositoryId: "00000000-0000-0000-0000-000000000000",
});
expect(result.ok).toBe(true);
expect(result.marker).toBe(slashCmdMarker("help"));
expect(result.body).toContain(slashCmdMarker("help"));
// Every recognised command is documented in the help output.
for (const cmd of SLASH_COMMANDS) {
expect(result.body).toContain(`/${cmd}`);
}
});
});
describe("executeSlashCommand — unrecognised input never throws", () => {
it("does not throw when no DB is available", async () => {
// parseSlashCommand would normally reject this; we call execute
// directly to make sure the route-side defence-in-depth is fine.
const result = await executeSlashCommand({
// @ts-expect-error — exercising the default-case branch
command: "does-not-exist",
args: [],
prId: "00000000-0000-0000-0000-000000000000",
userId: "00000000-0000-0000-0000-000000000000",
repositoryId: "00000000-0000-0000-0000-000000000000",
});
expect(result.ok).toBe(false);
expect(result.body).toContain("Unrecognised slash command");
});
});
// ---------------------------------------------------------------------------
// 3. DB-backed flows — /explain (mock Claude), /merge (mock performMerge)
// ---------------------------------------------------------------------------
function fakeAnthropic(responseText: string) {
return {
messages: {
create: async () => ({
id: "msg_test",
type: "message" as const,
role: "assistant" as const,
model: "claude-sonnet-4-test",
stop_reason: "end_turn" as const,
stop_sequence: null,
usage: { input_tokens: 0, output_tokens: 0 },
content: [{ type: "text" as const, text: responseText }],
}),
},
} as any;
}
interface SlashFixture {
userId: string;
repoId: string;
prId: string;
ownerUsername: string;
repoName: string;
}
async function seedFixture(label: string): Promise<SlashFixture> {
const username = `slash_${label}_${Date.now()}_${Math.random()
.toString(36)
.slice(2, 6)}`;
const [u] = await db
.insert(users)
.values({
username,
email: `${username}@example.com`,
passwordHash: "x",
})
.returning({ id: users.id });
const repoName = `subject_${label}_${Date.now()}`;
const [r] = await db
.insert(repositories)
.values({
ownerId: u.id,
name: repoName,
diskPath: `/tmp/${username}/${repoName}`,
defaultBranch: "main",
})
.returning({ id: repositories.id });
await initBareRepo(username, repoName);
// Seed a base commit on main + a head commit on feat-x so the diff
// /explain consumes is non-empty.
await createOrUpdateFileOnBranch({
owner: username,
name: repoName,
branch: "main",
filePath: "src/index.ts",
bytes: new TextEncoder().encode("export const v = 1;\n"),
message: "base",
authorName: "Seeder",
authorEmail: "s@e.com",
});
await createOrUpdateFileOnBranch({
owner: username,
name: repoName,
branch: "feat-x",
filePath: "src/index.ts",
bytes: new TextEncoder().encode("export const v = 2;\n"),
message: "feat: bump v",
authorName: "Seeder",
authorEmail: "s@e.com",
});
const [pr] = await db
.insert(pullRequests)
.values({
repositoryId: r.id,
number: 1,
title: "Bump v",
body: "Bumps the version constant.",
authorId: u.id,
baseBranch: "main",
headBranch: "feat-x",
state: "open",
})
.returning({ id: pullRequests.id });
return {
userId: u.id,
repoId: r.id,
prId: pr.id,
ownerUsername: username,
repoName,
};
}
describe.skipIf(!HAS_DB)("executeSlashCommand — /explain (DB-backed)", () => {
it("calls Claude and returns its explanation in the result body", async () => {
const fx = await seedFixture("explain");
const result = await executeSlashCommand({
command: "explain",
args: [],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
deps: { anthropic: fakeAnthropic("This PR bumps `v` from 1 to 2.") },
});
expect(result.ok).toBe(true);
expect(result.body).toContain(slashCmdMarker("explain"));
expect(result.body).toContain("This PR bumps `v` from 1 to 2.");
}, 15_000);
});
describe.skipIf(!HAS_DB)("executeSlashCommand — /merge (DB-backed)", () => {
it("forwards the requested strategy and reports the merge result", async () => {
const fx = await seedFixture("merge");
const observed: { actor: string | null } = { actor: null };
const fakeMerge = async (mergeArgs: any) => {
observed.actor = mergeArgs.actorUserId;
expect(mergeArgs.pr.id).toBe(fx.prId);
expect(mergeArgs.pr.headBranch).toBe("feat-x");
expect(mergeArgs.pr.baseBranch).toBe("main");
return {
ok: true as const,
closedIssueNumbers: [42],
resolvedFiles: [],
};
};
const result = await executeSlashCommand({
command: "merge",
args: ["squash"],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
deps: {
merge: fakeMerge as any,
// Repo owner = userId, so resolveAccess would return "owner"
// anyway; pin it for determinism + DB isolation.
resolveAccess: async () => "owner",
},
});
expect(result.ok).toBe(true);
expect(observed.actor).toBe(fx.userId);
expect(result.body).toContain(slashCmdMarker("merge"));
expect(result.body).toContain("Merged");
expect(result.body).toContain("/merge squash");
expect(result.body).toContain("#42");
});
it("refuses to merge when the actor lacks write access", async () => {
const fx = await seedFixture("merge-noauth");
const result = await executeSlashCommand({
command: "merge",
args: [],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
deps: {
merge: async () => {
throw new Error("should not be called");
},
resolveAccess: async () => "read",
},
});
expect(result.ok).toBe(false);
expect(result.body).toContain("denied");
});
it("surfaces the underlying performMerge error verbatim", async () => {
const fx = await seedFixture("merge-fail");
const result = await executeSlashCommand({
command: "merge",
args: [],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
deps: {
merge: async () => ({
ok: false as const,
error: "git update-ref failed: not-fast-forward",
closedIssueNumbers: [],
resolvedFiles: [],
}),
resolveAccess: async () => "owner",
},
});
expect(result.ok).toBe(false);
expect(result.body).toContain("not-fast-forward");
});
});
describe.skipIf(!HAS_DB)("executeSlashCommand — /lgtm + /needs-work (DB-backed)", () => {
it("posts an approval-style body for /lgtm", async () => {
const fx = await seedFixture("lgtm");
const result = await executeSlashCommand({
command: "lgtm",
args: [],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
});
expect(result.ok).toBe(true);
expect(result.body.toLowerCase()).toContain("approved");
});
it("captures the reason given to /needs-work", async () => {
const fx = await seedFixture("nw");
const result = await executeSlashCommand({
command: "needs-work",
args: ["please", "tighten", "the", "loop"],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
});
expect(result.ok).toBe(true);
expect(result.body).toContain("please tighten the loop");
});
});
describe.skipIf(!HAS_DB)("executeSlashCommand — /cc (DB-backed)", () => {
it("resolves known users and flags unknowns", async () => {
const fx = await seedFixture("cc");
const knownName = `slash_cc_known_${Date.now()}`;
await db
.insert(users)
.values({
username: knownName,
email: `${knownName}@example.com`,
passwordHash: "x",
});
const result = await executeSlashCommand({
command: "cc",
args: [`@${knownName}`, "@nobody-knows-this"],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
});
expect(result.ok).toBe(true);
expect(result.body).toContain(knownName);
expect(result.body).toContain("Skipped");
});
it("rejects when no @users are supplied", async () => {
const fx = await seedFixture("cc-empty");
const result = await executeSlashCommand({
command: "cc",
args: [],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
});
expect(result.ok).toBe(false);
expect(result.body).toContain("requires one or more");
});
});
describe.skipIf(!HAS_DB)("executeSlashCommand — /test (DB-backed)", () => {
it("enqueues a workflow_dispatch when a test.yml workflow exists", async () => {
const fx = await seedFixture("test");
const [w] = await db
.insert(workflows)
.values({
repositoryId: fx.repoId,
name: "Tests",
path: ".gluecron/workflows/test.yml",
yaml: "name: Tests\non: [push, workflow_dispatch]\njobs:\n t:\n steps:\n - run: bun test\n",
parsed: JSON.stringify({
name: "Tests",
on: ["push", "workflow_dispatch"],
jobs: { t: { steps: [{ run: "bun test" }] } },
}),
onEvents: JSON.stringify(["push", "workflow_dispatch"]),
})
.returning({ id: workflows.id });
const result = await executeSlashCommand({
command: "test",
args: [],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
});
expect(result.ok).toBe(true);
expect(result.body).toContain("dispatched");
// Sanity: the lookup helper actually finds the row we just inserted.
const found = await __test.findTestWorkflow(fx.repoId);
expect(found?.id).toBe(w.id);
}, 15_000);
it("falls back gracefully when no test workflow is configured", async () => {
const fx = await seedFixture("test-missing");
const result = await executeSlashCommand({
command: "test",
args: [],
prId: fx.prId,
userId: fx.userId,
repositoryId: fx.repoId,
});
expect(result.ok).toBe(false);
expect(result.body).toContain("could not find a test workflow");
});
});
// ---------------------------------------------------------------------------
// 4. Integration smoke — parse → execute round-trip preserves identity
// ---------------------------------------------------------------------------
describe("parse → execute round-trip (no DB needed)", () => {
it("a slash that doesn't pass parseSlashCommand stays a normal comment", () => {
// The route handler stores the comment verbatim and only THEN consults
// parseSlashCommand. This test asserts the contract:
// - "/usr/bin/x" returns null → route does NOT execute anything
// - "/help" returns a parse → route executes
expect(parseSlashCommand("/usr/bin/x")).toBeNull();
expect(parseSlashCommand("hello world")).toBeNull();
expect(parseSlashCommand("/help")).not.toBeNull();
});
});
|