peppa/packages/testkit/test/pick-question.test.ts
peppa-bot 39e58da6fe PEP-185: «Взять в работу?» also in auto projects — for stories the dispatcher can't take now
BRM (pick=auto, WIP 1, 8 in work): ready stories waited forever and nobody was asked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 01:48:56 +05:00

95 lines
4.1 KiB
TypeScript

// PEP-185, spec pick-question: a ready backlog story comes as «Взять в работу?».
import assert from "node:assert/strict";
import { after, before, describe, test } from "node:test";
import { core, db, runner, teardown } from "./helpers.ts";
process.env.PEPPA_SECRET = "mock-secret-for-tests";
before(() => {
delete process.env.PEPPA_PICK_QUESTION;
});
after(async () => {
process.env.PEPPA_PICK_QUESTION = "off";
await teardown();
});
let n = 0;
async function proj() {
n++;
return core.createProject(db, { slug: `mock-pick-${n}`, key: `PK${String.fromCharCode(64 + n)}`, name: `Выбор ${n}`, giteaOwner: "peppa-bot", giteaRepo: `mock-pick-${n}`, workspacePath: `/tmp/mock-pick-${n}` });
}
const story = (projectId: number, title: string) => core.createTask(db, { projectId, title });
const open = async (projectId: number) => (await core.pendingQuestions(db, projectId)).filter((q) => q.kind === "question" && q.options?.includes(runner.PICK_TAKE));
describe("PEP-185: «Взять в работу?»", () => {
test("ready story → one question with two options; «Пока нет» is not asked again", async () => {
const p = await proj();
const t = await story(p.id, "Каталог");
await runner.pickQuestionTick(db);
await runner.pickQuestionTick(db);
const qs = await open(p.id);
assert.equal(qs.length, 1);
assert.deepEqual(qs[0]!.options, [runner.PICK_TAKE, runner.PICK_LATER]);
assert.equal(qs[0]!.msgType, "question");
assert.equal(qs[0]!.defaultOption, runner.PICK_TAKE);
await runner.answerAndDeliver(db, qs[0]!.id, runner.PICK_LATER, "web");
await runner.pickQuestionTick(db);
assert.equal((await open(p.id)).length, 0);
assert.equal((await runner.resolveTaskById(db, t.task.id)).task.status, "backlog");
});
test("story moved by the board button → question closed silently", async () => {
const p = await proj();
const t = await story(p.id, "Корзина");
await runner.pickQuestionTick(db);
const [q] = await open(p.id);
assert.ok(q);
await core.updateTask(db, t, { status: "in_progress" });
await runner.pickQuestionTick(db);
assert.equal((await open(p.id)).length, 0);
const closed = await core.getQuestion(db, q.id);
assert.equal(closed.answeredVia, "system");
});
test("5 ready stories → 3 open, the 4th after an answer", async () => {
const p = await proj();
for (let i = 1; i <= 5; i++) await story(p.id, `Задача ${i}`);
await runner.pickQuestionTick(db);
const qs = await open(p.id);
assert.equal(qs.length, 3);
await runner.answerAndDeliver(db, qs[0]!.id, runner.PICK_LATER, "web");
await runner.pickQuestionTick(db);
assert.equal((await open(p.id)).length, 3);
});
test("paused / blocked projects are not asked; auto — only what the dispatcher can't take", async () => {
const paused = await proj();
await story(paused.id, "A");
await core.setSetting(db, `project.paused:${paused.id}`, { since: Date.now(), by: "x", sessions: [] });
await runner.pickQuestionTick(db);
assert.equal((await open(paused.id)).length, 0);
const blocked = await proj();
const first = await story(blocked.id, "Первая");
const second = await story(blocked.id, "Вторая");
await core.addDependency(db, second, first);
await runner.pickQuestionTick(db);
const qs = await open(blocked.id);
assert.equal(qs.length, 1);
assert.equal(qs[0]!.taskId, first.task.id);
assert.notEqual(qs[0]!.taskId, second.task.id);
const auto = await proj();
await core.setAutonomy(db, auto.id, { level: "auto" });
await core.setAutonomy(db, auto.id, { wipLimit: 1 });
await story(auto.id, "B");
await runner.pickQuestionTick(db);
assert.equal((await open(auto.id)).length, 0, "the dispatcher takes the first story itself");
// The WIP limit is reached (one place, one story for it) — the next story waits for a person.
const c = await story(auto.id, "C");
await runner.pickQuestionTick(db);
const aq = await open(auto.id);
assert.equal(aq.length, 1);
assert.equal(aq[0]!.taskId, c.task.id);
});
});