Files
OmniRoute/tests/unit/feature-flags-route-virtual-lanes.test.ts
Praveen K Palaniswamy 65e81158ab fix(ollama): route models by advertised capability (#11088)
Landed with the design call resolved per the owner's pick — **option 1**: the synced store is now endpoint-agnostic (persistDiscoveredModels and managedModelImport no longer drop non-chat models at write time), and chat selectability moved to read time (auto-pool expansion in autoStrategy applies filterChatSelectableModels; the models-route projection already had its chatOnly filter). Your discovery test now passes end-to-end (3/3): /api/show capabilities persist per connection and image/embedding requests route through the advertising host.

Reconciliation notes: conflicted areas merged onto the current tip (adobe discovery import, requestedModel preflight signature, resolvedProvider fast-path coexists with the synced-route override — explicit resolution wins); carried base-red drains (#10055 memoization, #11071 test variants) dropped as already-landed; the managed-model-import exclusion test was propagated to the new contract (image/video models persist; the read filter still hides them from chat pickers — pinned by a new assertion). Full battery: 205/206 focused (the one red is a confirmed periodic-timer timing flake on the loaded devbox — 20/20 isolated), autoCombo vitest 30/30, combo suites 46/46, gates + typecheck clean.

Thank you @yourspraveen — the capability probe + routing design was right; it just needed the store contract opened up. Fixes #11087.
2026-08-23 11:45:01 -03:00

128 lines
4.7 KiB
TypeScript

/**
* U7 (#9654 Wave 2) — route-level acceptance for the adaptive virtual-lanes flag.
*
* Ticket acceptance: "flag appears in GET /api/settings/feature-flags; env
* still wins." Exercises the GET + PUT handlers directly (JWT cookie auth),
* asserting the env-wins source reporting and the requiresRestart surface.
*
* Run: bun test tests/unit/feature-flags-route-virtual-lanes.test.ts
*/
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test, { after, before } from "node:test";
import { SignJWT } from "jose";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-ff-vl-route-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const { GET, PUT } = await import("../../src/app/api/settings/feature-flags/route.ts");
const { removeFeatureFlagOverride, setFeatureFlagOverride } =
await import("../../src/lib/db/featureFlags");
const { ADAPTIVE_VIRTUAL_LANES_FLAG_KEY } = await import("../../src/lib/admissionVirtualLanes.ts");
const ORIGINAL_ENV_VALUE = process.env[ADAPTIVE_VIRTUAL_LANES_FLAG_KEY];
type FlagPayload = {
key: string;
label: string;
type: string;
defaultValue: string;
effectiveValue: string;
source: string;
requiresRestart: boolean;
};
async function authCookie(): Promise<string> {
process.env.JWT_SECRET = "test-feature-flags-route-secret";
const secret = new TextEncoder().encode(process.env.JWT_SECRET);
const token = await new SignJWT({ sub: "test-user" })
.setProtectedHeader({ alg: "HS256" })
.setIssuedAt()
.setExpirationTime("1h")
.sign(secret);
return `auth_token=${token}`;
}
async function buildGetRequest(): Promise<Request> {
const cookie = await authCookie();
return new Request("http://localhost/api/settings/feature-flags", {
headers: { cookie },
});
}
async function buildPutRequest(value: string): Promise<Request> {
const cookie = await authCookie();
return new Request("http://localhost/api/settings/feature-flags", {
method: "PUT",
headers: { cookie, "Content-Type": "application/json" },
body: JSON.stringify({ key: ADAPTIVE_VIRTUAL_LANES_FLAG_KEY, value }),
});
}
async function getFlag(): Promise<FlagPayload> {
const res = await GET(await buildGetRequest());
assert.equal(res.status, 200);
const json = (await res.json()) as { flags: FlagPayload[] };
const flag = json.flags.find((f) => f.key === ADAPTIVE_VIRTUAL_LANES_FLAG_KEY);
assert.ok(flag, `flag ${ADAPTIVE_VIRTUAL_LANES_FLAG_KEY} must appear in GET`);
return flag;
}
before(() => {
removeFeatureFlagOverride(ADAPTIVE_VIRTUAL_LANES_FLAG_KEY);
});
after(() => {
if (ORIGINAL_ENV_VALUE === undefined) {
delete process.env[ADAPTIVE_VIRTUAL_LANES_FLAG_KEY];
} else {
process.env[ADAPTIVE_VIRTUAL_LANES_FLAG_KEY] = ORIGINAL_ENV_VALUE;
}
removeFeatureFlagOverride(ADAPTIVE_VIRTUAL_LANES_FLAG_KEY);
});
test("flag appears in GET with the requiresRestart boolean surface (default off)", async () => {
delete process.env[ADAPTIVE_VIRTUAL_LANES_FLAG_KEY];
const flag = await getFlag();
assert.equal(flag.type, "boolean");
assert.equal(flag.defaultValue, "false");
assert.equal(flag.requiresRestart, true, "runtime reads env at construction — restart required");
assert.equal(flag.effectiveValue, "false");
assert.equal(flag.source, "default");
});
test('env wins over a DB override in GET (env "1" + DB false -> env)', async () => {
process.env[ADAPTIVE_VIRTUAL_LANES_FLAG_KEY] = "1";
setFeatureFlagOverride(ADAPTIVE_VIRTUAL_LANES_FLAG_KEY, "false");
const flag = await getFlag();
assert.equal(flag.effectiveValue, "true");
assert.equal(flag.source, "env");
});
test('env explicit off still wins in GET (env "0" + DB true -> env off)', async () => {
process.env[ADAPTIVE_VIRTUAL_LANES_FLAG_KEY] = "0";
setFeatureFlagOverride(ADAPTIVE_VIRTUAL_LANES_FLAG_KEY, "true");
const flag = await getFlag();
assert.equal(flag.effectiveValue, "false");
assert.equal(flag.source, "env");
});
test("DB override enables when env is absent (source db)", async () => {
delete process.env[ADAPTIVE_VIRTUAL_LANES_FLAG_KEY];
setFeatureFlagOverride(ADAPTIVE_VIRTUAL_LANES_FLAG_KEY, "true");
const flag = await getFlag();
assert.equal(flag.effectiveValue, "true");
assert.equal(flag.source, "db");
});
test("PUT response reports env-wins truth when env is set (operator toggle cannot lie)", async () => {
process.env[ADAPTIVE_VIRTUAL_LANES_FLAG_KEY] = "0";
const res = await PUT(await buildPutRequest("true"));
assert.equal(res.status, 200);
const json = (await res.json()) as { effectiveValue: string; source: string };
assert.equal(json.effectiveValue, "false", 'env "0" must still win over a dashboard PUT "true"');
assert.equal(json.source, "env");
});