shell-parse.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { describe, expect, test } from "bun:test"
  2. import { Effect } from "effect"
  3. import os from "os"
  4. import path from "path"
  5. import { ShellParse } from "@opencode-ai/core/shell/parse"
  6. describe("ShellParse", () => {
  7. test("splits bash commands and derives reusable prefixes", async () => {
  8. const result = await Effect.runPromise(
  9. ShellParse.scan("git status && npm run test -- --watch", "/bin/bash", "/workspace"),
  10. )
  11. expect(result).toEqual({
  12. commands: [
  13. { resource: "git status", save: "git status *" },
  14. { resource: "npm run test -- --watch", save: "npm run test *" },
  15. ],
  16. directories: [],
  17. })
  18. })
  19. test("splits PowerShell commands case-insensitively", async () => {
  20. const result = await Effect.runPromise(
  21. ShellParse.scan("Get-ChildItem; Write-Output 'done'", "C:\\Program Files\\PowerShell\\7\\pwsh.exe", "C:\\workspace"),
  22. )
  23. expect(result.commands).toEqual([
  24. { resource: "Get-ChildItem", save: "Get-ChildItem *" },
  25. { resource: "Write-Output 'done'", save: "Write-Output *" },
  26. ])
  27. })
  28. test("does not permission directory changes separately", async () => {
  29. const result = await Effect.runPromise(ShellParse.scan("cd 'src dir' && git status", "/bin/bash", "/workspace"))
  30. expect(result).toEqual({
  31. commands: [{ resource: "git status", save: "git status *" }],
  32. directories: ["src dir"],
  33. })
  34. })
  35. test("extracts PowerShell directory parameters", async () => {
  36. const result = await Effect.runPromise(
  37. ShellParse.scan("Set-Location -LiteralPath '..\\outside'; Get-ChildItem", "pwsh", "C:\\workspace"),
  38. )
  39. expect(result.directories).toEqual(["..\\outside"])
  40. })
  41. test("expands deterministic directory variables", async () => {
  42. const bash = await Effect.runPromise(ShellParse.scan("cd ~/src", "/bin/bash", "/workspace"))
  43. expect(bash.directories).toEqual([path.join(os.homedir(), "src")])
  44. const powershell = await Effect.runPromise(
  45. ShellParse.scan('Set-Location "$PWD/src"; Set-Location $PSHOME', "/usr/local/bin/pwsh", "/workspace"),
  46. )
  47. expect(powershell.directories).toEqual(["/workspace/src", "/usr/local/bin"])
  48. })
  49. })