mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 10:52:17 +03:00
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.
101 lines
3.9 KiB
TypeScript
101 lines
3.9 KiB
TypeScript
import { describe, test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { z } from "zod";
|
|
import { isValidationFailure, validateBody, validatedJsonBody } from "@/shared/validation/helpers";
|
|
|
|
function makeRequest(body: string, contentType = "application/json"): Request {
|
|
return new Request("http://localhost/test", {
|
|
method: "POST",
|
|
headers: { "content-type": contentType },
|
|
body,
|
|
});
|
|
}
|
|
|
|
describe("validatedJsonBody", () => {
|
|
const schema = z.object({
|
|
name: z.string().min(1),
|
|
count: z.number().int().nonnegative(),
|
|
});
|
|
|
|
test("returns the parsed and validated data on success", async () => {
|
|
const result = await validatedJsonBody(makeRequest('{"name":"hello","count":3}'), schema);
|
|
assert.equal(result.success, true);
|
|
if (result.success) {
|
|
assert.deepEqual(result.data, { name: "hello", count: 3 });
|
|
}
|
|
});
|
|
|
|
test("returns a 400 with structured details when the body fails Zod validation", async () => {
|
|
const result = await validatedJsonBody(makeRequest('{"name":"","count":-1}'), schema);
|
|
assert.equal(result.success, false);
|
|
if (!result.success) {
|
|
assert.equal(result.response.status, 400);
|
|
const body = await result.response.json();
|
|
assert.equal(body.error.message, "Invalid request");
|
|
assert.ok(Array.isArray(body.error.details));
|
|
const fields = body.error.details.map((d: { field: string }) => d.field);
|
|
assert.ok(fields.includes("name"));
|
|
assert.ok(fields.includes("count"));
|
|
}
|
|
});
|
|
|
|
test("returns a 400 with a body-parse failure for malformed JSON", async () => {
|
|
const result = await validatedJsonBody(makeRequest("not json at all"), schema);
|
|
assert.equal(result.success, false);
|
|
if (!result.success) {
|
|
assert.equal(result.response.status, 400);
|
|
const body = await result.response.json();
|
|
assert.deepEqual(body, {
|
|
error: {
|
|
message: "Invalid request",
|
|
details: [{ field: "body", message: "Invalid JSON body" }],
|
|
},
|
|
});
|
|
}
|
|
});
|
|
|
|
test("returns a 400 for an empty body", async () => {
|
|
const result = await validatedJsonBody(makeRequest(""), schema);
|
|
assert.equal(result.success, false);
|
|
if (!result.success) {
|
|
assert.equal(result.response.status, 400);
|
|
}
|
|
});
|
|
|
|
test("returns a 400 when required fields are missing entirely", async () => {
|
|
const result = await validatedJsonBody(makeRequest("{}"), schema);
|
|
assert.equal(result.success, false);
|
|
if (!result.success) {
|
|
assert.equal(result.response.status, 400);
|
|
const body = await result.response.json();
|
|
const fields = body.error.details.map((d: { field: string }) => d.field);
|
|
assert.ok(fields.includes("name"));
|
|
assert.ok(fields.includes("count"));
|
|
}
|
|
});
|
|
|
|
test("isValidationFailure narrows a validateBody failure", () => {
|
|
const result = validateBody(schema, { name: "", count: -1 });
|
|
assert.equal(isValidationFailure(result), true);
|
|
if (isValidationFailure(result)) {
|
|
assert.equal(result.error.message, "Invalid request");
|
|
assert.equal(result.error.details.length, 2);
|
|
}
|
|
});
|
|
|
|
test("preserves the same envelope shape between parse and validate failure", async () => {
|
|
const parseFailure = await validatedJsonBody(makeRequest("nope"), schema);
|
|
const validateFailure = await validatedJsonBody(makeRequest("{}"), schema);
|
|
assert.equal(parseFailure.success, false);
|
|
assert.equal(validateFailure.success, false);
|
|
if (!parseFailure.success && !validateFailure.success) {
|
|
const parseBody = await parseFailure.response.json();
|
|
const validateBody = await validateFailure.response.json();
|
|
assert.equal(typeof parseBody.error.message, "string");
|
|
assert.equal(typeof validateBody.error.message, "string");
|
|
assert.ok(Array.isArray(parseBody.error.details));
|
|
assert.ok(Array.isArray(validateBody.error.details));
|
|
}
|
|
});
|
|
});
|