Skip to content

Commit 437f05c

Browse files
committed
fix: add error message
1 parent a71e654 commit 437f05c

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
import { BYTE_LENGTH_SOURCE, byteLengthSource } from "./byteLengthSource";
4+
import { runtimeConfig } from "./runtimeConfig";
5+
6+
vi.mock("./runtimeConfig", () => ({
7+
runtimeConfig: {
8+
lstatSync: vi.fn(),
9+
},
10+
}));
11+
12+
describe("byteLengthSource", () => {
13+
it("should return CONTENT_LENGTH when override is provided", () => {
14+
expect(byteLengthSource({}, 100)).toBe(BYTE_LENGTH_SOURCE.CONTENT_LENGTH);
15+
});
16+
17+
it("should return EMPTY_INPUT for null input", () => {
18+
expect(byteLengthSource(null)).toBe(BYTE_LENGTH_SOURCE.EMPTY_INPUT);
19+
});
20+
21+
it("should return EMPTY_INPUT for undefined input", () => {
22+
expect(byteLengthSource(undefined)).toBe(BYTE_LENGTH_SOURCE.EMPTY_INPUT);
23+
});
24+
25+
it("should return STRING_LENGTH for string input", () => {
26+
expect(byteLengthSource("test")).toBe(BYTE_LENGTH_SOURCE.STRING_LENGTH);
27+
});
28+
29+
it("should return TYPED_ARRAY for input with byteLength", () => {
30+
const input = new Uint8Array(10);
31+
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.TYPED_ARRAY);
32+
});
33+
34+
it("should return LENGTH for input with length property", () => {
35+
const input = { length: 10 };
36+
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.LENGTH);
37+
});
38+
39+
it("should return SIZE for input with size property", () => {
40+
const input = { size: 10 };
41+
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.SIZE);
42+
});
43+
44+
it("should return START_END_DIFF for input with start and end properties", () => {
45+
const input = { start: 0, end: 10 };
46+
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.START_END_DIFF);
47+
});
48+
49+
it("should return LSTAT for input with path that exists", () => {
50+
const input = { path: "/test/path" };
51+
vi.mocked(runtimeConfig.lstatSync).mockReturnValue({ size: 100 } as any);
52+
53+
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.LSTAT);
54+
expect(runtimeConfig.lstatSync).toHaveBeenCalledWith("/test/path");
55+
});
56+
57+
it("should return undefined for input with path that throws error", () => {
58+
const input = { path: "/test/path" };
59+
vi.mocked(runtimeConfig.lstatSync).mockImplementation(() => {
60+
throw new Error("File not found");
61+
});
62+
63+
expect(byteLengthSource(input)).toBeUndefined();
64+
});
65+
66+
it("should return undefined for input with no matching properties", () => {
67+
const input = { foo: "bar" };
68+
expect(byteLengthSource(input)).toBeUndefined();
69+
});
70+
});

lib/lib-storage/src/chunks/getChunkUint8Array.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, test as it } from "vitest";
22

3-
import { byteLength } from "../bytelength";
3+
import { byteLength } from "../byteLength";
4+
import { BYTE_LENGTH_SOURCE, byteLengthSource } from "../byteLengthSource";
45
import { RawDataPart } from "../Upload";
56
import { getChunkUint8Array } from "./getChunkUint8Array";
67

@@ -21,6 +22,7 @@ describe(getChunkUint8Array.name, () => {
2122
for await (const chunk of chunker) {
2223
chunkNum += 1;
2324
expect(byteLength(chunk.data)).toEqual(chunklength);
25+
expect(byteLengthSource(chunk.data)).toEqual(BYTE_LENGTH_SOURCE.TYPED_ARRAY);
2426
expect(chunk.partNumber).toEqual(chunkNum);
2527
if (chunkNum < expectedNumberOfChunks) {
2628
expect(chunk.lastPart).toBe(undefined);
@@ -45,10 +47,13 @@ describe(getChunkUint8Array.name, () => {
4547

4648
expect(chunks.length).toEqual(3);
4749
expect(byteLength(chunks[0].data)).toBe(chunklength);
50+
expect(byteLengthSource(chunks[0].data)).toEqual(BYTE_LENGTH_SOURCE.TYPED_ARRAY);
4851
expect(chunks[0].lastPart).toBe(undefined);
4952
expect(byteLength(chunks[1].data)).toBe(chunklength);
53+
expect(byteLengthSource(chunks[1].data)).toEqual(BYTE_LENGTH_SOURCE.TYPED_ARRAY);
5054
expect(chunks[1].lastPart).toBe(undefined);
5155
expect(byteLength(chunks[2].data)).toBe(totalLength % chunklength);
56+
expect(byteLengthSource(chunks[2].data)).toEqual(BYTE_LENGTH_SOURCE.TYPED_ARRAY);
5257
expect(chunks[2].lastPart).toBe(true);
5358
});
5459

@@ -65,6 +70,7 @@ describe(getChunkUint8Array.name, () => {
6570

6671
expect(chunks.length).toEqual(1);
6772
expect(byteLength(chunks[0].data)).toBe(totalLength % chunklength);
73+
expect(byteLengthSource(chunks[0].data)).toEqual(BYTE_LENGTH_SOURCE.TYPED_ARRAY);
6874
expect(chunks[0].lastPart).toBe(true);
6975
});
7076
});

lib/lib-storage/src/chunks/getDataReadable.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Readable } from "stream";
22
import { describe, expect, test as it } from "vitest";
33

4-
import { byteLength } from "../bytelength";
4+
import { byteLength } from "../byteLength";
55
import { RawDataPart as DataPart } from "../Upload";
66
import { getChunkStream as chunkFromReadable } from "./getChunkStream";
77
import { getDataReadable } from "./getDataReadable";

lib/lib-storage/src/chunks/getDataReadableStream.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, test as it } from "vitest";
22
// polyfill exposes the same ReadableStream API as web, allowing easy testing
33
import { ReadableStream } from "web-streams-polyfill";
44

5-
import { byteLength } from "../bytelength";
5+
import { byteLength } from "../byteLength";
66
import { RawDataPart as DataPart } from "../Upload";
77
import { getChunkStream as chunkFromReadable } from "./getChunkStream";
88
import { getDataReadableStream } from "./getDataReadableStream";

0 commit comments

Comments
 (0)