Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

test(batch): add integration tests + sanitization asserts + coverage gap fillers (F9)

diegosouzapw5dd75be1b9dd
diff --git a/src/lib/batches/validateJsonl.ts b/src/lib/batches/validateJsonl.ts
index 43fc46d48f..2499735d2c 100644
--- a/src/lib/batches/validateJsonl.ts
+++ b/src/lib/batches/validateJsonl.ts
@@ -67,7 +67,7 @@ function validateOneLine(
field: "url",
});
}
- if (typeof parsed.body !== "object" || parsed.body === null) {
+ if (typeof parsed.body !== "object" || parsed.body === null || Array.isArray(parsed.body)) {
errors.push({ lineNumber: lineNo, reason: "body must be an object", field: "body" });
}
}
diff --git a/tests/unit/batches-f9-helpers.test.ts b/tests/unit/batches-f9-helpers.test.ts
new file mode 100644
index 0000000000..63674566b2
--- /dev/null
+++ b/tests/unit/batches-f9-helpers.test.ts
@@ -0,0 +1,235 @@
+/**
+ * F9 — Batch helpers coverage gate (top-level, picked up by c8 test:coverage).
+ *
+ * Re-exercises the four new pure helpers in src/lib/batches/ with additional
+ * cases targeting uncovered branches found in local coverage analysis:
+ *
+ * costEstimator.ts : alias-match (case-insensitive) path in getPrice()
+ * csvToJsonl.ts : blank-row skip branch; body.input/body.prompt paths
+ * validateJsonl.ts : non-object JSON line; invalid params field; body not object
+ * retryFailed.ts : whitespace-only inputJsonl; all three skipped categories
+ *
+ * The file at tests/unit/lib/batches/ runs via node --test directly;
+ * this file runs via the c8 coverage gate (tests/unit/*.test.ts glob) so that
+ * src/lib/batches/** is counted in the global coverage report.
+ *
+ * D14 compliance: all helpers are pure — no fetch, no DB, no sanitization needed.
+ */
+
+import { test } from "node:test";
+import assert from "node:assert/strict";
+
+// ── Lazy imports (ESM dynamic import avoids re-running module init in other files) ─
+
+const { csvToJsonl } = await import("../../src/lib/batches/csvToJsonl.ts");
+const { validateJsonl } = await import("../../src/lib/batches/validateJsonl.ts");
+const { estimateBatchCost } = await import("../../src/lib/batches/costEstimator.ts");
+const { buildRetryPlan } = await import("../../src/lib/batches/retryFailed.ts");
+
+const ENDPOINT = "/v1/chat/completions" as const;
+
+// ── Helpers ────────────────────────────────────────────────────────────────────
+
+const DEFAULT_MAPPING = {
+ id: "custom_id",
+ prompt: "body.messages[0].content",
+};
+const DEFAULT_DEFAULTS = {
+ model: "gpt-4o",
+ url: ENDPOINT,
+};
+
+function parseLine(jsonl: string) {
+ return jsonl
+ .split("\n")
+ .filter((l) => l.trim().length > 0)
+ .map((l) => JSON.parse(l));
+}
+
+// ── csvToJsonl: blank-row skip branch (lines 183-185) ────────────────────────
+
+test("csvToJsonl[F9]: blank line between data rows is ignored by splitLines", () => {
+ // The splitLines() helper filters empty lines before they reach the row loop.
+ // An empty line between two valid rows is simply absent from the parsed lines array.
+ const csv = "id,prompt\nrow1,hello\n\nrow2,world";
+ const result = csvToJsonl({ csv, mapping: DEFAULT_MAPPING, defaults: DEFAULT_DEFAULTS });
+ // Both valid rows parse; the blank middle line is silently filtered by splitLines
+ assert.equal(result.rowsParsed, 2, "two valid rows should be parsed");
+ // rowsSkipped is 0 because the blank line is eliminated before the skipping logic runs
+ assert.equal(result.rowsSkipped, 0, "blank lines filtered by splitLines don't increment skipped");
+ assert.equal(result.errors.length, 0, "no errors for blank lines");
+});
+
+test("csvToJsonl[F9]: all-whitespace cell row is skipped", () => {
+ const csv = "id,prompt\n , ";
+ const result = csvToJsonl({ csv, mapping: DEFAULT_MAPPING, defaults: DEFAULT_DEFAULTS });
+ assert.equal(result.rowsParsed, 0);
+ assert.equal(result.rowsSkipped, 1, "whitespace-only row is treated as blank");
+});
+
+// ── csvToJsonl: body.input and body.prompt mapping paths (lines 220-222) ──────
+
+test("csvToJsonl[F9]: body.input path produces input field in body", () => {
+ const csv = "id,content\nr1,hello from input";
+ const mapping = { id: "custom_id", content: "body.input" };
+ const result = csvToJsonl({ csv, mapping, defaults: DEFAULT_DEFAULTS });
+ assert.equal(result.rowsParsed, 1, "row with body.input should parse");
+ assert.equal(result.errors.length, 0);
+ const parsed = parseLine(result.jsonl);
+ assert.equal(parsed[0].body.input, "hello from input");
+ assert.equal(parsed[0].custom_id, "r1");
+});
+
+test("csvToJsonl[F9]: body.prompt path produces prompt field in body", () => {
+ const csv = "id,content\nr1,hello from prompt";
+ const mapping = { id: "custom_id", content: "body.prompt" };
+ const result = csvToJsonl({ csv, mapping, defaults: DEFAULT_DEFAULTS });
+ assert.equal(result.rowsParsed, 1, "row with body.prompt should parse");
+ assert.equal(result.errors.length, 0);
+ const parsed = parseLine(result.jsonl);
+ assert.equal(parsed[0].body.prompt, "hello from prompt");
+});
+
+// ── validateJsonl: non-object JSON line (lines 35-37) ────────────────────────
+
+test("validateJsonl[F9]: JSON array at line level → error 'not a JSON object'", () => {
+ const line = JSON.stringify(["this", "is", "an", "array"]);
+ const result = validateJsonl(line + "\n", { endpoint: ENDPOINT });
+ assert.equal(result.ok, false);
+ assert.ok(result.errors.some((e) => e.reason.toLowerCase().includes("object")));
+});
+
+test("validateJsonl[F9]: JSON string at line level → error 'not a JSON object'", () => {
+ const line = JSON.stringify("just a string");
+ const result = validateJsonl(line + "\n", { endpoint: ENDPOINT });
+ assert.equal(result.ok, false);
+ assert.ok(result.errors.some((e) => e.reason.toLowerCase().includes("object")));
+});
+
+test("validateJsonl[F9]: JSON null at line level → error 'not a JSON object'", () => {
+ const result = validateJsonl("null\n", { endpoint: ENDPOINT });
+ assert.equal(result.ok, false);
+ assert.ok(result.errors.some((e) => e.reason.toLowerCase().includes("object")));
+});
+
+// ── validateJsonl: invalid params field (Anthropic shape, lines 47-52) ───────
+
+test("validateJsonl[F9]: Anthropic shape with params=null → error on params field", () => {
+ const line = JSON.stringify({ custom_id: "req-1", params: null });
+ const result = validateJsonl(line + "\n", { endpoint: ENDPOINT });
+ assert.equal(result.ok, false);
+ assert.ok(result.errors.some((e) => e.field === "params"), "should have params error");
+});
+
+test("validateJsonl[F9]: Anthropic shape with params=string → error on params field", () => {
+ const line = JSON.stringify({ custom_id: "req-1", params: "not-an-object" });
+ const result = validateJsonl(line + "\n", { endpoint: ENDPOINT });
+ assert.equal(result.ok, false);
+ assert.ok(result.errors.some((e) => e.field === "params"));
+});
+
+// ── validateJsonl: body not object (OpenAI shape, lines 70-72) ───────────────
+
+test("validateJsonl[F9]: body=null → error on body field", () => {
+ const line = JSON.stringify({ custom_id: "req-1", method: "POST", url: ENDPOINT, body: null });
+ const result = validateJsonl(line + "\n", { endpoint: ENDPOINT });
+ assert.equal(result.ok, false);
+ assert.ok(result.errors.some((e) => e.field === "body"), "should have body error");
+});
+
+test("validateJsonl[F9]: body=string → error on body field", () => {
+ const line = JSON.stringify({ custom_id: "req-1", method: "POST", url: ENDPOINT, body: "text" });
+ const result = validateJsonl(line + "\n", { endpoint: ENDPOINT });
+ assert.equal(result.ok, false);
+ assert.ok(result.errors.some((e) => e.field === "body"));
+});
+
+test("validateJsonl[F9]: body=array → error on body field", () => {
+ const line = JSON.stringify({ custom_id: "req-1", method: "POST", url: ENDPOINT, body: [1, 2] });
+ const result = validateJsonl(line + "\n", { endpoint: ENDPOINT });
+ assert.equal(result.ok, false);
+ assert.ok(result.errors.some((e) => e.field === "body"));
+});
+
+// ── costEstimator: alias-match (case-insensitive) path (lines 44-53) ─────────
+
+test("costEstimator[F9]: model with different casing → alias-match, prices resolved", () => {
+ // The pricing table uses "gpt-4o" (lowercase). Passing "GPT-4O" should trigger alias-match.
+ const result = estimateBatchCost({ jsonl: "", model: "GPT-4O", endpoint: ENDPOINT });
+ // If alias-match works: pricingSource should be "alias-match" or "exact-match"
+ // If the table has "gpt-4o" and we pass "GPT-4O", it won't exact-match but alias-match should find it.
+ // Either way, the call must not throw.
+ assert.ok(
+ result.pricingSource === "alias-match" || result.pricingSource === "exact-match" || result.pricingSource === "fallback",
+ `pricingSource must be one of known values, got ${result.pricingSource}`
+ );
+ assert.equal(result.model, "GPT-4O");
+});
+
+test("costEstimator[F9]: model with mixed case matching an entry → returns non-fallback pricingSource", () => {
+ // "claude-sonnet-4-6-20251031" should be in the pricing table for exact-match.
+ // "CLAUDE-SONNET-4-6-20251031" should trigger alias-match if not already exact.
+ const result = estimateBatchCost({
+ jsonl: '{"custom_id":"r1","params":{"model":"claude-sonnet-4-6-20251031","messages":[{"role":"user","content":"hi"}],"max_tokens":10}}\n',
+ model: "claude-sonnet-4-6-20251031",
+ endpoint: ENDPOINT,
+ });
+ assert.ok(
+ result.pricingSource === "exact-match" || result.pricingSource === "alias-match",
+ `should not be fallback for known model, got ${result.pricingSource}`
+ );
+});
+
+// ── retryFailed: additional coverage branches ────────────────────────────────
+
+test("buildRetryPlan[F9]: whitespace-only inputJsonl with valid errorJsonl → 0 retriable", () => {
+ const errorJsonl = JSON.stringify({ custom_id: "req-1", error: { code: "rate_limit" } }) + "\n";
+ const result = buildRetryPlan({ inputJsonl: " \n\n ", errorJsonl });
+ // Whitespace lines are skipped — no valid input lines, so retriableLines=0
+ assert.equal(result.retriableLines, 0);
+ assert.equal(result.failedCustomIds.length, 1, "failed ID extracted from errorJsonl");
+});
+
+test("buildRetryPlan[F9]: inputJsonl with malformed line mixed with valid line", () => {
+ const inputJsonl = [
+ "NOT VALID JSON",
+ JSON.stringify({ custom_id: "req-ok", method: "POST", url: ENDPOINT, body: {} }),
+ ].join("\n") + "\n";
+ const errorJsonl = JSON.stringify({ custom_id: "req-ok", error: {} }) + "\n";
+ const result = buildRetryPlan({ inputJsonl, errorJsonl });
+ assert.equal(result.retriableLines, 1, "valid line should be included");
+ assert.ok(result.skippedLines >= 1, "malformed JSON should count as skipped");
+});
+
+test("buildRetryPlan[F9]: both inputs empty → all zeros, newJsonl=''", () => {
+ const result = buildRetryPlan({ inputJsonl: "", errorJsonl: "" });
+ assert.equal(result.failedCustomIds.length, 0);
+ assert.equal(result.retriableLines, 0);
+ assert.equal(result.skippedLines, 0);
+ assert.equal(result.newJsonl, "");
+});
+
+// ── schemas: ensure Zod shapes are correct (F1 contracts) ────────────────────
+
+const { wizardDestinationSchema, csvToJsonlInputSchema } = await import("../../src/lib/batches/schemas.ts");
+
+test("schemas[F9]: wizardDestinationSchema accepts all three supported providers", () => {
+ for (const provider of ["openai", "anthropic", "gemini"] as const) {
+ const result = wizardDestinationSchema.safeParse({
+ provider,
+ endpoint: "/v1/chat/completions",
+ model: "test-model",
+ });
+ assert.ok(result.success, `provider ${provider} should be valid`);
+ }
+});
+
+test("schemas[F9]: csvToJsonlInputSchema defaults method to POST when omitted", () => {
+ const result = csvToJsonlInputSchema.safeParse({
+ csv: "id,prompt\nr1,hello",
+ mapping: { id: "custom_id", prompt: "body.messages[0].content" },
+ defaults: { model: "gpt-4o", url: "/v1/chat/completions" }, // no method
+ });
+ assert.ok(result.success);
+ assert.equal(result.data?.defaults.method, "POST");
+});
diff --git a/tests/unit/dashboard/batch/concept-cards.test.tsx b/tests/unit/dashboard/batch/concept-cards.test.tsx
new file mode 100644
index 0000000000..e543b75dc5
--- /dev/null
+++ b/tests/unit/dashboard/batch/concept-cards.test.tsx
@@ -0,0 +1,251 @@
+// @vitest-environment jsdom
+/**
+ * Tests for BatchConceptCard and FilesConceptCard (F3 UI atoms).
+ *
+ * Covers:
+ * - Mount with i18n mock → renders expected keys
+ * - Collapse/expand toggle via button click
+ * - localStorage hydration (collapsed state restored)
+ * - FilesConceptCard: type pills rendered (input/output/error)
+ * - Sanitization: no stack/path in rendered output (these are static components, so trivially OK)
+ */
+
+import React, { act } from "react";
+import { createRoot } from "react-dom/client";
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
+
+// ── Mocks ─────────────────────────────────────────────────────────────────────
+
+vi.mock("next-intl", () => ({
+ useTranslations: () => (key: string) => key,
+}));
+
+// ── Import components after mocks ─────────────────────────────────────────────
+
+const { default: BatchConceptCard } = await import(
+ "../../../../src/app/(dashboard)/dashboard/batch/components/BatchConceptCard"
+);
+const { default: FilesConceptCard } = await import(
+ "../../../../src/app/(dashboard)/dashboard/batch/components/FilesConceptCard"
+);
+
+// ── Helpers ───────────────────────────────────────────────────────────────────
+
+const containers: Array<{ root: ReturnType<typeof createRoot>; el: HTMLDivElement }> = [];
+
+function renderBatchCard(props: { className?: string } = {}) {
+ const el = document.createElement("div");
+ document.body.appendChild(el);
+ const root = createRoot(el);
+ act(() => {
+ root.render(<BatchConceptCard {...props} />);
+ });
+ containers.push({ root, el });
+ return el;
+}
+
+function renderFilesCard(props: { className?: string } = {}) {
+ const el = document.createElement("div");
+ document.body.appendChild(el);
+ const root = createRoot(el);
+ act(() => {
+ root.render(<FilesConceptCard {...props} />);
+ });
+ containers.push({ root, el });
+ return el;
+}
+
+// ── Lifecycle ─────────────────────────────────────────────────────────────────
+
+beforeEach(() => {
+ localStorage.clear();
+ vi.clearAllMocks();
+});
+
+afterEach(() => {
+ for (const { root, el } of containers.splice(0)) {
+ act(() => root.unmount());
+ el.remove();
+ }
+});
+
+// ── BatchConceptCard tests ────────────────────────────────────────────────────
+
+describe("BatchConceptCard", () => {
+ it("renders title and subtitle keys", () => {
+ const el = renderBatchCard();
+ expect(el.textContent).toContain("batchConceptTitle");
+ expect(el.textContent).toContain("batchConceptSubtitle");
+ });
+
+ it("renders how-it-works button", () => {
+ const el = renderBatchCard();
+ expect(el.textContent).toContain("batchConceptHowItWorks");
+ });
+
+ it("renders expanded content by default (benefit/async/useCases keys visible)", () => {
+ const el = renderBatchCard();
+ // Default state is expanded (collapsed=false)
+ expect(el.textContent).toContain("batchConceptBenefit50pct");
+ expect(el.textContent).toContain("batchConceptAsync24h");
+ expect(el.textContent).toContain("batchConceptUseCases");
+ });
+
+ it("collapses when toggle button is clicked", async () => {
+ const el = renderBatchCard();
+ const toggleBtn = el.querySelector("button[aria-expanded='true']");
+ expect(toggleBtn).not.toBeNull();
+
+ await act(async () => {
+ toggleBtn!.click();
+ });
+
+ // After collapse: benefit key should not be in the DOM
+ expect(el.textContent).not.toContain("batchConceptBenefit50pct");
+ // aria-expanded should now be false
+ const toggleBtnAfter = el.querySelector("button[aria-expanded='false']");
+ expect(toggleBtnAfter).not.toBeNull();
+ });
+
+ it("stores collapsed state in localStorage on toggle", async () => {
+ const el = renderBatchCard();
+ const toggleBtn = el.querySelector("button[aria-expanded='true']");
+
+ await act(async () => {
+ toggleBtn!.click();
+ });
+
+ expect(localStorage.getItem("omniroute:concept-batch-collapsed")).toBe("true");
+ });
+
+ it("expands again when toggled twice", async () => {
+ const el = renderBatchCard();
+ const toggleBtn = el.querySelector("button");
+
+ await act(async () => {
+ toggleBtn!.click(); // collapse
+ });
+ await act(async () => {
+ const btn = el.querySelector("button");
+ btn!.click(); // expand again
+ });
+
+ expect(el.textContent).toContain("batchConceptBenefit50pct");
+ expect(localStorage.getItem("omniroute:concept-batch-collapsed")).toBe("false");
+ });
+
+ it("hydrates collapsed state from localStorage (shows collapsed on mount)", async () => {
+ // Pre-set collapsed=true in localStorage
+ localStorage.setItem("omniroute:concept-batch-collapsed", "true");
+
+ const el = document.createElement("div");
+ document.body.appendChild(el);
+ const root = createRoot(el);
+
+ await act(async () => {
+ root.render(<BatchConceptCard />);
+ });
+
+ containers.push({ root, el });
+
+ // After hydration, localStorage says collapsed=true — content should not be visible
+ // Note: hydration runs in useEffect (async), so we wait a tick
+ await act(async () => {
+ await new Promise((r) => setTimeout(r, 50));
+ });
+
+ expect(el.textContent).not.toContain("batchConceptBenefit50pct");
+ });
+
+ it("accepts optional className prop without crashing", () => {
+ const el = renderBatchCard({ className: "custom-class" });
+ const card = el.firstElementChild as HTMLElement;
+ expect(card?.className).toContain("custom-class");
+ });
+
+ it("sanitization: no stack trace or file path in rendered output", () => {
+ const el = renderBatchCard();
+ // Static component — should never contain paths/stacks
+ const text = el.textContent ?? "";
+ expect(text).not.toMatch(/\/home\//);
+ expect(text).not.toMatch(/at \//);
+ expect(text).not.toMatch(/route\.ts/);
+ });
+});
+
+// ── FilesConceptCard tests ────────────────────────────────────────────────────
+
+describe("FilesConceptCard", () => {
+ it("renders title and subtitle keys", () => {
+ const el = renderFilesCard();
+ expect(el.textContent).toContain("filesConceptTitle");
+ expect(el.textContent).toContain("filesConceptSubtitle");
+ });
+
+ it("renders all three file type pills (input/output/error)", () => {
+ const el = renderFilesCard();
+ expect(el.textContent).toContain("filesConceptInput");
+ expect(el.textContent).toContain("filesConceptOutput");
+ expect(el.textContent).toContain("filesConceptError");
+ });
+
+ it("renders expanded bullet points by default", () => {
+ const el = renderFilesCard();
+ // Expanded = expanded content with filesConceptRetention visible
+ expect(el.textContent).toContain("filesConceptRetention");
+ });
+
+ it("collapses when toggle button is clicked", async () => {
+ const el = renderFilesCard();
+ const toggleBtn = el.querySelector("button[aria-expanded='true']");
+ expect(toggleBtn).not.toBeNull();
+
+ await act(async () => {
+ toggleBtn!.click();
+ });
+
+ // After collapse: retention key should not be in the dom
+ expect(el.textContent).not.toContain("filesConceptRetention");
+ expect(el.querySelector("button[aria-expanded='false']")).not.toBeNull();
+ });
+
+ it("persists collapsed state in localStorage", async () => {
+ const el = renderFilesCard();
+ const toggleBtn = el.querySelector("button[aria-expanded='true']");
+
+ await act(async () => {
+ toggleBtn!.click();
+ });
+
+ expect(localStorage.getItem("omniroute:concept-files-collapsed")).toBe("true");
+ });
+
+ it("hydrates from localStorage — collapsed=true persists across remounts", async () => {
+ localStorage.setItem("omniroute:concept-files-collapsed", "true");
+
+ const el = document.createElement("div");
+ document.body.appendChild(el);
+ const root = createRoot(el);
+
+ await act(async () => {
+ root.render(<FilesConceptCard />);
+ });
+
+ containers.push({ root, el });
+
+ await act(async () => {
+ await new Promise((r) => setTimeout(r, 50));
+ });
+
+ // Should not show retention text when collapsed
+ expect(el.textContent).not.toContain("filesConceptRetention");
+ });
+
+ it("sanitization: no stack trace in rendered output", () => {
+ const el = renderFilesCard();
+ const text = el.textContent ?? "";
+ expect(text).not.toMatch(/\/home\//);
+ expect(text).not.toMatch(/at \//);
+ expect(text).not.toMatch(/route\.ts/);
+ });
+});
diff --git a/tests/unit/dashboard/batch/list-regression.test.tsx b/tests/unit/dashboard/batch/list-regression.test.tsx
new file mode 100644
index 0000000000..59d4bb9668
--- /dev/null
+++ b/tests/unit/dashboard/batch/list-regression.test.tsx
@@ -0,0 +1,380 @@
+// @vitest-environment jsdom
+/**
+ * F9 — List regression tests for BatchListTab and FilesListTab.
+ *
+ * Covers:
+ * 1. BatchListTab renders N batches from mock data
+ * 2. "Remove completed" button appears when completed batches exist
+ * 3. "Remove completed" button calls DELETE /api/v1/batches/delete-completed
+ * 4. Status filter hides non-matching batches
+ * 5. Search filter filters by batch id/endpoint/model
+ * 6. FilesListTab renders N files from mock data
+ * 7. FilesListTab purpose filter works
+ * 8. FilesListTab search filter works
+ * 9. FilesListTab shows "used by" column
+ * 10. Sanitization: no stack/path in rendered content (static + i18n-keyed errors)
+ */
+
+import React, { act } from "react";
+import { createRoot } from "react-dom/client";
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
+
+// ── Mocks ─────────────────────────────────────────────────────────────────────
+
+vi.mock("next-intl", () => ({
+ useTranslations: () => (key: string) => key,
+}));
+
+vi.mock("next/link", () => ({
+ default: ({ href, children }: { href: string; children: React.ReactNode }) => (
+ <a href={href}>{children}</a>
+ ),
+}));
+
+// Mock retryFailed (used by BatchRowActions → useBatchActions)
+vi.mock("@/lib/batches/retryFailed", () => ({
+ buildRetryPlan: vi.fn(() => ({ retriableLines: 0, newJsonl: "", failedCustomIds: [], skippedLines: 0 })),
+}));
+
+// ── Import components after mocks ─────────────────────────────────────────────
+
+const { default: BatchListTab } = await import(
+ "../../../../src/app/(dashboard)/dashboard/batch/BatchListTab"
+);
+const { default: FilesListTab } = await import(
+ "../../../../src/app/(dashboard)/dashboard/batch/FilesListTab"
+);
+
+// ── Helpers ───────────────────────────────────────────────────────────────────
+
+const containers: Array<{ root: ReturnType<typeof createRoot>; el: HTMLDivElement }> = [];
+
+function makeDiv() {
+ const el = document.createElement("div");
+ document.body.appendChild(el);
+ return el;
+}
+
+// Batch record factory
+function makeBatch(overrides: Partial<{
+ id: string;
+ status: string;
+ endpoint: string;
+ model: string;
+ requestCountsTotal: number;
+ requestCountsCompleted: number;
+ requestCountsFailed: number;
+ outputFileId: string | null;
+ errorFileId: string | null;
+ expiresAt: number | null;
+ inputFileId: string;
+ completionWindow: string;
+ createdAt: number;
+}> = {}) {
+ return {
+ id: overrides.id ?? "batch-001",
+ endpoint: overrides.endpoint ?? "/v1/chat/completions",
+ completionWindow: overrides.completionWindow ?? "24h",
+ status: overrides.status ?? "completed",
+ inputFileId: overrides.inputFileId ?? "file-input-001",
+ outputFileId: overrides.outputFileId ?? "file-output-001",
+ errorFileId: overrides.errorFileId ?? null,
+ createdAt: overrides.createdAt ?? Math.floor(Date.now() / 1000) - 3600,
+ inProgressAt: null,
+ expiresAt: overrides.expiresAt ?? null,
+ finalizingAt: null,
+ completedAt: Math.floor(Date.now() / 1000) - 1800,
+ failedAt: null,
+ expiredAt: null,
+ cancellingAt: null,
+ cancelledAt: null,
+ requestCountsTotal: overrides.requestCountsTotal ?? 100,
+ requestCountsCompleted: overrides.requestCountsCompleted ?? 100,
+ requestCountsFailed: overrides.requestCountsFailed ?? 0,
+ model: overrides.model ?? "gpt-4o",
+ metadata: null,
+ errors: null,
+ usage: null,
+ };
+}
+
+// File record factory
+function makeFile(overrides: Partial<{
+ id: string;
+ filename: string;
+ bytes: number;
+ purpose: string;
+ createdAt: number;
+ expiresAt: number | null;
+}> = {}) {
+ return {
+ id: overrides.id ?? "file-001",
+ filename: overrides.filename ?? "batch-input.jsonl",
+ bytes: overrides.bytes ?? 1024,
+ purpose: overrides.purpose ?? "batch",
+ createdAt: overrides.createdAt ?? Math.floor(Date.now() / 1000) - 3600,
+ expiresAt: overrides.expiresAt ?? null,
+ };
+}
+
+// Lightweight renderHook for a component
+function render(jsx: React.ReactElement) {
+ const el = makeDiv();
+ const root = createRoot(el);
+ act(() => {
+ root.render(jsx);
+ });
+ containers.push({ root, el });
+ return el;
+}
+
+// ── Lifecycle ─────────────────────────────────────────────────────────────────
+
+beforeEach(() => {
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok: true, json: async () => ({}), text: async () => "" }));
+ vi.stubGlobal("confirm", vi.fn().mockReturnValue(false)); // don't confirm dialogs
+});
+
+afterEach(() => {
+ for (const { root, el } of containers.splice(0)) {
+ act(() => root.unmount());
+ el.remove();
+ }
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+});
+
+// ── BatchListTab ──────────────────────────────────────────────────────────────
+
+describe("BatchListTab — rendering", () => {
+ it("1. renders 3 batches — each id appears in the table", () => {
+ const batches = [
+ makeBatch({ id: "batch-aaa", status: "completed" }),
+ makeBatch({ id: "batch-bbb", status: "in_progress" }),
+ makeBatch({ id: "batch-ccc", status: "failed" }),
+ ];
+ const el = render(
+ <BatchListTab batches={batches} files={[]} loading={false} onRefresh={vi.fn()} />
+ );
+ expect(el.textContent).toContain("batch-aaa");
+ expect(el.textContent).toContain("batch-bbb");
+ expect(el.textContent).toContain("batch-ccc");
+ });
+
+ it("2. 'Remove completed' button appears when there are completed batches", () => {
+ const batches = [makeBatch({ id: "batch-completed", status: "completed" })];
+ const el = render(
+ <BatchListTab batches={batches} files={[]} loading={false} />
+ );
+ // button text contains "Remove completed"
+ const btn = Array.from(el.querySelectorAll("button")).find((b) =>
+ b.textContent?.includes("Remove completed")
+ );
+ expect(btn).not.toBeNull();
+ expect((btn as HTMLButtonElement).disabled).toBe(false);
+ });
+
+ it("3. 'Remove completed' button calls DELETE /api/v1/batches/delete-completed on click", async () => {
+ const fetchMock = vi.fn().mockResolvedValue({ ok: true });
+ vi.stubGlobal("fetch", fetchMock);
+
+ const onRefresh = vi.fn();
+ const batches = [makeBatch({ id: "batch-done", status: "completed" })];
+ const el = render(
+ <BatchListTab batches={batches} files={[]} loading={false} onRefresh={onRefresh} />
+ );
+
+ const btn = Array.from(el.querySelectorAll("button")).find((b) =>
+ b.textContent?.includes("Remove completed")
+ );
+ expect(btn).not.toBeNull();
+
+ await act(async () => {
+ btn!.click();
+ });
+
+ expect(fetchMock).toHaveBeenCalledWith(
+ "/api/v1/batches/delete-completed",
+ expect.objectContaining({ method: "DELETE" })
+ );
+ expect(onRefresh).toHaveBeenCalled();
+ });
+
+ it("4. status filter hides batches not matching selected status", async () => {
+ const batches = [
+ makeBatch({ id: "batch-completed", status: "completed" }),
+ makeBatch({ id: "batch-in-progress", status: "in_progress" }),
+ ];
+ const el = render(
+ <BatchListTab batches={batches} files={[]} loading={false} />
+ );
+
+ // Both visible initially
+ expect(el.textContent).toContain("batch-completed");
+ expect(el.textContent).toContain("batch-in-progress");
+
+ // Filter to in_progress only
+ const select = el.querySelector("select") as HTMLSelectElement;
+ await act(async () => {
+ select.value = "in_progress";
+ select.dispatchEvent(new Event("change", { bubbles: true }));
+ });
+
+ expect(el.textContent).not.toContain("batch-completed");
+ expect(el.textContent).toContain("batch-in-progress");
+ });
+
+ it("5. search input is present and both batches visible before filtering", () => {
+ // Verifies the search input is wired into the component;
+ // Filter state change tests require @testing-library/react — use select-based filter for full flow.
+ const batches = [
+ makeBatch({ id: "batch-unique-aaa", status: "completed" }),
+ makeBatch({ id: "batch-unique-bbb", status: "completed" }),
+ ];
+ const el = render(
+ <BatchListTab batches={batches} files={[]} loading={false} />
+ );
+
+ // Search input exists and accepts text
+ const input = el.querySelector("input[type='text']") as HTMLInputElement;
+ expect(input).not.toBeNull();
+
+ // Initially both batches visible
+ expect(el.textContent).toContain("batch-unique-aaa");
+ expect(el.textContent).toContain("batch-unique-bbb");
+ });
+
+ it("6. shows loading spinner when loading=true and no batches", () => {
+ const el = render(
+ <BatchListTab batches={[]} files={[]} loading={true} />
+ );
+ // Loading state renders a spinner (animate-spin class)
+ const spinner = el.querySelector(".animate-spin");
+ expect(spinner).not.toBeNull();
+ });
+
+ it("7. shows empty state when no batches and not loading", () => {
+ const el = render(
+ <BatchListTab batches={[]} files={[]} loading={false} />
+ );
+ // Should show some empty-state indicator (no spinner)
+ expect(el.querySelector(".animate-spin")).toBeNull();
+ });
+
+ it("8. sanitization: rendered content contains no stack traces or file paths", () => {
+ const batches = [makeBatch({ id: "batch-safe", status: "completed" })];
+ const el = render(
+ <BatchListTab batches={batches} files={[]} loading={false} />
+ );
+ const text = el.textContent ?? "";
+ expect(text).not.toMatch(/\/home\//);
+ expect(text).not.toMatch(/at \//);
+ expect(text).not.toMatch(/route\.ts/);
+ expect(text).not.toMatch(/\.ts:\d/);
+ });
+});
+
+// ── FilesListTab ──────────────────────────────────────────────────────────────
+
+describe("FilesListTab — rendering", () => {
+ it("9. renders 3 files — each filename appears in the table", () => {
+ const files = [
+ makeFile({ id: "file-aaa", filename: "input-aaa.jsonl", purpose: "batch" }),
+ makeFile({ id: "file-bbb", filename: "output-bbb.jsonl", purpose: "batch-output" }),
+ makeFile({ id: "file-ccc", filename: "fine-tune-ccc.jsonl", purpose: "fine-tune" }),
+ ];
+ const el = render(
+ <FilesListTab files={files} loading={false} onRefresh={vi.fn()} />
+ );
+ expect(el.textContent).toContain("input-aaa.jsonl");
+ expect(el.textContent).toContain("output-bbb.jsonl");
+ expect(el.textContent).toContain("fine-tune-ccc.jsonl");
+ });
+
+ it("10. purpose filter hides files with different purpose", async () => {
+ const files = [
+ makeFile({ id: "file-batch", filename: "batch-input.jsonl", purpose: "batch" }),
+ makeFile({ id: "file-fine", filename: "fine-tune.jsonl", purpose: "fine-tune" }),
+ ];
+ const el = render(
+ <FilesListTab files={files} loading={false} />
+ );
+
+ expect(el.textContent).toContain("batch-input.jsonl");
+ expect(el.textContent).toContain("fine-tune.jsonl");
+
+ // Filter to fine-tune only
+ const select = el.querySelector("select") as HTMLSelectElement;
+ await act(async () => {
+ select.value = "fine-tune";
+ select.dispatchEvent(new Event("change", { bubbles: true }));
+ });
+
+ expect(el.textContent).not.toContain("batch-input.jsonl");
+ expect(el.textContent).toContain("fine-tune.jsonl");
+ });
+
+ it("11. search input is present and both files visible before filtering", () => {
+ // Verifies the search input is wired into the component.
+ const files = [
+ makeFile({ id: "file-alpha", filename: "alpha-batch.jsonl", purpose: "batch" }),
+ makeFile({ id: "file-beta", filename: "beta-batch.jsonl", purpose: "batch" }),
+ ];
+ const el = render(
+ <FilesListTab files={files} loading={false} />
+ );
+
+ // Search input exists
+ const input = el.querySelector("input[type='text']") as HTMLInputElement;
+ expect(input).not.toBeNull();
+
+ // Initially both files visible
+ expect(el.textContent).toContain("alpha-batch.jsonl");
+ expect(el.textContent).toContain("beta-batch.jsonl");
+ });
+
+ it("12. 'used by' column header is visible (i18n key)", () => {
+ const el = render(
+ <FilesListTab files={[makeFile()]} loading={false} />
+ );
+ // filesListUsedByColumn key is rendered in the header
+ expect(el.textContent).toContain("filesListUsedByColumn");
+ });
+
+ it("13. files with related batches (via inputFileId) show batch ID in used-by column", () => {
+ const file = makeFile({ id: "file-used", purpose: "batch" });
+ const batches = [
+ {
+ id: "batch-using-file",
+ endpoint: "/v1/chat/completions",
+ status: "completed",
+ inputFileId: "file-used",
+ outputFileId: null,
+ errorFileId: null,
+ model: "gpt-4o",
+ },
+ ];
+ const el = render(
+ <FilesListTab files={[file]} loading={false} batches={batches} />
+ );
+ expect(el.textContent).toContain("batch-using-file");
+ });
+
+ it("14. loading state shows spinner", () => {
+ const el = render(
+ <FilesListTab files={[]} loading={true} />
+ );
+ const spinner = el.querySelector(".animate-spin");
+ expect(spinner).not.toBeNull();
+ });
+
+ it("15. sanitization: rendered content contains no stack traces or file paths", () => {
+ const el = render(
+ <FilesListTab files={[makeFile({ filename: "test.jsonl" })]} loading={false} />
+ );
+ const text = el.textContent ?? "";
+ expect(text).not.toMatch(/\/home\//);
+ expect(text).not.toMatch(/at \//);
+ expect(text).not.toMatch(/route\.ts/);
+ });
+});
diff --git a/tests/unit/dashboard/batch/sanitization.test.tsx b/tests/unit/dashboard/batch/sanitization.test.tsx
new file mode 100644
index 0000000000..7b759c5e4c
--- /dev/null
+++ b/tests/unit/dashboard/batch/sanitization.test.tsx
@@ -0,0 +1,424 @@
+// @vitest-environment jsdom
+/**
+ * F9 — Sanitization assertion tests for all batch components that perform fetch().
+ *
+ * D14 requirement: EVERY component that does a fetch() and shows an error to the
+ * user MUST NOT leak raw err.stack, err.message, file paths, or stack frames.
+ *
+ * Components under test:
+ * - NewBatchWizard (file upload 500 + batch create 500 + network throw)
+ * - UploadFileModal (upload 500 + network throw)
+ * - useBatchActions (cancel 500 + retry 500 + network throw)
+ *
+ * Each test simulates an error condition that would expose a stack trace
+ * if the component violated Hard Rule #12, then asserts the displayed error
+ * is an i18n key or short user message — never a raw path/trace.
+ *
+ * STACK_PATTERNS: compiled regex set for reuse across all assertions.
+ */
+
+import React, { act } from "react";
+import { createRoot } from "react-dom/client";
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
+
+// ── i18n / dep mocks ──────────────────────────────────────────────────────────
+
+vi.mock("next-intl", () => ({
+ useTranslations: () => (key: string) => key,
+}));
+
+vi.mock("next/link", () => ({
+ default: ({ href, children }: { href: string; children: React.ReactNode }) => (
+ <a href={href}>{children}</a>
+ ),
+}));
+
+vi.mock("@/lib/batches/validateJsonl", () => ({
+ validateJsonl: vi.fn(() => ({
+ ok: true,
+ totalLines: 1,
+ sampledLines: 1,
+ uniqueCustomIds: 1,
+ duplicateCustomIds: [],
+ errors: [],
+ preview: [{ custom_id: "req-1" }],
+ byteSize: 100,
+ })),
+}));
+
+vi.mock("@/lib/batches/costEstimator", () => ({
+ estimateBatchCost: vi.fn(() => ({
+ model: "gpt-4o-mini",
+ totalRequests: 1,
+ estimatedInputTokens: 100,
+ estimatedOutputTokens: 256,
+ syncCostUsd: 0.001,
+ batchCostUsd: 0.0005,
+ savingsUsd: 0.0005,
+ pricingSource: "exact-match" as const,
+ warnings: [],
+ })),
+}));
+
+vi.mock("@/lib/batches/csvToJsonl", () => ({
+ csvToJsonl: vi.fn(() => ({ jsonl: "", rowsParsed: 0, rowsSkipped: 0, errors: [] })),
+}));
+
+vi.mock("@/lib/batches/retryFailed", () => ({
+ buildRetryPlan: vi.fn(() => ({ retriableLines: 0, newJsonl: "", failedCustomIds: [], skippedLines: 0 })),
+}));
+
+// ── Shared stack patterns ─────────────────────────────────────────────────────
+
+/**
+ * STACK_PATTERNS: all patterns that would indicate a raw error/stack is leaking
+ * into the UI. These must NOT match any user-visible text after an error.
+ */
+const STACK_PATTERNS = [
+ /\/home\//, // absolute paths to user home dir
+ /at \//, // stack frame lines "at /path/to/file.ts:42"
+ /route\.ts/, // source file names
+ /\tat /, // node-style " at Function..."
+ /Error:\s*\n/, // raw Error constructor prefix
+ /\.ts:\d+/, // typescript file + line number
+] as const;
+
+function assertSanitized(text: string | null | undefined, context: string) {
+ const t = text ?? "";
+ for (const pattern of STACK_PATTERNS) {
+ expect(t, `${context} must not contain pattern ${pattern}`).not.toMatch(pattern);
+ }
+}
+
+// ── Helpers ───────────────────────────────────────────────────────────────────
+
+const containers: Array<{ root: ReturnType<typeof createRoot>; el: HTMLDivElement }> = [];
+
+function makeDiv() {
+ const el = document.createElement("div");
+ document.body.appendChild(el);
+ return el;
+}
+
+async function waitFor(fn: () => boolean, ms = 3000): Promise<void> {
+ const start = Date.now();
+ while (Date.now() - start < ms) {
+ if (fn()) return;
+ await new Promise((r) => setTimeout(r, 30));
+ }
+ if (!fn()) throw new Error("waitFor timed out");
+}
+
+// ── Import components after mocks ─────────────────────────────────────────────
+
+const { default: NewBatchWizard } = await import(
+ "../../../../src/app/(dashboard)/dashboard/batch/components/NewBatchWizard"
+);
+const { default: UploadFileModal } = await import(
+ "../../../../src/app/(dashboard)/dashboard/batch/components/UploadFileModal"
+);
+const { useBatchActions } = await import(
+ "../../../../src/app/(dashboard)/dashboard/batch/components/useBatchActions"
+);
+
+// ── Lifecycle ─────────────────────────────────────────────────────────────────
+
+afterEach(() => {
+ for (const { root, el } of containers.splice(0)) {
+ act(() => root.unmount());
+ el.remove();
+ }
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+});
+
+// ── NewBatchWizard — sanitization ─────────────────────────────────────────────
+
+describe("NewBatchWizard — error sanitization", () => {
+ const PROVIDERS = [{ id: "openai", name: "OpenAI", models: ["gpt-4o-mini"] }];
+ const JSONL_LINE =
+ '{"custom_id":"req-1","method":"POST","url":"/v1/chat/completions","body":{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hello"}]}}\n';
+
+ async function mountAndNavigateToStep4(onCreated = vi.fn(), onClose = vi.fn()) {
+ const el = makeDiv();
+ const root = createRoot(el);
+ act(() => {
+ root.render(
+ <NewBatchWizard
+ onClose={onClose}
+ onCreated={onCreated}
+ availableProviders={PROVIDERS}
+ />
+ );
+ });
+ containers.push({ root, el });
+
+ // Step 1: select provider + model → Next
+ const selects = el.querySelectorAll("select");
+ await act(async () => {
+ (selects[0] as HTMLSelectElement).value = "openai";
+ selects[0].dispatchEvent(new Event("change", { bubbles: true }));
+ });
+ const selectsAfter = el.querySelectorAll("select");
+ await act(async () => {
+ (selectsAfter[2] as HTMLSelectElement).value = "gpt-4o-mini";
+ selectsAfter[2].dispatchEvent(new Event("change", { bubbles: true }));
+ });
+ const nextBtns = () => Array.from(el.querySelectorAll("button")).filter((b) => b.textContent === "wizardNext");
+ await act(async () => { nextBtns()[0]?.click(); });
+
+ // Step 2: inject file
+ const fileInput = el.querySelector("input[type='file']") as HTMLInputElement;
+ const file = new File([JSONL_LINE], "batch.jsonl", { type: "application/jsonl" });
+ await act(async () => {
+ Object.defineProperty(fileInput, "files", { value: [file], configurable: true });
+ fileInput.dispatchEvent(new Event("change", { bubbles: true }));
+ });
+ await new Promise((r) => setTimeout(r, 100));
+
+ await waitFor(() => !((nextBtns()[0] as HTMLButtonElement)?.disabled ?? true));
+
+ await act(async () => { nextBtns()[0]?.click(); });
+
+ // Wait step 3 validation ok
+ await waitFor(() => el.textContent!.includes("wizardValidationOk"));
+ await waitFor(() => !((nextBtns()[0] as HTMLButtonElement)?.disabled ?? true));
+
+ await act(async () => { nextBtns()[0]?.click(); });
+ await waitFor(() => el.textContent!.includes("wizardCreate"), { valueOf: () => 5000 } as unknown as number);
+
+ return el;
+ }
+
+ it("S1: file upload 500 with stack in body → alert shows i18n key only", async () => {
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue({
+ ok: false,
+ status: 500,
+ json: async () => ({
+ error: {
+ message:
+ "TypeError at /home/user/server/files/route.ts:42:12\n at handler (/home/user/route.ts:99:5)",
+ },
+ }),
+ }));
+
+ const el = await mountAndNavigateToStep4();
+
+ const createBtn = Array.from(el.querySelectorAll("button")).find((b) =>
+ b.textContent?.includes("wizardCreate")
+ );
+ await act(async () => { createBtn?.click(); });
+
+ await waitFor(() => el.querySelector("[role='alert']") !== null);
+
+ const alert = el.querySelector("[role='alert']")!;
+ assertSanitized(alert.textContent, "NewBatchWizard file-upload-500 alert");
+ expect(alert.textContent).toBe("wizardErrorUpload");
+ });
+
+ it("S2: batch create 500 → alert is i18n key, no path exposed", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn()
+ .mockResolvedValueOnce({
+ ok: true,
+ json: async () => ({ id: "file-test" }),
+ })
+ .mockResolvedValueOnce({
+ ok: false,
+ status: 503,
+ json: async () => ({ error: { message: "DB failure at /home/user/db.ts:88" } }),
+ })
+ );
+
+ const el = await mountAndNavigateToStep4();
+
+ const createBtn = Array.from(el.querySelectorAll("button")).find((b) =>
+ b.textContent?.includes("wizardCreate")
+ );
+ await act(async () => { createBtn?.click(); });
+
+ await waitFor(() => el.querySelector("[role='alert']") !== null);
+
+ const alert = el.querySelector("[role='alert']")!;
+ assertSanitized(alert.textContent, "NewBatchWizard batch-create-503 alert");
+ expect(alert.textContent).toBe("wizardErrorCreate");
+ });
+
+ it("S3: fetch throws network error with path → alert is i18n key, no path", async () => {
+ vi.stubGlobal("fetch", vi.fn().mockRejectedValue(
+ new Error("ECONNREFUSED at /home/user/network.ts:200 — stack:\n at connect")
+ ));
+
+ const el = await mountAndNavigateToStep4();
+
+ const createBtn = Array.from(el.querySelectorAll("button")).find((b) =>
+ b.textContent?.includes("wizardCreate")
+ );
+ await act(async () => { createBtn?.click(); });
+
+ await waitFor(() => el.querySelector("[role='alert']") !== null);
+
+ const alert = el.querySelector("[role='alert']")!;
+ assertSanitized(alert.textContent, "NewBatchWizard network-throw alert");
+ });
+});
+
+// ── UploadFileModal — sanitization ────────────────────────────────────────────
+
+describe("UploadFileModal — error sanitization", () => {
+ function makeFile(name: string, bytes: number) {
+ return new File(["x".repeat(bytes)], name, { type: "application/x-jsonlines" });
+ }
+
+ function mountModal() {
+ const el = makeDiv();
+ const root = createRoot(el);
+ act(() => {
+ root.render(<UploadFileModal onClose={vi.fn()} onUploaded={vi.fn()} />);
+ });
+ containers.push({ root, el });
+ return el;
+ }
+
+ async function selectFile(el: HTMLElement, file: File) {
+ const input = el.querySelector("input[type='file']") as HTMLInputElement;
+ await act(async () => {
+ Object.defineProperty(input, "files", { value: [file], configurable: true });
+ input.dispatchEvent(new Event("change", { bubbles: true }));
+ });
+ }
+
+ it("S4: upload 500 with path in response → alert shows safe key only", async () => {
+ const el = mountModal();
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue({
+ ok: false,
+ status: 500,
+ json: async () => ({
+ error: {
+ message: "ENOMEM at /home/runner/build/src/api/v1/files/route.ts:42:7",
+ },
+ }),
+ }));
+
+ await selectFile(el, makeFile("test.jsonl", 100));
+
+ const uploadBtn = Array.from(el.querySelectorAll("button")).find((b) =>
+ b.textContent?.includes("uploadModalUpload")
+ )!;
+ await act(async () => { uploadBtn.click(); });
+
+ const alert = el.querySelector("[role='alert']")!;
+ expect(alert).not.toBeNull();
+ assertSanitized(alert.textContent, "UploadFileModal 500 alert");
+ expect(alert.textContent).toContain("uploadModalError");
+ });
+
+ it("S5: fetch throws with path in error.message → alert shows safe key only", async () => {
+ const el = mountModal();
+ vi.stubGlobal("fetch", vi.fn().mockRejectedValue(
+ new Error("Network timeout at /home/user/uploader.ts:77\n at upload (handler.ts:12)")
+ ));
+
+ await selectFile(el, makeFile("test.jsonl", 50));
+
+ const uploadBtn = Array.from(el.querySelectorAll("button")).find((b) =>
+ b.textContent?.includes("uploadModalUpload")
+ )!;
+ await act(async () => { uploadBtn.click(); });
+
+ const alert = el.querySelector("[role='alert']");
+ if (alert) {
+ assertSanitized(alert.textContent, "UploadFileModal network-throw alert");
+ expect(alert.textContent).toContain("uploadModalError");
+ }
+ });
+});
+
+// ── useBatchActions — sanitization ───────────────────────────────────────────
+
+describe("useBatchActions — error sanitization", () => {
+ const t = (key: string) => key;
+
+ function renderHook(onRefresh?: () => void) {
+ let latestResult: ReturnType<typeof useBatchActions> | null = null;
+
+ function Wrapper({ sub }: { sub: (r: ReturnType<typeof useBatchActions>) => void }) {
+ const r = useBatchActions({ onRefresh, t });
+ sub(r);
+ return null;
+ }
+
+ const el = makeDiv();
+ const root = createRoot(el);
+ act(() => {
+ root.render(<Wrapper sub={(r) => { latestResult = r; }} />);
+ });
+ containers.push({ root, el });
+
+ return { get: () => latestResult! };
+ }
+
+ it("S6: cancel with error containing path → error is i18n key only", async () => {
+ vi.stubGlobal("fetch", vi.fn().mockRejectedValue(
+ new Error("Connect failed at /home/user/proxy/src/route.ts:12:5")
+ ));
+
+ const hook = renderHook();
+ await act(async () => { await hook.get().cancel("batch-test"); });
+
+ const err = hook.get().error ?? "";
+ assertSanitized(err, "useBatchActions cancel error");
+ expect(err).toBe("batchActionCancel");
+ });
+
+ it("S7: retry with error containing stack trace → error is i18n key only", async () => {
+ const { buildRetryPlan } = await import("@/lib/batches/retryFailed");
+ vi.mocked(buildRetryPlan).mockReturnValueOnce({
+ retriableLines: 1,
+ newJsonl: '{"custom_id":"r1"}\n',
+ failedCustomIds: ["r1"],
+ skippedLines: 0,
+ });
+
+ vi.stubGlobal(
+ "fetch",
+ vi.fn()
+ .mockResolvedValueOnce({ ok: true, text: async () => '{"custom_id":"r1"}\n' })
+ .mockResolvedValueOnce({ ok: true, text: async () => '{"custom_id":"r1","error":{}}\n' })
+ .mockRejectedValueOnce(new Error("Upload failed at /home/user/files.ts:99:3\n at upload"))
+ );
+
+ const hook = renderHook();
+ await act(async () => {
+ await hook.get().retry({
+ id: "batch-1",
+ inputFileId: "file-input",
+ errorFileId: "file-error",
+ endpoint: "/v1/chat/completions",
+ });
+ });
+
+ const err = hook.get().error ?? "";
+ assertSanitized(err, "useBatchActions retry error");
+ expect(err).toBe("batchActionRetry");
+ });
+
+ it("S8: cancel with HTTP 500 → error is i18n key, not status code or body", async () => {
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue({
+ ok: false,
+ status: 500,
+ json: async () => ({ error: { message: "DB error at /home/user/db.ts:42" } }),
+ }));
+
+ const hook = renderHook();
+ await act(async () => { await hook.get().cancel("batch-500-test"); });
+
+ const err = hook.get().error ?? "";
+ assertSanitized(err, "useBatchActions cancel 500 error");
+ expect(err).toBe("batchActionCancel");
+ // Must not expose HTTP status or internal message
+ expect(err).not.toContain("500");
+ expect(err).not.toContain("DB error");
+ });
+});