|
| 1 | +import { describe, expect, mock, test } from "bun:test" |
| 2 | +import type { Hooks, PluginInput } from "@opencode-ai/plugin" |
| 3 | +import { CopilotAuthPlugin } from "../../src/plugin/github-copilot/copilot" |
| 4 | + |
| 5 | +type ChatInput = Parameters<NonNullable<Hooks["chat.headers"]>>[0] |
| 6 | +type ChatOutput = Parameters<NonNullable<Hooks["chat.headers"]>>[1] |
| 7 | + |
| 8 | +function model() { |
| 9 | + return { |
| 10 | + providerID: "github-copilot", |
| 11 | + api: { |
| 12 | + npm: "@ai-sdk/github-copilot", |
| 13 | + }, |
| 14 | + } as ChatInput["model"] |
| 15 | +} |
| 16 | + |
| 17 | +function incoming(input?: Partial<ChatInput>): ChatInput { |
| 18 | + return { |
| 19 | + sessionID: "session", |
| 20 | + agent: "build", |
| 21 | + model: model(), |
| 22 | + provider: {} as ChatInput["provider"], |
| 23 | + message: { |
| 24 | + id: "message", |
| 25 | + sessionID: "session", |
| 26 | + } as ChatInput["message"], |
| 27 | + ...input, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +async function plugin(input?: { message?: () => Promise<unknown>; session?: () => Promise<unknown> }) { |
| 32 | + const client = { |
| 33 | + session: { |
| 34 | + message: mock(input?.message ?? (() => Promise.resolve({ data: { parts: [] } }))), |
| 35 | + get: mock(input?.session ?? (() => Promise.resolve({ data: {} }))), |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + const hooks = await CopilotAuthPlugin({ |
| 40 | + client: client as unknown as PluginInput["client"], |
| 41 | + directory: "/tmp", |
| 42 | + project: {} as PluginInput["project"], |
| 43 | + worktree: "/tmp", |
| 44 | + serverUrl: new URL("http://localhost:4096"), |
| 45 | + $: Bun.$, |
| 46 | + }) |
| 47 | + |
| 48 | + return { hooks, client } |
| 49 | +} |
| 50 | + |
| 51 | +describe("plugin.github-copilot", () => { |
| 52 | + test("marks compaction agent requests as agent initiated", async () => { |
| 53 | + const { hooks, client } = await plugin({ |
| 54 | + message: () => Promise.reject(new Error("should not fetch message")), |
| 55 | + }) |
| 56 | + const output: ChatOutput = { headers: {} } |
| 57 | + |
| 58 | + await hooks["chat.headers"]?.( |
| 59 | + incoming({ |
| 60 | + agent: "compaction", |
| 61 | + }), |
| 62 | + output, |
| 63 | + ) |
| 64 | + |
| 65 | + expect(output.headers["x-initiator"]).toBe("agent") |
| 66 | + expect(client.session.message).not.toHaveBeenCalled() |
| 67 | + expect(client.session.get).not.toHaveBeenCalled() |
| 68 | + }) |
| 69 | + |
| 70 | + test("marks synthetic-only follow-up messages as agent initiated", async () => { |
| 71 | + const { hooks } = await plugin({ |
| 72 | + message: () => |
| 73 | + Promise.resolve({ |
| 74 | + data: { |
| 75 | + parts: [{ type: "text", synthetic: true }], |
| 76 | + }, |
| 77 | + }), |
| 78 | + }) |
| 79 | + const output: ChatOutput = { headers: {} } |
| 80 | + |
| 81 | + await hooks["chat.headers"]?.(incoming(), output) |
| 82 | + |
| 83 | + expect(output.headers["x-initiator"]).toBe("agent") |
| 84 | + }) |
| 85 | + |
| 86 | + test("marks auto-compaction marker messages as agent initiated", async () => { |
| 87 | + const { hooks } = await plugin({ |
| 88 | + message: () => |
| 89 | + Promise.resolve({ |
| 90 | + data: { |
| 91 | + parts: [ |
| 92 | + { |
| 93 | + type: "text", |
| 94 | + text: "[auto-compaction-followup]", |
| 95 | + synthetic: true, |
| 96 | + }, |
| 97 | + { |
| 98 | + type: "text", |
| 99 | + text: "Continue if you have next steps", |
| 100 | + synthetic: true, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + }), |
| 105 | + }) |
| 106 | + const output: ChatOutput = { headers: {} } |
| 107 | + |
| 108 | + await hooks["chat.headers"]?.(incoming(), output) |
| 109 | + |
| 110 | + expect(output.headers["x-initiator"]).toBe("agent") |
| 111 | + }) |
| 112 | + |
| 113 | + test("does not override normal top-level user messages", async () => { |
| 114 | + const { hooks } = await plugin({ |
| 115 | + message: () => |
| 116 | + Promise.resolve({ |
| 117 | + data: { |
| 118 | + parts: [{ type: "text", text: "hello" }], |
| 119 | + }, |
| 120 | + }), |
| 121 | + session: () => Promise.resolve({ data: {} }), |
| 122 | + }) |
| 123 | + const output: ChatOutput = { headers: {} } |
| 124 | + |
| 125 | + await hooks["chat.headers"]?.(incoming(), output) |
| 126 | + |
| 127 | + expect(output.headers["x-initiator"]).toBeUndefined() |
| 128 | + }) |
| 129 | +}) |
0 commit comments