180 lines
8.2 KiB
TypeScript
180 lines
8.2 KiB
TypeScript
// PEP-127, spec task-autonomy «"Принять" при конфликте доводится до конца без PM»:
|
|
// mock-claude runners, mock Gitea.
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { after, before, describe, test } from "node:test";
|
|
import { core, db, project, runner, teardown, waitFor, type TestProject } from "./helpers.ts";
|
|
import { MockGitea } from "../src/mock-gitea.ts";
|
|
import { events } from "@peppa/db";
|
|
import { and, eq } from "drizzle-orm";
|
|
|
|
process.env.PEPPA_SECRET = "mock-secret-for-tests";
|
|
const gitea = new MockGitea(join(mkdtempSync(join(tmpdir(), "peppa-accept-")), "gitea"));
|
|
const BOT = { id: 71, login: "peppa-bot", token: "mock-token-bot" };
|
|
const CHECKS = { typecheck: "pnpm typecheck", test: "pnpm test" };
|
|
|
|
before(async () => {
|
|
await gitea.start();
|
|
process.env.GITEA_URL = gitea.url;
|
|
process.env.GITEA_TOKEN = BOT.token;
|
|
gitea.addUser(BOT);
|
|
});
|
|
after(async () => {
|
|
await gitea.stop();
|
|
await teardown();
|
|
});
|
|
|
|
type Turns = Parameters<typeof project>[0]["turns"];
|
|
|
|
/** A story with an open green PR (task/<KEY>-1) in mock Gitea; `review` puts it in review right away. */
|
|
async function prTask(turns: Turns, review = true) {
|
|
const tp: TestProject = await project({ turns });
|
|
const proj = await core.getProject(db, tp.key);
|
|
const pj = JSON.stringify({ slug: proj.slug, key: tp.key, name: tp.key, peppaUrl: "http://test", checks: CHECKS });
|
|
writeFileSync(join(tp.dir, ".peppa/project.json"), pj);
|
|
gitea.addRepo(proj.giteaOwner, proj.giteaRepo, { files: { "README.md": "x\n", ".peppa/project.json": pj } });
|
|
gitea.repo(proj.giteaOwner, proj.giteaRepo).collaborators[BOT.login] = "write";
|
|
let n = 0;
|
|
const push = () => gitea.commit(proj.giteaOwner, proj.giteaRepo, `task/${tp.taskKey}`, { [`f${n++}.txt`]: String(n) });
|
|
const green = (s: string) => {
|
|
for (const c of Object.keys(CHECKS)) gitea.setStatus(proj.giteaOwner, proj.giteaRepo, s, `peppa/${c}`, "success");
|
|
};
|
|
green(push());
|
|
const pr = gitea.openPull(proj.giteaOwner, proj.giteaRepo, `task/${tp.taskKey}`);
|
|
const prUrl = `${gitea.url}/${proj.giteaOwner}/${proj.giteaRepo}/pulls/${pr.number}`;
|
|
const ref = () => core.resolveTask(db, tp.taskKey);
|
|
if (review) await core.updateTask(db, await ref(), { status: "review", prUrl });
|
|
const pull = () => gitea.repo(proj.giteaOwner, proj.giteaRepo).pulls[pr.number - 1]!;
|
|
const status = async () => (await ref()).task.status;
|
|
const toReview = async () => core.updateTask(db, await ref(), { status: "review" });
|
|
const stopAll = async () => {
|
|
for (const s of (await core.listSessions(db, { active: true })).filter((s) => s.projectId === proj.id)) await runner.stopSession(db, s.id);
|
|
};
|
|
const taskEvents = async (type: string) => db.select().from(events).where(and(eq(events.taskId, (await ref()).task.id), eq(events.type, type)));
|
|
return { tp, proj, prUrl, push, green, ref, pull, status, toReview, stopAll, taskEvents };
|
|
}
|
|
|
|
const tick = (now = new Date()) => runner.pendingAcceptTick(db, { now });
|
|
const failures = async (projectId: number) => (await core.pendingQuestions(db, projectId)).filter((q) => q.text.includes("не получилось довести приёмку"));
|
|
|
|
describe("accept over a conflict", () => {
|
|
test("Конфликт при «Принять» — закрывается само", async () => {
|
|
const t = await prTask(
|
|
[
|
|
[{ mcp: "update_task", args: { task: "1", status: "review" } }, { say: "PR открыт." }],
|
|
[{ mcp: "update_task", args: { task: "1", status: "review" } }, { say: "Rebase сделан." }],
|
|
],
|
|
false,
|
|
);
|
|
// The agent's first turn hands the story to review with the PR.
|
|
await core.updateTask(db, await t.ref(), { prUrl: t.prUrl });
|
|
const sid = await t.tp.start();
|
|
const review = await t.tp.waitQuestion(sid, "attention");
|
|
assert.match(review.text, /готова к ревью/);
|
|
|
|
t.pull().mergeable = false;
|
|
const r = await runner.acceptTask(db, review.id, "telegram");
|
|
t.pull().mergeable = true; // the agent "rebases" before its turn reaches update_task
|
|
assert.equal(r.pending, true, "accepted, carried through by Peppa");
|
|
assert.equal((await core.getQuestion(db, review.id)).answer, core.PENDING_ACCEPT_ANSWER);
|
|
assert.ok(await core.getPendingAccept(db, (await t.ref()).task.id));
|
|
const ev = await t.tp.waitLog("rebase prompt", (e) => e.ev === "prompt" && String(e.prompt).includes("rebase"));
|
|
assert.match(String(ev.prompt), /force-with-lease/);
|
|
|
|
// Back in review: no new «готова к ревью» for the PM.
|
|
await waitFor("stop hook saw the accept", async () => (await t.taskEvents("awaiting_accept_merge")).length || undefined);
|
|
assert.equal(await t.status(), "review");
|
|
assert.equal((await core.pendingQuestions(db, t.proj.id)).length, 0, "nothing new to answer");
|
|
assert.equal(t.pull().merged, false);
|
|
|
|
await tick();
|
|
assert.equal(t.pull().merged, true);
|
|
assert.equal(await t.status(), "done");
|
|
assert.equal(await core.getPendingAccept(db, (await t.ref()).task.id), null);
|
|
assert.equal((await t.taskEvents("accept_completed")).length, 1);
|
|
await t.tp.waitStatus(sid, "done");
|
|
});
|
|
|
|
test("Проверки после rebase ещё идут", async () => {
|
|
const t = await prTask([]);
|
|
t.pull().mergeable = false;
|
|
const r = await runner.acceptStory(db, await t.ref(), "web");
|
|
assert.equal(r.pending, true);
|
|
assert.equal(await t.status(), "in_progress", "the agent is rebasing");
|
|
await tick();
|
|
assert.equal(t.pull().merged, false, "not merged while the agent works");
|
|
|
|
const sha = t.push(); // the rebased head, checks not reported yet
|
|
t.pull().mergeable = true;
|
|
await t.toReview();
|
|
await tick();
|
|
assert.equal(t.pull().merged, false);
|
|
assert.equal(await t.status(), "review");
|
|
assert.equal((await failures(t.proj.id)).length, 0);
|
|
|
|
t.green(sha);
|
|
await tick();
|
|
assert.equal(t.pull().merged, true);
|
|
assert.equal(await t.status(), "done");
|
|
});
|
|
|
|
test("Три конфликта подряд", async () => {
|
|
const t = await prTask([]);
|
|
t.pull().mergeable = false;
|
|
await runner.acceptStory(db, await t.ref(), "web");
|
|
for (let i = 2; i <= runner.ACCEPT_MAX_REBASES; i++) {
|
|
await t.toReview();
|
|
await tick();
|
|
assert.equal(await t.status(), "in_progress", `rebase ${i} requested`);
|
|
assert.equal((await core.getPendingAccept(db, (await t.ref()).task.id))?.rebases, i);
|
|
}
|
|
await t.toReview();
|
|
await tick();
|
|
await tick();
|
|
const qs = await failures(t.proj.id);
|
|
assert.equal(qs.length, 1, "one message");
|
|
assert.match(qs[0]!.text, /Нажмите «Принять» ещё раз/);
|
|
assert.equal(await core.getPendingAccept(db, (await t.ref()).task.id), null);
|
|
assert.equal(await t.status(), "review");
|
|
// Nothing merges by itself any more, even when the conflict is gone.
|
|
t.pull().mergeable = true;
|
|
await tick();
|
|
assert.equal(t.pull().merged, false);
|
|
await t.stopAll();
|
|
});
|
|
|
|
test("Срок вышел: одно сообщение PM", async () => {
|
|
const t = await prTask([]);
|
|
t.pull().mergeable = false;
|
|
await runner.acceptStory(db, await t.ref(), "web");
|
|
await tick(new Date(Date.now() + 25 * 3_600_000));
|
|
const qs = await failures(t.proj.id);
|
|
assert.equal(qs.length, 1);
|
|
assert.match(qs[0]!.text, /за 24 ч/);
|
|
await t.stopAll();
|
|
});
|
|
|
|
test("PM забрал стори", async () => {
|
|
const t = await prTask([]);
|
|
t.pull().mergeable = false;
|
|
await runner.acceptStory(db, await t.ref(), "web");
|
|
await runner.moveTask(db, await t.ref(), "backlog");
|
|
await tick();
|
|
assert.equal(await core.getPendingAccept(db, (await t.ref()).task.id), null);
|
|
t.pull().mergeable = true;
|
|
await tick();
|
|
assert.equal(t.pull().merged, false);
|
|
assert.equal(await t.status(), "backlog");
|
|
});
|
|
|
|
test("без конфликта «Принять» работает как раньше", async () => {
|
|
const t = await prTask([]);
|
|
const r = await runner.acceptStory(db, await t.ref(), "web");
|
|
assert.ok(!r.pending);
|
|
assert.equal(t.pull().merged, true);
|
|
assert.equal(await t.status(), "done");
|
|
});
|
|
});
|