// PEP-48: every event records who did it; the journal lists them with filters. import assert from "node:assert/strict"; import { after, describe, test } from "node:test"; import { oauthClients } from "@peppa/db"; import { core, db, runner, teardown } from "./helpers.ts"; after(teardown); let seq = 0; async function newTask() { const key = `J${String.fromCharCode(65 + seq++)}`; const p = await core.createProject(db, { slug: key.toLowerCase(), key, name: key, giteaOwner: "test", workspacePath: `/tmp/${key}` }); return core.createTask(db, { projectId: p.id, title: `story ${key}` }); } const user = (login: string) => core.createLocalUser(db, { login, password: "correct-horse-battery" }); const last = async (taskId: number, type: string) => (await core.listJournal(db, { taskId })).find((e) => e.type === type)!; describe("Автор каждого события", () => { test("Карточку подвинул участник через веб", async () => { const anna = await user("anna"); const ref = await newTask(); await core.updateTask(db, ref, { status: "in_progress" }); const fresh = await core.resolveTask(db, ref.key); await core.runAsActor({ kind: "user", userId: anna.id, channel: "web" }, () => runner.moveTask(db, fresh, "backlog")); const e = await last(ref.task.id, "task_updated"); assert.deepEqual([e.actorKind, e.actorLogin, e.actorChannel], ["user", "anna", "web"]); assert.equal(core.actorLabel(e), "anna · веб"); }); test("Ответ через удалённый MCP", async () => { const pm = await user("pm"); await db.insert(oauthClients).values({ id: "client-chatgpt", name: "ChatGPT", redirectUris: ["https://chat.example/cb"] }); const ref = await newTask(); const q = await core.askPm(db, ref, { kind: "question", text: "Postgres или SQLite?" }); await core.runAsActor({ kind: "user", userId: pm.id, channel: "mcp", clientId: "client-chatgpt" }, () => runner.answerAndDeliver(db, q.id, "SQLite", "web")); const e = await last(ref.task.id, "question_answered"); assert.equal(core.actorLabel(e), "pm · MCP (ChatGPT)"); assert.equal((e.payload as { answer: string }).answer, "SQLite"); }); test("Ответ в терминале и закрытие самой FORGE", async () => { const ref = await newTask(); const q1 = await core.askPm(db, ref, { kind: "question", text: "a" }); const q2 = await core.askPm(db, ref, { kind: "question", text: "b" }); await core.answerQuestion(db, q1.id, "ok", "terminal"); await core.answerQuestion(db, q2.id, "(неактуально)", "system"); const answers = (await core.listJournal(db, { taskId: ref.task.id })).filter((e) => e.type === "question_answered"); assert.equal(answers.length, 1, "system closings are not actions"); assert.equal(core.actorLabel(answers[0]!), "человек · терминал"); }); test("Действие агента", async () => { const ref = await newTask(); const s = await core.createSession(db, { projectId: ref.project.id, taskId: ref.task.id }); await core.reportProgress(db, ref, "сделал 1.1", s.id); const e = await last(ref.task.id, "progress"); assert.equal(e.actorKind, "agent"); assert.equal(core.actorLabel(e), `агент · раннер #${s.id}`); }); test("Без контекста и без сессии — FORGE", async () => { const ref = await newTask(); await core.updateTask(db, ref, { title: "renamed" }); assert.equal(core.actorLabel(await last(ref.task.id, "task_updated")), "FORGE"); }); }); describe("Журнал действий", () => { test("Фильтр по проекту", async () => { const a = await newTask(); const b = await newTask(); await core.reportProgress(db, a, "A"); await core.reportProgress(db, b, "B"); const onlyB = await core.listJournal(db, { projectId: b.project.id }); assert.ok(onlyB.length > 0); assert.ok(onlyB.every((e) => e.projectKey === b.project.key)); assert.deepEqual(await core.listJournal(db, { projectIds: [] }), []); }); test("Фильтр по автору и каналу", async () => { const ref = await newTask(); const u = await user("ops"); await core.runAsActor({ kind: "user", userId: u.id, channel: "telegram" }, () => core.reportProgress(db, ref, "из телеграма")); await core.reportProgress(db, ref, "сам"); const tg = await core.listJournal(db, { projectId: ref.project.id, channel: "telegram" }); assert.deepEqual(tg.map((e) => (e.payload as { text: string }).text), ["из телеграма"]); const sys = await core.listJournal(db, { projectId: ref.project.id, actorKind: "system" }); assert.ok(sys.some((e) => (e.payload as { text?: string }).text === "сам")); }); test("Не суперадмин", async () => { const u = await user("guest"); await core.setPlatformAdmins(db, ["root"]); assert.equal(await core.isSuperadmin(db, u), false); }); });