Spaces:
Running
Running
File size: 1,748 Bytes
780d695 | 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 | import { test, expect, mock } from "bun:test"
import type { ChatCompletionsPayload } from "../src/services/copilot/create-chat-completions"
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
test("sets X-Initiator to agent if tool/assistant present", async () => {
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
expect(headers["X-Initiator"]).toBe("agent")
})
test("sets X-Initiator to user if only user present", async () => {
const payload: ChatCompletionsPayload = {
messages: [
{ role: "user", content: "hi" },
{ role: "user", content: "hello again" },
],
model: "gpt-test",
}
await createChatCompletions(payload)
expect(fetchMock).toHaveBeenCalled()
const headers = (
fetchMock.mock.calls[1][1] as { headers: Record<string, string> }
).headers
expect(headers["X-Initiator"]).toBe("user")
})
|