Files
OmniRoute/tests/unit/aihorde-image-catalog.test.ts
pageragatz b9cd5ed138 feat(providers): optional AI Horde API key and live image catalog (#10542)
* feat(providers): optional AI Horde API key and live image catalog

Allow a registered Horde key on the no-auth connection and send it for
chat and image jobs. List only image models that currently have workers,
and generate through Horde's native async API.

# Conflicts:
#	open-sse/config/imageRegistry.ts
#	src/app/(dashboard)/dashboard/providers/[id]/ProviderDetailPageClient.tsx
#	src/shared/constants/providers.ts
#	src/sse/services/auth.ts

* fix(providers): validate AI Horde keys against find_user

The OpenAI-compatible /v1/models probe returns 200 for any Bearer token
on oai.aihorde.net, so Check always succeeded. Use Horde's /v2/find_user
lookup instead; an empty key still counts as the optional anonymous path.

* chore(changelog): name the AI Horde fragment for #10542

* fix(images): harden AI Horde optional-key selection and outbound fetches

- Optional-key selection now honors connection health (rate-limit cooldown
  and terminal/unavailable test status) before handing a stored key back,
  rotating to the next healthy key or falling back to the anonymous no-auth
  path instead of using an unhealthy stored key.
- Route the Horde submit/check/status/cancel and catalog calls through the
  repository's bounded outbound-fetch helper (timeout, no more bare fetch())
  and route R2 image downloads through the established bounded remote-image
  fetch (SSRF host guard, DNS-rebinding pin, streaming byte cap, redirect
  limit) instead of an unbounded fetch().
- Extend the generation deadline to cover the full request lifecycle
  (catalog freshness check, submit, polling, and image download), and add a
  regression test proving that exceeding the deadline issues a DELETE
  cancel to Horde's API rather than only timing out locally.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: pqr <pqr@soraka.ititti.es>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-08-18 10:51:57 -03:00

105 lines
3.1 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
HordeImageCatalog,
parseHordeImageModels,
resetAiHordeImageCatalog,
getCachedAiHordeImageCatalogEntries,
aiHordeImageCatalog,
} from "../../open-sse/services/aihordeImageCatalog.ts";
import {
parseImageModel,
getImageProvider,
getAllImageModels,
} from "../../open-sse/config/imageRegistry.ts";
const HORDE_MODELS = [
{
name: "FLUX.1-schnell",
count: 6,
queued: 2,
eta: 12,
performance: 18.5,
type: "image",
},
{
name: "AlbedoBase XL (SDXL)",
count: 1,
queued: 0,
eta: 4,
type: "image",
},
{ name: "DeadModel", count: 0, type: "image" },
{ name: "koboldcpp/Gemma", count: 3, type: "text" },
];
test.afterEach(() => {
resetAiHordeImageCatalog();
});
test("parseHordeImageModels drops zero-worker and text models", () => {
const models = parseHordeImageModels(HORDE_MODELS);
assert.deepEqual(
models.map((model) => model.name),
["AlbedoBase XL (SDXL)", "FLUX.1-schnell"]
);
assert.equal(models[1].count, 6);
});
test("parseHordeImageModels rejects a non-array payload", () => {
assert.throws(() => parseHordeImageModels({ name: "nope" }), /JSON array/);
});
test("refresh keeps the last good snapshot when Horde is down", async () => {
let calls = 0;
const catalog = new HordeImageCatalog({
pollMs: 5_000,
fetchImpl: async () => {
calls += 1;
if (calls === 1) {
return new Response(JSON.stringify(HORDE_MODELS), { status: 200 });
}
return new Response(JSON.stringify({ message: "maintenance" }), { status: 503 });
},
});
await catalog.refresh();
assert.equal(catalog.isServed("FLUX.1-schnell"), true);
assert.equal(catalog.snapshot.lastError, null);
await catalog.refresh();
assert.equal(catalog.isServed("FLUX.1-schnell"), true);
assert.equal(catalog.stale, true);
assert.ok(catalog.snapshot.lastError);
assert.equal(
catalog.listModels().some((model) => model.name === "DeadModel"),
false
);
});
test("live catalog entries use exact Horde names and the aihorde prefix", async () => {
aiHordeImageCatalog.setFetch(
async () => new Response(JSON.stringify(HORDE_MODELS), { status: 200 })
);
await aiHordeImageCatalog.refresh();
const ids = getCachedAiHordeImageCatalogEntries().map((model) => model.id);
assert.deepEqual(ids, ["aihorde/AlbedoBase XL (SDXL)", "aihorde/FLUX.1-schnell"]);
const listed = getAllImageModels().map((model) => model.id);
assert.ok(listed.includes("aihorde/FLUX.1-schnell"));
assert.equal(listed.includes("aihorde/DeadModel"), false);
});
test("parseImageModel accepts aihorde/ and horde/ prefixes for live names", () => {
assert.deepEqual(parseImageModel("aihorde/Flux.1-Schnell fp8 (Compact)"), {
provider: "aihorde",
model: "Flux.1-Schnell fp8 (Compact)",
});
assert.deepEqual(parseImageModel("horde/AlbedoBase XL (SDXL)"), {
provider: "aihorde",
model: "AlbedoBase XL (SDXL)",
});
assert.equal(getImageProvider("aihorde")?.format, "aihorde");
assert.equal(getImageProvider("aihorde")?.alias, "horde");
});