|
| 1 | +import { test, expect, mock } from "bun:test" |
| 2 | + |
| 3 | +import { state } from "../src/lib/state" |
| 4 | +import { createResponses } from "../src/services/copilot/create-responses" |
| 5 | + |
| 6 | +// Mock state |
| 7 | +state.copilotToken = "test-token" |
| 8 | +state.vsCodeVersion = "1.0.0" |
| 9 | +state.accountType = "individual" |
| 10 | + |
| 11 | +// Helper to mock fetch |
| 12 | +const fetchMock = mock( |
| 13 | + (_url: string, opts: { headers: Record<string, string> }) => { |
| 14 | + return { |
| 15 | + ok: true, |
| 16 | + json: () => ({ |
| 17 | + id: "resp-123", |
| 18 | + output: [ |
| 19 | + { |
| 20 | + type: "message", |
| 21 | + role: "assistant", |
| 22 | + content: [{ type: "output_text", text: "ok" }], |
| 23 | + }, |
| 24 | + ], |
| 25 | + usage: { input_tokens: 10, output_tokens: 5 }, |
| 26 | + }), |
| 27 | + headers: opts.headers, |
| 28 | + } |
| 29 | + }, |
| 30 | +) |
| 31 | +// @ts-expect-error - Mock fetch doesn't implement all fetch properties |
| 32 | +;(globalThis as unknown as { fetch: typeof fetch }).fetch = fetchMock |
| 33 | + |
| 34 | +test("sets X-Initiator to user when initiator is user", async () => { |
| 35 | + const payload = { |
| 36 | + model: "gpt-test", |
| 37 | + input: [{ role: "user" as const, content: "hi" }], |
| 38 | + } |
| 39 | + await createResponses(payload, { vision: false, initiator: "user" }) |
| 40 | + expect(fetchMock).toHaveBeenCalled() |
| 41 | + const headers = ( |
| 42 | + fetchMock.mock.calls[0][1] as { headers: Record<string, string> } |
| 43 | + ).headers |
| 44 | + expect(headers["X-Initiator"]).toBe("user") |
| 45 | +}) |
| 46 | + |
| 47 | +test("sets X-Initiator to agent when initiator is agent", async () => { |
| 48 | + const payload = { |
| 49 | + model: "gpt-test", |
| 50 | + input: [{ role: "user" as const, content: "hi" }], |
| 51 | + } |
| 52 | + await createResponses(payload, { vision: false, initiator: "agent" }) |
| 53 | + const callIndex = fetchMock.mock.calls.length - 1 |
| 54 | + const headers = ( |
| 55 | + fetchMock.mock.calls[callIndex][1] as { headers: Record<string, string> } |
| 56 | + ).headers |
| 57 | + expect(headers["X-Initiator"]).toBe("agent") |
| 58 | +}) |
| 59 | + |
| 60 | +test("forces X-Initiator to agent when state.forceAgent is true", async () => { |
| 61 | + state.forceAgent = true |
| 62 | + const payload = { |
| 63 | + model: "gpt-test", |
| 64 | + input: [{ role: "user" as const, content: "hi" }], |
| 65 | + } |
| 66 | + await createResponses(payload, { vision: false, initiator: "user" }) |
| 67 | + const callIndex = fetchMock.mock.calls.length - 1 |
| 68 | + const headers = ( |
| 69 | + fetchMock.mock.calls[callIndex][1] as { headers: Record<string, string> } |
| 70 | + ).headers |
| 71 | + expect(headers["X-Initiator"]).toBe("agent") |
| 72 | + state.forceAgent = false // reset |
| 73 | +}) |
0 commit comments