|
| 1 | +import { describe, it, expect, vi, afterEach } from "vitest"; |
| 2 | +import { render, screen, fireEvent, waitFor } from "@testing-library/react"; |
| 3 | +import { ModelBrowser } from "./ModelBrowser"; |
| 4 | + |
| 5 | +// Pins the #1548 fix: a download that fails must stay visible with its |
| 6 | +// backend error and a Retry button, not silently vanish (which made an |
| 7 | +// instant failure look like a completed install). |
| 8 | + |
| 9 | +const CATALOG = { |
| 10 | + models: [ |
| 11 | + { |
| 12 | + id: "qwen-test", |
| 13 | + name: "Qwen Test", |
| 14 | + description: "test model", |
| 15 | + capabilities: ["chat"], |
| 16 | + variants: [ |
| 17 | + { |
| 18 | + id: "q4", |
| 19 | + name: "Q4", |
| 20 | + size_mb: 500, |
| 21 | + compatibility: "green", |
| 22 | + downloaded: false, |
| 23 | + }, |
| 24 | + ], |
| 25 | + }, |
| 26 | + ], |
| 27 | +}; |
| 28 | + |
| 29 | +function mockFetch(routes: Record<string, () => Response>) { |
| 30 | + vi.stubGlobal( |
| 31 | + "fetch", |
| 32 | + vi.fn((url: string) => { |
| 33 | + for (const [prefix, make] of Object.entries(routes)) { |
| 34 | + if (url.startsWith(prefix)) return Promise.resolve(make()); |
| 35 | + } |
| 36 | + return Promise.resolve( |
| 37 | + new Response("[]", { |
| 38 | + status: 200, |
| 39 | + headers: { "Content-Type": "application/json" }, |
| 40 | + }), |
| 41 | + ); |
| 42 | + }) as unknown as typeof fetch, |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +function json(body: unknown): Response { |
| 47 | + return new Response(JSON.stringify(body), { |
| 48 | + status: 200, |
| 49 | + headers: { "Content-Type": "application/json" }, |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +describe("ModelBrowser download errors", () => { |
| 54 | + afterEach(() => { |
| 55 | + vi.unstubAllGlobals(); |
| 56 | + vi.useRealTimers(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("shows the backend error and a Retry button when a download fails", async () => { |
| 60 | + mockFetch({ |
| 61 | + "/api/models/downloads/": () => |
| 62 | + json({ status: "error", percent: 0, error: "SHA256 mismatch" }), |
| 63 | + "/api/models/download": () => json({ download_id: "dl-1" }), |
| 64 | + "/api/models/loaded": () => json({ models: [] }), |
| 65 | + "/api/models": () => json(CATALOG), |
| 66 | + "/api/providers": () => json([]), |
| 67 | + }); |
| 68 | + |
| 69 | + render( |
| 70 | + <ModelBrowser open={true} onClose={() => {}} capability="chat" />, |
| 71 | + ); |
| 72 | + |
| 73 | + const btn = await screen.findByRole("button", { name: "Download" }); |
| 74 | + fireEvent.click(btn); |
| 75 | + |
| 76 | + // Poll interval is 2s; the error must surface, not be cleared. |
| 77 | + const alert = await screen.findByRole( |
| 78 | + "alert", |
| 79 | + {}, |
| 80 | + { timeout: 5000 }, |
| 81 | + ); |
| 82 | + expect(alert.textContent).toContain("SHA256 mismatch"); |
| 83 | + expect( |
| 84 | + screen.getByRole("button", { name: "Retry" }), |
| 85 | + ).toBeInTheDocument(); |
| 86 | + }, 10000); |
| 87 | + |
| 88 | + it("shows an error when the download cannot even be started", async () => { |
| 89 | + mockFetch({ |
| 90 | + "/api/models/download": () => json({ error: "no download_url for variant" }), |
| 91 | + "/api/models/loaded": () => json({ models: [] }), |
| 92 | + "/api/models": () => json(CATALOG), |
| 93 | + "/api/providers": () => json([]), |
| 94 | + }); |
| 95 | + |
| 96 | + render( |
| 97 | + <ModelBrowser open={true} onClose={() => {}} capability="chat" />, |
| 98 | + ); |
| 99 | + |
| 100 | + const btn = await screen.findByRole("button", { name: "Download" }); |
| 101 | + fireEvent.click(btn); |
| 102 | + |
| 103 | + const alert = await screen.findByRole("alert"); |
| 104 | + expect(alert.textContent).toContain("no download_url for variant"); |
| 105 | + }); |
| 106 | +}); |
0 commit comments