// PEP-207: chats where it is clear what is wanted from the person and it can be done in place. // One status — one truth (an agent can't reopen an accepted story), «N нов.» counts what asks for // attention, the chat with the analyst gets a status line instead of service messages, the // «готово» message after «Принять» asks for nothing. import assert from "node:assert/strict"; import { after, describe, test } from "node:test"; import { core, db, project, runner, setDefaultOwner, teardown } from "./helpers.ts"; import { chatMessages } from "@peppa/db"; import { eq } from "drizzle-orm"; process.env.PEPPA_SECRET ??= "mock-secret-for-tests"; after(teardown); const taskOf = async (key: string) => (await core.resolveTask(db, key)).task; describe("Один статус — одна правда", () => { test("агент не возвращает принятую задачу в ревью; после «Принять» тред не в «Ждут вас», просить принять нечего", async () => { const tp = await project({ turns: [[{ mcp: "update_task", args: { task: "$TASK", status: "review" } }, { say: "Готово." }]] }); const ref = await core.resolveTask(db, tp.taskKey); await core.updateTask(db, ref, { status: "review" }); // The PM's «Принять»: the «готово» message is answered, the story is done. const q = await core.askPm(db, ref, { kind: "attention", msgType: "done", text: `✅ Задача ${tp.taskKey} готова к ревью` }); await runner.acceptTask(db, q.id, "web"); assert.equal((await taskOf(tp.taskKey)).status, "done"); assert.ok((await core.getQuestion(db, q.id))?.answeredAt, "the request to accept is closed"); await assert.rejects(runner.acceptTask(db, q.id, "web"), /Уже отвечено/, "a second press does nothing"); // The agent (PEP-204 on 17.11 — a console call of its tool) tries to move it back: refused. await tp.start(); const refused = await tp.waitLog("refused reopen", (e) => e.ev === "mcp" && e.tool === "update_task" && e.isError === true); assert.match(String(refused.text), /already accepted and closed/); assert.equal((await taskOf(tp.taskKey)).status, "done", "the story stays done"); assert.equal(core.threadGroup({ status: "done", open: 0 }), "done", "an accepted story's thread is not «Ждут вас»"); }); }); describe("Метка «N нов.»", () => { test("считает вопросы и сообщения человеку, не события фабрики", async () => { const owner = await core.upsertUserFromGitea(db, { id: 207_001, login: "mock-pm-207" }, { access_token: "x" }); setDefaultOwner(owner.id); const tp = await project({ turns: [] }); setDefaultOwner(null); const p = await core.getProject(db, tp.key); const ref = await core.resolveTask(db, tp.taskKey); const chat = await core.ensureProjectChat(db, p.id); const thread = `task:${ref.task.id}`; await core.addChatMessage(db, { chatId: chat.id, threadKey: thread, kind: "event", authorKind: "system", text: "📍 событие до первого взгляда" }); // First look: nothing earlier is «new». await core.projectThreadList(db, owner, chat, {}); for (const text of ["▶️ Агент начал работу", "📈 Агент закончил шаг", "⏹ Агент остановлен"]) await core.addChatMessage(db, { chatId: chat.id, threadKey: thread, kind: "event", authorKind: "system", text }); await core.addChatMessage(db, { chatId: chat.id, threadKey: thread, kind: "user", authorKind: "agent", authorLogin: "Аналитик", text: "Уточните, пожалуйста, цвет кнопки." }); const list = await core.projectThreadList(db, owner, chat, {}); const row = [...list.waiting, ...list.working, ...list.done].find((r) => r.key === thread)!; assert.equal(row.unread, 1, "three events don't count, the message to the person does"); assert.ok((await db.select().from(chatMessages).where(eq(chatMessages.chatId, chat.id))).length >= 5); }); }); describe("Строка состояния в чате с аналитиком", () => { test("делается N, задерживаются на нашей стороне M — по задачам в работе", async () => { const tp = await project({ turns: [] }); const p = await core.getProject(db, tp.key); const ref = await core.resolveTask(db, tp.taskKey); await core.updateTask(db, ref, { status: "in_progress" }); assert.deepEqual((await core.chatStatusLine(db, p.id)).working.map((r) => r.key), [tp.taskKey]); await core.platformDelay(db, { projectId: p.id, taskId: ref.task.id }, { reason: "stuck", state: "idle", details: "mock" }); const s = await core.chatStatusLine(db, p.id); assert.deepEqual(s.delayed.map((r) => r.key), [tp.taskKey]); assert.equal(s.working.length, 0); }); }); describe("«Задача описана. Начать работу?»", () => { test("слова вопроса и кнопок", () => { assert.equal(runner.PICK_TAKE, "Начать"); assert.equal(runner.PICK_LATER, "Позже"); }); });