|
| 1 | +import { assert, it, describe } from "vitest"; |
| 2 | +import { __internal_sha256 } from "@huggingface/hub"; |
| 3 | + |
| 4 | +const lfsContent = "0123456789".repeat(1_000_000); |
| 5 | + |
| 6 | +describe("hub", () => { |
| 7 | + it("should compute sha256 with webworker", async function () { |
| 8 | + const blob = new Blob([lfsContent]); |
| 9 | + |
| 10 | + const iterator = __internal_sha256(blob, { useWebWorker: { minSize: 1 } }); |
| 11 | + // Get return value of the generator |
| 12 | + const values: number[] = []; |
| 13 | + while (1) { |
| 14 | + const { done, value } = await iterator.next(); |
| 15 | + if (done) { |
| 16 | + assert.strictEqual(value, "d52fcc26b48dbd4d79b125eb0a29b803ade07613c67ac7c6f2751aefef008486"); |
| 17 | + |
| 18 | + const builtInResult = await crypto.subtle.digest("SHA-256", await blob.arrayBuffer()); |
| 19 | + const hex = |
| 20 | + builtInResult instanceof ArrayBuffer |
| 21 | + ? new Uint8Array(builtInResult).reduce((acc, i) => acc + i.toString(16).padStart(2, "0"), "") |
| 22 | + : builtInResult; |
| 23 | + assert.strictEqual(hex, "d52fcc26b48dbd4d79b125eb0a29b803ade07613c67ac7c6f2751aefef008486"); |
| 24 | + break; |
| 25 | + } |
| 26 | + |
| 27 | + if (typeof value === "number") { |
| 28 | + values.push(value); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Check that we received progress values, which should happen with a webworker |
| 33 | + assert(values.length > 2); |
| 34 | + assert.strictEqual(values[0], 0); |
| 35 | + assert.strictEqual(values[values.length - 1], 1); |
| 36 | + }, 60_000); |
| 37 | +}); |
0 commit comments