Files
OmniRoute/tests/unit/claude-cli-config.test.ts
Diego Rodrigues de Sa e Souza ed7db3ee5f feat(dashboard): copy-paste settings.json block for Claude Code discovery (#8722)
The discovery-alias info button explains the gate and links to the flag, but the
operator still had to assemble the Claude Code config by hand from the guide.
Render it instead, on the same Claude tool card, from the base URL the card has
already resolved (custom override included, normalized — no /v1, no trailing
slash), with a copy button.

The key slot holds a placeholder, never the real key: this card renders before a
key is necessarily selected, and a real key in copyable text is an easy way to
leak one into a screenshot or a pasted snippet.

buildClaudeDiscoverySettingsSnippet is a pure builder in claudeCliConfig so the
shape is unit-tested (7 cases, including that no `sk-` ever reaches the output
and that an invalid CLAUDE_CODE_AUTO_COMPACT_WINDOW is dropped rather than
emitted). Guide gains the matching section; the five new strings are translated
for pt-BR and vi (the locales with strict parity/marker tests) and marked in the
rest, per the repo's convention.
2026-07-26 18:22:00 -03:00

100 lines
3.6 KiB
TypeScript

import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
buildClaudeDiscoverySettingsSnippet,
getStoredClaudeAuthValue,
normalizeClaudeBaseUrl,
} from "../../src/shared/services/claudeCliConfig.ts";
describe("claudeCliConfig", () => {
it("keeps the unified Claude gateway root without forcing /v1", () => {
assert.equal(normalizeClaudeBaseUrl("http://localhost:20128"), "http://localhost:20128");
assert.equal(normalizeClaudeBaseUrl("http://localhost:20128/"), "http://localhost:20128");
assert.equal(normalizeClaudeBaseUrl("http://localhost:20128/v1"), "http://localhost:20128/v1");
});
it("prefers a stored auth token but can read legacy api key config", () => {
assert.equal(
getStoredClaudeAuthValue({ ANTHROPIC_AUTH_TOKEN: "sk-live-token" }),
"sk-live-token"
);
assert.equal(getStoredClaudeAuthValue({ ANTHROPIC_API_KEY: "sk-legacy-key" }), "sk-legacy-key");
assert.equal(
getStoredClaudeAuthValue({
ANTHROPIC_AUTH_TOKEN: "sk-live-token",
ANTHROPIC_API_KEY: "sk-legacy-key",
}),
"sk-live-token"
);
assert.equal(getStoredClaudeAuthValue({}), null);
});
});
describe("buildClaudeDiscoverySettingsSnippet", () => {
it("emits a settings.json env block with the gateway URL and the discovery flag", () => {
const snippet = buildClaudeDiscoverySettingsSnippet({
baseUrl: "http://192.168.0.111:20128/",
apiKeyPlaceholder: "<sua chave>",
});
const parsed = JSON.parse(snippet);
// Trailing slash normalized — Claude Code appends /v1/messages itself.
assert.equal(parsed.env.ANTHROPIC_BASE_URL, "http://192.168.0.111:20128");
assert.equal(parsed.env.ANTHROPIC_AUTH_TOKEN, "<sua chave>");
assert.equal(parsed.env.CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY, "1");
});
it("never emits a real key — the caller passes the placeholder to render", () => {
const snippet = buildClaudeDiscoverySettingsSnippet({
baseUrl: "http://localhost:20128",
apiKeyPlaceholder: "<paste-your-key>",
});
assert.match(snippet, /<paste-your-key>/);
assert.doesNotMatch(snippet, /sk-/);
});
it("carries the auto-compact window only when a window is supplied", () => {
const without = JSON.parse(
buildClaudeDiscoverySettingsSnippet({ baseUrl: "http://x", apiKeyPlaceholder: "k" })
);
assert.equal("CLAUDE_CODE_AUTO_COMPACT_WINDOW" in without.env, false);
const withWindow = JSON.parse(
buildClaudeDiscoverySettingsSnippet({
baseUrl: "http://x",
apiKeyPlaceholder: "k",
autoCompactWindow: 230000,
})
);
// Emitted as a string: Claude Code reads settings env values as strings.
assert.equal(withWindow.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW, "230000");
});
it("ignores a non-positive or non-finite auto-compact window", () => {
for (const bad of [0, -1, Number.NaN, Number.POSITIVE_INFINITY]) {
const parsed = JSON.parse(
buildClaudeDiscoverySettingsSnippet({
baseUrl: "http://x",
apiKeyPlaceholder: "k",
autoCompactWindow: bad,
})
);
assert.equal(
"CLAUDE_CODE_AUTO_COMPACT_WINDOW" in parsed.env,
false,
`window ${bad} must not reach the snippet`
);
}
});
it("is valid, indented JSON so the operator can paste it straight into settings.json", () => {
const snippet = buildClaudeDiscoverySettingsSnippet({
baseUrl: "http://localhost:20128",
apiKeyPlaceholder: "k",
});
assert.match(snippet, /^\{\n {2}"env": \{\n/);
assert.doesNotThrow(() => JSON.parse(snippet));
});
});