|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import os from "node:os"; |
| 3 | +import { test, type TestContext } from "node:test"; |
| 4 | +import React from "react"; |
| 5 | +import { renderToString } from "ink"; |
| 6 | +import { getImagePasteShortcutLabel, isImagePasteShortcut } from "../utils/image-paste-shortcut"; |
| 7 | +import { dispatchTerminalInput, parseTerminalInput } from "../ui/hooks/useTerminalInput"; |
| 8 | +import { PromptInput } from "../ui/views/PromptInput"; |
| 9 | +import { buildWelcomeTips } from "../ui/views/WelcomeScreen"; |
| 10 | +import { stripTerminalControls } from "./terminal-output"; |
| 11 | + |
| 12 | +const environments: { |
| 13 | + name: string; |
| 14 | + platform: NodeJS.Platform; |
| 15 | + release?: string; |
| 16 | + env?: NodeJS.ProcessEnv; |
| 17 | + label: string; |
| 18 | + ctrl: boolean; |
| 19 | + alt: boolean; |
| 20 | +}[] = [ |
| 21 | + { name: "win32", platform: "win32", label: "Alt+V", ctrl: false, alt: true }, |
| 22 | + { name: "darwin", platform: "darwin", label: "Ctrl+V", ctrl: true, alt: false }, |
| 23 | + { name: "linux", platform: "linux", label: "Ctrl+V", ctrl: true, alt: false }, |
| 24 | + { |
| 25 | + name: "wsl-distro", |
| 26 | + platform: "linux", |
| 27 | + env: { WSL_DISTRO_NAME: "Ubuntu" }, |
| 28 | + label: "Ctrl+V / Alt+V", |
| 29 | + ctrl: true, |
| 30 | + alt: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "wsl-interop", |
| 34 | + platform: "linux", |
| 35 | + env: { WSL_INTEROP: "/run/WSL/123_interop" }, |
| 36 | + label: "Ctrl+V / Alt+V", |
| 37 | + ctrl: true, |
| 38 | + alt: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "wsl1-kernel", |
| 42 | + platform: "linux", |
| 43 | + release: "4.4.0-19041-Microsoft", |
| 44 | + label: "Ctrl+V / Alt+V", |
| 45 | + ctrl: true, |
| 46 | + alt: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "wsl2-kernel", |
| 50 | + platform: "linux", |
| 51 | + release: "6.6.87.2-microsoft-standard-WSL2", |
| 52 | + label: "Ctrl+V / Alt+V", |
| 53 | + ctrl: true, |
| 54 | + alt: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "win32-with-wsl-env", |
| 58 | + platform: "win32", |
| 59 | + env: { WSL_DISTRO_NAME: "Ubuntu" }, |
| 60 | + label: "Alt+V", |
| 61 | + ctrl: false, |
| 62 | + alt: true, |
| 63 | + }, |
| 64 | +]; |
| 65 | + |
| 66 | +function mockEnvironment(t: TestContext, environment: (typeof environments)[number]): void { |
| 67 | + const descriptor = Object.getOwnPropertyDescriptor(process, "platform")!; |
| 68 | + Object.defineProperty(process, "platform", { value: environment.platform }); |
| 69 | + t.after(() => Object.defineProperty(process, "platform", descriptor)); |
| 70 | + t.mock.method(os, "release", () => environment.release ?? "6.8.0-generic"); |
| 71 | + for (const key of ["WSL_DISTRO_NAME", "WSL_INTEROP"]) { |
| 72 | + const original = process.env[key]; |
| 73 | + const value = environment.env?.[key]; |
| 74 | + if (value === undefined) delete process.env[key]; |
| 75 | + else process.env[key] = value; |
| 76 | + t.after(() => { |
| 77 | + if (original === undefined) delete process.env[key]; |
| 78 | + else process.env[key] = original; |
| 79 | + }); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +for (const environment of environments) { |
| 84 | + const { name, platform, label, ctrl, alt } = environment; |
| 85 | + |
| 86 | + test(`${name}: only the image paste shortcuts trigger image reading`, (t) => { |
| 87 | + mockEnvironment(t, environment); |
| 88 | + for (const [raw, expected] of [ |
| 89 | + ["\u001bv", alt], |
| 90 | + ["\u001bV", alt], |
| 91 | + ["\u0016", ctrl], |
| 92 | + ["v", false], |
| 93 | + ["V", false], |
| 94 | + ["\u001bx", false], |
| 95 | + ["\u0018", false], |
| 96 | + ["pasted text", false], |
| 97 | + ] as const) { |
| 98 | + const events: boolean[] = []; |
| 99 | + dispatchTerminalInput(raw, (input, key) => events.push(isImagePasteShortcut(input, key, platform))); |
| 100 | + assert.deepEqual(events, [expected], JSON.stringify(raw)); |
| 101 | + } |
| 102 | + |
| 103 | + const shortcut = parseTerminalInput(platform === "win32" ? "\u001bv" : "\u0016"); |
| 104 | + assert.equal(isImagePasteShortcut(shortcut.input, { ...shortcut.key, paste: true }, platform), false); |
| 105 | + assert.equal(isImagePasteShortcut("v", { ctrl: true, meta: true, paste: false }, platform), false); |
| 106 | + assert.equal(getImagePasteShortcutLabel(platform), label); |
| 107 | + }); |
| 108 | + |
| 109 | + test(`${name}: help, welcome tips and prompt footer show ${label}`, async (t) => { |
| 110 | + mockEnvironment(t, environment); |
| 111 | + |
| 112 | + assert.equal(getImagePasteShortcutLabel(), label); |
| 113 | + const shortcut = parseTerminalInput(platform === "win32" ? "\u001bv" : "\u0016"); |
| 114 | + assert.equal(isImagePasteShortcut(shortcut.input, shortcut.key), true); |
| 115 | + |
| 116 | + const tips = buildWelcomeTips([]); |
| 117 | + assert.equal(tips.find((tip) => tip.description === "Paste an image from the clipboard")?.label, label); |
| 118 | + |
| 119 | + const footer = stripTerminalControls( |
| 120 | + renderToString( |
| 121 | + React.createElement(PromptInput, { |
| 122 | + projectRoot: process.cwd(), |
| 123 | + screenWidth: 160, |
| 124 | + skills: [], |
| 125 | + modelConfig: { model: "test", thinkingEnabled: false, reasoningEffort: "high" }, |
| 126 | + promptHistory: [], |
| 127 | + busy: false, |
| 128 | + disabled: true, |
| 129 | + planMode: false, |
| 130 | + onSubmit() {}, |
| 131 | + onPromptDraftApplied() {}, |
| 132 | + onModelConfigChange: () => "", |
| 133 | + onPermissionSettingsChange: () => "", |
| 134 | + onPlanModeChange() {}, |
| 135 | + onInterrupt() {}, |
| 136 | + }), |
| 137 | + { columns: 160 } |
| 138 | + ) |
| 139 | + ); |
| 140 | + assert.ok(footer.includes(`${label.toLowerCase()} image`), footer); |
| 141 | + if (!ctrl) assert.ok(!footer.includes("ctrl+v image"), footer); |
| 142 | + if (!alt) assert.ok(!footer.includes("alt+v image"), footer); |
| 143 | + |
| 144 | + // Reload the module so its static help text uses the simulated platform. |
| 145 | + const moduleUrl = new URL(`../cli-args.ts?image-paste-platform=${name}`, import.meta.url).href; |
| 146 | + const { parseArguments } = await import(moduleUrl); |
| 147 | + const output: string[] = []; |
| 148 | + t.mock.method(console, "log", (...args: unknown[]) => output.push(args.join(" "))); |
| 149 | + await parseArguments(["--help"]); |
| 150 | + const help = output.join("\n"); |
| 151 | + assert.ok(help.includes(`${label.toLowerCase().padEnd(17)}Paste an image from the clipboard`), help); |
| 152 | + if (!ctrl) assert.ok(!help.includes(`${"ctrl+v".padEnd(17)}Paste an image from the clipboard`), help); |
| 153 | + if (!alt) assert.ok(!help.includes(`${"alt+v".padEnd(17)}Paste an image from the clipboard`), help); |
| 154 | + }); |
| 155 | +} |
0 commit comments