|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { prepareStartupPlan } from "../src/core/startup"; |
| 3 | +import type { AppBootstrap, CliInput, ParsedCliInput } from "../src/core/types"; |
| 4 | + |
| 5 | +function createBootstrap(input: CliInput): AppBootstrap { |
| 6 | + return { |
| 7 | + input, |
| 8 | + changeset: { |
| 9 | + id: "changeset:startup", |
| 10 | + sourceLabel: "repo", |
| 11 | + title: "repo working tree", |
| 12 | + files: [], |
| 13 | + }, |
| 14 | + initialMode: input.options.mode ?? "auto", |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +describe("startup planning", () => { |
| 19 | + test("returns help output without entering app startup", async () => { |
| 20 | + let loaded = false; |
| 21 | + |
| 22 | + const plan = await prepareStartupPlan(["bun", "hunk"], { |
| 23 | + parseCliImpl: async () => ({ kind: "help", text: "Usage: hunk\n" }), |
| 24 | + loadAppBootstrapImpl: async () => { |
| 25 | + loaded = true; |
| 26 | + throw new Error("unreachable"); |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + expect(plan).toEqual({ kind: "help", text: "Usage: hunk\n" }); |
| 31 | + expect(loaded).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + test("passes the MCP serve command through without app bootstrap work", async () => { |
| 35 | + let loaded = false; |
| 36 | + |
| 37 | + const plan = await prepareStartupPlan(["bun", "hunk", "mcp", "serve"], { |
| 38 | + parseCliImpl: async () => ({ kind: "mcp-serve" }), |
| 39 | + loadAppBootstrapImpl: async () => { |
| 40 | + loaded = true; |
| 41 | + throw new Error("unreachable"); |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + expect(plan).toEqual({ kind: "mcp-serve" }); |
| 46 | + expect(loaded).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + test("routes non-diff pager stdin to the plain-text pager path", async () => { |
| 50 | + let loaded = false; |
| 51 | + |
| 52 | + const plan = await prepareStartupPlan(["bun", "hunk", "pager"], { |
| 53 | + parseCliImpl: async () => ({ kind: "pager", options: { theme: "paper" } }), |
| 54 | + readStdinText: async () => "* main\n feature/demo\n", |
| 55 | + looksLikePatchInputImpl: () => false, |
| 56 | + loadAppBootstrapImpl: async () => { |
| 57 | + loaded = true; |
| 58 | + throw new Error("unreachable"); |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + expect(plan).toEqual({ kind: "plain-text-pager", text: "* main\n feature/demo\n" }); |
| 63 | + expect(loaded).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + test("normalizes diff-like pager stdin into patch app startup", async () => { |
| 67 | + const seenInputs: CliInput[] = []; |
| 68 | + |
| 69 | + const plan = await prepareStartupPlan(["bun", "hunk", "pager"], { |
| 70 | + parseCliImpl: async () => ({ kind: "pager", options: { theme: "paper" } }), |
| 71 | + readStdinText: async () => "diff --git a/a.ts b/a.ts\n@@ -1 +1 @@\n-old\n+new\n", |
| 72 | + looksLikePatchInputImpl: () => true, |
| 73 | + resolveRuntimeCliInputImpl(input) { |
| 74 | + seenInputs.push(input); |
| 75 | + return input; |
| 76 | + }, |
| 77 | + resolveConfiguredCliInputImpl(input) { |
| 78 | + seenInputs.push(input); |
| 79 | + return { input } as never; |
| 80 | + }, |
| 81 | + loadAppBootstrapImpl: async (input) => { |
| 82 | + seenInputs.push(input); |
| 83 | + return createBootstrap(input); |
| 84 | + }, |
| 85 | + usesPipedPatchInputImpl: () => false, |
| 86 | + }); |
| 87 | + |
| 88 | + expect(plan.kind).toBe("app"); |
| 89 | + if (plan.kind !== "app") { |
| 90 | + throw new Error("Expected app startup plan."); |
| 91 | + } |
| 92 | + |
| 93 | + expect(plan.cliInput).toMatchObject({ |
| 94 | + kind: "patch", |
| 95 | + file: "-", |
| 96 | + text: "diff --git a/a.ts b/a.ts\n@@ -1 +1 @@\n-old\n+new\n", |
| 97 | + options: { |
| 98 | + theme: "paper", |
| 99 | + pager: true, |
| 100 | + }, |
| 101 | + }); |
| 102 | + expect(seenInputs).toHaveLength(3); |
| 103 | + }); |
| 104 | + |
| 105 | + test("opens the controlling terminal for piped patch startup", async () => { |
| 106 | + const cliInput: CliInput = { |
| 107 | + kind: "patch", |
| 108 | + file: "-", |
| 109 | + options: { |
| 110 | + mode: "auto", |
| 111 | + pager: true, |
| 112 | + }, |
| 113 | + }; |
| 114 | + const controllingTerminal = { stdin: {} as never, stdout: {} as never, close: () => {} }; |
| 115 | + let opened = 0; |
| 116 | + |
| 117 | + const plan = await prepareStartupPlan(["bun", "hunk", "patch", "-"], { |
| 118 | + parseCliImpl: async () => cliInput as ParsedCliInput, |
| 119 | + resolveRuntimeCliInputImpl: (input) => input, |
| 120 | + resolveConfiguredCliInputImpl: (input) => ({ input }) as never, |
| 121 | + loadAppBootstrapImpl: async (input) => createBootstrap(input), |
| 122 | + usesPipedPatchInputImpl: (input) => { |
| 123 | + expect(input).toBe(cliInput); |
| 124 | + return true; |
| 125 | + }, |
| 126 | + openControllingTerminalImpl: () => { |
| 127 | + opened += 1; |
| 128 | + return controllingTerminal; |
| 129 | + }, |
| 130 | + }); |
| 131 | + |
| 132 | + expect(plan).toMatchObject({ |
| 133 | + kind: "app", |
| 134 | + cliInput, |
| 135 | + controllingTerminal, |
| 136 | + }); |
| 137 | + expect(opened).toBe(1); |
| 138 | + }); |
| 139 | +}); |
0 commit comments