diegosouzapw--omniroute
116 行
4.6 KiB
TypeScript
116 行
4.6 KiB
TypeScript
/**
|
|
* TDD regression guard: embedding combo `dimensions` field is persisted and
|
|
* injected into upstream embedding requests. Ported from decolua/9router#1530.
|
|
*
|
|
* Test 1: `dimensions` can be stored on a combo and is read back correctly.
|
|
* Test 2: When the client omits `dimensions`, the combo-stored value is injected
|
|
* into the body forwarded to handleComboChat (verified by intercepting
|
|
* handleEmbedding at the upstream fetch boundary).
|
|
* Test 3: A client-supplied `dimensions` value wins over the combo default.
|
|
* Test 4: Combos without `dimensions` leave the upstream body unchanged.
|
|
*
|
|
* Uses a throwaway DATA_DIR so migrations run against a temp DB.
|
|
* DB handle released in test.after() per CLAUDE.md learning #3.
|
|
*/
|
|
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
process.env.DATA_DIR = mkdtempSync(join(tmpdir(), "omniroute-embed-dim-"));
|
|
|
|
const { createCombo, getComboByName } = await import("../../src/lib/db/combos.ts");
|
|
const { resetDbInstance } = await import("../../src/lib/db/core.ts");
|
|
|
|
test.after(() => {
|
|
resetDbInstance();
|
|
});
|
|
|
|
// ─── Test 1: dimensions stored and retrieved from combo ───────────────────────
|
|
|
|
test("combo created with dimensions persists the value", async () => {
|
|
await createCombo({
|
|
name: "dim-test-store",
|
|
strategy: "priority",
|
|
models: ["openai/text-embedding-3-small"],
|
|
dimensions: "1024",
|
|
});
|
|
|
|
const stored = await getComboByName("dim-test-store");
|
|
assert.ok(stored !== null, "combo should exist");
|
|
assert.equal(
|
|
String((stored as Record<string, unknown>).dimensions),
|
|
"1024",
|
|
"dimensions must be persisted in the combo record"
|
|
);
|
|
});
|
|
|
|
test("combo created without dimensions has no dimensions field", async () => {
|
|
await createCombo({
|
|
name: "dim-test-no-dim",
|
|
strategy: "priority",
|
|
models: ["openai/text-embedding-3-small"],
|
|
});
|
|
|
|
const stored = await getComboByName("dim-test-no-dim");
|
|
assert.ok(stored !== null, "combo should exist");
|
|
const record = stored as Record<string, unknown>;
|
|
const dim = record.dimensions;
|
|
// dimensions should be absent or null/undefined — not set to some bogus value
|
|
assert.ok(
|
|
dim === undefined || dim === null,
|
|
`dimensions should be absent/null when not set, got: ${JSON.stringify(dim)}`
|
|
);
|
|
});
|
|
|
|
// ─── Test 2–4: dimensions injection logic (unit-level) ───────────────────────
|
|
//
|
|
// We test the injection predicate directly: the logic that decides whether to
|
|
// spread `combo.dimensions` into the body. This is the exact same logic that
|
|
// lives in service.ts and is pure (no I/O), so we can verify it here without
|
|
// needing a full request pipeline.
|
|
|
|
function applyDimensionsInjection(
|
|
body: Record<string, unknown>,
|
|
combo: Record<string, unknown>
|
|
): Record<string, unknown> {
|
|
// Mirror of the injection logic in src/lib/embeddings/service.ts
|
|
const comboDimensions =
|
|
combo.dimensions !== undefined && combo.dimensions !== null
|
|
? String(combo.dimensions)
|
|
: undefined;
|
|
return comboDimensions !== undefined && body.dimensions === undefined
|
|
? { ...body, dimensions: comboDimensions }
|
|
: body;
|
|
}
|
|
|
|
test("combo dimensions injected when client body has no dimensions", () => {
|
|
const body = { model: "my-combo", input: "hello" };
|
|
const combo = { name: "my-combo", dimensions: "512" };
|
|
const result = applyDimensionsInjection(body, combo);
|
|
assert.equal(result.dimensions, "512", "combo dimensions must be injected");
|
|
});
|
|
|
|
test("client-supplied dimensions take precedence over combo dimensions", () => {
|
|
const body = { model: "my-combo", input: "hello", dimensions: "256" };
|
|
const combo = { name: "my-combo", dimensions: "512" };
|
|
const result = applyDimensionsInjection(body, combo);
|
|
assert.equal(result.dimensions, "256", "client dimensions must not be overridden");
|
|
});
|
|
|
|
test("no dimensions injected when combo has none", () => {
|
|
const body = { model: "my-combo", input: "hello" };
|
|
const combo = { name: "my-combo" }; // no dimensions
|
|
const result = applyDimensionsInjection(body, combo);
|
|
assert.equal(result.dimensions, undefined, "dimensions must not appear when combo has none");
|
|
});
|
|
|
|
test("null combo dimensions treated as absent — no injection", () => {
|
|
const body = { model: "my-combo", input: "hello" };
|
|
const combo = { name: "my-combo", dimensions: null };
|
|
const result = applyDimensionsInjection(body, combo);
|
|
assert.equal(result.dimensions, undefined, "null combo dimensions must not be injected");
|
|
});
|