|
| 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) |
0 commit comments