Files
OmniRoute/tests/unit/api-manager-page-static.test.ts
Moseyuh333 eeec4d9e87 feat(chaos): big update - optimize, fix bugs, add features, enhance UX (#6728)
* feat(chaos): add Chaos Mode — multi-model parallel/collaborative execution

- New DB column chaos_mode_enabled on api_keys table
- API key create/PATCH routes support chaosModeEnabled toggle
- Core library src/lib/chaos/chaosConfig.ts for persistent config
- API routes: GET/PUT/DELETE /api/chaos/config
- Chaos execution POST /api/skills/collect/chaos with key auth
- Dashboard page at /dashboard/chaos with full config UI
- Sidebar entry in Agentic Features section
- Chaos mode toggle in API Key editor permissions panel
- i18n keys for chaos config (en.json)

* feat(chaos): big update — optimize, fix bugs, add features

=== Changes ===

1. NEW: src/lib/chaos/chaosExecutor.ts — shared execution engine
   - Removed ~150 lines of duplicate dispatch logic between two API routes
   - Single executeChaosRun() function used by both endpoints
   - Added concurrency limit (max 10 parallel requests)
   - Added proper TypeScript interfaces (ChaosRunInput, ChaosRunResult)
   - Added error logging throughout

2. FIX: src/app/api/skills/collect/chaos/route.ts
   - Was MISSING logger import (log.error was undefined at runtime)
   - Reduced from 388 lines → 142 lines by delegating to shared executor
   - Added maxTokens support in schema validation

3. REFACTOR: src/app/api/chaos/run/route.ts
   - Simplified to thin wrapper: auth + validate + delegate to executor
   - Added maxTokens support

4. ENHANCE: src/lib/chaos/chaosConfig.ts
   - Added maxTokens config field (256-128k, default 4096)
   - Persisted per-instance via settings table

5. ENHANCE: UI — ChaosConfigPageClient.tsx
   - Loads available providers from /api/models for dropdown autocomplete
   - Added datalist-based provider selector in overrides section
   - Added Max Tokens configuration input
   - Added expandable provider list showing all detected providers
   - Fixed duplicate override detection

* fix(chaos): fetch providers from /api/providers instead of /api/keys

* fix(chaos): remove dead code isOverrideDuplicate, fix maxTokens fallback to include global config

* fix(chaos): resetConfig now shows error on HTTP failure (was silent)

* feat(dashboard): Chaos Mode — multi-model parallel/collaborative execution

Splits the PR down to only the genuinely new Chaos Mode feature (drops the
duplicate Skill Collector/GitHub-discovery portion already shipped via
#6186). Replaces the loopback fetch() dispatch (hardcoded to the wrong port)
with the established in-process synthetic-Request/route-handler pattern used
by src/lib/batches/dispatch.ts, moves settings persistence off raw SQL, and
adds unit test coverage for chaosConfig, chaosExecutor and the 3 chaos API
routes.

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

* fix(chaos): fix external Bearer-auth bypass and stale config cache in tests

validateApiKey() returns a plain boolean for both the deployment-time env key
and a DB-backed key, so branching on `keyInfo === true` in
verifyChaosKey() (src/app/api/skills/collect/chaos/route.ts) treated every
valid API key as having full env-key access, silently skipping the
chaosModeEnabled permission check entirely. Now always resolves through
getApiKeyMetadata() and only bypasses the per-key check for the synthesized
env-key record (id: "env-key").

Also exports invalidateChaosConfigCache() from chaosConfig.ts and wires it
into the route tests' resetStorage() — the in-process config cache was
surviving DB resets between tests, causing state to leak across cases.

Fixes CHANGELOG-eat from the release merge (re-inserted the Chaos Mode
bullet against the base CHANGELOG.md, verified additive via
check-changelog-integrity.mjs) and re-syncs against release/v3.8.47 tip.

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

* docs(changelog): Chaos Mode overhaul bullet referencing #6728 after release sync

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

* fix(merge): restore #6126 clinepass files reverted by release auto-resolve + baseline re-merge

The release sync's auto-resolve reverted sibling PR #6126's clinepass work
(registry, catalog, oauth constants, clineAuth.ts, token-refresh case, tests)
and the file-size baseline — all outside this PR's scope. Restored to the
release versions, re-applied only this PR's own baseline entries, restored the
#6126 CHANGELOG bullet (re-inserting only this PR's own).

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

* fix(dashboard): chaos client hook must not import the server Pino logger

useChaosConfigData ("use client") pulled @/sse/utils/logger → shared Pino →
logRotation/dataPaths → node:fs into the browser bundle, breaking next build
(Turbopack: Can't resolve 'fs') — caught by the DAST smoke's isolated build.
console.error matches every other dashboard client component.

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

* test(api-manager): align switch-count invariant with the extracted toggle components

The Self-service block now renders 4 inline switches; the #5731 quota-bypass
and #6728 chaos-access toggles were extracted into dedicated components. The
type="button" invariant is preserved AND extended: the test now also asserts
each extracted component's switches declare type="button".

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

* chore(sync): merge release tip + restore own CHANGELOG bullet

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

---------

Co-authored-by: Moseyuh333 <Moseyuh333@users.noreply.github.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
2026-07-09 23:49:48 -03:00

162 lines
7.1 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const pagePath = path.join(
repoRoot,
"src/app/(dashboard)/dashboard/api-manager/ApiManagerPageClient.tsx"
);
const messagesDir = path.join(repoRoot, "src/i18n/messages");
const selfServiceScopeMessageKeys = [
"selfServiceVisibility",
"selfServiceVisibilityDesc",
"ownUsageVisibility",
"ownUsageVisibilityDesc",
"sharedAccountQuotaVisibility",
"sharedAccountQuotaVisibilityDesc",
];
function readApiManagerPage() {
return fs.readFileSync(pagePath, "utf8");
}
test("permissions modal uses i18n for management access description", () => {
const source = readApiManagerPage();
const managementBlock = source.slice(
source.indexOf("{/* Management Access */}", source.indexOf("const PermissionsModal")),
source.indexOf("{/* Self-service Visibility */}", source.indexOf("const PermissionsModal"))
);
assert.match(managementBlock, /\{t\("managementAccessDesc"\)\}/);
assert.doesNotMatch(managementBlock, /Allow this API key to manage OmniRoute configuration\./);
});
test("permissions modal converts API key expiration ISO timestamps to local datetime input values", () => {
const source = readApiManagerPage();
const expirationBlock = source.slice(
source.indexOf("{/* Expiration Date */}", source.indexOf("const PermissionsModal")),
source.indexOf("{/* Management Access */}", source.indexOf("const PermissionsModal"))
);
assert.match(expirationBlock, /value=\{toLocalDateTimeInputValue\(expiresAt\)\}/);
assert.match(expirationBlock, /const date = new Date\(val\)/);
assert.match(expirationBlock, /setExpiresAt\(date\.toISOString\(\)\)/);
assert.match(expirationBlock, /onClick=\{\(\) => setExpiresAt\(""\)\}/);
assert.match(expirationBlock, /\{tc\("clear"\)\}/);
assert.doesNotMatch(expirationBlock, /expiresAt\.slice\(0, 16\)/);
});
test("permissions modal switch buttons declare button type", () => {
const source = readApiManagerPage();
const modalStart = source.indexOf("const PermissionsModal");
const visibilityStart = source.indexOf("{/* Self-service Visibility */}", modalStart);
const visibilityEnd = source.indexOf("{/* Selected Models Summary", visibilityStart);
const selfServiceBlock = source.slice(visibilityStart, visibilityEnd);
const switchButtonCount = (selfServiceBlock.match(/role="switch"/g) ?? []).length;
const typedSwitchButtonCount = (
selfServiceBlock.match(/<button\s+type="button"\s+role="switch"/g) ?? []
).length;
// Self-service Visibility block has 4 inline switches: own-usage visibility,
// shared-account quota visibility, disable-non-public-models (#3041), and the
// per-key local usage command allowance (#4034). The API-key provider
// quota-policy bypass scope (#5731) and the Chaos Mode access scope (#6728)
// were extracted into dedicated toggle components (asserted below).
// The invariant is that every switch declares type="button"
// (typedSwitchButtonCount === switchButtonCount) to avoid implicit submit.
assert.equal(switchButtonCount, 4);
assert.equal(typedSwitchButtonCount, 4);
// The extracted toggle components keep the same invariant.
for (const rel of [
"src/app/(dashboard)/dashboard/api-manager/components/BypassProviderQuotaToggle.tsx",
"src/app/(dashboard)/dashboard/api-manager/components/ChaosModeAccessToggle.tsx",
]) {
const componentSource = fs.readFileSync(path.join(repoRoot, rel), "utf8");
const compSwitches = (componentSource.match(/role="switch"/g) ?? []).length;
const compTyped = (componentSource.match(/<button\s+type="button"\s+role="switch"/g) ?? [])
.length;
assert.ok(compSwitches >= 1, `${rel} must render a switch`);
assert.equal(compTyped, compSwitches, `${rel}: every switch declares type="button"`);
}
});
test("permissions modal exposes Claude Code default wildcard model", () => {
const source = readApiManagerPage();
assert.match(source, /const CLAUDE_CODE_DEFAULT_MODEL_ID = "cc\/\*";/);
assert.match(source, /const CLAUDE_CODE_DEFAULT_MODEL_NAME = "Claude Code default";/);
assert.match(source, /withClaudeCodeDefaultModel\(allModels\)/);
assert.match(source, /getModelDisplayName\(model\.id\)/);
assert.match(
source,
/modelId === CLAUDE_CODE_DEFAULT_MODEL_ID\s+\?\s+CLAUDE_CODE_DEFAULT_MODEL_NAME\s+:\s+modelId/
);
assert.doesNotMatch(source, /modelById\.get\(modelId\)\?\.name/);
});
test("permissions modal expands Claude Code default families in selected models summary", () => {
const source = readApiManagerPage();
assert.match(source, /const CLAUDE_CODE_DEFAULT_FAMILIES = \[/);
assert.match(source, /id: "other",\s+label: "other"/);
assert.match(source, /id: "fable",\s+label: "fable"/);
assert.match(source, /id: "opus",\s+label: "opus"/);
assert.match(source, /id: "sonnet",\s+label: "sonnet"/);
assert.match(source, /id: "haiku",\s+label: "haiku"/);
assert.match(source, /const orderedSelectedModels = useMemo/);
assert.match(source, /modelId === CLAUDE_CODE_DEFAULT_MODEL_ID/);
assert.match(source, /setClaudeCodeFamiliesExpanded/);
assert.match(
source,
/const \[claudeCodeFamiliesExpanded,\s*setClaudeCodeFamiliesExpanded\] = useState\(false\)/
);
assert.doesNotMatch(source, /setClaudeCodeFamiliesExpanded\(true\)/);
assert.match(source, /aria-expanded=\{claudeCodeFamiliesExpanded\}/);
assert.match(source, /bg-primary\/25/);
assert.match(source, /handleBlockClaudeCodeFamily/);
assert.match(source, /blockedModels: validBlockedModels/);
assert.match(
source,
/blockedModels\.push\(\.\.\.CLAUDE_CODE_FAMILY_BLOCK_PATTERNS\[familyId\]\)/
);
assert.doesNotMatch(source, /Block Fable family/);
});
test("API-key model fallback preserves combo pseudo-models", () => {
const source = readApiManagerPage();
const fallbackBlock = source.slice(
source.indexOf("const [fallbackRes, combosRes] = await Promise.all"),
source.indexOf("} catch (error)", source.indexOf("const [fallbackRes, combosRes] = await Promise.all"))
);
assert.match(fallbackBlock, /fetch\("\/api\/models\?all=true"\)/);
assert.match(fallbackBlock, /fetch\("\/api\/combos"\)/);
assert.match(fallbackBlock, /owned_by: "combo"/);
assert.match(fallbackBlock, /\[\.\.\.comboModels, \.\.\.modelEntries\]/);
assert.match(fallbackBlock, /seen\.has\(m\.id\)/);
});
test("self-service API key scope labels do not expose missing placeholders", () => {
const messageFiles = fs.readdirSync(messagesDir).filter((file) => file.endsWith(".json"));
for (const file of messageFiles) {
const messages = JSON.parse(fs.readFileSync(path.join(messagesDir, file), "utf8"));
for (const key of selfServiceScopeMessageKeys) {
const value = messages.apiManager?.[key];
assert.equal(typeof value, "string", `${file}: apiManager.${key} should exist`);
assert.ok(value.length > 0, `${file}: apiManager.${key} should not be empty`);
assert.ok(
!value.startsWith("__MISSING__:"),
`${file}: apiManager.${key} should not expose a missing placeholder`
);
}
}
});