Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/api-client-ts/src/generated-core-extra.test.ts
T
2026-07-08 16:15:37 +02:00

166 lines
6.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
separatorArrayExplode,
separatorArrayNoExplode,
separatorObjectExplode,
serializeArrayParam,
serializeObjectParam,
serializePrimitiveParam,
} from "./generated/core/pathSerializer.gen.js";
import { defaultPathSerializer, getUrl, getValidRequestBody } from "./generated/core/utils.gen.js";
import { queryKeyJsonReplacer, serializeQueryKeyValue } from "./generated/core/queryKeySerializer.gen.js";
import { buildClientParams } from "./generated/core/params.gen.js";
describe("pathSerializer separators (RFC6570 style table)", () => {
it("maps explode separators per style", () => {
expect(separatorArrayExplode("matrix")).toBe(";");
expect(separatorArrayExplode("simple")).toBe(",");
expect(separatorArrayExplode("form")).toBe("&");
});
it("maps no-explode separators per style", () => {
expect(separatorArrayNoExplode("form")).toBe(",");
expect(separatorArrayNoExplode("spaceDelimited")).toBe("%20");
// unknown styles fall back to a comma
expect(separatorArrayNoExplode("simple")).toBe(",");
});
it("maps object explode separators per style", () => {
expect(separatorObjectExplode("label")).toBe(".");
expect(separatorObjectExplode("simple")).toBe(",");
expect(separatorObjectExplode("form")).toBe("&");
});
});
describe("serializeArrayParam — non-explode styles", () => {
it("serializes label, matrix, and simple styles", () => {
expect(serializeArrayParam({ explode: false, name: "x", style: "label", value: ["a", "b"] })).toBe(".a,b");
expect(serializeArrayParam({ explode: false, name: "x", style: "matrix", value: ["a", "b"] })).toBe(";x=a,b");
expect(serializeArrayParam({ explode: false, name: "x", style: "simple", value: ["a", "b"] })).toBe("a,b");
});
it("keeps reserved characters when allowReserved is set", () => {
expect(serializeArrayParam({ allowReserved: true, explode: false, name: "x", style: "form", value: ["a/b", "c"] })).toBe("x=a/b,c");
});
});
describe("serializeArrayParam — explode styles", () => {
it("uses the style separator and encodes each member for label/simple", () => {
expect(serializeArrayParam({ explode: true, name: "x", style: "label", value: ["a/b", "c"] })).toBe(".a%2Fb.c");
expect(serializeArrayParam({ explode: true, name: "x", style: "simple", value: ["a/b", "c"] })).toBe("a%2Fb,c");
});
it("emits name=value pairs for matrix and form", () => {
expect(serializeArrayParam({ explode: true, name: "id", style: "matrix", value: ["a", "b"] })).toBe(";id=a;id=b");
expect(serializeArrayParam({ explode: true, name: "id", style: "form", value: ["a", "b"] })).toBe("id=a&id=b");
});
});
describe("serializePrimitiveParam — nullish guards", () => {
it("returns an empty string for null and undefined", () => {
expect(serializePrimitiveParam({ name: "x", value: undefined as never })).toBe("");
expect(serializePrimitiveParam({ name: "x", value: null as never })).toBe("");
});
});
describe("serializeObjectParam — non-explode styles", () => {
it("serializes matrix and the default (simple) styles", () => {
expect(serializeObjectParam({ explode: false, name: "f", style: "matrix", value: { status: "open", owner: "me" } })).toBe(
";f=status,open,owner,me"
);
expect(serializeObjectParam({ explode: false, name: "f", style: "simple" as never, value: { status: "open" } })).toBe("status,open");
});
it("keeps reserved characters in object values when allowReserved is set", () => {
expect(serializeObjectParam({ allowReserved: true, explode: false, name: "f", style: "form", value: { path: "a/b" } })).toBe(
"f=path,a/b"
);
});
});
describe("defaultPathSerializer — uncommon param shapes", () => {
it("leaves a placeholder untouched when the value is missing", () => {
expect(defaultPathSerializer({ path: {}, url: "/v1/{ghost}" })).toBe("/v1/{ghost}");
});
it("serializes a matrix-style primitive path parameter", () => {
expect(defaultPathSerializer({ path: { id: "5" }, url: "/v1/{;id}" })).toBe("/v1/;id=5");
});
});
describe("getUrl — without a base url", () => {
it("prefixes nothing when baseUrl is omitted", () => {
const url = getUrl({
path: { id: "a" },
query: { x: 1 },
querySerializer: (q) => `x=${(q as { x: number }).x}`,
url: "v1/{id}",
});
expect(url).toBe("/v1/a?x=1");
});
});
describe("getValidRequestBody — serializer without serializedBody property", () => {
it("returns the raw body when no serializedBody key is present", () => {
expect(getValidRequestBody({ body: { ok: true }, bodySerializer: JSON.stringify })).toEqual({ ok: true });
});
it("returns null for an empty body even without a serializedBody key", () => {
expect(getValidRequestBody({ body: "", bodySerializer: JSON.stringify })).toBeNull();
});
});
describe("queryKeyJsonReplacer", () => {
it("converts Date and bigint, and drops functions/undefined", () => {
expect(queryKeyJsonReplacer("k", new Date("2026-01-02T03:04:05.000Z"))).toBe("2026-01-02T03:04:05.000Z");
expect(queryKeyJsonReplacer("k", 7n)).toBe("7");
expect(queryKeyJsonReplacer("k", undefined)).toBeUndefined();
expect(queryKeyJsonReplacer("k", () => undefined)).toBeUndefined();
expect(queryKeyJsonReplacer("k", "kept")).toBe("kept");
});
});
describe("serializeQueryKeyValue — primitive and edge inputs", () => {
it("passes through null, numbers, and booleans", () => {
expect(serializeQueryKeyValue(null)).toBeNull();
expect(serializeQueryKeyValue(42)).toBe(42);
expect(serializeQueryKeyValue(true)).toBe(true);
});
it("normalizes bigint and Date scalars", () => {
expect(serializeQueryKeyValue(99n)).toBe("99");
expect(serializeQueryKeyValue(new Date("2026-01-02T00:00:00.000Z"))).toBe("2026-01-02T00:00:00.000Z");
});
it("serializes arrays via JSON round-trip", () => {
expect(serializeQueryKeyValue([1, "a", true])).toEqual([1, "a", true]);
});
it("collapses three repeated search-param keys into one array", () => {
const params = new URLSearchParams([
["a", "1"],
["a", "2"],
["a", "3"],
]);
expect(serializeQueryKeyValue(params)).toEqual({ a: ["1", "2", "3"] });
});
it("returns undefined for non-plain objects like Map", () => {
expect(serializeQueryKeyValue(new Map([["a", 1]]))).toBeUndefined();
});
});
describe("buildClientParams — sparse and aliasing configs", () => {
it("skips arguments that have no matching field config", () => {
expect(buildClientParams(["orphan"], [])).toEqual({});
});
it("tolerates an undefined object argument", () => {
expect(buildClientParams([undefined], [{ args: [{ in: "query", key: "q" }] }])).toEqual({});
});
it("falls back to the field key when no map override is given", () => {
expect(buildClientParams([{ q: "drill" }], [{ args: [{ in: "query", key: "q" }] }])).toEqual({ query: { q: "drill" } });
});
});