|
1 |
| -import { expect, test, describe, vi } from "vitest"; |
| 1 | +import { expect, test, describe, assert } from "vitest"; |
2 | 2 | import { downloadFile } from "./download-file";
|
3 |
| -import type { RepoId } from "../types/public"; |
4 |
| - |
5 |
| -const DUMMY_REPO: RepoId = { |
6 |
| - name: "hello-world", |
7 |
| - type: "model", |
8 |
| -}; |
| 3 | +import { deleteRepo } from "./delete-repo"; |
| 4 | +import { createRepo } from "./create-repo"; |
| 5 | +import { TEST_ACCESS_TOKEN, TEST_HUB_URL, TEST_USER } from "../test/consts"; |
| 6 | +import { insecureRandomString } from "../utils/insecureRandomString"; |
9 | 7 |
|
10 | 8 | describe("downloadFile", () => {
|
11 |
| - test("hubUrl params should overwrite HUB_URL", async () => { |
12 |
| - const fetchMock: typeof fetch = vi.fn(); |
13 |
| - vi.mocked(fetchMock).mockResolvedValue({ |
14 |
| - status: 200, |
15 |
| - ok: true, |
16 |
| - } as Response); |
| 9 | + test("should download regular file", async () => { |
| 10 | + const blob = await downloadFile({ |
| 11 | + repo: { |
| 12 | + type: "model", |
| 13 | + name: "openai-community/gpt2", |
| 14 | + }, |
| 15 | + path: "README.md", |
| 16 | + }); |
| 17 | + |
| 18 | + const text = await blob?.slice(0, 1000).text(); |
| 19 | + assert( |
| 20 | + text?.includes(`--- |
| 21 | +language: en |
| 22 | +tags: |
| 23 | +- exbert |
| 24 | +
|
| 25 | +license: mit |
| 26 | +--- |
| 27 | +
|
| 28 | +
|
| 29 | +# GPT-2 |
17 | 30 |
|
18 |
| - await downloadFile({ |
19 |
| - repo: DUMMY_REPO, |
20 |
| - path: "/README.md", |
21 |
| - hubUrl: "http://dummy-hub", |
22 |
| - fetch: fetchMock, |
| 31 | +Test the whole generation capabilities here: https://transformer.huggingface.co/doc/gpt2-large`) |
| 32 | + ); |
| 33 | + }); |
| 34 | + test("should downoad xet file", async () => { |
| 35 | + const blob = await downloadFile({ |
| 36 | + repo: { |
| 37 | + type: "model", |
| 38 | + name: "celinah/xet-experiments", |
| 39 | + }, |
| 40 | + path: "large_text.txt", |
23 | 41 | });
|
24 | 42 |
|
25 |
| - expect(fetchMock).toHaveBeenCalledWith("http://dummy-hub/hello-world/resolve/main//README.md", expect.anything()); |
| 43 | + const text = await blob?.slice(0, 100).text(); |
| 44 | + expect(text).toMatch("this is a text file.".repeat(10).slice(0, 100)); |
26 | 45 | });
|
27 | 46 |
|
28 |
| - test("raw params should use raw url", async () => { |
29 |
| - const fetchMock: typeof fetch = vi.fn(); |
30 |
| - vi.mocked(fetchMock).mockResolvedValue({ |
31 |
| - status: 200, |
32 |
| - ok: true, |
33 |
| - } as Response); |
| 47 | + test("should download private file", async () => { |
| 48 | + const repoName = `datasets/${TEST_USER}/TEST-${insecureRandomString()}`; |
34 | 49 |
|
35 |
| - await downloadFile({ |
36 |
| - repo: DUMMY_REPO, |
37 |
| - path: "README.md", |
38 |
| - raw: true, |
39 |
| - fetch: fetchMock, |
| 50 | + const result = await createRepo({ |
| 51 | + accessToken: TEST_ACCESS_TOKEN, |
| 52 | + hubUrl: TEST_HUB_URL, |
| 53 | + private: true, |
| 54 | + repo: repoName, |
| 55 | + files: [{ path: ".gitattributes", content: new Blob(["*.html filter=lfs diff=lfs merge=lfs -text"]) }], |
40 | 56 | });
|
41 | 57 |
|
42 |
| - expect(fetchMock).toHaveBeenCalledWith("https://huggingface.co/hello-world/raw/main/README.md", expect.anything()); |
43 |
| - }); |
| 58 | + assert.deepStrictEqual(result, { |
| 59 | + repoUrl: `${TEST_HUB_URL}/${repoName}`, |
| 60 | + }); |
| 61 | + |
| 62 | + try { |
| 63 | + const blob = await downloadFile({ |
| 64 | + repo: repoName, |
| 65 | + path: ".gitattributes", |
| 66 | + hubUrl: TEST_HUB_URL, |
| 67 | + accessToken: TEST_ACCESS_TOKEN, |
| 68 | + }); |
44 | 69 |
|
45 |
| - test("internal server error should propagate the error", async () => { |
46 |
| - const fetchMock: typeof fetch = vi.fn(); |
47 |
| - vi.mocked(fetchMock).mockResolvedValue({ |
48 |
| - status: 500, |
49 |
| - ok: false, |
50 |
| - headers: new Map<string, string>([["Content-Type", "application/json"]]), |
51 |
| - json: () => ({ |
52 |
| - error: "Dummy internal error", |
53 |
| - }), |
54 |
| - } as unknown as Response); |
| 70 | + assert(blob, "File should be found"); |
55 | 71 |
|
56 |
| - await expect(async () => { |
57 |
| - await downloadFile({ |
58 |
| - repo: DUMMY_REPO, |
59 |
| - path: "README.md", |
60 |
| - raw: true, |
61 |
| - fetch: fetchMock, |
| 72 | + const text = await blob?.text(); |
| 73 | + assert.strictEqual(text, "*.html filter=lfs diff=lfs merge=lfs -text"); |
| 74 | + } finally { |
| 75 | + await deleteRepo({ |
| 76 | + repo: repoName, |
| 77 | + hubUrl: TEST_HUB_URL, |
| 78 | + accessToken: TEST_ACCESS_TOKEN, |
62 | 79 | });
|
63 |
| - }).rejects.toThrowError("Dummy internal error"); |
| 80 | + } |
64 | 81 | });
|
65 | 82 | });
|
0 commit comments