|
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); |
|
}); |
|
}); |
|
|