Skip to content

Commit 4f643c7

Browse files
authored
fix(prompts): use ordered map for enabled tools (#1551)
And add notify test See #1550 Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 8b58670 commit 4f643c7

2 files changed

Lines changed: 130 additions & 3 deletions

File tree

lua/CopilotChat/prompts.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local constants = require('CopilotChat.constants')
33
local functions = require('CopilotChat.functions')
44
local notify = require('CopilotChat.notify')
55
local files = require('CopilotChat.utils.files')
6+
local orderedmap = require('CopilotChat.utils.orderedmap')
67
local utils = require('CopilotChat.utils')
78

89
local WORD = '([^%s:]+)'
@@ -66,7 +67,7 @@ function M.resolve_tools(prompt, config)
6667
tools[tool.name] = tool
6768
end
6869

69-
local enabled_tools = {}
70+
local enabled_tools = orderedmap()
7071
local tool_matches = utils.to_table(config.tools)
7172

7273
-- Check for @tool pattern to find enabled tools
@@ -82,12 +83,12 @@ function M.resolve_tools(prompt, config)
8283
for _, match in ipairs(tool_matches) do
8384
for name, tool in pairs(config.functions) do
8485
if name == match or tool.group == match then
85-
table.insert(enabled_tools, tools[name])
86+
enabled_tools:set(name, tools[name])
8687
end
8788
end
8889
end
8990

90-
return enabled_tools, prompt
91+
return enabled_tools:values(), prompt
9192
end
9293

9394
--- Call and resolve function calls from the prompt.

tests/notify_spec.lua

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
local notify = require('CopilotChat.notify')
2+
3+
describe('CopilotChat.notify', function()
4+
before_each(function()
5+
-- Clear all listeners before each test
6+
notify.clear()
7+
end)
8+
9+
describe('publish and listen', function()
10+
it('calls listener when event is published', function()
11+
local called = false
12+
local received_data = nil
13+
14+
notify.listen('test_event', function(data)
15+
called = true
16+
received_data = data
17+
end)
18+
19+
notify.publish('test_event', 'test_data')
20+
21+
assert.is_true(called)
22+
assert.equals('test_data', received_data)
23+
end)
24+
25+
it('supports multiple listeners for same event', function()
26+
local count = 0
27+
28+
notify.listen('test_event', function(data)
29+
count = count + 1
30+
end)
31+
notify.listen('test_event', function(data)
32+
count = count + 10
33+
end)
34+
35+
notify.publish('test_event', 'data')
36+
37+
assert.equals(11, count)
38+
end)
39+
40+
it('does not call listeners for different events', function()
41+
local called = false
42+
43+
notify.listen('event_a', function(data)
44+
called = true
45+
end)
46+
47+
notify.publish('event_b', 'data')
48+
49+
assert.is_false(called)
50+
end)
51+
52+
it('passes correct data to listeners', function()
53+
local received = nil
54+
55+
notify.listen('test_event', function(data)
56+
received = data
57+
end)
58+
59+
notify.publish('test_event', { foo = 'bar', num = 123 })
60+
61+
assert.are.same({ foo = 'bar', num = 123 }, received)
62+
end)
63+
64+
it('handles nil and empty data', function()
65+
local received = 'not_called'
66+
67+
notify.listen('test_event', function(data)
68+
received = data
69+
end)
70+
71+
notify.publish('test_event', nil)
72+
assert.is_nil(received)
73+
74+
notify.publish('test_event', '')
75+
assert.equals('', received)
76+
end)
77+
78+
it('handles publishing to events with no listeners', function()
79+
-- Should not error
80+
assert.has_no.errors(function()
81+
notify.publish('nonexistent_event', 'data')
82+
end)
83+
end)
84+
end)
85+
86+
describe('clear', function()
87+
it('removes all listeners', function()
88+
local called = false
89+
90+
notify.listen('test_event', function(data)
91+
called = true
92+
end)
93+
94+
notify.clear()
95+
notify.publish('test_event', 'data')
96+
97+
assert.is_false(called)
98+
end)
99+
100+
it('allows adding new listeners after clear', function()
101+
local called = false
102+
103+
notify.listen('test_event', function(data)
104+
called = true
105+
end)
106+
notify.clear()
107+
108+
notify.listen('test_event', function(data)
109+
called = true
110+
end)
111+
notify.publish('test_event', 'data')
112+
113+
assert.is_true(called)
114+
end)
115+
end)
116+
117+
describe('constants', function()
118+
it('defines STATUS constant', function()
119+
assert.equals('status', notify.STATUS)
120+
end)
121+
122+
it('defines MESSAGE constant', function()
123+
assert.equals('message', notify.MESSAGE)
124+
end)
125+
end)
126+
end)

0 commit comments

Comments
 (0)