Files
OmniRoute/tests/unit/cursor-agent-host.test.ts
Tuan Dinh fa0cd5af1c fix(cursor): discover account Agent endpoint (#10804)
Requests AgentUrlConfig from Cursor with each selected account token and selects the account's actual server-assigned Agent endpoint (agentUrl/agentnUrl) instead of a fixed global/us host, which fails for teams pinned to a different region. Caches validated endpoints by connection+token. Closes #10802.

Validated in an isolated worktree boarded onto origin/release/v3.8.50 (0 conflicts, 4 files):
- 18/18 focused tests pass (cursor-agent-host, cursor-apikey-provider).
- provider-translate-path-golden.test.ts initially failed — traced to a pre-existing base-red (stale golden snapshot left by an earlier freebuff merge, #10531, unrelated to this PR) and confirmed it reproduces on the pure release tip without this PR's changes. Fixed directly on release/v3.8.50 (mechanical key-ordering regen, values unchanged) rather than folding it into this PR's scope; green after merging that fix in.
- check-file-size, check-changelog-integrity: OK.
- typecheck:core: clean.
- check-complexity / check-cognitive-complexity: OK, both under baseline.
- Author additionally validated live: a real Cursor request selected agentn.us.api5.cursor.sh and returned HTTP 200/PING.

Co-authored-by: tuandinh0801 <tuandinh0801@users.noreply.github.com>
2026-08-21 00:07:45 -03:00

110 lines
3.6 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { resolveCursorAgentUrl } from "../../open-sse/executors/cursor/agentEndpoint.ts";
import { encodeMessage, encodeString } from "../../open-sse/utils/cursorAgentProtobuf/wire.ts";
function serverConfig(agentUrl: string, agentnUrl: string): Buffer {
return encodeMessage(27, [encodeString(1, agentUrl), encodeString(2, agentnUrl)]);
}
test("Cursor Agent uses each connection's server-assigned endpoint", async () => {
const originalFetch = globalThis.fetch;
const requestedTokens: string[] = [];
globalThis.fetch = async (input, init) => {
assert.equal(
String(input),
"https://api2.cursor.sh/aiserver.v1.ServerConfigService/GetServerConfig"
);
const token = new Headers(init?.headers).get("authorization") ?? "";
requestedTokens.push(token);
const region = token === "Bearer token-us" ? "us" : "eu";
return new Response(
serverConfig(
`https://agent.${region}.api5.cursor.sh`,
`https://agentn.${region}.api5.cursor.sh`
),
{ status: 200, headers: { "Content-Type": "application/proto" } }
);
};
try {
const usCredentials = {
accessToken: "token-us",
connectionId: "connection-us",
providerSpecificData: { ghostMode: false },
};
assert.equal(
await resolveCursorAgentUrl(usCredentials),
"https://agentn.us.api5.cursor.sh/agent.v1.AgentService/Run"
);
assert.equal(
await resolveCursorAgentUrl(usCredentials),
await resolveCursorAgentUrl(usCredentials)
);
assert.equal(
await resolveCursorAgentUrl({
accessToken: "token-eu",
connectionId: "connection-eu",
providerSpecificData: { ghostMode: true },
}),
"https://agent.eu.api5.cursor.sh/agent.v1.AgentService/Run"
);
assert.deepEqual(requestedTokens, ["Bearer token-us", "Bearer token-eu"]);
} finally {
globalThis.fetch = originalFetch;
}
});
test("Cursor Agent uses the token with the connection cache key", async () => {
const originalFetch = globalThis.fetch;
const requestedTokens: string[] = [];
globalThis.fetch = async (_input, init) => {
const token = new Headers(init?.headers).get("authorization") ?? "";
requestedTokens.push(token);
const region = token.endsWith("new") ? "new" : "old";
return new Response(
serverConfig(
`https://agent.${region}.api5.cursor.sh`,
`https://agentn.${region}.api5.cursor.sh`
),
{ status: 200, headers: { "Content-Type": "application/proto" } }
);
};
try {
assert.equal(
await resolveCursorAgentUrl({ accessToken: "token-old", connectionId: "connection-rotate" }),
"https://agent.old.api5.cursor.sh/agent.v1.AgentService/Run"
);
assert.equal(
await resolveCursorAgentUrl({ accessToken: "token-new", connectionId: "connection-rotate" }),
"https://agent.new.api5.cursor.sh/agent.v1.AgentService/Run"
);
assert.deepEqual(requestedTokens, ["Bearer token-old", "Bearer token-new"]);
} finally {
globalThis.fetch = originalFetch;
}
});
test("Cursor Agent rejects an endpoint outside Cursor's API domain", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
new Response(
serverConfig("https://attacker.example/agent", "https://attacker.example/agentn"),
{ status: 200, headers: { "Content-Type": "application/proto" } }
);
try {
await assert.rejects(
resolveCursorAgentUrl({
accessToken: "token-invalid-host",
connectionId: "connection-invalid-host",
}),
/invalid Agent URL/
);
} finally {
globalThis.fetch = originalFetch;
}
});