github-copilot-anthropic.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { describe, expect, it, vi } from "vitest";
  2. import { getModel } from "../src/models.ts";
  3. import { streamAnthropic } from "../src/providers/anthropic.ts";
  4. import type { Context } from "../src/types.ts";
  5. const mockState = vi.hoisted(() => ({
  6. constructorOpts: undefined as Record<string, unknown> | undefined,
  7. createParams: undefined as Record<string, unknown> | undefined,
  8. }));
  9. vi.mock("@anthropic-ai/sdk", () => {
  10. function createSseResponse(): Response {
  11. const body = [
  12. `event: message_start\ndata: ${JSON.stringify({
  13. type: "message_start",
  14. message: {
  15. id: "msg_test",
  16. usage: { input_tokens: 10, output_tokens: 0 },
  17. },
  18. })}\n`,
  19. `event: message_delta\ndata: ${JSON.stringify({
  20. type: "message_delta",
  21. delta: { stop_reason: "end_turn" },
  22. usage: { output_tokens: 5 },
  23. })}\n`,
  24. ].join("\n");
  25. return new Response(body, {
  26. status: 200,
  27. headers: { "content-type": "text/event-stream" },
  28. });
  29. }
  30. class FakeAnthropic {
  31. constructor(opts: Record<string, unknown>) {
  32. mockState.constructorOpts = opts;
  33. }
  34. messages = {
  35. create: (params: Record<string, unknown>) => {
  36. mockState.createParams = params;
  37. return {
  38. asResponse: async () => createSseResponse(),
  39. };
  40. },
  41. };
  42. }
  43. return { default: FakeAnthropic };
  44. });
  45. describe("Copilot Claude via Anthropic Messages", () => {
  46. const context: Context = {
  47. systemPrompt: "You are a helpful assistant.",
  48. messages: [{ role: "user", content: "Hello", timestamp: Date.now() }],
  49. };
  50. it("uses Bearer auth, Copilot headers, and valid Anthropic Messages payload", async () => {
  51. const model = getModel("github-copilot", "claude-sonnet-4.6");
  52. expect(model.api).toBe("anthropic-messages");
  53. const s = streamAnthropic(model, context, { apiKey: "tid_copilot_session_test_token" });
  54. for await (const event of s) {
  55. if (event.type === "error") break;
  56. }
  57. const opts = mockState.constructorOpts!;
  58. expect(opts).toBeDefined();
  59. // Auth: apiKey null, authToken for Bearer
  60. expect(opts.apiKey).toBeNull();
  61. expect(opts.authToken).toBe("tid_copilot_session_test_token");
  62. const headers = opts.defaultHeaders as Record<string, string>;
  63. // Copilot static headers from model.headers
  64. expect(headers["User-Agent"]).toContain("GitHubCopilotChat");
  65. expect(headers["Copilot-Integration-Id"]).toBe("vscode-chat");
  66. // Dynamic headers
  67. expect(headers["X-Initiator"]).toBe("user");
  68. expect(headers["Openai-Intent"]).toBe("conversation-edits");
  69. // No fine-grained-tool-streaming (Copilot doesn't support it)
  70. const beta = headers["anthropic-beta"] ?? "";
  71. expect(beta).not.toContain("fine-grained-tool-streaming");
  72. // Payload is valid Anthropic Messages format
  73. const params = mockState.createParams!;
  74. expect(params.model).toBe("claude-sonnet-4.6");
  75. expect(params.stream).toBe(true);
  76. expect(params.max_tokens).toBe(model.maxTokens);
  77. expect(Array.isArray(params.messages)).toBe(true);
  78. });
  79. it("omits interleaved-thinking beta for adaptive-thinking models", async () => {
  80. const model = getModel("github-copilot", "claude-sonnet-4.6");
  81. const s = streamAnthropic(model, context, {
  82. apiKey: "tid_copilot_session_test_token",
  83. interleavedThinking: true,
  84. });
  85. for await (const event of s) {
  86. if (event.type === "error") break;
  87. }
  88. const headers = mockState.constructorOpts!.defaultHeaders as Record<string, string>;
  89. expect(headers["anthropic-beta"] ?? "").not.toContain("interleaved-thinking-2025-05-14");
  90. });
  91. });