Files
Kilo-Org_kilocode/packages/opencode/test/kilocode/bash-hierarchy.test.ts
Imanol Maiztegui 0484318191 Granular bash permission rules (#7091)
* fix: renamed pattern arrays used for permission rules

* fix: rename savePatternRules to saveAlwaysRules

* feat: generate hierarchical always patterns in bash tool

* feat: validate saveAlwaysRules against always array only

* feat: pass always field through extension bridge to webview

* feat: show hierarchical always patterns in PermissionDock dropdown

* fix: rename pattern to rule in PermissionDock translations, styles, and data-slots

* fix(vscode): correct auto-run comment in PermissionDock

* feat(kilo-vscode): strip trailing wildcard from permission rule labels

* fix: deduplicate permission rules and strip wildcards from display labels

* refactor: move hierarchy to metadata.rules, restore always to arity prefix

* feat: pass always field to permission dock for non-bash tool rules

* fix: correct bash test assertion to check metadata.rules and fix indentation

* fix: hide permission rules dropdown for non-bash tools

* fix: align always-rules route descriptions, whitespace, and restore multi-command tests
2026-03-16 17:43:16 +01:00

65 lines
2.7 KiB
TypeScript

import { test, expect, describe } from "bun:test"
import { BashHierarchy } from "../../src/kilocode/bash-hierarchy"
function collect(command: string[], text: string): string[] {
const set = new Set<string>()
BashHierarchy.addAll(set, command, text)
return [...set]
}
describe("BashHierarchy.addAll", () => {
test("arity-1 command with args produces base wildcard + exact", () => {
// "ls" has arity 1, prefix = ["ls"], text "ls -la" !== "ls" → exact is added
const result = collect(["ls", "-la"], "ls -la")
expect(result).toContain("ls *")
expect(result).toContain("ls -la")
})
test("arity-2 command without extra args skips redundant exact text", () => {
// "git status" has arity 2, prefix = ["git", "status"], text === prefix → no exact
const result = collect(["git", "status"], "git status")
expect(result).toEqual(["git *", "git status *"])
})
test("arity-2 command with extra args includes exact text", () => {
// "npm install lodash" has arity 2, prefix = ["npm", "install"], text !== prefix → exact added
const result = collect(["npm", "install", "lodash"], "npm install lodash")
expect(result).toEqual(["npm *", "npm install *", "npm install lodash"])
})
test("arity-3 command without extra args skips redundant exact text", () => {
// "npm run dev" has arity 3, prefix = ["npm", "run", "dev"], text === prefix → no exact
const result = collect(["npm", "run", "dev"], "npm run dev")
expect(result).toEqual(["npm *", "npm run *", "npm run dev *"])
})
test("arity-3 command with extra args includes exact text", () => {
const result = collect(["docker", "compose", "up", "-d"], "docker compose up -d")
expect(result).toEqual(["docker *", "docker compose *", "docker compose up *", "docker compose up -d"])
})
test("single token command without args skips redundant exact text", () => {
// "pwd" has arity 1, prefix = ["pwd"], text === prefix → no exact
const result = collect(["pwd"], "pwd")
expect(result).toEqual(["pwd *"])
})
test("empty command returns empty", () => {
const result = collect([], "")
expect(result).toEqual([])
})
test("unknown command with args includes exact text", () => {
const result = collect(["mycustomtool", "arg1", "arg2"], "mycustomtool arg1 arg2")
expect(result).toEqual(["mycustomtool *", "mycustomtool arg1 arg2"])
})
test("duplicates are deduplicated by Set", () => {
const set = new Set<string>()
BashHierarchy.addAll(set, ["git", "status"], "git status")
BashHierarchy.addAll(set, ["git", "diff"], "git diff")
// "git *" appears in both but Set deduplicates
expect([...set].filter((p) => p === "git *")).toHaveLength(1)
})
})