Spaces:
Runtime error
Runtime error
File size: 12,037 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | // @vitest-environment jsdom
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 component after mocks βββββββββββββββββββββββββββββββββββββββββββββ
const { default: UploadFileModal } = await import(
"../../../../../src/app/(dashboard)/dashboard/batch/components/UploadFileModal"
);
// ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const containers: Array<{ root: ReturnType<typeof createRoot>; el: HTMLDivElement }> = [];
function renderModal(
props: Partial<{ onClose: () => void; onUploaded: (fileId: string) => void }> = {}
) {
const onClose = props.onClose ?? vi.fn();
const onUploaded = props.onUploaded ?? vi.fn();
const el = document.createElement("div");
document.body.appendChild(el);
const root = createRoot(el);
act(() => {
root.render(<UploadFileModal onClose={onClose} onUploaded={onUploaded} />);
});
containers.push({ root, el });
return { el, onClose, onUploaded };
}
function makeFile(name: string, sizeBytes: number, type = "application/x-jsonlines"): File {
const content = "x".repeat(sizeBytes);
return new File([content], name, { type });
}
// ββ Lifecycle βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
afterEach(() => {
for (const { root, el } of containers.splice(0)) {
act(() => root.unmount());
el.remove();
}
vi.restoreAllMocks();
});
beforeEach(() => {
vi.stubGlobal("fetch", vi.fn());
});
// ββ Tests βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe("UploadFileModal", () => {
// 1. Render: text + Upload button disabled
it("renders drop area and Upload button initially disabled", () => {
const { el } = renderModal();
// Title renders (i18n key returned as-is by mock)
const header = el.querySelector("h2");
expect(header).not.toBeNull();
expect(header!.textContent).toContain("uploadModalTitle");
// Drop area text
expect(el.textContent).toContain("uploadModalDropOrPick");
expect(el.textContent).toContain("uploadModalSizeLimit");
// Upload button should be disabled (no file selected)
const buttons = Array.from(el.querySelectorAll("button"));
const uploadBtn = buttons.find((b) => b.textContent?.includes("uploadModalUpload"));
expect(uploadBtn).not.toBeNull();
expect(uploadBtn!.disabled).toBe(true);
});
// 2. Select .txt file β error (invalid extension)
it("shows error when a non-.jsonl file is selected via input", () => {
const { el } = renderModal();
const input = el.querySelector("input[type='file']") as HTMLInputElement;
expect(input).not.toBeNull();
const txtFile = makeFile("data.txt", 100, "text/plain");
act(() => {
Object.defineProperty(input, "files", { value: [txtFile], configurable: true });
input.dispatchEvent(new Event("change", { bubbles: true }));
});
// Error banner should appear
const alert = el.querySelector("[role='alert']");
expect(alert).not.toBeNull();
expect(alert!.textContent).toContain("uploadModalError");
// Upload button still disabled
const buttons = Array.from(el.querySelectorAll("button"));
const uploadBtn = buttons.find((b) => b.textContent?.includes("uploadModalUpload"));
expect(uploadBtn!.disabled).toBe(true);
});
// 3. Select valid .jsonl β shows filename + enables Upload
it("shows filename and enables Upload after selecting a valid .jsonl file", () => {
const { el } = renderModal();
const input = el.querySelector("input[type='file']") as HTMLInputElement;
const jsonlFile = makeFile("batch.jsonl", 1024);
act(() => {
Object.defineProperty(input, "files", { value: [jsonlFile], configurable: true });
input.dispatchEvent(new Event("change", { bubbles: true }));
});
// Filename visible
expect(el.textContent).toContain("batch.jsonl");
// Upload button now enabled
const buttons = Array.from(el.querySelectorAll("button"));
const uploadBtn = buttons.find((b) => b.textContent?.includes("uploadModalUpload"));
expect(uploadBtn!.disabled).toBe(false);
// No error
expect(el.querySelector("[role='alert']")).toBeNull();
});
// 4. Select file > 512 MB β error
it("shows error when file exceeds 512 MB size limit", () => {
const { el } = renderModal();
const input = el.querySelector("input[type='file']") as HTMLInputElement;
// Cannot allocate 513MB string in V8; simulate large size via Object.defineProperty on a small File
const smallFile = makeFile("huge.jsonl", 10, "application/x-jsonlines");
const oversizedFile = Object.defineProperty(smallFile, "size", {
value: 513 * 1024 * 1024,
configurable: true,
}) as File;
act(() => {
Object.defineProperty(input, "files", {
value: [oversizedFile],
configurable: true,
});
input.dispatchEvent(new Event("change", { bubbles: true }));
});
const alert = el.querySelector("[role='alert']");
expect(alert).not.toBeNull();
expect(alert!.textContent).toContain("uploadModalError");
});
// 5. Upload with mock fetch 200 β onUploaded called with file id
it("calls onUploaded with the file id on successful upload", async () => {
const onUploaded = vi.fn();
const onClose = vi.fn();
const { el } = renderModal({ onUploaded, onClose });
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ id: "file-id-test" }),
});
vi.stubGlobal("fetch", fetchMock);
const input = el.querySelector("input[type='file']") as HTMLInputElement;
const jsonlFile = makeFile("batch.jsonl", 100);
act(() => {
Object.defineProperty(input, "files", { value: [jsonlFile], configurable: true });
input.dispatchEvent(new Event("change", { bubbles: true }));
});
const buttons = Array.from(el.querySelectorAll("button"));
const uploadBtn = buttons.find((b) => b.textContent?.includes("uploadModalUpload"));
expect(uploadBtn!.disabled).toBe(false);
await act(async () => {
uploadBtn!.click();
});
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, opts] = fetchMock.mock.calls[0] as [string, RequestInit & { body: FormData }];
expect(url).toBe("/api/v1/files");
expect(opts.method).toBe("POST");
expect(opts.body).toBeInstanceOf(FormData);
expect(onUploaded).toHaveBeenCalledWith("file-id-test");
expect(onClose).toHaveBeenCalled();
});
// 6. Upload with mock fetch 500 β shows error, never exposes raw message
it("shows generic error on fetch 500 β never exposes raw error message", async () => {
const onUploaded = vi.fn();
const { el } = renderModal({ onUploaded });
const fetchMock = vi.fn().mockResolvedValue({
ok: false,
status: 500,
json: async () => ({ error: { message: "stack at /home/user/x.ts:10" } }),
});
vi.stubGlobal("fetch", fetchMock);
const input = el.querySelector("input[type='file']") as HTMLInputElement;
const jsonlFile = makeFile("batch.jsonl", 100);
act(() => {
Object.defineProperty(input, "files", { value: [jsonlFile], configurable: true });
input.dispatchEvent(new Event("change", { bubbles: true }));
});
const buttons = Array.from(el.querySelectorAll("button"));
const uploadBtn = buttons.find((b) => b.textContent?.includes("uploadModalUpload"));
await act(async () => {
uploadBtn!.click();
});
// error banner visible
const alert = el.querySelector("[role='alert']");
expect(alert).not.toBeNull();
expect(alert!.textContent).toContain("uploadModalError");
// Sanitization assert: raw server message must NOT appear in UI
expect(alert!.textContent).not.toMatch(/home\//);
expect(alert!.textContent).not.toMatch(/stack at/);
// onUploaded never called
expect(onUploaded).not.toHaveBeenCalled();
});
// 7. Escape key β onClose
it("calls onClose when Escape key is pressed", () => {
const onClose = vi.fn();
renderModal({ onClose });
act(() => {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
});
expect(onClose).toHaveBeenCalledTimes(1);
});
// 8. Drag-and-drop valid .jsonl β same flow as click
it("accepts a .jsonl file via drag and drop", () => {
const { el } = renderModal();
// Find the drop zone (div with onDrop)
const dropZone = el.querySelector("[role='button']") as HTMLDivElement;
expect(dropZone).not.toBeNull();
const jsonlFile = makeFile("dropped.jsonl", 512);
act(() => {
const dragOverEvent = new Event("dragover", { bubbles: true }) as DragEvent;
Object.defineProperty(dragOverEvent, "dataTransfer", {
value: { files: [jsonlFile] },
configurable: true,
});
Object.defineProperty(dragOverEvent, "preventDefault", { value: vi.fn() });
dropZone.dispatchEvent(dragOverEvent);
});
act(() => {
const dropEvent = new Event("drop", { bubbles: true }) as DragEvent;
Object.defineProperty(dropEvent, "dataTransfer", {
value: { files: [jsonlFile] },
configurable: true,
});
Object.defineProperty(dropEvent, "preventDefault", { value: vi.fn() });
dropZone.dispatchEvent(dropEvent);
});
// Filename should be visible
expect(el.textContent).toContain("dropped.jsonl");
// Upload button enabled
const buttons = Array.from(el.querySelectorAll("button"));
const uploadBtn = buttons.find((b) => b.textContent?.includes("uploadModalUpload"));
expect(uploadBtn!.disabled).toBe(false);
});
// 9. Sanitization assert: 500 with stack trace in body β UI does NOT show path
it("sanitization: 500 with raw stack trace in error.message β UI never shows file path", async () => {
const { el } = renderModal();
const fetchMock = vi.fn().mockResolvedValue({
ok: false,
status: 500,
json: async () => ({
error: {
message:
"TypeError: Cannot read properties of undefined\n at /home/user/server/route.ts:45:12",
},
}),
});
vi.stubGlobal("fetch", fetchMock);
const input = el.querySelector("input[type='file']") as HTMLInputElement;
const jsonlFile = makeFile("test.jsonl", 50);
act(() => {
Object.defineProperty(input, "files", { value: [jsonlFile], configurable: true });
input.dispatchEvent(new Event("change", { bubbles: true }));
});
const buttons = Array.from(el.querySelectorAll("button"));
const uploadBtn = buttons.find((b) => b.textContent?.includes("uploadModalUpload"));
await act(async () => {
uploadBtn!.click();
});
const alert = el.querySelector("[role='alert']");
expect(alert).not.toBeNull();
// Must NOT contain any path-like content
expect(alert!.textContent).not.toMatch(/\/home\//);
expect(alert!.textContent).not.toMatch(/route\.ts/);
expect(alert!.textContent).not.toMatch(/at \//);
// But must show the safe generic key
expect(alert!.textContent).toContain("uploadModalError");
});
});
|