65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
// Smoke-test for the FindKeyByAlias parsing logic — runs the JSON
|
|
// permutations LiteLLM has been seen to emit through the unmarshal
|
|
// paths and confirms each ends up at the expected outcome.
|
|
//
|
|
// Since the operator can't run inside this sandbox, this is a
|
|
// JS port of the parsing flow. It exercises decisions the Go code
|
|
// makes line-for-line.
|
|
|
|
const cases = [
|
|
{
|
|
name: "newer object shape, alias matches",
|
|
body: { keys: [{ token: "tk-1", key_alias: "acme-abc12345" }, { token: "tk-2", key_alias: "beta-def67890" }] },
|
|
expected: "tk-1",
|
|
},
|
|
{
|
|
name: "newer object shape, alias does not match",
|
|
body: { keys: [{ token: "tk-2", key_alias: "beta-def67890" }] },
|
|
expected: "",
|
|
},
|
|
{
|
|
name: "newer object shape, empty keys array",
|
|
body: { keys: [] },
|
|
expected: "",
|
|
},
|
|
{
|
|
name: "older string shape — cannot filter, return empty",
|
|
body: { keys: ["sk-abc", "sk-def"] },
|
|
expected: "",
|
|
},
|
|
{
|
|
name: "matching alias but missing token field",
|
|
body: { keys: [{ key_alias: "acme-abc12345" }] },
|
|
expected: "",
|
|
},
|
|
];
|
|
|
|
function findKeyByAlias(body, keyAlias) {
|
|
// Mirror the Go logic exactly.
|
|
let asObjects;
|
|
try {
|
|
asObjects = body;
|
|
if (!asObjects || !Array.isArray(asObjects.keys)) return "";
|
|
for (const k of asObjects.keys) {
|
|
// Skip non-objects (= older string shape)
|
|
if (typeof k !== "object" || k === null) continue;
|
|
if (k.key_alias === keyAlias && k.token) {
|
|
return k.token;
|
|
}
|
|
}
|
|
} catch {
|
|
return "";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
let pass = 0, fail = 0;
|
|
for (const c of cases) {
|
|
const got = findKeyByAlias(c.body, "acme-abc12345");
|
|
const ok = got === c.expected;
|
|
console.log(`${ok ? "PASS" : "FAIL"} got="${got}" want="${c.expected}" [${c.name}]`);
|
|
if (ok) pass++; else fail++;
|
|
}
|
|
console.log(`\n${pass} pass, ${fail} fail`);
|
|
process.exit(fail === 0 ? 0 : 1);
|