// Shared helpers for the skill-script tests (scripts/test-*script*.mjs). // // The tests run the scripts the way an agent does: as `node /scripts/x.mjs` // from the built skills/ tree, against throwaway fixtures in the OS temp dir, // with the clock pinned by PLAN2CODE_NOW. They live in scripts/ so nothing // here ships. import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { spawnSync, execFileSync } from "node:child_process"; import { fileURLToPath } from "node:url"; export const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url))); export const SKILLS = path.join(ROOT, "skills"); export const NOW = "2026-08-03 14:05"; export { copyFixture, slashed, fixtureCleanup } from "./fixture-helpers.mjs"; // Rebuild skills/ on every import, not only when it is missing: a test file run // directly after an edit to src/ must not test a stale tree. The build is // idempotent and takes over a second, once per test file, so `npm test`, whose // script has just built the tree before `node --test`, skips it: npm sets // npm_lifecycle_event for every script it runs. if (process.env.npm_lifecycle_event !== "test") { execFileSync(process.execPath, [path.join(ROOT, "install.js"), "--build-skills"], { stdio: "ignore" }); } // Env a spawned script or printed git command must not inherit: relocation // vars (GIT_DIR and friends) would redirect every fixture command, a // developer's global git config can perturb printed commands, and stray // SSH_*/PLAN2CODE_* vars change what the scripts report. const AMBIENT_GIT_ENV = [ "GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR", "GIT_OBJECT_DIRECTORY", "GIT_CONFIG", "GIT_CONFIG_GLOBAL", "GIT_CONFIG_SYSTEM", "GIT_CONFIG_NOSYSTEM", "SSH_CONNECTION", "PLAN2CODE_PICKER_ECHO", ]; // GIT_CONFIG_GLOBAL wants a real path: os.devNull is `\\.\nul` here, which git // cannot open. An empty file is the portable "no global config". const EMPTY_GITCONFIG = path.join(os.tmpdir(), "p2c-empty-gitconfig"); try { fs.writeFileSync(EMPTY_GITCONFIG, ""); } catch {} export function scrubbedEnv(extra = {}) { const e = { ...process.env }; for (const k of AMBIENT_GIT_ENV) delete e[k]; return { ...e, GIT_CONFIG_NOSYSTEM: "1", GIT_CONFIG_GLOBAL: EMPTY_GITCONFIG, ...extra }; } // Every git in a test process that imports this file runs under that same // scrubbed config: fixture commands, the modules a test imports and the // scripts it spawns. Mixed configs disagree: a global core.autocrlf=true once // checked fixtures out with CRLF that the scrubbed scripts read as changed. for (const k of AMBIENT_GIT_ENV) delete process.env[k]; Object.assign(process.env, { GIT_CONFIG_NOSYSTEM: "1", GIT_CONFIG_GLOBAL: EMPTY_GITCONFIG }); // Learning's store on this machine (~/.plan2code/learning): a script run from a // test never reaches the real one. A test that checks the store passes its own // PLAN2CODE_LEARNING_DIR in `env`; every other run shares this throwaway one. let sandboxLearningDir; const learningDirFor = (env) => { if (env && env.PLAN2CODE_LEARNING_DIR) return env.PLAN2CODE_LEARNING_DIR; sandboxLearningDir ||= fs.mkdtempSync(path.join(fs.realpathSync.native(os.tmpdir()), "p2c-learning-")); return sandboxLearningDir; }; export function runScript(skill, script, args, { cwd, env, input } = {}) { const r = spawnSync(process.execPath, [path.join(SKILLS, skill, "scripts", script), ...args], { cwd: cwd || ROOT, encoding: "utf8", input, env: scrubbedEnv({ PLAN2CODE_NOW: NOW, ...env, PLAN2CODE_LEARNING_DIR: learningDirFor(env) }), }); let json = null; try { json = JSON.parse(r.stdout); } catch {} return { code: r.status, json, stdout: r.stdout, stderr: r.stderr }; } export function tmp(name) { return fs.mkdtempSync(path.join(fs.realpathSync.native(os.tmpdir()), `p2c-${name}-`)); } export function write(root, rel, content) { const p = path.join(root, rel); fs.mkdirSync(path.dirname(p), { recursive: true }); fs.writeFileSync(p, content); return p; } export const read = (root, rel) => fs.readFileSync(path.join(root, rel), "utf8"); /** A git runner for `dir`, under the scrubbed config set beside `scrubbedEnv`. */ export const gitIn = (dir) => (...a) => execFileSync("git", a, { cwd: dir, stdio: "pipe", encoding: "utf8" }); export function gitInit(dir, branch = "main") { const g = gitIn(dir); g("init", "-q", "-b", branch); g("config", "user.email", "t@example.com"); g("config", "user.name", "Test"); g("config", "commit.gpgsign", "false"); // No background maintenance after a commit: on macOS it held // .git/objects/maintenance.lock while copyFixture copied the template, and // the copy failed with ENOENT once the lock was gone. g("config", "maintenance.auto", "false"); g("config", "gc.auto", "0"); return g; } /** A phase-N.md in the Documentation template's shape. tasks: [[marker, id], ...] */ export function phase(n, status, tasks, extra = "") { return `# Phase ${n}: Name ${n}\n\n**Status:** ${status}\n**Estimated Tasks:** ${tasks.length}\n\n## Overview\n\nPhase ${n} builds the thing. More words here.\n\n## Prerequisites\n\n- Something exists\n\n## Tasks\n\n${tasks.map(([m, id]) => `- [${m}] **Task ${id}:** Do ${id}`).join("\n")}\n${extra}\n## Acceptance Criteria\n\n- It works\n`; }