File size: 3,306 Bytes
21dd449 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
import { describe, expect, it, beforeAll } from "vitest";
import { WebBlob } from "./WebBlob";
describe("WebBlob", () => {
const resourceUrl = new URL("https://huggingface.co/spaces/aschen/push-model-from-web/raw/main/mobilenet/model.json");
let fullText: string;
let size: number;
let contentType: string;
beforeAll(async () => {
const response = await fetch(resourceUrl, { method: "HEAD" });
size = Number(response.headers.get("content-length"));
contentType = response.headers.get("content-type") || "";
fullText = await (await fetch(resourceUrl)).text();
});
it("should create a WebBlob with a slice on the entire resource", async () => {
const webBlob = await WebBlob.create(resourceUrl, { cacheBelow: 0, accessToken: undefined });
expect(webBlob).toMatchObject({
url: resourceUrl,
start: 0,
end: size,
contentType,
});
expect(webBlob).toBeInstanceOf(WebBlob);
expect(webBlob.size).toBe(size);
expect(webBlob.type).toBe(contentType);
const text = await webBlob.text();
expect(text).toBe(fullText);
const streamText = await new Response(webBlob.stream()).text();
expect(streamText).toBe(fullText);
});
it("should create a WebBlob with a slice on the entire resource, cached", async () => {
const webBlob = await WebBlob.create(resourceUrl, { cacheBelow: 1_000_000, accessToken: undefined });
expect(webBlob).not.toBeInstanceOf(WebBlob);
expect(webBlob.size).toBe(size);
expect(webBlob.type.replace(/;\s*charset=utf-8/, "")).toBe(contentType.replace(/;\s*charset=utf-8/, ""));
const text = await webBlob.text();
expect(text).toBe(fullText);
const streamText = await new Response(webBlob.stream()).text();
expect(streamText).toBe(fullText);
});
it("should lazy load a LFS file hosted on Hugging Face", async () => {
const zephyrUrl =
"https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha/resolve/main/model-00001-of-00008.safetensors";
const url = new URL(zephyrUrl);
const webBlob = await WebBlob.create(url);
expect(webBlob.size).toBe(1_889_587_040);
expect(webBlob).toBeInstanceOf(WebBlob);
expect(webBlob).toMatchObject({ url });
expect(await webBlob.slice(10, 22).text()).toBe("__metadata__");
});
it("should lazy load a Xet file hosted on Hugging Face", async () => {
const stableDiffusionUrl =
"https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/unet/diffusion_pytorch_model.fp16.safetensors";
const url = new URL(stableDiffusionUrl);
const webBlob = await WebBlob.create(url);
expect(webBlob.size).toBe(5_135_149_760);
expect(webBlob).toBeInstanceOf(WebBlob);
expect(webBlob).toMatchObject({ url });
expect(await webBlob.slice(10, 22).text()).toBe("__metadata__");
});
it("should create a slice on the file", async () => {
const expectedText = fullText.slice(10, 20);
const slice = (await WebBlob.create(resourceUrl, { cacheBelow: 0, accessToken: undefined })).slice(10, 20);
expect(slice).toMatchObject({
url: resourceUrl,
start: 10,
end: 20,
contentType,
});
expect(slice.size).toBe(10);
expect(slice.type).toBe(contentType);
const sliceText = await slice.text();
expect(sliceText).toBe(expectedText);
const streamText = await new Response(slice.stream()).text();
expect(streamText).toBe(expectedText);
});
});
|