summaryrefslogtreecommitdiff
path: root/lua/99/test/agents_spec.lua
blob: 8638e6e46ddb815d27c9f83e9015685becebc2b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
-- luacheck: globals describe it assert
local Agents = require("99.extensions.agents")
local eq = assert.are.same

local function a(p)
  return vim.fs.joinpath(vim.uv.cwd(), p)
end

local custom_mds = {
  {
    name = "back-end",
    path = "scratch/custom_rules/back-end/SKILL.md",
    absolute_path = a("scratch/custom_rules/back-end/SKILL.md"),
  },
  {
    name = "foo",
    path = "scratch/custom_rules/foo/SKILL.md",
    absolute_path = a("scratch/custom_rules/foo/SKILL.md"),
  },
  {
    name = "front-end",
    path = "scratch/custom_rules/front-end/SKILL.md",
    absolute_path = a("scratch/custom_rules/front-end/SKILL.md"),
  },
  {
    name = "vim.lsp",
    path = "scratch/custom_rules/vim.lsp/SKILL.md",
    absolute_path = a("scratch/custom_rules/vim.lsp/SKILL.md"),
  },
  {
    name = "vim",
    path = "scratch/custom_rules/vim/SKILL.md",
    absolute_path = a("scratch/custom_rules/vim/SKILL.md"),
  },
  {
    name = "vim",
    path = "scratch/custom_rules_2/vim/SKILL.md",
    absolute_path = a("scratch/custom_rules_2/vim/SKILL.md"),
  },
  {
    name = "vim.treesitter",
    path = "scratch/custom_rules/vim.treesitter/SKILL.md",
    absolute_path = a("scratch/custom_rules/vim.treesitter/SKILL.md"),
  },
}

--- @param custom string | string[]
--- @return _99.State
local function r(custom)
  custom = type(custom) == "string" and { custom } or custom
  return {
    completion = {
      custom_rules = custom,
    },
  }
end

--- @param rules _99.Agents.Rules
--- @return string[]
local function get_names(rules)
  local names = {}
  local found = {}
  for _, rule in ipairs(rules.custom or {}) do
    if not found[rule.name] then
      found[rule.name] = true
      table.insert(names, rule.name)
    end
  end
  return names
end

--- @param rule_to_find _99.Agents.Rule
local function rule_exists(rule_to_find)
  for _, rule in ipairs(custom_mds) do
    if rule.name == rule_to_find.name and rule.path == rule_to_find.path then
      return
    end
  end
  assert(false, "could not find rule: " .. vim.inspect(rule_to_find))
end

describe("rules: <name>/SKILL.md", function()
  it("rules", function()
    local _99 = r({
      "scratch/custom_rules/",
      "scratch/custom_rules_2/",
    })
    local rules = Agents.rules(_99)
    local names = get_names(rules)
    eq(6, #names)
    for _, n in ipairs(names) do
      local rule_set = rules.by_name[n]
      eq("table", type(rule_set))
      eq(n == "vim" and 2 or 1, #rule_set)
      for _, rule in ipairs(rule_set) do
        rule_exists(rule)
      end
    end
  end)

  it("find rules", function()
    local _99 = r({
      "scratch/custom_rules/",
      "scratch/custom_rules_2/",
    })
    local rules = Agents.rules(_99)
    local prompt = "here is a test back-end #front-end and #vim.ls"
    local found = Agents.by_name(rules, prompt)

    eq({ "front-end" }, found.names)
    eq(rules.by_name["front-end"], found.rules)
  end)

  it("should validate that tokens exist by path and name", function()
    local _99 = r({
      "scratch/custom_rules/",
      "scratch/custom_rules_2/",
    })
    local rules = Agents.rules(_99)

    -- Test by path
    eq(true, Agents.is_rule(rules, "scratch/custom_rules/back-end/SKILL.md"))
    eq(true, Agents.is_rule(rules, "scratch/custom_rules/foo/SKILL.md"))
    eq(true, Agents.is_rule(rules, "scratch/custom_rules/front-end/SKILL.md"))
    eq(true, Agents.is_rule(rules, "scratch/custom_rules/vim.lsp/SKILL.md"))
    eq(true, Agents.is_rule(rules, "scratch/custom_rules/vim/SKILL.md"))
    eq(
      true,
      Agents.is_rule(rules, "scratch/custom_rules/vim.treesitter/SKILL.md")
    )

    -- Test by name
    eq(true, Agents.is_rule(rules, "back-end"))
    eq(true, Agents.is_rule(rules, "foo"))
    eq(true, Agents.is_rule(rules, "front-end"))
    eq(true, Agents.is_rule(rules, "vim"))

    -- Test invalid
    eq(false, Agents.is_rule(rules, "nonexistent"))
    eq(false, Agents.is_rule(rules, "invalid-token"))
    eq(false, Agents.is_rule(rules, ""))
  end)
end)