|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { createEmbeddedHunkSession, embeddedSourceToCliInput } from "./index"; |
| 6 | + |
| 7 | +const patchText = [ |
| 8 | + "diff --git a/example.ts b/example.ts", |
| 9 | + "--- a/example.ts", |
| 10 | + "+++ b/example.ts", |
| 11 | + "@@ -1 +1 @@", |
| 12 | + "-const value = 1;", |
| 13 | + "+const value = 2;", |
| 14 | + "", |
| 15 | +].join("\n"); |
| 16 | + |
| 17 | +describe("embeddedSourceToCliInput", () => { |
| 18 | + test("maps embedded review sources to Hunk CLI inputs", () => { |
| 19 | + expect(embeddedSourceToCliInput({ kind: "worktree", options: { theme: "midnight" } })).toEqual({ |
| 20 | + kind: "vcs", |
| 21 | + staged: false, |
| 22 | + options: { theme: "midnight" }, |
| 23 | + }); |
| 24 | + expect(embeddedSourceToCliInput({ kind: "staged" })).toEqual({ |
| 25 | + kind: "vcs", |
| 26 | + staged: true, |
| 27 | + options: {}, |
| 28 | + }); |
| 29 | + expect(embeddedSourceToCliInput({ kind: "show", ref: "HEAD~1" })).toEqual({ |
| 30 | + kind: "show", |
| 31 | + ref: "HEAD~1", |
| 32 | + options: {}, |
| 33 | + }); |
| 34 | + expect( |
| 35 | + embeddedSourceToCliInput({ |
| 36 | + kind: "vcs", |
| 37 | + range: "main", |
| 38 | + staged: false, |
| 39 | + pathspecs: ["src/app.ts"], |
| 40 | + options: { vcs: "jj" }, |
| 41 | + }), |
| 42 | + ).toEqual({ |
| 43 | + kind: "vcs", |
| 44 | + range: "main", |
| 45 | + staged: false, |
| 46 | + pathspecs: ["src/app.ts"], |
| 47 | + options: { vcs: "jj" }, |
| 48 | + }); |
| 49 | + expect( |
| 50 | + embeddedSourceToCliInput({ kind: "diff", left: "before.ts", right: "after.ts" }), |
| 51 | + ).toEqual({ |
| 52 | + kind: "diff", |
| 53 | + left: "before.ts", |
| 54 | + right: "after.ts", |
| 55 | + options: {}, |
| 56 | + }); |
| 57 | + expect(embeddedSourceToCliInput({ kind: "stash-show", ref: "stash@{1}" })).toEqual({ |
| 58 | + kind: "stash-show", |
| 59 | + ref: "stash@{1}", |
| 60 | + options: {}, |
| 61 | + }); |
| 62 | + expect(embeddedSourceToCliInput({ kind: "patch", file: "changes.patch" })).toEqual({ |
| 63 | + kind: "patch", |
| 64 | + file: "changes.patch", |
| 65 | + options: {}, |
| 66 | + }); |
| 67 | + expect( |
| 68 | + embeddedSourceToCliInput({ |
| 69 | + kind: "difftool", |
| 70 | + left: "left.ts", |
| 71 | + right: "right.ts", |
| 72 | + path: "src/app.ts", |
| 73 | + }), |
| 74 | + ).toEqual({ |
| 75 | + kind: "difftool", |
| 76 | + left: "left.ts", |
| 77 | + right: "right.ts", |
| 78 | + path: "src/app.ts", |
| 79 | + options: {}, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + test("loads embedded sessions through Hunk config resolution", async () => { |
| 84 | + const root = mkdtempSync(join(tmpdir(), "hunk-embedded-config-")); |
| 85 | + const previousXdgConfigHome = process.env.XDG_CONFIG_HOME; |
| 86 | + |
| 87 | + try { |
| 88 | + const configHome = join(root, "config"); |
| 89 | + mkdirSync(join(configHome, "hunk"), { recursive: true }); |
| 90 | + writeFileSync( |
| 91 | + join(configHome, "hunk", "config.toml"), |
| 92 | + ['theme = "midnight"', 'mode = "stack"', "line_numbers = false"].join("\n"), |
| 93 | + ); |
| 94 | + process.env.XDG_CONFIG_HOME = configHome; |
| 95 | + |
| 96 | + const session = await createEmbeddedHunkSession({ |
| 97 | + cwd: root, |
| 98 | + source: { kind: "patch", text: patchText, options: { theme: "paper" } }, |
| 99 | + }); |
| 100 | + const snapshot = session.getSnapshot(); |
| 101 | + |
| 102 | + expect(snapshot.status).toBe("ready"); |
| 103 | + if (snapshot.status !== "ready") throw new Error("Expected embedded session to load."); |
| 104 | + expect(snapshot.bootstrap.initialMode).toBe("stack"); |
| 105 | + expect(snapshot.bootstrap.initialShowLineNumbers).toBe(false); |
| 106 | + expect(snapshot.bootstrap.initialTheme).toBe("paper"); |
| 107 | + |
| 108 | + session.dispose(); |
| 109 | + } finally { |
| 110 | + if (previousXdgConfigHome === undefined) { |
| 111 | + delete process.env.XDG_CONFIG_HOME; |
| 112 | + } else { |
| 113 | + process.env.XDG_CONFIG_HOME = previousXdgConfigHome; |
| 114 | + } |
| 115 | + rmSync(root, { force: true, recursive: true }); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + test("keeps the previous source and reports errors when reload fails", async () => { |
| 120 | + const root = mkdtempSync(join(tmpdir(), "hunk-embedded-reload-error-")); |
| 121 | + |
| 122 | + try { |
| 123 | + const initialSource = { kind: "patch", text: patchText, label: "initial patch" } as const; |
| 124 | + const session = await createEmbeddedHunkSession({ |
| 125 | + cwd: root, |
| 126 | + source: initialSource, |
| 127 | + }); |
| 128 | + |
| 129 | + await expect(session.load({ kind: "patch", file: "missing.patch" })).rejects.toThrow(); |
| 130 | + |
| 131 | + expect(session.source).toEqual(initialSource); |
| 132 | + const snapshot = session.getSnapshot(); |
| 133 | + expect(snapshot.status).toBe("error"); |
| 134 | + expect(snapshot.bootstrap).toBeDefined(); |
| 135 | + |
| 136 | + session.dispose(); |
| 137 | + } finally { |
| 138 | + rmSync(root, { force: true, recursive: true }); |
| 139 | + } |
| 140 | + }); |
| 141 | +}); |
0 commit comments