forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-chat-completions.test.ts
More file actions
83 lines (73 loc) · 2.6 KB
/
create-chat-completions.test.ts
File metadata and controls
83 lines (73 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { beforeEach, expect, mock, test } from "bun:test"
import type { ChatCompletionsPayload } from "../src/services/copilot/create-chat-completions"
import { billingCycleManager } from "../src/lib/billing-cycle"
import { state } from "../src/lib/state"
import { createChatCompletions } from "../src/services/copilot/create-chat-completions"
// Mock state
state.copilotToken = "test-token"
state.vsCodeVersion = "1.0.0"
state.accountType = "individual"
// Helper to mock fetch
const fetchMock = mock(
(_url: string, opts: { headers: Record<string, string> }) => {
return {
ok: true,
json: () => ({ id: "123", object: "chat.completion", choices: [] }),
headers: opts.headers,
}
},
)
// @ts-expect-error - Mock fetch doesn't implement all fetch properties
;(globalThis as unknown as { fetch: typeof fetch }).fetch = fetchMock
beforeEach(() => {
billingCycleManager.reset()
fetchMock.mockClear()
})
test("sets X-Initiator to user for first request in billing cycle", async () => {
const payload: ChatCompletionsPayload = {
messages: [{ role: "user", content: "hi" }],
model: "gpt-test",
}
await createChatCompletions(payload)
expect(fetchMock).toHaveBeenCalled()
const headers = (
fetchMock.mock.calls[0][1] as { headers: Record<string, string> }
).headers
expect(headers["X-Initiator"]).toBe("user")
})
test("sets X-Initiator to agent for subsequent requests in billing cycle", async () => {
// First request
const payload1: ChatCompletionsPayload = {
messages: [{ role: "user", content: "hi" }],
model: "gpt-test",
}
await createChatCompletions(payload1)
// Second request (should be agent)
const payload2: ChatCompletionsPayload = {
messages: [{ role: "user", content: "hello again" }],
model: "gpt-test",
}
await createChatCompletions(payload2)
expect(fetchMock).toHaveBeenCalledTimes(2)
const headers = (
fetchMock.mock.calls[1][1] as { headers: Record<string, string> }
).headers
expect(headers["X-Initiator"]).toBe("agent")
})
test("message role does not affect X-Initiator (billing cycle only)", async () => {
// First request with tool/assistant messages
const payload: ChatCompletionsPayload = {
messages: [
{ role: "user", content: "hi" },
{ role: "tool", content: "tool call" },
],
model: "gpt-test",
}
await createChatCompletions(payload)
expect(fetchMock).toHaveBeenCalled()
const headers = (
fetchMock.mock.calls[0][1] as { headers: Record<string, string> }
).headers
// Should still be "user" because it's the first request in the cycle
expect(headers["X-Initiator"]).toBe("user")
})