Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

8.7 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";

import {
  GITHUB_COPILOT_API_VERSION,
  GITHUB_COPILOT_CHAT_PLUGIN_VERSION,
  GITHUB_COPILOT_CHAT_USER_AGENT,
  GITHUB_COPILOT_EDITOR_VERSION,
  GITHUB_COPILOT_CLI_INTEGRATION_ID,
  GITHUB_COPILOT_CHAT_INTEGRATION_ID,
  GITHUB_COPILOT_INTEGRATION_ID,
  GITHUB_COPILOT_INTERACTION_TYPE,
  GITHUB_COPILOT_HARNESS_ID,
  GITHUB_COPILOT_REFRESH_PLUGIN_VERSION,
  GITHUB_COPILOT_REFRESH_USER_AGENT,
  KIRO_AMZ_USER_AGENT,
  KIRO_SDK_USER_AGENT,
  QWEN_CLI_VERSION,
  getGitHubCopilotMachineId,
  getQwenCliUserAgent,
  getGitHubCopilotChatHeaders,
  getGitHubCopilotInternalUserHeaders,
  getGitHubCopilotRefreshHeaders,
  getKiroServiceHeaders,
  getQoderDashscopeCompatHeaders,
  normalizeCopilotIntegrationId,
  resolveCopilotIntegrationIdOverride,
} from "../../open-sse/config/providerHeaderProfiles.ts";

test("provider header profiles expose current GitHub chat and internal headers", () => {
  const chatHeaders = getGitHubCopilotChatHeaders("text/event-stream", "agent");
  // Header shape follows the captured CLI identity; the version uses the current pin.
  assert.equal(chatHeaders["editor-version"], GITHUB_COPILOT_EDITOR_VERSION);
  assert.equal(chatHeaders["user-agent"], `copilot/1.0.91 (${process.platform}) term/unknown`);
  assert.equal(chatHeaders["x-github-api-version"], GITHUB_COPILOT_API_VERSION);
  assert.equal(chatHeaders["copilot-integration-id"], GITHUB_COPILOT_INTEGRATION_ID);
  assert.equal(chatHeaders["x-interaction-type"], GITHUB_COPILOT_INTERACTION_TYPE);
  assert.equal(chatHeaders["copilot-harness-id"], GITHUB_COPILOT_HARNESS_ID);
  assert.equal(chatHeaders["x-client-machine-id"], getGitHubCopilotMachineId());
  assert.equal(chatHeaders["X-Initiator"], "agent");
  assert.equal(chatHeaders.Accept, "text/event-stream");
  // The CLI does NOT send these on inference (VS Code Copilot Chat extension only).
  assert.equal(
    chatHeaders["editor-plugin-version"],
    undefined,
    "editor-plugin-version must NOT be on the CLI inference path"
  );
  assert.equal(
    chatHeaders["x-vscode-user-agent-library-version"],
    undefined,
    "x-vscode-user-agent-library-version must NOT be on the CLI inference path"
  );

  const internalHeaders = getGitHubCopilotInternalUserHeaders("token gh-access");
  assert.equal(internalHeaders.Authorization, "token gh-access");
  assert.equal(internalHeaders["User-Agent"], GITHUB_COPILOT_CHAT_USER_AGENT);
  assert.equal(internalHeaders["Editor-Version"], GITHUB_COPILOT_EDITOR_VERSION);
  assert.equal(internalHeaders["Editor-Plugin-Version"], GITHUB_COPILOT_CHAT_PLUGIN_VERSION);
  assert.equal(internalHeaders["X-GitHub-Api-Version"], GITHUB_COPILOT_API_VERSION);
});

test("getGitHubCopilotMachineId is stable across calls and vision toggles the vision header", () => {
  // Stable per-install fingerprint: same value every call (matches the CLI).
  assert.equal(getGitHubCopilotMachineId(), getGitHubCopilotMachineId());
  const plain = getGitHubCopilotChatHeaders("application/json");
  assert.equal(plain["copilot-vision-request"], undefined);
  const vision = getGitHubCopilotChatHeaders("application/json", "user", { vision: true });
  assert.equal(vision["copilot-vision-request"], "true");
  // Machine id is consistent between two header builds in the same process.
  assert.equal(plain["x-client-machine-id"], vision["x-client-machine-id"]);
});

test("Copilot chat headers honor the CLI version environment override", () => {
  const previous = process.env.GITHUB_COPILOT_CLI_VERSION;
  process.env.GITHUB_COPILOT_CLI_VERSION = "1.0.82";
  try {
    assert.equal(
      getGitHubCopilotChatHeaders()["user-agent"],
      `copilot/1.0.82 (${process.platform}) term/unknown`
    );
  } finally {
    if (previous === undefined) {
      delete process.env.GITHUB_COPILOT_CLI_VERSION;
    } else {
      process.env.GITHUB_COPILOT_CLI_VERSION = previous;
    }
  }
});

