Skip to content

Commit 37c9986

Browse files
committed
refactor openai tests
1 parent a77bf39 commit 37c9986

File tree

3 files changed

+106
-76
lines changed

3 files changed

+106
-76
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"id": "chatcmpl-ADx5bEkzrSB6WjrnB9ce1ofWcaOAq",
3+
"object": "chat.completion",
4+
"created": 1727888767,
5+
"model": "gpt-4o-2024-05-13",
6+
"choices": [
7+
{
8+
"index": 0,
9+
"message": {
10+
"role": "assistant",
11+
"content": "Elegant simplicity.",
12+
"refusal": null
13+
},
14+
"logprobs": null,
15+
"finish_reason": "stop"
16+
}
17+
],
18+
"usage": {
19+
"prompt_tokens": 343,
20+
"completion_tokens": 3,
21+
"total_tokens": 346,
22+
"prompt_tokens_details": {
23+
"cached_tokens": 0
24+
},
25+
"completion_tokens_details": {
26+
"reasoning_tokens": 0
27+
}
28+
},
29+
"system_fingerprint": "fp_5796ac6771"
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
2+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":"Dynamic"},"logprobs":null,"finish_reason":null}],"usage":null}
3+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":","},"logprobs":null,"finish_reason":null}],"usage":null}
4+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" Flexible"},"logprobs":null,"finish_reason":null}],"usage":null}
5+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}],"usage":null}
6+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" \n\n"},"logprobs":null,"finish_reason":null}],"usage":null}
7+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":"Is"},"logprobs":null,"finish_reason":null}],"usage":null}
8+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" there"},"logprobs":null,"finish_reason":null}],"usage":null}
9+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" anything"},"logprobs":null,"finish_reason":null}],"usage":null}
10+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" specific"},"logprobs":null,"finish_reason":null}],"usage":null}
11+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" you"},"logprobs":null,"finish_reason":null}],"usage":null}
12+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" would"},"logprobs":null,"finish_reason":null}],"usage":null}
13+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" like"},"logprobs":null,"finish_reason":null}],"usage":null}
14+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" to"},"logprobs":null,"finish_reason":null}],"usage":null}
15+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" know"},"logprobs":null,"finish_reason":null}],"usage":null}
16+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" about"},"logprobs":null,"finish_reason":null}],"usage":null}
17+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":" Ruby"},"logprobs":null,"finish_reason":null}],"usage":null}
18+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{"content":"?"},"logprobs":null,"finish_reason":null}],"usage":null}
19+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}],"usage":null}
20+
data: {"id":"chatcmpl-BAw4xsiRYWNZfSmwnxtjDnm8RYXG5","object":"chat.completion.chunk","created":1741946235,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_f9f4fb6dbf","choices":[],"usage":{"prompt_tokens":394,"completion_tokens":18,"total_tokens":412,"prompt_tokens_details":{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":0,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}}}

tests/adapters/test_openai.lua

Lines changed: 56 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,68 @@
1+
local h = require("tests.helpers")
12
local adapter
2-
local messages
3-
local response
43

5-
local adapter_helpers = require("tests.adapters.helpers")
6-
local h = require("tests.helpers")
4+
local new_set = MiniTest.new_set
5+
T = new_set()
76

