|
1 | | -local h = require("tests.helpers") |
2 | | - |
3 | 1 | local assert = require("luassert") |
4 | | -local codecompanion = require("codecompanion") |
5 | 2 | local stub = require("luassert.stub") |
6 | 3 |
|
7 | 4 | local Client |
8 | 5 |
|
9 | 6 | local adapter = { |
10 | 7 | name = "TestAdapter", |
11 | | - url = "https://api.openai.com/v1/chat/completions", |
| 8 | + url = "https://api.openai.com/v1/${url_value}/completions", |
| 9 | + env = { |
| 10 | + url_value = "chat", |
| 11 | + header_value = "json", |
| 12 | + raw_value = "RAW_VALUE", |
| 13 | + }, |
12 | 14 | headers = { |
13 | | - content_type = "application/json", |
| 15 | + content_type = "application/${header_value}", |
14 | 16 | }, |
15 | 17 | parameters = { |
16 | 18 | stream = true, |
17 | 19 | }, |
| 20 | + raw = { |
| 21 | + "--arg1-${raw_value}", |
| 22 | + "--arg2-${raw_value}", |
| 23 | + }, |
18 | 24 | handlers = { |
19 | 25 | form_parameters = function() |
20 | 26 | return {} |
@@ -65,4 +71,31 @@ describe("Client", function() |
65 | 71 |
|
66 | 72 | assert.stub(mock_request).was_called(1) |
67 | 73 | end) |
| 74 | + |
| 75 | + it("substitutes variables", function() |
| 76 | + local mock_request = stub.new().returns(nil) |
| 77 | + |
| 78 | + Client.static.opts = { |
| 79 | + post = { default = mock_request }, |
| 80 | + encode = { default = stub.new().returns("{}") }, |
| 81 | + } |
| 82 | + |
| 83 | + adapter = require("codecompanion.adapters").new(adapter) |
| 84 | + |
| 85 | + Client.new({ adapter = adapter }):request({}, { callback = stub.new() }) |
| 86 | + |
| 87 | + assert.stub(mock_request).was_called(1) |
| 88 | + local request_args = mock_request.calls[1].refs[1] |
| 89 | + |
| 90 | + -- can substitute in 'address' |
| 91 | + assert.equal(request_args.url, "https://api.openai.com/v1/chat/completions") |
| 92 | + |
| 93 | + -- can substitute in 'headers' |
| 94 | + assert.equal(request_args.headers.content_type, "application/json") |
| 95 | + |
| 96 | + -- can substitute in 'raw' |
| 97 | + local raw_args = request_args.raw |
| 98 | + assert.equal(raw_args[#raw_args - 1], "--arg1-RAW_VALUE") |
| 99 | + assert.equal(raw_args[#raw_args], "--arg2-RAW_VALUE") |
| 100 | + end) |
68 | 101 | end) |
0 commit comments