|
| 1 | +// E-AUDIT-01: DataInspector file upload picker. |
| 2 | + |
| 3 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 4 | +import { render, screen, fireEvent, waitFor } from "@testing-library/react"; |
| 5 | +import { DataInspector } from "@/components/DataInspector"; |
| 6 | +import type { RpcClient } from "@/lib/rpc"; |
| 7 | + |
| 8 | +function makeRpc(): RpcClient { |
| 9 | + return { call: vi.fn(async () => ({} as never)) } as unknown as RpcClient; |
| 10 | +} |
| 11 | + |
| 12 | +describe("E-AUDIT-01 DataInspector file upload", () => { |
| 13 | + let origFetch: typeof fetch | undefined; |
| 14 | + beforeEach(() => { |
| 15 | + origFetch = globalThis.fetch; |
| 16 | + }); |
| 17 | + afterEach(() => { |
| 18 | + if (origFetch) globalThis.fetch = origFetch; |
| 19 | + }); |
| 20 | + |
| 21 | + it("renders file input with the spec'd testid + accept=.parquet", () => { |
| 22 | + render(<DataInspector rpc={makeRpc()} />); |
| 23 | + const input = screen.getByTestId( |
| 24 | + "data-inspector-file-upload") as HTMLInputElement; |
| 25 | + expect(input).toBeTruthy(); |
| 26 | + expect(input.type).toBe("file"); |
| 27 | + expect(input.accept).toBe(".parquet"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("uploads selected file via POST and auto-populates path field", |
| 31 | + async () => { |
| 32 | + const fetchMock = vi.fn(async () => ({ |
| 33 | + ok: true, |
| 34 | + json: async () => ({ path: "/tmp/vbgui_uploads/abc123.parquet", |
| 35 | + bytes: 42, filename: "shard.parquet" }), |
| 36 | + } as Response)); |
| 37 | + globalThis.fetch = fetchMock as unknown as typeof fetch; |
| 38 | + |
| 39 | + render(<DataInspector rpc={makeRpc()} />); |
| 40 | + const fileInput = screen.getByTestId( |
| 41 | + "data-inspector-file-upload") as HTMLInputElement; |
| 42 | + const blob = new File([new Uint8Array(42)], "shard.parquet", |
| 43 | + { type: "application/octet-stream" }); |
| 44 | + fireEvent.change(fileInput, { target: { files: [blob] } }); |
| 45 | + |
| 46 | + await waitFor(() => { |
| 47 | + expect(fetchMock).toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + const call = (fetchMock.mock.calls[0] ?? []) as |
| 50 | + [string, RequestInit]; |
| 51 | + expect(call[0]).toMatch(/\/upload\/parquet$/); |
| 52 | + expect(call[1].method).toBe("POST"); |
| 53 | + expect(call[1].body).toBeInstanceOf(FormData); |
| 54 | + |
| 55 | + await waitFor(() => { |
| 56 | + const pathInput = screen.getByTestId("data-path") as HTMLInputElement; |
| 57 | + expect(pathInput.value).toBe("/tmp/vbgui_uploads/abc123.parquet"); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it("surfaces HTTP error into the upload-error span", async () => { |
| 62 | + const fetchMock = vi.fn(async () => ({ |
| 63 | + ok: false, status: 400, |
| 64 | + json: async () => ({}), |
| 65 | + } as Response)); |
| 66 | + globalThis.fetch = fetchMock as unknown as typeof fetch; |
| 67 | + |
| 68 | + render(<DataInspector rpc={makeRpc()} />); |
| 69 | + const fileInput = screen.getByTestId( |
| 70 | + "data-inspector-file-upload") as HTMLInputElement; |
| 71 | + const blob = new File([new Uint8Array(8)], "x.parquet", |
| 72 | + { type: "application/octet-stream" }); |
| 73 | + fireEvent.change(fileInput, { target: { files: [blob] } }); |
| 74 | + |
| 75 | + await waitFor(() => { |
| 76 | + const err = screen.getByTestId( |
| 77 | + "data-inspector-file-upload-error"); |
| 78 | + expect(err.textContent).toContain("400"); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments