|
1 | | -import { describe, expect, it, vi } from "vitest"; |
| 1 | +import fs from "node:fs"; |
| 2 | +import { describe, expect, it } from "vitest"; |
2 | 3 |
|
3 | | -import { BYTE_LENGTH_SOURCE, byteLengthSource } from "./byteLengthSource"; |
4 | | -import { runtimeConfig } from "./runtimeConfig"; |
| 4 | +import { byteLength } from "./byteLength"; |
5 | 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); |
| 6 | +describe("byteLength", () => { |
| 7 | + it("should handle null and undefined input", () => { |
| 8 | + expect(byteLength(null)).toBe(0); |
| 9 | + expect(byteLength(undefined)).toBe(0); |
15 | 10 | }); |
16 | 11 |
|
17 | | - it("should return EMPTY_INPUT for null input", () => { |
18 | | - expect(byteLengthSource(null)).toBe(BYTE_LENGTH_SOURCE.EMPTY_INPUT); |
19 | | - }); |
| 12 | + describe("strings", () => { |
| 13 | + it("empty", () => { |
| 14 | + expect(byteLength("")).toBe(0); |
| 15 | + }); |
20 | 16 |
|
21 | | - it("should return EMPTY_INPUT for undefined input", () => { |
22 | | - expect(byteLengthSource(undefined)).toBe(BYTE_LENGTH_SOURCE.EMPTY_INPUT); |
23 | | - }); |
| 17 | + it("should return correct length for ASCII characters", () => { |
| 18 | + expect(byteLength("hello")).toBe(5); |
| 19 | + expect(byteLength("12345")).toBe(5); |
| 20 | + expect(byteLength("!@#$%")).toBe(5); |
| 21 | + }); |
24 | 22 |
|
25 | | - it("should return STRING_LENGTH for string input", () => { |
26 | | - expect(byteLengthSource("test")).toBe(BYTE_LENGTH_SOURCE.STRING_LENGTH); |
27 | | - }); |
| 23 | + it("should return correct length for unicode characters", () => { |
| 24 | + expect(byteLength("😀")).toBe(4); |
| 25 | + }); |
28 | 26 |
|
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); |
| 27 | + it("should handle mixed ASCII and unicode characters", () => { |
| 28 | + expect(byteLength("hello 世界")).toBe(12); |
| 29 | + expect(byteLength("hi 😀")).toBe(7); |
| 30 | + }); |
32 | 31 | }); |
33 | 32 |
|
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 | | - }); |
| 33 | + describe("byte arrays", () => { |
| 34 | + it("should handle Uint8Array", () => { |
| 35 | + expect(byteLength(new Uint8Array([1, 2, 3]))).toBe(3); |
| 36 | + expect(byteLength(new Uint8Array([]))).toBe(0); |
| 37 | + }); |
38 | 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); |
| 39 | + it("should handle Buffer", () => { |
| 40 | + expect(byteLength(Buffer.from([1, 2, 3]))).toBe(3); |
| 41 | + expect(byteLength(Buffer.from([]))).toBe(0); |
| 42 | + }); |
42 | 43 | }); |
43 | 44 |
|
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 | | - }); |
| 45 | + describe("things with length or size properties", () => { |
| 46 | + it("should handle arrays", () => { |
| 47 | + expect(byteLength([1, 2, 3])).toBe(3); |
| 48 | + expect(byteLength([])).toBe(0); |
| 49 | + }); |
48 | 50 |
|
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); |
| 51 | + it("should handle objects with length property", () => { |
| 52 | + expect(byteLength({ length: 5 })).toBe(5); |
| 53 | + expect(byteLength({ length: 0 })).toBe(0); |
| 54 | + }); |
52 | 55 |
|
53 | | - expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.LSTAT); |
54 | | - expect(runtimeConfig.lstatSync).toHaveBeenCalledWith("/test/path"); |
| 56 | + it("should handle objects with size property", () => { |
| 57 | + expect(byteLength({ size: 10 })).toBe(10); |
| 58 | + expect(byteLength({ size: 0 })).toBe(0); |
| 59 | + }); |
55 | 60 | }); |
56 | 61 |
|
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"); |
| 62 | + describe("start end differentials", () => { |
| 63 | + it("should handle readable streams", () => { |
| 64 | + const stream = fs.createReadStream(__filename, { |
| 65 | + start: 1000, |
| 66 | + end: 1499, |
| 67 | + }); |
| 68 | + expect(byteLength(stream)).toBe(500); |
61 | 69 | }); |
62 | | - |
63 | | - expect(byteLengthSource(input)).toBeUndefined(); |
64 | 70 | }); |
65 | 71 |
|
66 | | - it("should return undefined for input with no matching properties", () => { |
67 | | - const input = { foo: "bar" }; |
68 | | - expect(byteLengthSource(input)).toBeUndefined(); |
| 72 | + describe("filestreams", () => { |
| 73 | + it("should handle readable streams", () => { |
| 74 | + const stream = fs.createReadStream(__filename); |
| 75 | + expect(byteLength(stream)).toBe(fs.lstatSync(__filename).size); |
| 76 | + }); |
69 | 77 | }); |
70 | 78 | }); |
0 commit comments