Download frontend/src/app/api/tasks/create/route.test.ts from new-arc/vector: direct link, hf CLI and curl.
- Browser
- Download file 4.11 kB
-
https://huggingface.co/spaces/new-arc/vector/resolve/main/frontend/src/app/api/tasks/create/route.test.ts
- Command line
-
hf download hf://spaces/new-arc/vector/frontend/src/app/api/tasks/create/route.test.ts
-
curl -L -o route.test.ts https://huggingface.co/spaces/new-arc/vector/resolve/main/frontend/src/app/api/tasks/create/route.test.ts
4.11 kB
| import { headers } from "next/headers"; | |
| import { POST } from "./route"; | |
| import { auth } from "@/lib/auth"; | |
| import { buildBackendAuthHeaders } from "@/lib/backend-auth"; | |
| import { parseApiError } from "@/lib/api-error"; | |
| vi.mock("next/headers", () => ({ | |
| headers: vi.fn(), | |
| })); | |
| vi.mock("@/lib/auth", () => ({ | |
| auth: { | |
| api: { | |
| getSession: vi.fn(), | |
| }, | |
| }, | |
| })); | |
| vi.mock("@/lib/backend-auth", () => ({ | |
| buildBackendAuthHeaders: vi.fn(), | |
| })); | |
| describe("/api/tasks/create", () => { | |
| beforeEach(() => { | |
| vi.resetAllMocks(); | |
| vi.stubGlobal("fetch", vi.fn()); | |
| vi.mocked(headers).mockResolvedValue(new Headers()); | |
| }); | |
| it("returns 401 when unauthenticated", async () => { | |
| vi.mocked(auth.api.getSession).mockResolvedValue(null as never); | |
| const response = await POST( | |
| new Request("http://localhost/api/tasks/create", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ source: { url: "https://example.com/video.mp4" } }), | |
| }), | |
| ); | |
| expect(response.status).toBe(401); | |
| await expect(response.json()).resolves.toEqual({ error: "Unauthorized" }); | |
| }); | |
| it("proxies task creation to the backend", async () => { | |
| vi.mocked(auth.api.getSession).mockResolvedValue({ | |
| user: { id: "user-1" }, | |
| } as never); | |
| vi.mocked(buildBackendAuthHeaders).mockReturnValue({ | |
| "x-supoclip-user-id": "user-1", | |
| }); | |
| vi.mocked(fetch).mockResolvedValue( | |
| new Response(JSON.stringify({ task_id: "task-1" }), { | |
| status: 200, | |
| headers: { "Content-Type": "application/json" }, | |
| }), | |
| ); | |
| const payload = { | |
| source: { url: "https://www.youtube.com/watch?v=demo" }, | |
| }; | |
| const response = await POST( | |
| new Request("http://localhost/api/tasks/create", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify(payload), | |
| }), | |
| ); | |
| expect(fetch).toHaveBeenCalledWith( | |
| "http://localhost:8000/tasks/", | |
| expect.objectContaining({ | |
| method: "POST", | |
| body: JSON.stringify(payload), | |
| headers: expect.objectContaining({ | |
| "Content-Type": "application/json", | |
| "x-supoclip-user-id": "user-1", | |
| }), | |
| }), | |
| ); | |
| expect(response.status).toBe(200); | |
| const init = vi.mocked(fetch).mock.calls[0][1] as RequestInit; | |
| expect(init.headers).not.toEqual(expect.objectContaining({ | |
| "idempotency-key": expect.anything(), | |
| })); | |
| }); | |
| it("forwards an idempotency key when the client sends one", async () => { | |
| vi.mocked(auth.api.getSession).mockResolvedValue({ | |
| user: { id: "user-1" }, | |
| } as never); | |
| vi.mocked(buildBackendAuthHeaders).mockReturnValue({ | |
| "x-supoclip-user-id": "user-1", | |
| }); | |
| vi.mocked(fetch).mockResolvedValue(new Response("{}", { status: 200 })); | |
| await POST(new Request("http://localhost/api/tasks/create", { | |
| method: "POST", | |
| headers: { "idempotency-key": "retry-key-1" }, | |
| body: JSON.stringify({ source: { url: "upload://clip.mp4" } }), | |
| })); | |
| const init = vi.mocked(fetch).mock.calls[0][1] as RequestInit; | |
| expect(init.headers).toEqual(expect.objectContaining({ | |
| "idempotency-key": "retry-key-1", | |
| })); | |
| }); | |
| it("preserves the duration rejection and actionable message for the form", async () => { | |
| vi.mocked(auth.api.getSession).mockResolvedValue({ user: { id: "user-1" } } as never); | |
| const message = "Your plan allows YouTube videos up to 90 minutes. No generation was used."; | |
| vi.mocked(fetch).mockResolvedValue(new Response(JSON.stringify({ | |
| detail: { code: "VIDEO_TOO_LONG", message, max_duration_seconds: 5400 }, | |
| }), { status: 422, headers: { "Content-Type": "application/json" } })); | |
| const response = await POST(new Request("http://localhost/api/tasks/create", { | |
| method: "POST", | |
| body: JSON.stringify({ source: { url: "https://youtu.be/abcdefghijk" } }), | |
| })); | |
| expect(response.status).toBe(422); | |
| expect((await parseApiError(response, "Failed to process video")).message).toBe(message); | |
| }); | |
| }); | |