Files
OmniRoute/tests/unit/oauth-keychain-import-only-guard.test.ts
Diego Rodrigues de Sa e Souza cf6c2798b4 fix(oauth): extract keychain-import-only guard to restore file-size freeze (base-red) (#6158)
`src/app/api/oauth/[provider]/[action]/route.ts` grew to 959 lines, past its
frozen cap of 924 (`check:file-size` → Fast Quality Gates red on release/v3.8.44).
The growth came from #6054 (graceful 400 for keychain-import-only providers / zed):
a doc block, two Sets (KEYCHAIN_IMPORT_ONLY_PROVIDERS, OAUTH_FLOW_ACTIONS) and a
keychainImportOnlyResponse() helper, plus two duplicated guard blocks in GET/POST.

That is a cohesive, self-contained leaf, so extract it to a new
`keychainImportOnly.ts` exposing `keychainImportOnlyGuard(provider, action)`
(returns the 400 NextResponse or null). The two route callsites collapse to a
2-line guard each. route.ts: 959 -> 918 (< 924, freeze restored). No behavior
change.

Tests (Rule #8/#18):
- Existing tests/unit/oauth-keychain-import-only-6041.test.ts (route-level GET/POST
  zed 400) still pass unchanged — behavior preserved.
- New tests/unit/oauth-keychain-import-only-guard.test.ts pins the extracted guard
  in isolation (zed+flow -> 400, normal provider -> null, zed+non-flow -> null).
2026-07-05 02:29:16 -03:00

39 lines
1.6 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
keychainImportOnlyGuard,
KEYCHAIN_IMPORT_ONLY_PROVIDERS,
OAUTH_FLOW_ACTIONS,
} from "../../src/app/api/oauth/[provider]/[action]/keychainImportOnly.ts";
// Unit coverage for the leaf extracted from the OAuth route (#6155 file-size
// base-red follow-up). The route-level behavior is guarded by
// oauth-keychain-import-only-6041.test.ts; this pins the guard in isolation.
test("keychainImportOnlyGuard returns a 400 for a keychain-import-only provider on an OAuth-flow action", async () => {
const res = keychainImportOnlyGuard("zed", "authorize");
assert.ok(res, "expected a response, not null");
assert.equal(res!.status, 400);
const body = await res!.json();
assert.match(body.error, /no browser OAuth flow/i);
assert.match(body.error, /Import/);
});
test("keychainImportOnlyGuard returns null for a normal OAuth provider", () => {
assert.equal(keychainImportOnlyGuard("openai", "authorize"), null);
assert.equal(keychainImportOnlyGuard("anthropic", "exchange"), null);
});
test("keychainImportOnlyGuard returns null for a keychain provider on a non-flow action", () => {
// e.g. a callback/status action that is not in OAUTH_FLOW_ACTIONS
assert.equal(keychainImportOnlyGuard("zed", "status"), null);
});
test("the sets stay in sync with the guard's expectations", () => {
assert.ok(KEYCHAIN_IMPORT_ONLY_PROVIDERS.has("zed"));
assert.ok(OAUTH_FLOW_ACTIONS.has("authorize"));
assert.ok(OAUTH_FLOW_ACTIONS.has("exchange"));
assert.ok(!OAUTH_FLOW_ACTIONS.has("status"));
});