// PEP-284: новые проекты получают FORGE Default Stack в своих инструкциях — из шаблона в Gitea, // а не только из файла в репозитории платформы; свой AGENTS.md подключённого репозитория не трогаем. // Тесты названы по сценариям openspec/changes/pep-284-template-default-stack/specs/engineering-standards. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { cpSync, mkdtempSync, readFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { after, before, describe, test } from "node:test"; import { core, db, runner, teardown } from "./helpers.ts"; import { MockGitea } from "../src/mock-gitea.ts"; const ROOT = resolve(fileURLToPath(import.meta.url), "../../../.."); const TMP = mkdtempSync(join(tmpdir(), "peppa-tpl-stack-")); process.env.PEPPA_SECRET = "mock-secret-for-tests"; process.env.PEPPA_WORKSPACES = join(TMP, "workspaces"); const STANDARD = "docs/standards/default-stack.md"; const read = (p: string) => readFileSync(join(ROOT, p), "utf8"); const gitea = new MockGitea(join(TMP, "gitea")); const ALICE = { id: 284, login: "mock-stack-alice", token: "mock-token-stack-alice" }; const BOT = { id: 285, login: "peppa-bot", token: "mock-token-stack-bot" }; let alice: Awaited>; let templateUrl = ""; before(async () => { await gitea.start(); process.env.GITEA_URL = gitea.url; process.env.GITEA_TOKEN = BOT.token; for (const u of [ALICE, BOT]) gitea.addUser(u); // The template repository in Gitea is out of date, as before a deploy. templateUrl = gitea.addRepo("peppa-bot", "template", { files: { "AGENTS.md": "# old agents\n", "RULES.md": "# old rules\n" } }).path; alice = await core.upsertUserFromGitea(db, ALICE, { access_token: ALICE.token }); }); after(async () => { await gitea.stop(); await teardown(); }); const show = (repo: string, path: string) => execFileSync("git", ["--git-dir", repo, "show", `main:${path}`], { encoding: "utf8" }); const provision = async (o: { slug: string; key: string; existing?: boolean }) => runner.provisionProject(db, { gitea: await core.giteaForUser(db, alice), owner: ALICE.login, ownerIsUser: true, name: `Mock ${o.key}`, ownerId: alice.id, templateUrl, ...o, }); describe("Новый проект получает стандарт стека в своих инструкциях", () => { test("Сценарий: Копия стандарта в шаблоне отстала", () => { assert.equal(read(`template/${STANDARD}`), read(STANDARD), `копия стандарта в шаблоне отличается от ${STANDARD}: скопируйте файл в template/`); }); test("Сценарий: Создание нового проекта", async () => { // The same road as the live system: template/ of the commit → template repo → new project. const dir = join(TMP, "template"); cpSync(join(ROOT, "template"), dir, { recursive: true }); assert.equal((await runner.syncTemplate({ push: true, dir, templateUrl, commit: "pep284abc" })).state, "pushed"); await provision({ slug: "stack-demo", key: "STD" }); const repo = gitea.repos.get(`${ALICE.login}/stack-demo`)!.path; const agents = show(repo, "AGENTS.md"); assert.equal(agents, read("template/AGENTS.md")); const stack = agents.slice(agents.indexOf("## Стек\n"), agents.indexOf("## Проект\n")); assert.ok(stack.length > 20, "в AGENTS.md нового проекта нет раздела «Стек»"); for (const w of ["Go", "Gin", "uptrace/bun", "Viper", "PostgreSQL", "UUIDv7", "Next.js App Router", "TanStack", "Vite", "DDD-Lite", STANDARD, "design.md", "remember"]) { assert.ok(stack.includes(w), `раздел «Стек» без «${w}»`); } assert.match(stack, /не переводи на стандарт сам/); assert.equal(show(repo, STANDARD), read(STANDARD)); }); }); describe("Существующий стек и инструкции проекта важнее стандарта", () => { test("Сценарий: Подключение репозитория со своим AGENTS.md", async () => { const own = "# AGENTS.md\n\nСтек: Python + Django\n"; const r = gitea.addRepo(ALICE.login, "legacy-app", { files: { "AGENTS.md": own } }); r.collaborators["peppa-bot"] = "write"; await provision({ slug: "legacy-app", key: "LEG", existing: true }); const agents = show(r.path, "AGENTS.md"); assert.equal(agents, own); assert.doesNotMatch(agents, /## Стек\n/); }); test("Сценарий: Агент в проекте на другом стеке", () => { const rules = read("template/RULES.md"); const sec = rules.slice(rules.indexOf("## Стек проекта\n"), rules.indexOf("## CI: проверки перед PR\n")); assert.ok(sec.length > 20, "в RULES.md нет раздела «Стек проекта»"); assert.match(sec, /важнее стандарта/); assert.match(sec, /не мигрируй сам/); assert.ok(sec.includes(STANDARD)); assert.match(sec, /design\.md/); assert.match(sec, /не переписывай/); }); });