Files
OmniRoute/tests/unit/cli-oauth-commands.test.ts
backryun acc066db3f [v3.8.50] feat(devin-desktop): replace public Windsurf provider (#8228)
* feat(devin-desktop): replace public Windsurf provider

* fix(migrations): renumber Devin Desktop migration to 151 (avoid 147 collision)

147_windsurf_to_devin_desktop.sql collided with the released
147_api_keys_model_access_mode.sql — getMigrationFiles throws
"Migration version collision detected" on every DB start. Base occupies
slots up to 150, so renumber the new migration to 151 and point the
windsurf→devin RENAMED_MIGRATION_COMPATIBILITY entries (and tests) at it.
147 is freed in KNOWN_GAPS since 147_api_keys now owns the slot.

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

---------

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-08-12 08:40:18 -03:00

230 lines
6.9 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
const CONNECTIONS = [
{
id: "conn1",
provider: "gemini",
name: "My Gemini",
authType: "oauth",
isActive: true,
testStatus: "ok",
},
{
id: "conn2",
provider: "copilot",
name: "Copilot",
authType: "oauth2",
isActive: true,
testStatus: "ok",
},
{
id: "conn3",
provider: "openai",
name: "OpenAI Key",
authType: "api_key",
isActive: true,
testStatus: "ok",
},
];
function makeResp(data: unknown, status = 200) {
const obj = {
ok: status < 400,
status,
exitCode: status < 400 ? 0 : 1,
json: () => Promise.resolve(data),
text: () => Promise.resolve(JSON.stringify(data)),
headers: new Headers(),
};
obj.json = obj.json.bind(obj);
obj.text = obj.text.bind(obj);
return obj;
}
async function captureStdout(fn: () => Promise<void>): Promise<string> {
const chunks: string[] = [];
const orig = process.stdout.write.bind(process.stdout);
process.stdout.write = (c: string | Uint8Array) => {
if (typeof c === "string") chunks.push(c);
return true;
};
try {
await fn();
} finally {
process.stdout.write = orig;
}
return chunks.join("");
}
function makeCmd(output = "json") {
return { optsWithGlobals: () => ({ output, quiet: output !== "table" }) };
}
test("runOAuthStatus filtra apenas conexões oauth/oauth2", async () => {
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string) => {
assert.ok(url.includes("/api/providers"));
return Promise.resolve(makeResp({ providers: CONNECTIONS }));
}) as any;
const { runOAuthStatus } = await import("../../bin/cli/commands/oauth.mjs");
const out = await captureStdout(() => runOAuthStatus({}, makeCmd() as any));
globalThis.fetch = origFetch;
const parsed = JSON.parse(out);
assert.ok(Array.isArray(parsed));
assert.equal(parsed.length, 2);
assert.ok(parsed.every((c: any) => c.authType === "oauth" || c.authType === "oauth2"));
});
test("runOAuthStatus filtra por provider", async () => {
let capturedUrl = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string) => {
capturedUrl = url;
return Promise.resolve(
makeResp({ providers: CONNECTIONS.filter((c) => c.provider === "gemini") })
);
}) as any;
const { runOAuthStatus } = await import("../../bin/cli/commands/oauth.mjs");
await captureStdout(() => runOAuthStatus({ provider: "gemini" }, makeCmd() as any));
globalThis.fetch = origFetch;
assert.ok(capturedUrl.includes("provider=gemini"));
});
test("runOAuthRevoke com --yes chama endpoint de revogação", async () => {
let capturedUrl = "";
let capturedMethod = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string, opts: any) => {
capturedUrl = url;
capturedMethod = opts?.method ?? "GET";
return Promise.resolve(makeResp({}));
}) as any;
const out = await captureStdout(async () => {
const { runOAuthRevoke } = await import("../../bin/cli/commands/oauth.mjs");
await runOAuthRevoke({ provider: "gemini", yes: true }, makeCmd() as any);
});
globalThis.fetch = origFetch;
assert.ok(capturedUrl.includes("/api/oauth/gemini/revoke"));
assert.equal(capturedMethod, "POST");
assert.ok(out.includes("Revoked"));
});
test("runOAuthRevoke com connectionId usa DELETE no provider", async () => {
let capturedUrl = "";
let capturedMethod = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string, opts: any) => {
capturedUrl = url;
capturedMethod = opts?.method ?? "GET";
return Promise.resolve(makeResp({}));
}) as any;
const out = await captureStdout(async () => {
const { runOAuthRevoke } = await import("../../bin/cli/commands/oauth.mjs");
await runOAuthRevoke(
{ provider: "gemini", connectionId: "conn1", yes: true },
makeCmd() as any
);
});
globalThis.fetch = origFetch;
assert.ok(capturedUrl.includes("/api/providers/conn1"));
assert.equal(capturedMethod, "DELETE");
assert.ok(out.includes("Revoked"));
});
test("runOAuthStart flow=import chama endpoint de import", async () => {
let capturedUrl = "";
let capturedMethod = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string, opts: any) => {
capturedUrl = url;
capturedMethod = opts?.method ?? "GET";
return Promise.resolve(makeResp({ count: 3 }));
}) as any;
const out = await captureStdout(async () => {
const { runOAuthStart } = await import("../../bin/cli/commands/oauth.mjs");
await runOAuthStart({ provider: "cursor" }, makeCmd() as any);
});
globalThis.fetch = origFetch;
assert.ok(capturedUrl.includes("/api/oauth/cursor/import"));
assert.equal(capturedMethod, "POST");
assert.ok(out.includes("3"));
});
test("runOAuthStart flow=import com --import-from-system usa auto-import", async () => {
let capturedUrl = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string) => {
capturedUrl = url;
return Promise.resolve(makeResp({ count: 1 }));
}) as any;
const out = await captureStdout(async () => {
const { runOAuthStart } = await import("../../bin/cli/commands/oauth.mjs");
await runOAuthStart({ provider: "zed", importFromSystem: true }, makeCmd() as any);
});
globalThis.fetch = origFetch;
assert.ok(capturedUrl.includes("/api/oauth/zed/auto-import"));
assert.ok(out.includes("1"));
});
test("runOAuthStart rejects retired Windsurf instead of starting a public OAuth flow", async () => {
const origFetch = globalThis.fetch;
const origExit = process.exit;
let fetchCalled = false;
let exitCode: number | undefined;
globalThis.fetch = (() => {
fetchCalled = true;
return Promise.reject(new Error("Windsurf must not start a public OAuth request"));
}) as typeof fetch;
process.exit = ((code?: number | string | null): never => {
exitCode = typeof code === "number" ? code : undefined;
throw new Error(`exit ${code}`);
}) as typeof process.exit;
try {
const { runOAuthStart } = await import("../../bin/cli/commands/oauth.mjs");
await assert.rejects(runOAuthStart({ provider: "windsurf" }, makeCmd()), /exit 2/);
} finally {
globalThis.fetch = origFetch;
process.exit = origExit;
}
assert.equal(exitCode, 2);
assert.equal(fetchCalled, false);
});
test("providers lista provedores OAuth conhecidos", async () => {
const { PROVIDERS_WITH_OAUTH_TEST } = await import("../../bin/cli/commands/oauth.mjs").catch(
() => ({ PROVIDERS_WITH_OAUTH_TEST: null })
);
// validate via runOAuthStart unknown provider exits
const origExit = process.exit;
let exitCode: number | undefined;
process.exit = ((code: number) => {
exitCode = code;
throw new Error("exit");
}) as any;
try {
const { runOAuthStart } = await import("../../bin/cli/commands/oauth.mjs");
await runOAuthStart({ provider: "unknown_provider_xyz" }, makeCmd() as any).catch(() => {});
} catch {
// expected
}
process.exit = origExit;
assert.equal(exitCode, 2);
});