peppa/packages/testkit/test/metrics.test.ts
Асель 14916079bc PEP-5/43/47/48: usage and limits, story budgets, factory metrics, audit journal
- PEP-5: statusLine hook (cost, context, 5h/7d limit windows), transcript tokens,
  sessions.usage, usage strip over every page, runner usage, bot /status, MCP get_usage
- PEP-43: project/story budgets in settings, watchdog pause (Escape, question to the PM:
  extend x2 / remove limit / stop), start/resume guard, budget on the story card
- PEP-47: project metrics tab (cycle time, first pass, review returns, questions, runner
  hours, PM wait, limits, spend by week/agent/epic), MCP get_metrics
- PEP-48: event authors (actor columns, AsyncLocalStorage context), question_answered,
  /journal for superadmins, project journal tab, authors in the story feed
- Migration 0007_usage_audit; OpenSpec changes archived

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 06:52:13 +05:00

99 lines
5.4 KiB
TypeScript

// PEP-47: factory metrics from status history, questions, sessions and reviews.
import assert from "node:assert/strict";
import { after, describe, test } from "node:test";
import { events, sessions, codeReviews } from "@peppa/db";
import { core, db, teardown } from "./helpers.ts";
after(teardown);
let seq = 0;
async function newProject() {
const key = `M${String.fromCharCode(65 + seq++)}`;
return core.createProject(db, { slug: key.toLowerCase(), key, name: key, giteaOwner: "test", workspacePath: `/tmp/${key}` });
}
const at = (s: string) => new Date(s);
async function move(projectId: number, taskId: number, from: string, to: string, when: string) {
await db.insert(events).values({ projectId, taskId, type: "task_updated", payload: { from: { status: from }, to: { status: to } }, createdAt: at(when) });
}
async function runner(projectId: number, taskId: number | null, provider: "claude" | "codex", start: string, hours: number) {
await db.insert(sessions).values({ projectId, taskId, provider, tmuxName: `m-${Math.random()}`, status: "done", generation: 1, startedAt: at(start), endedAt: new Date(at(start).getTime() + hours * 3_600_000) });
}
// Monday 2026-11-09 … Sunday 2026-11-15; "now" = Saturday 2026-11-14.
const now = at("2026-11-14T12:00:00");
describe("Метрики проекта", () => {
test("Время цикла и первый проход", async () => {
const p = await newProject();
const t1 = (await core.createTask(db, { projectId: p.id, title: "PEP-1" })).task;
const t2 = (await core.createTask(db, { projectId: p.id, title: "PEP-2" })).task;
await move(p.id, t1.id, "backlog", "in_progress", "2026-11-09T10:00:00");
await move(p.id, t1.id, "in_progress", "review", "2026-11-09T18:00:00");
await move(p.id, t1.id, "review", "done", "2026-11-10T10:00:00");
await move(p.id, t2.id, "backlog", "in_progress", "2026-11-09T10:00:00");
await move(p.id, t2.id, "in_progress", "review", "2026-11-10T10:00:00");
await move(p.id, t2.id, "review", "in_progress", "2026-11-10T12:00:00");
await move(p.id, t2.id, "in_progress", "review", "2026-11-11T10:00:00");
await move(p.id, t2.id, "review", "done", "2026-11-12T10:00:00");
const m = await core.projectMetrics(db, p.id, { weeks: 4, now });
assert.equal(m.summary.done, 2);
assert.equal(m.summary.firstPassRate, 0.5);
assert.equal(m.summary.reviewReturnRate, 0.5);
assert.equal(m.summary.cycleHoursMedian, (24 + 72) / 2, "PEP-1: 24 h, PEP-2: 72 h");
assert.equal(m.weekly.length, 4);
assert.equal(m.weekly.at(-1)!.done, 2, "both in this week");
assert.equal(m.weekly[0]!.done, 0);
});
test("Замечания ревьюера — не первый проход", async () => {
const p = await newProject();
const t = (await core.createTask(db, { projectId: p.id, title: "x" })).task;
await move(p.id, t.id, "backlog", "in_progress", "2026-11-09T10:00:00");
await move(p.id, t.id, "in_progress", "done", "2026-11-09T12:00:00");
await db.insert(codeReviews).values({ projectId: p.id, taskId: t.id, prNumber: 1, sha: "abc", state: "done", verdict: "changes" });
assert.equal((await core.projectMetrics(db, p.id, { weeks: 1, now })).summary.firstPassRate, 0);
});
test("Пустой проект", async () => {
const p = await newProject();
const m = await core.projectMetrics(db, p.id, { now });
assert.equal(m.summary.done, 0);
assert.equal(m.summary.cycleHoursMedian, null);
assert.equal(core.fmtPct(m.summary.firstPassRate), "—");
assert.equal(core.fmtHours(m.summary.cycleHoursMedian), "—");
});
test("Вопросы на стори и ожидание PM", async () => {
const p = await newProject();
const t = (await core.createTask(db, { projectId: p.id, title: "q" })).task;
await runner(p.id, t.id, "claude", "2026-11-13T09:00:00", 1);
const [s] = await db.select().from(sessions).where((await import("drizzle-orm")).eq(sessions.taskId, t.id));
const { questions } = await import("@peppa/db");
await db.insert(questions).values([
{ projectId: p.id, taskId: t.id, sessionId: s!.id, kind: "question", text: "?", answer: "да", answeredVia: "web", createdAt: at("2026-11-13T09:10:00"), answeredAt: at("2026-11-13T09:40:00") },
{ projectId: p.id, taskId: t.id, sessionId: s!.id, kind: "attention", text: "ждёт", createdAt: at("2026-11-13T09:50:00") },
]);
const m = await core.projectMetrics(db, p.id, { weeks: 1, now });
assert.equal(m.summary.questions, 1, "notices are not questions");
assert.equal(m.summary.questionsPerStory, 1);
assert.equal(m.summary.pmWaitHours, 0.5);
});
});
describe("Тренд по неделям, агентам и эпикам", () => {
test("Разбивка по агентам", async () => {
const p = await newProject();
const epic = await core.createGoal(db, p.id, "Платежи");
const t = (await core.createTask(db, { projectId: p.id, title: "a", goalId: epic.id })).task;
await runner(p.id, t.id, "claude", "2026-11-12T10:00:00", 2);
await runner(p.id, t.id, "codex", "2026-11-13T10:00:00", 1);
const m = await core.projectMetrics(db, p.id, { weeks: 2, now });
const by = Object.fromEntries(m.byProvider.map((x) => [x.provider, x.hours]));
assert.deepEqual(by, { claude: 2, codex: 1 });
assert.equal(m.summary.runnerHours, 3);
const e = m.byEpic.find((x) => x.goalId === epic.id)!;
assert.equal(e.title, "Платежи");
assert.equal(e.spend.minutes, 180);
});
});