auto-mode.test.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // kilocode_change - new file
  2. import { describe, expect, test } from "bun:test"
  3. describe("Auto mode flag", () => {
  4. test("auto mode should create session with allow-all permissions except questions", () => {
  5. // When --auto flag is set, the session should be created with:
  6. // 1. Wildcard allow rule for all permissions
  7. // 2. Explicit deny rule for questions (to prevent user interaction)
  8. const autoPermissions = [
  9. {
  10. permission: "*",
  11. action: "allow" as const,
  12. pattern: "*",
  13. },
  14. {
  15. permission: "question",
  16. action: "deny" as const,
  17. pattern: "*",
  18. },
  19. ]
  20. expect(autoPermissions).toHaveLength(2)
  21. // First rule: allow all
  22. expect(autoPermissions[0].permission).toBe("*")
  23. expect(autoPermissions[0].action).toBe("allow")
  24. expect(autoPermissions[0].pattern).toBe("*")
  25. // Second rule: deny questions (comes after wildcard to override it)
  26. expect(autoPermissions[1].permission).toBe("question")
  27. expect(autoPermissions[1].action).toBe("deny")
  28. expect(autoPermissions[1].pattern).toBe("*")
  29. })
  30. test("non-auto mode should not set allow-all permissions", () => {
  31. // When --auto flag is NOT set, permissions should be undefined or default
  32. const normalPermissions = undefined
  33. expect(normalPermissions).toBeUndefined()
  34. })
  35. test("permission evaluation order matters (findLast behavior)", () => {
  36. // The permission system uses findLast, so the last matching rule wins
  37. // This test verifies that the question deny rule comes AFTER the wildcard
  38. const autoPermissions = [
  39. { permission: "*", action: "allow" as const, pattern: "*" },
  40. { permission: "question", action: "deny" as const, pattern: "*" },
  41. ]
  42. // Simulate findLast behavior
  43. const findLastMatch = (permission: string) => {
  44. for (let i = autoPermissions.length - 1; i >= 0; i--) {
  45. if (permission === autoPermissions[i].permission || autoPermissions[i].permission === "*") {
  46. return autoPermissions[i]
  47. }
  48. }
  49. return null
  50. }
  51. // Test that "question" permission resolves to "deny"
  52. const questionRule = findLastMatch("question")
  53. expect(questionRule?.action).toBe("deny")
  54. // Test that other permissions resolve to "allow"
  55. const bashRule = findLastMatch("bash")
  56. expect(bashRule?.action).toBe("allow")
  57. const editRule = findLastMatch("edit")
  58. expect(editRule?.action).toBe("allow")
  59. const externalDirRule = findLastMatch("external_directory")
  60. expect(externalDirRule?.action).toBe("allow")
  61. })
  62. })