Files
OmniRoute/tests/unit/provider-header-profiles.test.ts
Tuan Dinh 6fec29ca2d fix(copilot): fallback to copilot-chat on 403 identity denial for standard provider (#13705)
* fix(copilot): fallback to copilot-chat on 403 identity denial for standard provider

* fix(copilot): document COPILOT_INTEGRATION_ID, extract identity fallback, add changelog

Adds the missing COPILOT_INTEGRATION_ID entry to .env.example (fixes
tests/unit/issue-7793-env-doc-sync-repro.test.ts), extracts the GitHub
Copilot 403 identity fallback out of open-sse/executors/base.ts into its
own module (open-sse/executors/copilotIdentityFallback.ts) to bring the
file back under the frozen file-size ratchet, and adds a changelog.d/fixes
fragment for the PR.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: tuandinh0801 <tuandinh0801@users.noreply.github.com>
2026-09-18 11:58:48 -03:00

177 lines
8.2 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
GITHUB_COPILOT_API_VERSION,
GITHUB_COPILOT_CHAT_PLUGIN_VERSION,
GITHUB_COPILOT_CLI_USER_AGENT,
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");
// Chat/inference path matches the @github/copilot CLI 1.0.81-6 wire identity.
assert.equal(chatHeaders["editor-version"], GITHUB_COPILOT_EDITOR_VERSION);
assert.equal(chatHeaders["user-agent"], GITHUB_COPILOT_CLI_USER_AGENT);
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("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;
}
}
});