Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/exports/src/limits.test.ts
T
2026-07-08 16:15:37 +02:00

85 lines
3.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import {
EXPORT_PLAN_ROW_CAPS,
ExportRowCapExceededError,
assertExportRowCap,
resolveExportRowCap,
} from "./limits.js";
import { PDF_STREAM_HARD_ROW_LIMIT, XLSX_STREAM_HARD_ROW_LIMIT } from "./format-caps.js";
const packageRoot = dirname(dirname(fileURLToPath(import.meta.url)));
describe("@hivegrid/exports/limits public entrypoint", () => {
it("exposes row-cap helpers without importing heavy PDF/XLSX runtimes", async () => {
const exportsModule = await import("./limits.js");
expect(Object.keys(exportsModule).sort()).toEqual([
"EXPORT_PLAN_ROW_CAPS",
"ExportRowCapExceededError",
"assertExportRowCap",
"resolveExportRowCap",
]);
});
it("keeps limits wired to lightweight cap constants", async () => {
const source = await readFile(join(packageRoot, "src/limits.ts"), "utf8");
expect(source).toContain("./format-caps.js");
expect(source).not.toMatch(/["']\.\/(?:pdf|xlsx)\/index\.js["']/);
});
});
describe("resolveExportRowCap", () => {
it("applies the plan cap to unbounded formats (csv, json)", () => {
expect(resolveExportRowCap("free", "csv")).toBe(EXPORT_PLAN_ROW_CAPS.free);
expect(resolveExportRowCap("pro", "json")).toBe(EXPORT_PLAN_ROW_CAPS.pro);
expect(resolveExportRowCap("enterprise", "csv")).toBe(Number.POSITIVE_INFINITY);
});
it("respects the per-format hard cap when stricter than the plan cap", () => {
expect(resolveExportRowCap("enterprise", "xlsx")).toBe(XLSX_STREAM_HARD_ROW_LIMIT);
expect(resolveExportRowCap("enterprise", "pdf")).toBe(PDF_STREAM_HARD_ROW_LIMIT);
// free plan cap (10k) is lower than pdf hard cap (5k), so pdf cap wins.
expect(resolveExportRowCap("free", "pdf")).toBe(PDF_STREAM_HARD_ROW_LIMIT);
});
it("falls back to the free-plan cap for unknown plan tiers", () => {
expect(resolveExportRowCap("vip" as never, "csv")).toBe(EXPORT_PLAN_ROW_CAPS.free);
});
});
describe("assertExportRowCap", () => {
it("returns silently when the requested count fits the cap", () => {
expect(() => assertExportRowCap(5_000, "free", "csv")).not.toThrow();
expect(() => assertExportRowCap(50_000, "pro", "json")).not.toThrow();
});
it("throws ExportRowCapExceededError with the resolved cap", () => {
let caught: ExportRowCapExceededError | undefined;
try {
assertExportRowCap(20_000, "free", "csv");
} catch (error) {
caught = error as ExportRowCapExceededError;
}
expect(caught).toBeInstanceOf(ExportRowCapExceededError);
expect(caught?.code).toBe("EXPORT_ROW_LIMIT_PLAN");
expect(caught?.effectiveCap).toBe(EXPORT_PLAN_ROW_CAPS.free);
expect(caught?.requestedRowCount).toBe(20_000);
expect(caught?.plan).toBe("free");
expect(caught?.format).toBe("csv");
});
it("enforces format hard caps even for enterprise plans", () => {
expect(() => assertExportRowCap(PDF_STREAM_HARD_ROW_LIMIT + 1, "enterprise", "pdf")).toThrow(
ExportRowCapExceededError,
);
expect(() => assertExportRowCap(XLSX_STREAM_HARD_ROW_LIMIT + 1, "enterprise", "xlsx")).toThrow(
ExportRowCapExceededError,
);
});
});