// PEP-266: the launch smoke test — every provider with a subscription is started the real way and must commit a file. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { existsSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { after, before, describe, test } from "node:test"; process.env.PEPPA_SECRET ??= "mock-secret-for-tests"; import { core, db, project, runner, setDefaultOwner, superadmin, teardown, waitFor, type TestProject } from "./helpers.ts"; import type { Action } from "../src/scenario.ts"; const { telegramOutbox } = await import("@peppa/db"); const { like } = await import("drizzle-orm"); const { stripAsIs } = await import("@peppa/shared/i18n"); const COMMIT: Action[][] = [[{ commit: { file: "forge-smoke.txt" } }, { say: "Готово" }]]; const HANG: Action[][] = [[{ hang: true }]]; let p: TestProject; const git = (...a: string[]) => execFileSync("git", ["-c", "user.name=t", "-c", "user.email=t@t", ...a], { cwd: p.dir, stdio: "ignore" }); /** The sandbox repo's scenario: Claude commits, Codex does what `codex` says. */ function scenario(codex: Action[][]) { writeFileSync(join(p.dir, ".peppa/mock-scenario.json"), JSON.stringify({ turns: COMMIT, providers: { claude: COMMIT, codex } })); git("add", "-A"); git("commit", "-qm", "scenario", "--allow-empty"); } const smokeMessages = async () => (await db.select().from(telegramOutbox).where(like(telegramOutbox.dedupeKey, "smoke:%"))).map((r) => stripAsIs(String(r.params.text))); /** One run from start to end: Claude's commit is awaited, then the clock jumps past the timeout. */ async function fullRun() { await runner.smokeTick(db); const started = (await core.smokeStatus(db)).current; assert.ok(started, "a run started"); await waitFor("claude committed", async () => { await runner.smokeTick(db); const st = await core.smokeStatus(db); return (st.current ?? st.history[0])?.items.find((i) => i.provider === "claude")?.status === "ok"; }, 30_000); if ((await core.smokeStatus(db)).current) { await waitFor("codex done or past timeout", async () => { await runner.smokeTick(db, { now: new Date(Date.now() + 10 * 60_000) }); return !(await core.smokeStatus(db)).current; }, 30_000); } return (await core.smokeStatus(db)).history[0]!; } before(async () => { const pm = await core.upsertUserFromGitea(db, { id: 3266, login: "mock-pm-3266" }, { access_token: "x" }); setDefaultOwner(pm.id); p = await project({ turns: COMMIT }); git("init", "-q", "-b", "main"); scenario(HANG); const claude = await core.createSubscription(db, { provider: "claude", name: "Claude smoke", authKind: "oauth", secret: "mock-token-smoke" }); const codex = await core.createSubscription(db, { provider: "codex", name: "Codex smoke", authKind: "device" }); for (const s of [claude, codex]) await core.updateSubscription(db, s.id, { status: "active" }); const pool = await core.createPool(db, { name: "Общий пул smoke", subscriptionIds: [claude.id, codex.id] }); await core.setSharedPool(db, pool.id); process.env.PEPPA_SMOKE_PROJECT = p.key; process.env.PEPPA_SMOKE_SETTLE_SEC = "0"; delete process.env.PEPPA_SMOKE; }); after(async () => { process.env.PEPPA_SMOKE = "off"; await teardown(); }); describe("PEP-266: проверка запуска агентов", () => { let sent: () => Promise; test("Выкатка: Claude коммитит, Codex завис — Codex красный, суперадминам одно сообщение", async () => { sent = await superadmin(); void sent; const r = await fullRun(); assert.equal(r.trigger, "deploy"); const by = Object.fromEntries(r.items.map((i) => [i.provider, i])); assert.equal(by.claude.status, "ok"); assert.equal(by.codex.status, "failed"); assert.match(stripAsIs(by.codex.reason ?? ""), /не сделал коммит за 5 мин/); assert.equal(by.opencode.status, "skipped"); assert.match(by.opencode.reason ?? "", /нет подписки/); // The agents are stopped and their worktrees gone; the project's own copy is untouched. for (const i of [by.claude, by.codex]) { assert.equal((await core.getSession(db, i.sessionId!)).status, "done"); assert.ok(i.cwd && i.cwd !== p.dir && !existsSync(i.cwd), `worktree removed: ${i.cwd}`); } assert.ok(!existsSync(join(p.dir, "forge-smoke.txt"))); const msgs = await smokeMessages(); assert.equal(msgs.length, 1, msgs.join("\n")); assert.match(msgs[0]!, /Агенты не запускаются/); assert.match(msgs[0]!, /Codex — агент не сделал коммит/); assert.doesNotMatch(msgs[0]!, /Claude/); }); test("Сразу после проверки новая не начинается (не прошёл час, коммит тот же)", async () => { await runner.smokeTick(db); assert.equal((await core.smokeStatus(db)).current, null); }); test("Повторный провал Codex на той же версии — без нового сообщения", async () => { await core.requestSmoke(db, "mock-admin"); const r = await fullRun(); assert.equal(r.trigger, "manual"); assert.equal(r.items.find((i) => i.provider === "codex")?.status, "failed"); assert.equal((await smokeMessages()).length, 1); }); test("Codex снова коммитит — одно сообщение «снова запускаются»", async () => { scenario(COMMIT); await core.requestSmoke(db, "mock-admin"); const r = await fullRun(); assert.ok(r.items.filter((i) => i.provider !== "opencode").every((i) => i.status === "ok"), JSON.stringify(r.items)); const msgs = await smokeMessages(); assert.equal(msgs.length, 2); assert.match(msgs[1]!, /Агенты снова запускаются: Codex/); assert.equal((await core.smokeStatus(db)).history.length, 3); }); test("Проект проверки на паузе — всё «не проверено», без сообщений", async () => { const proj = await core.getProject(db, p.key); await core.setProjectPause(db, proj.id, { since: Date.now(), by: null, sessions: [] }); await core.requestSmoke(db); await runner.smokeTick(db); const r = (await core.smokeStatus(db)).history[0]!; assert.ok(r.items.every((i) => i.status === "skipped")); assert.equal((await smokeMessages()).length, 2); await core.clearProjectPause(db, proj.id); }); });