File size: 1,538 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 |
import { open, stat } from "node:fs/promises";
import { TextDecoder } from "node:util";
import { describe, expect, it } from "vitest";
import { FileBlob } from "./FileBlob";
describe("FileBlob", () => {
it("should create a FileBlob with a slice on the entire file", async () => {
const file = await open("package.json", "r");
const { size } = await stat("package.json");
const fileBlob = await FileBlob.create("package.json");
expect(fileBlob).toMatchObject({
path: "package.json",
start: 0,
end: size,
});
expect(fileBlob.size).toBe(size);
expect(fileBlob.type).toBe("");
const text = await fileBlob.text();
const expectedText = (await file.read(Buffer.alloc(size), 0, size)).buffer.toString("utf8");
expect(text).toBe(expectedText);
const result = await fileBlob.stream().getReader().read();
expect(new TextDecoder().decode(result.value)).toBe(expectedText);
});
it("should create a slice on the file", async () => {
const file = await open("package.json", "r");
const fileBlob = await FileBlob.create("package.json");
const slice = fileBlob.slice(10, 20);
expect(slice).toMatchObject({
path: "package.json",
start: 10,
end: 20,
});
expect(slice.size).toBe(10);
const sliceText = await slice.text();
const expectedText = (await file.read(Buffer.alloc(10), 0, 10, 10)).buffer.toString("utf8");
expect(sliceText).toBe(expectedText);
const result = await slice.stream().getReader().read();
expect(new TextDecoder().decode(result.value)).toBe(expectedText);
});
});
|