arity.test.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. import { test, expect } from "bun:test"
  2. import { BashArity } from "../../src/permission/arity"
  3. test("arity 1 - unknown commands default to first token", () => {
  4. expect(BashArity.prefix(["unknown", "command", "subcommand"])).toEqual(["unknown"])
  5. expect(BashArity.prefix(["touch", "foo.txt"])).toEqual(["touch"])
  6. })
  7. test("arity 2 - two token commands", () => {
  8. expect(BashArity.prefix(["git", "checkout", "main"])).toEqual(["git", "checkout"])
  9. expect(BashArity.prefix(["docker", "run", "nginx"])).toEqual(["docker", "run"])
  10. })
  11. test("arity 3 - three token commands", () => {
  12. expect(BashArity.prefix(["aws", "s3", "ls", "my-bucket"])).toEqual(["aws", "s3", "ls"])
  13. expect(BashArity.prefix(["npm", "run", "dev", "script"])).toEqual(["npm", "run", "dev"])
  14. })
  15. test("longest match wins - nested prefixes", () => {
  16. expect(BashArity.prefix(["docker", "compose", "up", "service"])).toEqual(["docker", "compose", "up"])
  17. expect(BashArity.prefix(["consul", "kv", "get", "config"])).toEqual(["consul", "kv", "get"])
  18. })
  19. test("exact length matches", () => {
  20. expect(BashArity.prefix(["git", "checkout"])).toEqual(["git", "checkout"])
  21. expect(BashArity.prefix(["npm", "run", "dev"])).toEqual(["npm", "run", "dev"])
  22. })
  23. test("edge cases", () => {
  24. expect(BashArity.prefix([])).toEqual([])
  25. expect(BashArity.prefix(["single"])).toEqual(["single"])
  26. expect(BashArity.prefix(["git"])).toEqual(["git"])
  27. })