|
| 1 | +// Path + diff helpers for `meta prompt-snapshot`. Pure (no I/O, no metadata) so |
| 2 | +// they unit-test trivially; the command does the filesystem work. |
| 3 | + |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +export interface SnapshotPaths { |
| 7 | + /** The per-template snapshot directory: <cwd>/.metaobjects/snapshots/<name>. */ |
| 8 | + dir: string; |
| 9 | + /** The committed fixture payload (author-owned input). */ |
| 10 | + payloadPath: string; |
| 11 | + /** The golden rendered output (tool-managed, byte-exact). */ |
| 12 | + snapPath: string; |
| 13 | +} |
| 14 | + |
| 15 | +export function snapshotPaths(cwd: string, templateName: string): SnapshotPaths { |
| 16 | + const dir = join(cwd, ".metaobjects", "snapshots", templateName); |
| 17 | + return { |
| 18 | + dir, |
| 19 | + payloadPath: join(dir, "payload.json"), |
| 20 | + snapPath: join(dir, "output.snap"), |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +// A compact line diff: trim the common leading/trailing lines, then show the |
| 25 | +// differing middle as `- <expected>` followed by `+ <actual>`. Enough to make |
| 26 | +// drift reviewable in CI output without pulling in a diff dependency. |
| 27 | +export function unifiedDiff(expected: string, actual: string): string { |
| 28 | + const e = expected.split("\n"); |
| 29 | + const a = actual.split("\n"); |
| 30 | + |
| 31 | + let pre = 0; |
| 32 | + while (pre < e.length && pre < a.length && e[pre] === a[pre]) pre++; |
| 33 | + |
| 34 | + let suf = 0; |
| 35 | + while ( |
| 36 | + suf < e.length - pre && |
| 37 | + suf < a.length - pre && |
| 38 | + e[e.length - 1 - suf] === a[a.length - 1 - suf] |
| 39 | + ) { |
| 40 | + suf++; |
| 41 | + } |
| 42 | + |
| 43 | + const eMid = e.slice(pre, e.length - suf); |
| 44 | + const aMid = a.slice(pre, a.length - suf); |
| 45 | + |
| 46 | + const out: string[] = [`@@ line ${pre + 1} @@`]; |
| 47 | + for (const line of eMid) out.push(`- ${line}`); |
| 48 | + for (const line of aMid) out.push(`+ ${line}`); |
| 49 | + return out.join("\n"); |
| 50 | +} |
0 commit comments