|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import type { Hooks } from "@opencode-ai/plugin" |
| 3 | +import { CopilotAuthPlugin } from "@/plugin/github-copilot/copilot" |
| 4 | + |
| 5 | +type ChatHeaders = NonNullable<Hooks["chat.headers"]> |
| 6 | + |
| 7 | +async function hook() { |
| 8 | + const hooks = await CopilotAuthPlugin({ |
| 9 | + directory: "", |
| 10 | + project: {} as never, |
| 11 | + worktree: "", |
| 12 | + experimental_workspace: { register() {} }, |
| 13 | + serverUrl: new URL("http://localhost"), |
| 14 | + $: {} as never, |
| 15 | + client: { |
| 16 | + session: { |
| 17 | + message: async () => ({ data: { parts: [] } }), |
| 18 | + get: async () => ({ data: {} }), |
| 19 | + }, |
| 20 | + } as never, |
| 21 | + }) |
| 22 | + return hooks["chat.headers"]! |
| 23 | +} |
| 24 | + |
| 25 | +function input(sessionID: string, providerID: string, npm: string) { |
| 26 | + return { |
| 27 | + sessionID, |
| 28 | + agent: "build", |
| 29 | + model: { providerID, api: { npm } }, |
| 30 | + message: { id: "msg_test", sessionID }, |
| 31 | + } as Parameters<ChatHeaders>[0] |
| 32 | +} |
| 33 | + |
| 34 | +test.each([ |
| 35 | + ["github-copilot", "@ai-sdk/github-copilot"], |
| 36 | + ["github-copilot", "@ai-sdk/anthropic"], |
| 37 | + ["github-copilot-enterprise", "@ai-sdk/github-copilot"], |
| 38 | + ["github-copilot-enterprise", "@ai-sdk/anthropic"], |
| 39 | +])("uses the session ID for %s interaction headers with %s", async (providerID, npm) => { |
| 40 | + const headers = await hook() |
| 41 | + for (const sessionID of ["ses_one", "ses_one", "ses_two"]) { |
| 42 | + const output = { headers: { "x-existing": "preserved" } } |
| 43 | + await headers(input(sessionID, providerID, npm), output) |
| 44 | + expect(output.headers).toMatchObject({ |
| 45 | + "X-Interaction-Id": sessionID, |
| 46 | + "x-existing": "preserved", |
| 47 | + }) |
| 48 | + } |
| 49 | +}) |
| 50 | + |
| 51 | +test("does not add interaction headers to other providers", async () => { |
| 52 | + const headers = await hook() |
| 53 | + const output = { headers: { "x-existing": "preserved" } } |
| 54 | + await headers(input("ses_one", "openai", "@ai-sdk/openai"), output) |
| 55 | + expect(output.headers).toEqual({ "x-existing": "preserved" }) |
| 56 | +}) |
0 commit comments