Skip to content

Commit 07d43b4

Browse files
test(functions): add unit tests for functions module (#1337)
* test(functions): add unit tests for functions module Add comprehensive unit tests for CopilotChat.functions covering uri_to_url, match_uri, parse_schema, and parse_input. This improves test coverage and ensures correct behavior for URI parsing and schema handling. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 97cc514 commit 07d43b4

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

tests/functions_spec.lua

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
local functions = require('CopilotChat.functions')
2+
3+
describe('CopilotChat.functions', function()
4+
describe('uri_to_url', function()
5+
it('replaces parameters in uri template', function()
6+
local uri = 'file://{path}'
7+
local input = { path = '/tmp/test.txt' }
8+
assert.equals('file:///tmp/test.txt', functions.uri_to_url(uri, input))
9+
end)
10+
it('leaves missing params empty', function()
11+
local uri = 'file://{path}/{id}'
12+
local input = { path = '/tmp' }
13+
assert.equals('file:///tmp/', functions.uri_to_url(uri, input))
14+
end)
15+
end)
16+
17+
describe('match_uri', function()
18+
it('matches uri and extracts parameters', function()
19+
local uri = 'file:///tmp/test.txt'
20+
local pattern = 'file://{path}'
21+
local result = functions.match_uri(uri, pattern)
22+
assert.are.same({ path = '/tmp/test.txt' }, result)
23+
end)
24+
it('returns nil for non-matching uri', function()
25+
assert.is_nil(functions.match_uri('abc', 'file://{path}'))
26+
end)
27+
it('returns empty table for exact match with no params', function()
28+
assert.are.same({}, functions.match_uri('abc', 'abc'))
29+
end)
30+
end)
31+
32+
describe('parse_schema', function()
33+
it('returns schema if present', function()
34+
local fn = { schema = { type = 'object', properties = { foo = { type = 'string' } } } }
35+
assert.equals(fn.schema, functions.parse_schema(fn))
36+
end)
37+
it('generates schema from uri if missing', function()
38+
local fn = { uri = 'file://{path}/{id}' }
39+
local schema = functions.parse_schema(fn)
40+
assert.are.same({
41+
type = 'object',
42+
properties = { path = { type = 'string' }, id = { type = 'string' } },
43+
required = { 'path', 'id' },
44+
}, schema)
45+
end)
46+
end)
47+
48+
describe('parse_input', function()
49+
it('parses input string into table', function()
50+
local schema = { properties = { a = {}, b = {} }, required = { 'a', 'b' } }
51+
local input = 'foo;;bar'
52+
assert.are.same({ a = 'foo', b = 'bar' }, functions.parse_input(input, schema))
53+
end)
54+
it('returns input if already table', function()
55+
local input = { a = 1 }
56+
assert.equals(input, functions.parse_input(input))
57+
end)
58+
it('returns empty table if no schema', function()
59+
assert.are.same({}, functions.parse_input('foo'))
60+
end)
61+
end)
62+
end)

tests/init_spec.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ describe('CopilotChat module', function()
44
require('CopilotChat')
55
end)
66
end)
7+
78
it('should be able to set up', function()
89
assert.has_no.errors(function()
910
require('CopilotChat').setup({})

0 commit comments

Comments
 (0)