test("provider header profiles expose dedicated refresh, qoder and kiro variants", () => {
  const refreshHeaders = getGitHubCopilotRefreshHeaders("token gh-access");
  assert.equal(refreshHeaders.Authorization, "token gh-access");
  assert.equal(refreshHeaders["User-Agent"], GITHUB_COPILOT_REFRESH_USER_AGENT);
  assert.equal(refreshHeaders["Editor-Version"], GITHUB_COPILOT_EDITOR_VERSION);
  assert.equal(refreshHeaders["Editor-Plugin-Version"], GITHUB_COPILOT_REFRESH_PLUGIN_VERSION);

  const qoderHeaders = getQoderDashscopeCompatHeaders();
  assert.equal(qoderHeaders["user-agent"], getQwenCliUserAgent());
  assert.equal(qoderHeaders["x-dashscope-useragent"], getQwenCliUserAgent());
  assert.equal(
    qoderHeaders["user-agent"],
    `QwenCode/${QWEN_CLI_VERSION} (${process.platform}; ${process.arch})`
  );

  const kiroHeaders = getKiroServiceHeaders("application/json");
  assert.equal(kiroHeaders.Accept, "application/json");
  assert.equal(kiroHeaders["User-Agent"], KIRO_SDK_USER_AGENT);
  assert.equal(kiroHeaders["X-Amz-User-Agent"], KIRO_AMZ_USER_AGENT);
});

test("provider header profiles tolerate browser-like process shims", async () => {
  const originalPlatform = process.platform;
  const originalArch = process.arch;
  const originalVersion = process.version;

  Object.defineProperty(process, "platform", { value: undefined, configurable: true });
  Object.defineProperty(process, "arch", { value: undefined, configurable: true });
  Object.defineProperty(process, "version", { value: undefined, configurable: true });

  try {
    assert.equal(getQwenCliUserAgent(), `QwenCode/${QWEN_CLI_VERSION} (unknown; unknown)`);
    const qoderHeaders = getQoderDashscopeCompatHeaders();
    assert.equal(qoderHeaders["user-agent"], `QwenCode/${QWEN_CLI_VERSION} (unknown; unknown)`);
  } finally {
    Object.defineProperty(process, "platform", { value: originalPlatform, configurable: true });
    Object.defineProperty(process, "arch", { value: originalArch, configurable: true });
    Object.defineProperty(process, "version", { value: originalVersion, configurable: true });
  }
});

test("Copilot integration ID constants and resolution precedence", () => {
  assert.equal(GITHUB_COPILOT_CLI_INTEGRATION_ID, "copilot-developer-cli");
  assert.equal(GITHUB_COPILOT_CHAT_INTEGRATION_ID, "copilot-chat");
  assert.equal(GITHUB_COPILOT_INTEGRATION_ID, GITHUB_COPILOT_CLI_INTEGRATION_ID);

  // normalizeCopilotIntegrationId validates strings and rejects CR/LF or whitespace
  assert.equal(normalizeCopilotIntegrationId("custom-client"), "custom-client");
  assert.equal(normalizeCopilotIntegrationId("  custom-client  "), "custom-client");
  assert.equal(normalizeCopilotIntegrationId(""), null);
  assert.equal(normalizeCopilotIntegrationId("   "), null);
  assert.equal(normalizeCopilotIntegrationId("bad\r\nheader"), null);
  assert.equal(normalizeCopilotIntegrationId("bad\nheader"), null);
  assert.equal(normalizeCopilotIntegrationId(123), null);
  assert.equal(normalizeCopilotIntegrationId(null), null);

  // Default headers use CLI integration ID
  const defaultHeaders = getGitHubCopilotChatHeaders();
  assert.equal(defaultHeaders["copilot-integration-id"], GITHUB_COPILOT_CLI_INTEGRATION_ID);

  // options.integrationId overrides default
  const optionHeaders = getGitHubCopilotChatHeaders("application/json", "user", {
    integrationId: "custom-opt-id",
  });
  assert.equal(optionHeaders["copilot-integration-id"], "custom-opt-id");

  // Environment variable override and precedence
  const originalEnv = process.env.COPILOT_INTEGRATION_ID;
  try {
    process.env.COPILOT_INTEGRATION_ID = "env-integration-id";
    assert.equal(resolveCopilotIntegrationIdOverride(), "env-integration-id");

    // Env overrides default
    const envHeaders = getGitHubCopilotChatHeaders();
    assert.equal(envHeaders["copilot-integration-id"], "env-integration-id");

    // options.integrationId takes precedence over env override
    const optOverEnvHeaders = getGitHubCopilotChatHeaders("application/json", "user", {
      integrationId: "opt-wins",
    });
    assert.equal(optOverEnvHeaders["copilot-integration-id"], "opt-wins");

    // Invalid env value with CR/LF falls back to CLI default
    process.env.COPILOT_INTEGRATION_ID = "invalid\r\nid";
    assert.equal(resolveCopilotIntegrationIdOverride(), null);
    const fallbackHeaders = getGitHubCopilotChatHeaders();
    assert.equal(fallbackHeaders["copilot-integration-id"], GITHUB_COPILOT_CLI_INTEGRATION_ID);

    // Empty/whitespace env value falls back to CLI default
    process.env.COPILOT_INTEGRATION_ID = "   ";
    assert.equal(resolveCopilotIntegrationIdOverride(), null);
    assert.equal(
      getGitHubCopilotChatHeaders()["copilot-integration-id"],
      GITHUB_COPILOT_CLI_INTEGRATION_ID
    );
  } finally {
    if (originalEnv === undefined) {
      delete process.env.COPILOT_INTEGRATION_ID;
    } else {
      process.env.COPILOT_INTEGRATION_ID = originalEnv;
    }
  }
});