Files
OmniRoute/tests/unit/provider-filters-url-sync.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

146 lines
4.8 KiB
TypeScript

import { describe, it, afterEach } from "node:test";
import assert from "node:assert/strict";
import {
readProviderFiltersFromUrl,
syncProviderFiltersToUrl,
} from "../../src/app/(dashboard)/dashboard/providers/providerPageUtils.ts";
type MockHistory = {
state: unknown;
replaceState(state: unknown, title: string, url: string): void;
};
type MockLocation = { href: string };
type MockWindow = { history: MockHistory; location: MockLocation };
const globals = globalThis as unknown as { window?: MockWindow };
const originalWindow = globals.window;
function mockWindow(href: string): string[] {
const calls: string[] = [];
globals.window = {
history: {
state: null,
replaceState(_state: unknown, _title: string, url: string) {
calls.push(url);
},
},
location: { href },
};
return calls;
}
afterEach(() => {
if (originalWindow === undefined) {
delete globals.window;
} else {
globals.window = originalWindow;
}
});
describe("readProviderFiltersFromUrl", () => {
it("parses every supported param", () => {
const state = readProviderFiltersFromUrl(
new URLSearchParams("search=openai&model=gpt&mode=compact&cat=free&media=image")
);
assert.deepEqual(state, {
searchQuery: "openai",
modelSearchQuery: "gpt",
displayMode: "compact",
showFreeOnly: true,
category: null,
mediaKind: "image",
});
});
it("reads a concrete category and the display mode", () => {
const state = readProviderFiltersFromUrl(new URLSearchParams("cat=oauth&mode=configured"));
assert.deepEqual(state, { displayMode: "configured", showFreeOnly: false, category: "oauth" });
});
it("ignores invalid category / media / mode values", () => {
const state = readProviderFiltersFromUrl(
new URLSearchParams("cat=bogus&mode=unknown&media=audio")
);
assert.deepEqual(state, {});
});
it("returns empty state for no params", () => {
assert.deepEqual(readProviderFiltersFromUrl(new URLSearchParams("")), {});
});
});
describe("syncProviderFiltersToUrl", () => {
it("does not throw when window is undefined (SSR environment)", () => {
assert.doesNotThrow(() => {
syncProviderFiltersToUrl({ displayMode: "compact", category: "oauth" });
});
});
it("writes all active filters via replaceState", () => {
const calls = mockWindow("http://localhost/dashboard/providers");
syncProviderFiltersToUrl({
searchQuery: "openai",
modelSearchQuery: "gpt",
displayMode: "compact",
category: "oauth",
showFreeOnly: false,
mediaKind: "image",
});
assert.equal(calls.length, 1);
const url = new URL(calls[0]);
assert.equal(url.searchParams.get("search"), "openai");
assert.equal(url.searchParams.get("model"), "gpt");
assert.equal(url.searchParams.get("mode"), "compact");
assert.equal(url.searchParams.get("cat"), "oauth");
assert.equal(url.searchParams.get("media"), "image");
});
it("encodes the free-tier filter as cat=free", () => {
const calls = mockWindow("http://localhost/dashboard/providers");
syncProviderFiltersToUrl({ showFreeOnly: true, category: null, displayMode: "all" });
assert.equal(calls.length, 1);
const url = new URL(calls[0]);
assert.equal(url.searchParams.get("cat"), "free");
assert.equal(url.searchParams.has("mode"), false);
});
it("drops the display-mode param when set back to All", () => {
const calls = mockWindow("http://localhost/dashboard/providers?mode=compact");
syncProviderFiltersToUrl({ displayMode: "all" });
assert.equal(calls.length, 1);
assert.equal(new URL(calls[0]).searchParams.has("mode"), false);
});
it("removes params whose filters have been cleared", () => {
const calls = mockWindow(
"http://localhost/dashboard/providers?mode=compact&cat=oauth&media=image&search=openai"
);
syncProviderFiltersToUrl({});
assert.equal(calls.length, 1);
const url = new URL(calls[0]);
assert.equal(url.searchParams.has("mode"), false);
assert.equal(url.searchParams.has("cat"), false);
assert.equal(url.searchParams.has("media"), false);
assert.equal(url.searchParams.has("search"), false);
});
it("preserves unrelated URL params and path", () => {
const calls = mockWindow("http://localhost/dashboard/providers?tab=x&mode=compact");
syncProviderFiltersToUrl({});
assert.equal(calls.length, 1);
const url = new URL(calls[0]);
assert.equal(url.pathname, "/dashboard/providers");
assert.equal(url.searchParams.get("tab"), "x");
assert.equal(url.searchParams.has("mode"), false);
});
it("does not replaceState when nothing changed", () => {
const calls = mockWindow("http://localhost/dashboard/providers?search=openai");
syncProviderFiltersToUrl({ searchQuery: "openai" });
assert.equal(calls.length, 0);
});
});