Skip to content

Commit

Permalink
🐛 Fix tests in browser?
Browse files Browse the repository at this point in the history
  • Loading branch information
coyotte508 committed Oct 24, 2023
1 parent 26302b8 commit 3879a5e
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 43 deletions.
6 changes: 2 additions & 4 deletions packages/hub/src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export const HUB_URL =
typeof process !== "undefined" && process.env.NODE_ENV === "test"
? "https://hub-ci.huggingface.co"
: "https://huggingface.co";
export const HUB_URL = "https://huggingface.co";
export const TEST_HUB_URL = "https://hub-ci.huggingface.co";
export const TEST_USER = "hub.js";
export const TEST_ACCESS_TOKEN = "hf_hub.js";
32 changes: 22 additions & 10 deletions packages/hub/src/lib/commit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, it, describe } from "vitest";

import { HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts";
import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts";
import type { RepoId } from "../types/public";
import type { CommitFile } from "./commit";
import { commit } from "./commit";
Expand Down Expand Up @@ -28,12 +28,13 @@ describe("commit", () => {
credentials: {
accessToken: TEST_ACCESS_TOKEN,
},
hubUrl: TEST_HUB_URL,
repo,
license: "mit",
});

try {
const readme1 = await downloadFile({ repo, path: "README.md" });
const readme1 = await downloadFile({ repo, path: "README.md", hubUrl: TEST_HUB_URL });
assert.strictEqual(readme1?.status, 200);

const nodeOperation: CommitFile[] = isFrontend
Expand All @@ -52,6 +53,7 @@ describe("commit", () => {
credentials: {
accessToken: TEST_ACCESS_TOKEN,
},
hubUrl: TEST_HUB_URL,
operations: [
{
operation: "addOrUpdate",
Expand All @@ -78,15 +80,15 @@ describe("commit", () => {
useWebWorkers: { minSize: 5_000 },
});

const fileContent = await downloadFile({ repo, path: "test.txt" });
const fileContent = await downloadFile({ repo, path: "test.txt", hubUrl: TEST_HUB_URL });
assert.strictEqual(fileContent?.status, 200);
assert.strictEqual(await fileContent?.text(), "This is me");

const lfsFileContent = await downloadFile({ repo, path: "test.lfs.txt" });
const lfsFileContent = await downloadFile({ repo, path: "test.lfs.txt", hubUrl: TEST_HUB_URL });
assert.strictEqual(lfsFileContent?.status, 200);
assert.strictEqual(await lfsFileContent?.text(), lfsContent);

const lfsFileUrl = `${HUB_URL}/${repoName}/raw/main/test.lfs.txt`;
const lfsFileUrl = `${TEST_HUB_URL}/${repoName}/raw/main/test.lfs.txt`;
const lfsFilePointer = await fetch(lfsFileUrl);
assert.strictEqual(lfsFilePointer.status, 200);
assert.strictEqual(
Expand All @@ -99,26 +101,27 @@ size ${lfsContent.length}
);

if (!isFrontend) {
const fileUrlContent = await downloadFile({ repo, path: "tsconfig.json" });
const fileUrlContent = await downloadFile({ repo, path: "tsconfig.json", hubUrl: TEST_HUB_URL });
assert.strictEqual(fileUrlContent?.status, 200);
assert.strictEqual(
await fileUrlContent?.text(),
(await import("node:fs")).readFileSync("./tsconfig.json", "utf-8")
);
}

const webResourceContent = await downloadFile({ repo, path: "lamaral.json" });
const webResourceContent = await downloadFile({ repo, path: "lamaral.json", hubUrl: TEST_HUB_URL });
assert.strictEqual(webResourceContent?.status, 200);
assert.strictEqual(await webResourceContent?.text(), await (await fetch(tokenizerJsonUrl)).text());

const readme2 = await downloadFile({ repo, path: "README.md" });
const readme2 = await downloadFile({ repo, path: "README.md", hubUrl: TEST_HUB_URL });
assert.strictEqual(readme2, null);
} finally {
await deleteRepo({
repo: {
name: repoName,
type: "model",
},
hubUrl: TEST_HUB_URL,
credentials: { accessToken: TEST_ACCESS_TOKEN },
});
}
Expand All @@ -136,6 +139,7 @@ size ${lfsContent.length}
accessToken: TEST_ACCESS_TOKEN,
},
repo,
hubUrl: TEST_HUB_URL,
});

try {
Expand All @@ -162,15 +166,22 @@ size ${lfsContent.length}
credentials: {
accessToken: TEST_ACCESS_TOKEN,
},
hubUrl: TEST_HUB_URL,
title: "upload model",
operations,
});

const LFSSize = (await fileDownloadInfo({ repo, path: "mobilenet/group1-shard1of2" }))?.size;
const LFSSize = (await fileDownloadInfo({ repo, path: "mobilenet/group1-shard1of2", hubUrl: TEST_HUB_URL }))
?.size;

assert.strictEqual(LFSSize, 4_194_304);

const pointerFile = await downloadFile({ repo, path: "mobilenet/group1-shard1of2", raw: true });
const pointerFile = await downloadFile({
repo,
path: "mobilenet/group1-shard1of2",
raw: true,
hubUrl: TEST_HUB_URL,
});

// Make sure SHA is computed properly as well
assert.strictEqual(
Expand All @@ -187,6 +198,7 @@ size 4194304
name: repoName,
type: "model",
},
hubUrl: TEST_HUB_URL,
credentials: { accessToken: TEST_ACCESS_TOKEN },
});
}
Expand Down
16 changes: 12 additions & 4 deletions packages/hub/src/lib/create-repo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, it, describe, expect } from "vitest";

import { HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts";
import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts";
import { insecureRandomString } from "../utils/insecureRandomString";
import { createRepo } from "./create-repo";
import { deleteRepo } from "./delete-repo";
Expand All @@ -18,11 +18,12 @@ describe("createRepo", () => {
name: repoName,
type: "model",
},
hubUrl: TEST_HUB_URL,
files: [{ path: ".gitattributes", content: new Blob(["*.html filter=lfs diff=lfs merge=lfs -text"]) }],
});

assert.deepStrictEqual(result, {
repoUrl: `${HUB_URL}/${repoName}`,
repoUrl: `${TEST_HUB_URL}/${repoName}`,
});

const content = await downloadFile({
Expand All @@ -31,6 +32,7 @@ describe("createRepo", () => {
type: "model",
},
path: ".gitattributes",
hubUrl: TEST_HUB_URL,
});

assert(content);
Expand All @@ -42,13 +44,15 @@ describe("createRepo", () => {
type: "model",
},
credentials: { accessToken: TEST_ACCESS_TOKEN },
hubUrl: TEST_HUB_URL,
});
});

it("should throw a client error when trying to create a repo without a fully-qualified name", async () => {
const tryCreate = createRepo({
repo: { name: "canonical", type: "model" },
credentials: { accessToken: TEST_ACCESS_TOKEN },
hubUrl: TEST_HUB_URL,
});

await expect(tryCreate).rejects.toBeInstanceOf(TypeError);
Expand All @@ -61,19 +65,21 @@ describe("createRepo", () => {
credentials: {
accessToken: TEST_ACCESS_TOKEN,
},
hubUrl: TEST_HUB_URL,
repo: repoName,
files: [{ path: ".gitattributes", content: new Blob(["*.html filter=lfs diff=lfs merge=lfs -text"]) }],
});

assert.deepStrictEqual(result, {
repoUrl: `${HUB_URL}/${repoName}`,
repoUrl: `${TEST_HUB_URL}/${repoName}`,
});

await deleteRepo({
repo: {
name: repoName,
type: "model",
},
hubUrl: TEST_HUB_URL,
credentials: { accessToken: TEST_ACCESS_TOKEN },
});
});
Expand All @@ -85,16 +91,18 @@ describe("createRepo", () => {
credentials: {
accessToken: TEST_ACCESS_TOKEN,
},
hubUrl: TEST_HUB_URL,
repo: repoName,
files: [{ path: ".gitattributes", content: new Blob(["*.html filter=lfs diff=lfs merge=lfs -text"]) }],
});

assert.deepStrictEqual(result, {
repoUrl: `${HUB_URL}/${repoName}`,
repoUrl: `${TEST_HUB_URL}/${repoName}`,
});

await deleteRepo({
repo: repoName,
hubUrl: TEST_HUB_URL,
credentials: { accessToken: TEST_ACCESS_TOKEN },
});
});
Expand Down
9 changes: 7 additions & 2 deletions packages/hub/src/lib/delete-file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, it, describe } from "vitest";

import { HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts";
import { TEST_ACCESS_TOKEN, TEST_HUB_URL, TEST_USER } from "../consts";
import type { RepoId } from "../types/public";
import { insecureRandomString } from "../utils/insecureRandomString";
import { createRepo } from "./create-repo";
Expand All @@ -19,6 +19,7 @@ describe("deleteFile", () => {
try {
const result = await createRepo({
credentials,
hubUrl: TEST_HUB_URL,
repo,
files: [
{ path: "file1", content: new Blob(["file1"]) },
Expand All @@ -27,10 +28,11 @@ describe("deleteFile", () => {
});

assert.deepStrictEqual(result, {
repoUrl: `${HUB_URL}/${repoName}`,
repoUrl: `${TEST_HUB_URL}/${repoName}`,
});

let content = await downloadFile({
hubUrl: TEST_HUB_URL,
repo,
path: "file1",
});
Expand All @@ -42,20 +44,23 @@ describe("deleteFile", () => {
content = await downloadFile({
repo,
path: "file1",
hubUrl: TEST_HUB_URL,
});

assert.strictEqual(content, null);

content = await downloadFile({
repo,
path: "file2",
hubUrl: TEST_HUB_URL,
});

assert.strictEqual(await content?.text(), "file2");
} finally {
await deleteRepo({
repo,
credentials,
hubUrl: TEST_HUB_URL,
});
}
});
Expand Down
13 changes: 10 additions & 3 deletions packages/hub/src/lib/delete-files.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, it, describe } from "vitest";

import { HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts";
import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts";
import type { RepoId } from "../types/public";
import { insecureRandomString } from "../utils/insecureRandomString";
import { createRepo } from "./create-repo";
Expand All @@ -25,52 +25,59 @@ describe("deleteFiles", () => {
{ path: "file2", content: new Blob(["file2"]) },
{ path: "file3", content: new Blob(["file3"]) },
],
hubUrl: TEST_HUB_URL,
});

assert.deepStrictEqual(result, {
repoUrl: `${HUB_URL}/${repoName}`,
repoUrl: `${TEST_HUB_URL}/${repoName}`,
});

let content = await downloadFile({
repo,
path: "file1",
hubUrl: TEST_HUB_URL,
});

assert.strictEqual(await content?.text(), "file1");

content = await downloadFile({
repo,
path: "file2",
hubUrl: TEST_HUB_URL,
});

assert.strictEqual(await content?.text(), "file2");

await deleteFiles({ paths: ["file1", "file2"], repo, credentials });
await deleteFiles({ paths: ["file1", "file2"], repo, credentials, hubUrl: TEST_HUB_URL });

content = await downloadFile({
repo,
path: "file1",
hubUrl: TEST_HUB_URL,
});

assert.strictEqual(content, null);

content = await downloadFile({
repo,
path: "file2",
hubUrl: TEST_HUB_URL,
});

assert.strictEqual(content, null);

content = await downloadFile({
repo,
path: "file3",
hubUrl: TEST_HUB_URL,
});

assert.strictEqual(await content?.text(), "file3");
} finally {
await deleteRepo({
repo,
credentials,
hubUrl: TEST_HUB_URL,
});
}
});
Expand Down
3 changes: 0 additions & 3 deletions packages/hub/src/lib/file-download-info.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe("fileDownloadInfo", () => {
},
path: "tf_model.h5",
revision: "dd4bc8b21efa05ec961e3efc4ee5e3832a3679c7",
hubUrl: "https://huggingface.co",
});

assert.strictEqual(info?.size, 536063208);
Expand All @@ -26,7 +25,6 @@ describe("fileDownloadInfo", () => {
},
path: "tf_model.h5",
revision: "dd4bc8b21efa05ec961e3efc4ee5e3832a3679c7",
hubUrl: "https://huggingface.co",
raw: true,
});

Expand All @@ -43,7 +41,6 @@ describe("fileDownloadInfo", () => {
},
path: "tokenizer_config.json",
revision: "1a7dd4986e3dab699c24ca19b2afd0f5e1a80f37",
hubUrl: "https://huggingface.co",
});

assert.strictEqual(info?.size, 28);
Expand Down
2 changes: 1 addition & 1 deletion packages/hub/src/lib/list-datasets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("listDatasets", () => {
it("should list datasets from hf-doc-builder", async () => {
const results: DatasetEntry[] = [];

for await (const entry of listDatasets({ search: { owner: "hf-doc-build" }, hubUrl: "https://huggingface.co" })) {
for await (const entry of listDatasets({ search: { owner: "hf-doc-build" } })) {
if (typeof entry.downloads === "number") {
entry.downloads = 0;
}
Expand Down
3 changes: 0 additions & 3 deletions packages/hub/src/lib/list-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe("listFiles", () => {
type: "model",
},
revision: "dd4bc8b21efa05ec961e3efc4ee5e3832a3679c7",
hubUrl: "https://huggingface.co",
});

const files: ListFileEntry[] = [];
Expand Down Expand Up @@ -71,7 +70,6 @@ describe("listFiles", () => {
},
revision: "dd4bc8b21efa05ec961e3efc4ee5e3832a3679c7",
expand: true,
hubUrl: "https://huggingface.co",
});

const files: ListFileEntry[] = [];
Expand Down Expand Up @@ -157,7 +155,6 @@ describe("listFiles", () => {
type: "dataset",
},
revision: "0f3ea2f2b55fcb11e71fb1e3aec6822e44ddcb0f",
hubUrl: "https://huggingface.co",
recursive: true,
});

Expand Down
Loading

0 comments on commit 3879a5e

Please sign in to comment.