|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { embeddedScripts } from "./embeddedScripts"; |
| 4 | + |
| 5 | +/** Each script's language, its text, and the part of the command that spells it. */ |
| 6 | +function scriptsOf(command: string) { |
| 7 | + return embeddedScripts(command).map((script) => { |
| 8 | + // Every character maps inside the command, in order. |
| 9 | + script.starts.forEach((start, index) => { |
| 10 | + expect(start).toBeLessThan(script.ends[index]!); |
| 11 | + if (index > 0) expect(start).toBeGreaterThanOrEqual(script.ends[index - 1]!); |
| 12 | + }); |
| 13 | + return [ |
| 14 | + script.language, |
| 15 | + script.text, |
| 16 | + command.slice(script.starts[0], script.ends.at(-1)), |
| 17 | + ] as const; |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +const pythonScript = `from pathlib import Path |
| 22 | +import json |
| 23 | +
|
| 24 | +# print every package.json name and version |
| 25 | +for p in sorted(Path('.').glob('**/package.json')): |
| 26 | + data = json.loads(p.read_text()) |
| 27 | + print(f"{data['name']:16} {data.get('version', '?')}") |
| 28 | +`; |
| 29 | + |
| 30 | +describe("embeddedScripts", () => { |
| 31 | + it("finds the script inside a Codex login-shell wrapper and the script inside that", () => { |
| 32 | + // Recorded in the turn_interrupt_mid_tool Codex transcript |
| 33 | + // (apps/server/src/orchestration-v2/testkit/fixtures). |
| 34 | + const inner = `console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)`; |
| 35 | + const command = `/bin/bash -lc "node -e \\"${inner}\\""`; |
| 36 | + expect(scriptsOf(command)).toEqual([ |
| 37 | + ["shellscript", `node -e "${inner}"`, `node -e \\"${inner}\\"`], |
| 38 | + ["javascript", inner, inner], |
| 39 | + ]); |
| 40 | + }); |
| 41 | + |
| 42 | + it("highlights a heredoc fed to an interpreter in that interpreter's language", () => { |
| 43 | + const command = `python3 - <<'PY'\n${pythonScript}PY`; |
| 44 | + expect(scriptsOf(command)).toEqual([["python", pythonScript, pythonScript]]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("maps escaped characters back to both source characters", () => { |
| 48 | + const command = `/bin/zsh -lc "python3 - <<'PY'\nprint(\\"\\$HOME\\")\nPY"`; |
| 49 | + const [shell, python] = scriptsOf(command); |
| 50 | + expect(shell?.[1]).toBe(`python3 - <<'PY'\nprint("$HOME")\nPY`); |
| 51 | + expect(python).toEqual(["python", `print("$HOME")\n`, `print(\\"\\$HOME\\")\n`]); |
| 52 | + }); |
| 53 | + |
| 54 | + it.each([ |
| 55 | + [`python3 -c 'print(1)'`, "python", "print(1)"], |
| 56 | + [`python3 -X dev -c 'print(1)'`, "python", "print(1)"], |
| 57 | + [`node --eval "process.exit(0)"`, "javascript", "process.exit(0)"], |
| 58 | + [`bun -e 'console.log(Bun.version)'`, "typescript", "console.log(Bun.version)"], |
| 59 | + [`deno eval 'console.log(1)'`, "typescript", "console.log(1)"], |
| 60 | + [`ruby -e 'puts 1'`, "ruby", "puts 1"], |
| 61 | + [`perl -E 'say 1'`, "perl", "say 1"], |
| 62 | + [`sqlite3 app.db "select * from users"`, "sql", "select * from users"], |
| 63 | + [`psql -d app -c 'select 1'`, "sql", "select 1"], |
| 64 | + [`pwsh -NoProfile -Command "Get-ChildItem"`, "powershell", "Get-ChildItem"], |
| 65 | + [`env FOO=1 sudo -u bob python3 -c 'print(1)'`, "python", "print(1)"], |
| 66 | + [`echo "$(node -e 'console.log(1)')"`, "javascript", "console.log(1)"], |
| 67 | + [`python3 <<< 'print(1)'`, "python", "print(1)"], |
| 68 | + ])("finds the inline script in %s", (command, language, script) => { |
| 69 | + expect(scriptsOf(command)).toEqual([[language, script, expect.any(String)]]); |
| 70 | + }); |
| 71 | + |
| 72 | + it.each([ |
| 73 | + ["the file `cat` writes", `cat > src/app.ts <<'EOF'\nconst a = 1;\nEOF`, "typescript"], |
| 74 | + ["the file `tee` writes", `tee -a config.json <<EOF\n{}\nEOF`, "json"], |
| 75 | + ["its delimiter", `kubectl apply -f - <<'YAML'\nkind: Pod\nYAML`, "yaml"], |
| 76 | + ["the shell reading it", `bash <<'EOF'\necho hi\nEOF`, "shellscript"], |
| 77 | + ])("takes a heredoc's language from %s", (_source, command, language) => { |
| 78 | + expect(scriptsOf(command).map(([lang]) => lang)).toEqual([language]); |
| 79 | + }); |
| 80 | + |
| 81 | + it.each([ |
| 82 | + ["a script file", `python3 build.py <<EOF\ndata\nEOF`], |
| 83 | + ["a module", `python3 -m json.tool <<EOF\n{}\nEOF`], |
| 84 | + ["plain commands", `rg -n "foo" src && git status`], |
| 85 | + ["an unnamed heredoc into a plain command", `git commit -F - <<'EOF'\nfix: x\nEOF`], |
| 86 | + ["an empty script", `bash -lc ' '`], |
| 87 | + ])("finds nothing for %s", (_case, command) => { |
| 88 | + expect(scriptsOf(command)).toEqual([]); |
| 89 | + }); |
| 90 | + |
| 91 | + it("trusts nothing in a command whose quoting does not parse", () => { |
| 92 | + // Codex's secret redaction dropped the backslash of an escaped quote, so |
| 93 | + // the -lc argument ends early and the rest no longer balances. |
| 94 | + const command = `/bin/zsh -lc "python3 -c 'token=[REDACTED_SECRET]"apiToken\\"]; print(token)'"`; |
| 95 | + expect(scriptsOf(command)).toEqual([]); |
| 96 | + }); |
| 97 | +}); |
0 commit comments