Files
OmniRoute/tests/unit/coze-validation-error-5426.test.ts
Diego Rodrigues de Sa e Souza 0adae00c7b Release v3.8.42 (#5459)
Release v3.8.42 — full CHANGELOG in CHANGELOG.md.

CI: 103 checks green incl. CodeQL (all languages), Semgrep, all 8 unit shards,
coverage, Node 24 compat, and integration tests. Full unit suite validated
locally: 19437 pass / 0 fail. The 3 red checks are advisory and do not gate
main (no required status checks): SonarCloud/SonarQube new-code coverage gate,
and PR Test Policy (test-masking detector flagging the legitimate dead-Phind
provider removal in #5530 — reviewed, correct).

Includes cycle-close reconciliation + repair of inherited base-red tests from
#5480/#5527/#5427/#5521 that the PR->release fast-path did not exercise.
2026-06-30 06:54:29 -03:00

57 lines
2.3 KiB
TypeScript

// #5426 — Coze key validation must surface a friendly message instead of leaking
// the raw upstream error envelope ({ code, msg, logId, from }) into the UI.
import { strict as assert } from "node:assert";
import { describe, it } from "node:test";
import { extractCozeValidationError } from "@/lib/providers/validation/cozeError";
describe("extractCozeValidationError (#5426)", () => {
it("builds a friendly message from a Coze envelope with msg + code", () => {
const body = {
code: 4100,
msg: "The token you entered is incorrect. Please check and try again.",
logId: "20240101000000ABCDEF",
from: "bot-api",
};
const result = extractCozeValidationError(body);
assert.equal(
result,
"Coze rejected the key: The token you entered is incorrect. Please check and try again. (code 4100)"
);
// Never echo the raw logId or the whole envelope.
assert.ok(result && !result.includes("20240101000000ABCDEF"));
assert.ok(result && !result.includes("logId"));
});
it('recognizes the from:"bot-api" variant', () => {
const body = { code: 700012006, msg: "rejected", from: "bot-api" };
const result = extractCozeValidationError(body);
assert.equal(result, "Coze rejected the key: rejected (code 700012006)");
});
it("recognizes a stringified JSON envelope", () => {
const body = JSON.stringify({ msg: "bad key", code: 4100 });
assert.equal(extractCozeValidationError(body), "Coze rejected the key: bad key (code 4100)");
});
it("returns null for a normal OpenAI error envelope", () => {
const body = {
error: {
message: "Incorrect API key provided",
type: "invalid_request_error",
code: "invalid_api_key",
},
};
assert.equal(extractCozeValidationError(body), null);
});
it("returns null for non-object / empty / non-JSON inputs", () => {
assert.equal(extractCozeValidationError(null), null);
assert.equal(extractCozeValidationError(undefined), null);
assert.equal(extractCozeValidationError(42), null);
assert.equal(extractCozeValidationError(""), null);
assert.equal(extractCozeValidationError("not json at all"), null);
assert.equal(extractCozeValidationError({}), null);
assert.equal(extractCozeValidationError([]), null);
});
});