|
| 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 | +}); |
0 commit comments