/** * gstack-pr-shots (lib/pr-shots.ts): change-centred hunks, the −/+ gutter, * escaping, section extraction at a base commit, and the refusals that keep * secrets, binaries, option-like refs and outside files off a public PR image. * Rendering itself is covered by the no-browser path (SHOT_PNG: unavailable). */ import { afterEach, describe, expect, test } from 'bun:test'; import { spawnSync } from 'node:child_process'; import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; import { buildPage, cleanText, defaultBase, diffLines, hunks, pathSides, resolveCommit } from '../lib/pr-shots'; import { wtreeFingerprint } from '../lib/script-invocation'; const ROOT = path.resolve(import.meta.dir, '..'); const dirs: string[] = []; const tmp = (prefix: string) => { const d = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); dirs.push(d); return d; }; afterEach(() => { while (dirs.length) fs.rmSync(dirs.pop()!, { recursive: true, force: true }); }); const GIT_ENV = { ...process.env, GIT_AUTHOR_NAME: 'Fixture', GIT_AUTHOR_EMAIL: 'fixture@example.test', GIT_COMMITTER_NAME: 'Fixture', GIT_COMMITTER_EMAIL: 'fixture@example.test' }; function repo(files: Record): { dir: string; base: string } { const dir = tmp('shots-repo-'); const git = (...a: string[]) => spawnSync('git', a, { cwd: dir, encoding: 'utf8', env: GIT_ENV, timeout: 30_000 }); git('init', '-q', '-b', 'main'); for (const [f, c] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(dir, f)), { recursive: true }); fs.writeFileSync(path.join(dir, f), c); } git('add', '-A'); git('commit', '-q', '-m', 'base'); return { dir, base: git('rev-parse', 'HEAD').stdout.trim() }; } describe('default base (#3733)', () => { const featureRepo = (baseBranch: string) => { const r = repo({ 'doc.md': 'before\n' }); const git = (...a: string[]) => { const out = spawnSync('git', a, { cwd: r.dir, encoding: 'utf8', env: GIT_ENV, timeout: 30_000 }); if (out.status !== 0) throw new Error(`fixture git ${a.join(' ')} failed: ${out.stderr}`); return out.stdout.trim(); }; if (baseBranch !== 'main') git('branch', '-m', baseBranch); const origin = tmp('shots-origin-'); git('init', '-q', '--bare', '--initial-branch', baseBranch, origin); git('remote', 'add', 'origin', origin); git('push', '-q', '-u', 'origin', baseBranch); git('remote', 'set-head', 'origin', baseBranch); git('checkout', '-q', '-b', 'feature'); fs.writeFileSync(path.join(r.dir, 'doc.md'), 'after\n'); git('add', 'doc.md'); git('commit', '-q', '-m', 'feature text'); git('push', '-q', '-u', 'origin', 'feature'); return { ...r, git }; }; // Value: protects=a clone whose origin/HEAD names the pushed feature branch still compares against the real base, so // committed PR changes show; fails_when=the feature head is its own before-image and generation refuses with // PR_SHOTS_IDENTICAL; why_new=path-mode tests always passed --base; seam=none test.each(['main', 'develop'])('origin/HEAD naming the feature branch still compares against %s', baseBranch => { const r = featureRepo(baseBranch); expect(defaultBase(r.dir)).toBe(r.base); r.git('remote', 'set-head', 'origin', 'feature'); const chosen = defaultBase(r.dir); expect(chosen).toBe(r.base); const sides = pathSides(r.dir, 'doc.md', chosen as string); if ('code' in sides) throw new Error(sides.message); expect([sides.before.text, sides.after.text]).toEqual(['before\n', 'after\n']); }); // Value: protects=a stale origin/HEAD (its branch is gone) falls back to main instead of refusing; fails_when=the // dangling target is taken as the base with no fallback; why_new=the case above always resolves; seam=none test('a dangling origin/HEAD falls back to main', () => { const r = featureRepo('main'); r.git('symbolic-ref', 'refs/remotes/origin/HEAD', 'refs/remotes/origin/retired'); expect(defaultBase(r.dir)).toBe(r.base); }); }); describe('diff and hunks', () => { test('a change at line 240 of 300 stays visible; gaps say how much they hide', () => { const a = Array.from({ length: 300 }, (_, i) => `line ${i + 1}`); const b = [...a]; b[239] = 'line 240 CHANGED'; const ops = diffLines(a, b); if (!Array.isArray(ops)) throw new Error(ops.message); const hs = hunks(ops); expect(hs.length).toBe(1); expect(hs[0]!.gapBefore).toBe(236); expect(hs[0]!.ops.map(o => o.t + o.line)).toContain('+line 240 CHANGED'); const page = buildPage({ before: { text: a.join('\n'), label: 'Before' }, after: { text: b.join('\n'), label: 'After' }, title: 't', caption: 'c', kind: 'prompt' }); if ('code' in page) throw new Error(page.message); expect(page.html).toContain('⋯ 236 unchanged lines'); expect(page.html).toContain('⋯ 57 unchanged lines'); expect(page.html).toContain('+line 240 CHANGED'); expect(page.html).toContain('−line 240'); expect(page.text).toContain('+line 240 CHANGED'); expect(page.changed).toBe(2); }); test('HTML is escaped, the page has a strict CSP, identical sides are refused', () => { const page = buildPage({ before: { text: 'a', label: 'B' }, after: { text: '', label: 'A' }, title: '', caption: '"c"', kind: 'terminal' }); if ('code' in page) throw new Error(page.message); expect(page.html).not.toContain('