import { describe, expect, test } from "bun:test" import { Effect } from "effect" import { ShellParse } from "../src/shell/parse.js" import { Wildcard } from "../src/util/wildcard.js" describe("native shell syntax compatibility", () => { test("PowerShell invocation approvals include the operator instead of saving an ineffective prefix", async () => { const command = "& $Command value" expect(await Effect.runPromise(ShellParse.scan(command, "pwsh", "/workspace"))).toEqual({ commands: [{ resource: command, save: "$Command *" }], directories: [], }) expect(await Effect.runPromise(ShellParse.scan(command, "pwsh", "/workspace", { portable: true }))).toEqual({ commands: [{ resource: command, save: "& $Command *" }], directories: [], }) }) test.each([ "ForEach-Object { Write-Output value }", "Write-Output before; ForEach-Object { Write-Output value }", "ForEach-Object { Write-Output value } | Write-Output done", "Write-Output before | ForEach-Object { Write-Output $_ }", "& ForEach-Object { Write-Output value }", "& 'ForEach-Object' { Write-Output value }", "% { Write-Output value }", "Where-Object { Write-Output value }", ])("PowerShell scriptblock callers preserve permission resources and usable approvals: %s", async (command) => { const legacy = await Effect.runPromise(ShellParse.scan(command, "pwsh", "/workspace")) const native = await Effect.runPromise(ShellParse.scan(command, "pwsh", "/workspace", { portable: true })) expect(native.commands.map((item) => item.resource)).toEqual(legacy.commands.map((item) => item.resource)) for (const item of native.commands) expect(Wildcard.match(item.resource, item.save), item.resource).toBe(true) }) for (const shell of ["bash", "zsh"]) { test.each([ "cat <<'EOF'\n$(not_a_command)\nEOF", "cat < { const legacy = await Effect.runPromise(ShellParse.scan(command, shell, "/workspace")) const native = await Effect.runPromise(ShellParse.scan(command, shell, "/workspace", { portable: true })) expect(native).toEqual(legacy) expect(await Effect.runPromise(ShellParse.scanPortable(command, shell, "/workspace"))).toEqual(native) }) } test("does not invent commands from a quoted second heredoc body", async () => { const command = "cat < Write-Output done", "Write-Output @'\nhello\n'@", "git st`atus", ])("PowerShell extracts commands without rejecting ordinary syntax: %s", async (command) => { const legacy = await Effect.runPromise(ShellParse.scan(command, "pwsh", "/workspace")) const native = await Effect.runPromise(ShellParse.scan(command, "pwsh", "/workspace", { portable: true })) expect(native).toEqual(legacy) expect(await Effect.runPromise(ShellParse.scanPortable(command, "pwsh", "/workspace"))).toEqual(native) }) })