8-
describe("OpenAI adapter", function()
9-
before_each(function()
10-
adapter = require("codecompanion.adapters").resolve("openai")
7+
T["OpenAI adapter"] = new_set({
8+
hooks = {
9+
pre_case = function()
10+
adapter = require("codecompanion.adapters").resolve("openai")
11+
end,
12+
},
13+
})
1114

12-
---------------------------------------------------------- STREAMING OUTPUT
13-
messages = { {
14-
content = "Explain Ruby in two words",
15-
role = "user",
16-
} }
15+
T["OpenAI adapter"]["it can form messages to be sent to the API"] = function()
16+
local messages = { {
17+
content = "Explain Ruby in two words",
18+
role = "user",
19+
} }
1720

18-
response = {
19-
{
20-
request = [[data: {"id":"chatcmpl-90DdmqMKOKpqFemxX0OhTVdH042gu","object":"chat.completion.chunk","created":1709839462,"model":"gpt-4-0125-preview","system_fingerprint":"fp_70b2088885","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}]],
21-
output = {
22-
content = "",
23-
role = "assistant",
24-
},
25-
},
26-
{
27-
request = [[data: {"id":"chatcmpl-90DdmqMKOKpqFemxX0OhTVdH042gu","object":"chat.completion.chunk","created":1709839462,"model":"gpt-4-0125-preview","system_fingerprint":"fp_70b2088885","choices":[{"index":0,"delta":{"content":"Programming"},"logprobs":null,"finish_reason":null}]}]],
28-
output = {
29-
content = "Programming",
30-
},
31-
},
32-
{
33-
request = [[data: {"id":"chatcmpl-90DdmqMKOKpqFemxX0OhTVdH042gu","object":"chat.completion.chunk","created":1709839462,"model":"gpt-4-0125-preview","system_fingerprint":"fp_70b2088885","choices":[{"index":0,"delta":{"content":" language"},"logprobs":null,"finish_reason":null}]}]],
34-
output = {
35-
content = " language",
36-
},
37-
},
38-
}
39-
-------------------------------------------------------------------- // END
40-
end)
21+
h.eq({ messages = messages }, adapter.handlers.form_messages(adapter, messages))
22+
end
4123

42-
it("can form messages to be sent to the API", function()
43-
h.eq({ messages = messages }, adapter.handlers.form_messages(adapter, messages))
44-
end)
24+
T["OpenAI adapter"]["Streaming"] = new_set()
4525

46-
it("can output streamed data into a format for the chat buffer", function()
47-
h.eq(
48-
{ content = "Programming language", role = "assistant" },
49-
adapter_helpers.chat_buffer_output(response, adapter)
50-
)
51-
end)
52-
end)
26+
T["OpenAI adapter"]["Streaming"]["can output streamed data into the chat buffer"] = function()
27+
local output = ""
28+
local lines = vim.fn.readfile("tests/adapters/stubs/openai_streaming.txt")
29+
for _, line in ipairs(lines) do
30+
local chat_output = adapter.handlers.chat_output(adapter, line)
31+
if chat_output and chat_output.output.content then
32+
output = output .. chat_output.output.content
33+
end
34+
end
5335

54-
describe("OpenAI adapter with NO STREAMING", function()
55-
before_each(function()
56-
response = {
57-
{
58-
request = {
59-
body = '{\n "id": "chatcmpl-ADx5bEkzrSB6WjrnB9ce1ofWcaOAq",\n "object": "chat.completion",\n "created": 1727888767,\n "model": "gpt-4o-2024-05-13",\n "choices": [\n {\n "index": 0,\n "message": {\n "role": "assistant",\n "content": "Elegant simplicity.",\n "refusal": null\n },\n "logprobs": null,\n "finish_reason": "stop"\n }\n ],\n "usage": {\n "prompt_tokens": 343,\n "completion_tokens": 3,\n "total_tokens": 346,\n "prompt_tokens_details": {\n "cached_tokens": 0\n },\n "completion_tokens_details": {\n "reasoning_tokens": 0\n }\n },\n "system_fingerprint": "fp_5796ac6771"\n}',
60-
exit = 0,
61-
headers = {
62-
"date: Wed, 02 Oct 2024 17:06:07 GMT",
63-
"content-type: application/json",
64-
},
65-
status = 200,
66-
},
67-
output = {
68-
content = "Elegant simplicity.",
69-
role = "assistant",
36+
h.expect_starts_with("Dynamic, Flexible", output)
37+
end
38+
39+
T["OpenAI adapter"]["No Streaming"] = new_set({
40+
hooks = {
41+
pre_case = function()
42+
adapter = require("codecompanion.adapters").extend("openai", {
43+
opts = {
44+
stream = false,
7045
},
71-
},
72-
}
46+
})
47+
end,
48+
},
49+
})
50+
51+
T["OpenAI adapter"]["No Streaming"]["can output for the chat buffer"] = function()
52+
local data = vim.fn.readfile("tests/adapters/stubs/openai_no_streaming.txt")
53+
data = table.concat(data, "\n")
54+
55+
h.eq("Elegant simplicity.", adapter.handlers.chat_output(adapter, data).output.content)
56+
end
57+
58+
T["OpenAI adapter"]["No Streaming"]["can output for the inline assistant"] = function()
59+
local data = vim.fn.readfile("tests/adapters/stubs/openai_no_streaming.txt")
60+
data = table.concat(data, "\n")
7361

74-
adapter = require("codecompanion.adapters").extend("openai", {
75-
opts = {
76-
stream = false,
77-
},
78-
})
79-
end)
62+
-- JSON object needs the body key
63+
local json = { body = data }
8064

81-
it("can output data into a format for the chat buffer", function()
82-
h.eq(response[#response].output, adapter_helpers.chat_buffer_output(response, adapter))
83-
end)
65+
h.eq("Elegant simplicity.", adapter.handlers.inline_output(adapter, json).output)
66+
end
8467

85-
it("can output data into a format for the inline assistant", function()
86-
h.eq(response[#response].output.content, adapter_helpers.inline_buffer_output(response, adapter))
87-
end)
88-
end)
68+
return T

0 commit comments

Comments
 (0)