Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

commandLabel.test.ts

598 lines23.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 commandDisplayText,
5 commandHighlightLanguage,
6 commandProgramName,
7} from "./commandLabel.ts";
8​
9describe("commandProgramName", () => {
10 it.each([
11 ["/bin/zsh -lc 'vp test run apps/web/src/session-logic.test.ts'", "vp"],
12 ["/bin/zsh -lc 'git diff --check'", "git"],
13 ['/bin/zsh -lc "npx -y react-doctor@latest apps/web"', "npx"],
14 ["/bin/zsh -lc 'rg -n \"registerHooks|worker\" apps/web/src'", "rg"],
15 ["/bin/bash --noprofile --norc -l -c 'sed -n 1,270p file.ts'", "sed"],
16 ["/bin/bash -o pipefail -lc 'vp test run'", "vp"],
17 ["/bin/bash --rcfile /tmp/config -c 'git status'", "git"],
18 ["sh -ec 'node scripts/check.js'", "node"],
19 ["fish --command 'rg --files'", "rg"],
20 ["zsh -lc 'CI=1 env -u DEBUG sudo -u root vp test run'", "vp"],
21 ["env CI=1 /bin/zsh -lc '\"/Applications/My Tools/bin/check\" --verbose'", "check"],
22 ["bash -lc \"zsh -c 'git status'\"", "git"],
23 ['"C:\\Program Files\\Git\\bin\\bash.exe" -lc "git status"', "git"],
24 ["/bin/zsh -lc 'git status\nsed -n '\"'1,20p' apps/web/src/components/DiffPanel.tsx\"", "git"],
25 ])("unwraps shell scripts without executing them: %s", (command, program) => {
26 expect(commandProgramName(command)).toBe(program);
27 });
28​
29 it.each([
30 ["vp test run", "vp"],
31 ["sudo -u root pnpm test", "pnpm"],
32 ["env --split-string='CI=1 node scripts/check.js'", "node"],
33 ['"C:\\Program Files\\nodejs\\node.exe" script.js', "node.exe"],
34 ["/bin/zsh", "zsh"],
35 ["/bin/bash -l", "bash"],
36 ["zsh script.sh -c 'git status'", "zsh"],
37 ["bash -- -c 'git status'", "bash"],
38 ["bash --rcfile config.sh", "bash"],
39 ["my-shell -c 'git status'", "my-shell"],
40 ["$HOME/.bun/bin/bun test", "bun"],
41 ['"$ANDROID_HOME/emulator/emulator" -list-avds', "emulator"],
42 ["${ROOT}/bin/tool --version", "tool"],
43 ])("preserves ordinary programs and actual shell launches: %s", (command, program) => {
44 expect(commandProgramName(command)).toBe(program);
45 });
46​
47 it.each([
48 "",
49 "zsh -lc",
50 "zsh -lc ''",
51 "zsh -lc 'git status",
52 "zsh -lc 'env'",
53 'zsh -lc "git \\\"',
54 ])("falls back for missing or malformed scripts: %s", (command) => {
55 expect(commandProgramName(command)).toBeNull();
56 });
57​
58 it.each([
59 "if test -f package.json; then vp test; fi",
60 "[ -f package.json ]",
61 "[[ -f package.json ]]",
62 "test -f package.json",
63 'for file in *; do echo "$file"; done',
64 "while true; do sleep 1; done",
65 "until false; do sleep 1; done",
66 "case $name in test) vp test;; esac",
67 'select item in one two; do echo "$item"; done',
68 "function check() { vp test; }",
69 "check() { vp test; }",
70 "k(){ echo ok; }; k",
71 "{ vp test; }",
72 "(vp test)",
73 "(( count += 1 ))",
74 "! vp test",
75 ":",
76 ". ./script.sh",
77 "source ./script.sh",
78 "eval 'vp test'",
79 "cd packages/client-runtime",
80 "export NODE_ENV=test",
81 "local name=value",
82 "set -e",
83 "alias ll='ls -la'",
84 "repeat 3 echo ok",
85 "and vp test",
86 "return 1",
87 "break",
88 "continue",
89 "true",
90 "false",
91 ])("falls back for shell syntax and internal control commands: %s", (command) => {
92 expect(commandProgramName(command)).toBeNull();
93 });
94​
95 it.each([
96 ['rg -n "if|for|while" src', "rg"],
97 ["printf '%s\\n' 'a;b|c'", "printf"],
98 ["node -e \"if (true) console.log('ok')\"", "node"],
99 ["echo '$(git status)'", "echo"],
100 ["vp test && git status", "vp"],
101 ["vp test || git status", "vp"],
102 ["rg needle src | head", "rg"],
103 ["vp test; git status", "vp"],
104 ["vp test &", "vp"],
105 ["vp test\ngit status", "vp"],
106 ['echo "$(git status)"', "echo"],
107 ["echo `git status`", "echo"],
108 ["cat <(rg needle src)", "cat"],
109 ])("uses the first executable-looking program: %s", (command, program) => {
110 expect(commandProgramName(command)).toBe(program);
111 });
112​
113 it.each([
114 ["cd packages/client-runtime && vp test run", "vp"],
115 ['cd "a path with spaces"; git status', "git"],
116 ["cd apps/web\npnpm test", "pnpm"],
117 ["cd first && cd second && bun test", "bun"],
118 ["CI=1 cd apps/web && npm test", "npm"],
119 ["PATH+=:/tools npm test", "npm"],
120 ["PATH+=:/tools && npm test", "npm"],
121 ['TMP=$(mktemp -d); cd "$TMP"; npm pack ./package', "npm"],
122 ["cd $(find . -type d | head -1) && git status", "git"],
123 ["cd `find . -type d | head -1` && node script.js", "node"],
124 ["cd /tmp 2>&1 && npm test", "npm"],
125 ["cd /tmp 2<&0 && pnpm test", "pnpm"],
126 ["cd /tmp &>/dev/null && bun test", "bun"],
127 ["cd work |& npm test", "npm"],
128 ["cd /tmp && # use the selected workspace\nnpm test", "npm"],
129 ["export CI=1; # first note\n# second note\npnpm test", "pnpm"],
130 ["cd&&npm test", "npm"],
131 ["export CI=1;pnpm test", "pnpm"],
132 ["cd ${ROOT:-path;with;semicolons} && bun test", "bun"],
133 ["cd ${ROOT:-path&&fallback} && node app.js", "node"],
134 ["cd @(first|second) && npm test", "npm"],
135 ["cd /tmp \\\n&& npm test", "npm"],
136 ["/bin/zsh -lc 'cd apps/web && vp test run'", "vp"],
137 ])("skips leading cd commands and uses the next useful program: %s", (command, program) => {
138 expect(commandProgramName(command)).toBe(program);
139 });
140​
141 it.each([
142 ["source ~/.nvm/nvm.sh && nvm use", "nvm"],
143 [". ./.env && pnpm test", "pnpm"],
144 ["export CI=1 && vp test run", "vp"],
145 ["unset DEBUG; node app.js", "node"],
146 ["export CI=1 && cd apps/web && pnpm test", "pnpm"],
147 ["/bin/zsh -lc 'source ~/.nvm/nvm.sh && nvm use'", "nvm"],
148 ])("skips shell setup commands and uses the next useful program: %s", (command, program) => {
149 expect(commandProgramName(command)).toBe(program);
150 });
151​
152 it.each([
153 ["set -eu; npm test", "npm"],
154 [": && npm test", "npm"],
155 ["true && npm test", "npm"],
156 ["false || npm test", "npm"],
157 ["false; npm test", "npm"],
158 ["sudo -n true && npm test", "npm"],
159 ["sudo -n true; echo checked", "echo"],
160 ["test -d node_modules || vp i", "vp"],
161 ["[ -d node_modules ] || vp i", "vp"],
162 ])("skips non-descriptive shell commands before a useful program: %s", (command, program) => {
163 expect(commandProgramName(command)).toBe(program);
164 });
165​
166 it.each([
167 ...["&&", " || ", ";", "\n", "|", " |& ", " & "].map(
168 (operator) => [`cd /tmp${operator}npm test`, "npm"] as const,
169 ),
170 ...["export CI=1", "unset DEBUG", "source env.sh", ". env.sh"].map(
171 (setup) => [`${setup}&&npm test`, "npm"] as const,
172 ),
173 ])("handles shell setup followed by every command separator: %s", (command, program) => {
174 expect(commandProgramName(command)).toBe(program);
175 });
176​
177 it.each([
178 ["command git status", "git"],
179 ["command -p git status", "git"],
180 ["command -- git status", "git"],
181 ["builtin printf ok", "printf"],
182 ["builtin -- echo ok", "echo"],
183 ["command cd /tmp && npm test", "npm"],
184 ["builtin cd /tmp && pnpm test", "pnpm"],
185 ["exec node app.js", "node"],
186 ["exec -cl -a worker node app.js", "node"],
187 ["exec env CI=1 /opt/tools/check --verbose", "check"],
188 ['exec "C:\\Program Files\\nodejs\\node.exe" app.js', "node.exe"],
189 ["exec sh -c 'cd /tmp && npm test'", "npm"],
190 ["exec sh -c 'cd /tmp\nnpm test'", "npm"],
191 ["exec bash -c 'set -e\nnpm test'", "npm"],
192 ])("unwraps shell command wrappers: %s", (command, program) => {
193 expect(commandProgramName(command)).toBe(program);
194 });
195​
196 it.each([
197 ["timeout 10 pnpm test", "pnpm"],
198 ["timeout 10s python3 script.py", "python3"],
199 ["gtimeout 1.5 node app.js", "node"],
200 ["nohup npx expo start >/tmp/metro.log 2>&1 &", "npx"],
201 ["nohup -- env CI=1 bun test", "bun"],
202 ["arch -x86_64 ./build/app-under-test", "app-under-test"],
203 ["arch -arch arm64 /opt/tools/check", "check"],
204 ["bundle exec pod install", "pod"],
205 ["timeout 30 nohup env CI=1 node app.js", "node"],
206 ["timeout 60 script -q /dev/null env CI=1 node app.js", "node"],
207 ])("unwraps process-launch wrappers: %s", (command, program) => {
208 expect(commandProgramName(command)).toBe(program);
209 });
210​
211 it.each([
212 ["& 'C:\\Program Files\\nodejs\\node.exe' script.js", "node.exe"],
213 ['& "$env:WINDIR\\Microsoft.NET\\Framework64\\v4\\csc.exe" file.cs', "csc.exe"],
214 ])("resolves literal PowerShell call operators: %s", (command, program) => {
215 expect(commandProgramName(command)).toBe(program);
216 });
217​
218 it.each([
219 ["$env:CI='1'; npm test", "npm"],
220 ["$value = 'configured'; node app.js", "node"],
221 ["$process = Get-Process node; $process.Id", "Get-Process"],
222 ["$tmp = Join-Path $env:TEMP repo; git clone example", "Join-Path"],
223 ["$html = (Invoke-WebRequest https://example.com).Content", "Invoke-WebRequest"],
224 ["$process = Start-Process -FilePath .\\app.exe -PassThru; $process.Id", "app.exe"],
225 ])("labels commands inside simple PowerShell assignments: %s", (command, program) => {
226 expect(commandProgramName(command)).toBe(program);
227 });
228​
229 it.each([
230 ["cmd /c bcdedit /enum", "bcdedit"],
231 ["cmd /c cd C:\\work && npm test", "npm"],
232 ['cmd.exe /d /s /c "cd C:\\work && npm test"', "npm"],
233 [
234 'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\\work\\scripts\\doctor.ps1"',
235 "doctor.ps1",
236 ],
237 ['pwsh -NoProfile -Command "Set-Location C:\\work; pnpm test"', "pnpm"],
238 ["pwsh -Command Set-Location C:\\work; pnpm test", "pnpm"],
239 ])("unwraps Windows shell launchers: %s", (command, program) => {
240 expect(commandProgramName(command)).toBe(program);
241 });
242​
243 it.each([
244 ["Set-Location C:\\work; npm test", "npm"],
245 ["Push-Location C:\\work; node app.js", "node"],
246 ["Start-Process -FilePath node -ArgumentList server.js", "node"],
247 ["Start-Process -ArgumentList '-FilePath helper' node", "node"],
248 ["Start-Process -ErrorAction Stop -FilePath node", "node"],
249 ["Start-Process -NoNewWindow -WorkingDirectory C:\\work node", "node"],
250 ['Start-Process "C:\\Program Files\\Example\\app.exe"', "app.exe"],
251 ['Start-Process -FilePath ".\\dist\\Example App.exe" -PassThru', "Example App.exe"],
252 [".\\.venv\\Scripts\\python.exe script.py", "python.exe"],
253 ["$env:LOCALAPPDATA\\Programs\\tool.exe --version", "tool.exe"],
254 ['"=== CHECK FILE ==="; Get-Content file.txt', "Get-Content"],
255 ["$x = @'\ndata'; Get-Fake\n'@\nGet-Process", "Get-Process"],
256 ["@'\nprint('ok; still data')\n'@ | python -", "python"],
257 ])("handles common PowerShell setup and launch commands: %s", (command, program) => {
258 expect(commandProgramName(command)).toBe(program);
259 });
260​
261 it.each([
262 ["timeout --help", "timeout"],
263 ["nohup --version", "nohup"],
264 ["arch", "arch"],
265 ["bundle install", "bundle"],
266 ["script output.log", "script"],
267 ["/usr/bin/timeout 10 node app.js", "timeout"],
268 ])("keeps process-launch wrappers when no safe payload is present: %s", (command, program) => {
269 expect(commandProgramName(command)).toBe(program);
270 });
271​
272 it.each([
273 "command -v git",
274 "command -V git",
275 "command -a git",
276 "command -pv git",
277 "builtin -p",
278 "exec",
279 "exec > output.log",
280 "exec --",
281 "exec cd /tmp && npm test",
282 "exec env CI=1 cd /tmp && npm test",
283 "exec CI=1 npm test",
284 "env CI=1 cd /tmp && npm test",
285 "env CI=1; npm test",
286 "sudo cd /tmp && npm test",
287 "command CI=1 npm test",
288 "command false && npm test",
289 "cmd /c false && npm test",
290 "cd /tmp <<EOF\nunterminated heredoc",
291 "cd /tmp && >/tmp/log",
292 "(xcrun simctl io booted recordVideo /tmp/video.mp4 &) ; wait",
293 "export CI=1 && (bundle exec pod install || pod install)",
294 "$PY scripts/check.py",
295 "${TOOL} --version",
296 "%TOOL% --version",
297 "!TOOL! --version",
298 "& $tool --version",
299 "& { Get-Process }",
300 "$value = 'configured'",
301 "$headers = @{ 'Accept' = 'application/json'; 'Content-Type' = 'application/json' }; Invoke-WebRequest https://example.com",
302 '"sha256(value)=$hash"',
303 "broken{",
304 "@echo off",
305 ":: comment",
306 "time -- npm test",
307 "time -v npm test",
308 "coproc npm test",
309 "coproc worker { npm test; }",
310 "cd [first|second] && pnpm test",
311 "npm) --version",
312 "try { Invoke-WebRequest https://example.com } catch { Write-Error $_ }",
313 "for($i=0; $i -lt 2; $i++){ Start-Sleep 1 }",
314 "# comment only",
315 ])("does not treat shell lookup and commandless wrapper forms as executions: %s", (command) => {
316 expect(commandProgramName(command)).toBeNull();
317 });
318​
319 it.each([
320 ["parallel -j4", "parallel"],
321 ["hash --help", "hash"],
322 ["process --help", "process"],
323 ["rem comment", "rem"],
324 ["Exec node app.js", "Exec"],
325 ["CD /tmp && npm test", "CD"],
326 ])("does not hide legitimate or case-distinct program names: %s", (command, program) => {
327 expect(commandProgramName(command)).toBe(program);
328 });
329​
330 it.each([
331 ["env sh -c 'cd /tmp && npm test'", "npm"],
332 ["sudo zsh -lc 'export CI=1 && pnpm test'", "pnpm"],
333 ])("parses shell setup inside an explicitly launched shell: %s", (command, program) => {
334 expect(commandProgramName(command)).toBe(program);
335 });
336​
337 it.each([
338 ["nocorrect pnpm test", "pnpm"],
339 ["noglob bun test", "bun"],
340 ["time node app.js", "node"],
341 ["time -p deno test", "deno"],
342 ["time nocorrect npm test", "npm"],
343 ])("skips shell precommand modifiers: %s", (command, program) => {
344 expect(commandProgramName(command)).toBe(program);
345 });
346​
347 it("uses the command after leading shell comments", () => {
348 expect(commandProgramName("# first comment\n # second comment\ngit status")).toBe("git");
349 });
350​
351 it.each([
352 ["CI=1 # note\nnpm test", "npm"],
353 ["CI=1 # it's configured\nnpm test", "npm"],
354 ['CI=1 # "unterminated quote\nbun test', "bun"],
355 [">/tmp/log # note\npnpm test", "pnpm"],
356 [">/tmp/log # it's configured\ndeno test", "deno"],
357 ])("skips comments after commandless shell setup: %s", (command, program) => {
358 expect(commandProgramName(command)).toBe(program);
359 });
360​
361 it.each([
362 ["./cd /tmp && npm test", "cd"],
363 ["/opt/exec node app.js", "exec"],
364 ["/usr/bin/time npm test", "time"],
365 ["/usr/bin/test -f package.json", "test"],
366 ])("does not treat qualified paths as shell syntax: %s", (command, program) => {
367 expect(commandProgramName(command)).toBe(program);
368 });
369​
370 it("skips a shell array assignment before the command", () => {
371 expect(commandProgramName("items=(one two); npm test")).toBe("npm");
372 });
373​
374 it.each([
375 ['EMU="/opt/android/emulator"; "$EMU" -list-avds', "emulator"],
376 ["AAPT=/opt/android/aapt2\n$AAPT dump badging app.apk", "aapt2"],
377 [
378 'SSH=(ssh -i /tmp/key -o IdentitiesOnly=yes); HOST=user@example; "${SSH[@]}" "$HOST" uptime',
379 "ssh",
380 ],
381 ["SCP=(/usr/bin/scp -i /tmp/key); if true; then ${SCP[@]} file user@example:/tmp; fi", "scp"],
382 ['TOOL="/Applications/My Tool/bin/check"; "$TOOL" --verbose', "check"],
383 ])("resolves literal command aliases from earlier shell segments: %s", (command, program) => {
384 expect(commandProgramName(command)).toBe(program);
385 });
386​
387 it.each([
388 'TOOL=$(pick-command); "$TOOL" --version',
389 'TOOL="git status"; "$TOOL"',
390 "# TOOL=git\n$TOOL status",
391 'TOOL=git true; "$TOOL" status',
392 'TOOL=git; TOOL=$(pick-command); "$TOOL" status',
393 'TOOL=git; unset TOOL; "$TOOL" status',
394 ])("does not evaluate dynamic or non-persistent command aliases: %s", (command) => {
395 expect(commandProgramName(command)).toBeNull();
396 });
397​
398 it("does not retain aliases assigned inside control flow", () => {
399 expect(commandProgramName('TOOL=git; if false; then\nTOOL=npm\nfi\n"$TOOL" status')).toBe(
400 "git",
401 );
402 });
403​
404 it.each([
405 ["ROOT=${BASE:-path with spaces}; npm test", "npm"],
406 ["ROOT=`printf 'path with spaces'`; pnpm test", "pnpm"],
407 ])("keeps expansions inside assignment words: %s", (command, program) => {
408 expect(commandProgramName(command)).toBe(program);
409 });
410​
411 it.each([
412 [">/tmp/log && npm test", "npm"],
413 ["2>/tmp/error.log; pnpm test", "pnpm"],
414 ["cd /tmp && >/tmp/log npm test", "npm"],
415 ["cd /tmp && > /tmp/log pnpm test", "pnpm"],
416 ["cd /tmp && 2>&1 bun test", "bun"],
417 ["cd /tmp && 2>& 1 node app.js", "node"],
418 ["cd /tmp && &>/tmp/log git status", "git"],
419 ["cd /tmp && *>>/tmp/log vp test", "vp"],
420 ["cd /tmp && {output}>/tmp/log deno test", "deno"],
421 ["cd /tmp && <<<input ruby script.rb", "ruby"],
422 ])("skips redirections before the next command: %s", (command, program) => {
423 expect(commandProgramName(command)).toBe(program);
424 });
425​
426 it.each([
427 ["cd /tmp <<EOF\nnot-a-command\nEOF\nnpm test", "npm"],
428 ["cd /tmp <<'EOF'\nnot-a-command\nEOF\npnpm test", "pnpm"],
429 ["cd /tmp <<'EOF'\nnot-a-command\\\nEOF\npnpm test", "pnpm"],
430 ["cd /tmp <<-EOF\n\tnot-a-command\n\tEOF\nbun test", "bun"],
431 ["cd /tmp <<A <<B\none\nA\ntwo\nB\ngit status", "git"],
432 ["cd /tmp <<EOF; # setup\nnot-a-command\nEOF\nnpm test", "npm"],
433 ["cd /tmp <<EOF && pnpm test\nnot-a-command\nEOF\nbun test", "pnpm"],
434 ])("skips heredoc bodies before finding the next command: %s", (command, program) => {
435 expect(commandProgramName(command)).toBe(program);
436 });
437​
438 it.each(["cd apps/web && [ -f package.json ]", "cd apps/web || exit 1", "cd one && cd two"])(
439 "falls back when no useful program follows cd: %s",
440 (command) => {
441 expect(commandProgramName(command)).toBeNull();
442 },
443 );
444​
445 it.each([
446 "if test -f package.json\nthen\n npm test\nfi",
447 "[[ -d first && -d second ]] && npm test",
448 'for file in *\ndo\n echo "$file"\ndone',
449 "while true\ndo\n sleep 1\ndone",
450 "cd /tmp; build () { npm test; }",
451 ])("does not label commands inside multiline shell control flow: %s", (command) => {
452 expect(commandProgramName(command)).toBeNull();
453 });
454​
455 it("bounds nested shell unwrapping", () => {
456 let command = "git status";
457 for (let depth = 0; depth < 9; depth += 1) {
458 command = `sh -c '${command.replaceAll("'", "'\\''")}'`;
459 }
460 expect(commandProgramName(command)).toBeNull();
461 });
462​
463 it("does not spend the shell nesting budget on setup commands", () => {
464 const command = [
465 ...Array.from({ length: 10 }, (_, index) => `export VALUE_${index}=configured`),
466 "npm test",
467 ].join("\n");
468​
469 expect(commandProgramName(command)).toBe("npm");
470 });
471​
472 it("bounds the number of top-level setup segments", () => {
473 const command = [
474 ...Array.from({ length: 2_000 }, (_, index) => `export VALUE_${index}=configured`),
475 "npm test",
476 ].join(";");
477​
478 expect(commandProgramName(command)).toBeNull();
479 });
480​
481 it("bounds nested command wrappers", () => {
482 expect(commandProgramName(`${"command ".repeat(9)}git status`)).toBeNull();
483 });
484});
485​
486describe("commandDisplayText", () => {
487 it.each([
488 ["/bin/zsh -lc 'git diff --stat'", "git diff --stat"],
489 ['bash -c "printf \\"%s\\" hello; git status"', 'printf "%s" hello; git status'],
490 ["/usr/bin/fish --command 'rg TODO src | head -20'", "rg TODO src | head -20"],
491 ["zsh -lc 'pwd\nls'", "pwd\nls"],
492 ["git status", "git status"],
493 ["zsh script.sh", "zsh script.sh"],
494 ["zsh -lc 'echo $1' name value", "zsh -lc 'echo $1' name value"],
495 ["zsh -lc 'pwd'; git status", "zsh -lc 'pwd'; git status"],
496 ["zsh -lc 'unterminated", "zsh -lc 'unterminated"],
497 ])("shows the script without hiding meaningful outer commands: %s", (input, expected) => {
498 expect(commandDisplayText(input)).toBe(expected);
499 });
500});
501​
502describe("commands recorded from each provider", () => {
503 const interruptScript = `node -e "console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)"`;
504 it.each([
505 ["Claude", interruptScript, interruptScript],
506 ["Cursor", interruptScript, interruptScript],
507 ["Pi", interruptScript, interruptScript],
508 [
509 "Codex",
510 `/bin/bash -lc "node -e \\"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\\""`,
511 interruptScript,
512 ],
513 [
514 "Codex",
515 "/bin/bash -lc 'cat package.json && cat tsconfig.json'",
516 "cat package.json && cat tsconfig.json",
517 ],
518 [
519 "Grok",
520 "for i in 1 2 3; do sleep 8; echo tock $i; done",
521 "for i in 1 2 3; do sleep 8; echo tock $i; done",
522 ],
523 [
524 "OpenCode",
525 "printf 'running child approval' > running-child.txt",
526 "printf 'running child approval' > running-child.txt",
527 ],
528 ])("%s shows the script it ran: %s", (_provider, input, script) => {
529 expect(commandDisplayText(input)).toBe(script);
530 expect(commandHighlightLanguage(commandDisplayText(input))).toBe("shellscript");
531 });
532});
533​
534describe("commandDisplayText with redacted Codex commands", () => {
535 // Codex replaces secrets in command text before sending it
536 // (codex-rs/secrets/src/sanitizer.rs). Usually the quoting survives.
537 it("unwraps a redacted command whose quoting is intact", () => {
538 expect(
539 commandDisplayText(
540 `/bin/zsh -lc 'curl -H "Authorization: Bearer [REDACTED_SECRET]" https://example.com'`,
541 ),
542 ).toBe('curl -H "Authorization: Bearer [REDACTED_SECRET]" https://example.com');
543 });
544​
545 // Its secret-assignment pattern can also swallow the backslash of an escaped
546 // quote: `token=config[\"apiToken\"]` becomes `token=[REDACTED_SECRET]"apiToken\"]`.
547 // The bare quote ends the -lc argument early, so where the script ends is unknown.
548 it("keeps the wrapper when redaction broke the quoting", () => {
549 const input = `/bin/zsh -lc "python3 -c 'token=[REDACTED_SECRET]"apiToken\\"]; print(token)'"`;
550 expect(commandDisplayText(input)).toBe(input);
551 });
552});
553​
554describe("commandDisplayText control characters", () => {
555 it.each([
556 ["printf '\u001b[31mred\u001b[0m'", "printf '␛[31mred␛[0m'"],
557 ["zsh -lc 'printf \"\u001b[1m\"'", 'printf "␛[1m"'],
558 ["echo bell\u0007 delete\u007f", "echo bell␇ delete␡"],
559 ["printf 'a\\tb'\r\necho done", "printf 'a\\tb'\r\necho done"],
560 ["cat <<'EOF'\n\tindented\nEOF", "cat <<'EOF'\n\tindented\nEOF"],
561 ])("shows %j as %j", (input, expected) => {
562 expect(commandDisplayText(input)).toBe(expected);
563 });
564});
565​
566describe("commandHighlightLanguage", () => {
567 it.each([
568 [
569 '"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -NoProfile -Command "Get-ChildItem -Recurse"',
570 "powershell",
571 ],
572 [
573 'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\\work\\scripts\\doctor.ps1"',
574 "powershell",
575 ],
576 ["& pwsh -c 'Get-Date'", "powershell"],
577 ["PWSH.EXE -Command Get-Date", "powershell"],
578 ["pwsh-preview -c 'Get-Date'", "shellscript"],
579 ["git status; pwsh -c 'Get-Date'", "shellscript"],
580 ["cat <<'EOF' > notes.txt\npwsh\nEOF", "shellscript"],
581 ["", "shellscript"],
582 ["Get-Content package.json | Select-String version", "powershell"],
583 ["$env:CI='1'; npm test", "powershell"],
584 ["$tmp = Join-Path $env:TEMP repo; git clone example", "powershell"],
585 ['"=== CHECK FILE ==="; Get-Content file.txt', "powershell"],
586 ["& 'C:\\Python312\\python.exe' -c 'print(1)'", "powershell"],
587 ["Install-Module Pester -Scope CurrentUser", "powershell"],
588 ["ConvertTo-Json @{ a = 1 }", "powershell"],
589 ["update-alternatives --list java", "shellscript"],
590 ["install-info --version", "shellscript"],
591 ["Make-Thing now", "shellscript"],
592 ["echo Get-Content; git status", "shellscript"],
593 ["FOO=1 npm test", "shellscript"],
594 ["echo $HOME && ls", "shellscript"],
595 ])("%s", (command, language) => {
596 expect(commandHighlightLanguage(command)).toBe(language);
597 });
598});