shell-parse.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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(
  22. "Get-ChildItem; Write-Output 'done'",
  23. "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
  24. "C:\\workspace",
  25. ),
  26. )
  27. expect(result.commands).toEqual([
  28. { resource: "Get-ChildItem", save: "Get-ChildItem *" },
  29. { resource: "Write-Output 'done'", save: "Write-Output *" },
  30. ])
  31. })
  32. test("does not permission directory changes separately", async () => {
  33. const result = await Effect.runPromise(ShellParse.scan("cd 'src dir' && git status", "/bin/bash", "/workspace"))
  34. expect(result).toEqual({
  35. commands: [{ resource: "git status", save: "git status *" }],
  36. directories: ["src dir"],
  37. })
  38. })
  39. test("extracts PowerShell directory parameters", async () => {
  40. const result = await Effect.runPromise(
  41. ShellParse.scan("Set-Location -LiteralPath '..\\outside'; Get-ChildItem", "pwsh", "C:\\workspace"),
  42. )
  43. expect(result.directories).toEqual(["..\\outside"])
  44. })
  45. test("expands deterministic directory variables", async () => {
  46. const bash = await Effect.runPromise(ShellParse.scan("cd ~/src", "/bin/bash", "/workspace"))
  47. expect(bash.directories).toEqual([path.join(os.homedir(), "src")])
  48. const powershell = await Effect.runPromise(
  49. ShellParse.scan('Set-Location "$PWD/src"; Set-Location $PSHOME', "/usr/local/bin/pwsh", "/workspace"),
  50. )
  51. expect(powershell.directories).toEqual(["/workspace/src", "/usr/local/bin"])
  52. })
  53. })