|
1 | | -import { expect, it } from "vitest"; |
| 1 | +import { describe, expect, it } from "vitest"; |
2 | 2 |
|
3 | 3 | import { decodeBase64, encodeBase64 } from "../src/base64.js"; |
4 | | -import { BASE64_INDEX } from "../src/constant.js"; |
5 | 4 |
|
6 | | -it("Have correct constant", () => { |
7 | | - expect(BASE64_INDEX).toEqual([ |
8 | | - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
9 | | - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
10 | | - -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 54, 55, 56, 57, 58, 59, 60, 61, 62, |
11 | | - 63, -1, -1, -1, -1, -1, -1, -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
12 | | - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1, |
13 | | - 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, |
14 | | - 47, 48, 49, 50, 51, 52, 53, -1, -1, -1, -1, -1, |
15 | | - ]); |
16 | | -}); |
| 5 | +describe("encodeBase64", () => { |
| 6 | + it("should encode byte array to base64 string", () => { |
| 7 | + expect( |
| 8 | + encodeBase64( |
| 9 | + [ |
| 10 | + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, |
| 11 | + 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 12 | + ], |
| 13 | + 16, |
| 14 | + ), |
| 15 | + ).toEqual("..CA.uOD/eaGAOmJB.yMBu"); |
| 16 | + }); |
17 | 17 |
|
18 | | -it("Encode Base64", () => { |
19 | | - expect( |
20 | | - encodeBase64( |
21 | | - [ |
22 | | - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
23 | | - 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
24 | | - ], |
25 | | - 16, |
26 | | - ), |
27 | | - ).toEqual("..CA.uOD/eaGAOmJB.yMBu"); |
| 18 | + it("should throw error for invalid length", () => { |
| 19 | + expect(() => encodeBase64([1, 2, 3], 0)).toThrow("Illegal length: 0"); |
| 20 | + expect(() => encodeBase64([1, 2, 3], -1)).toThrow("Illegal length: -1"); |
| 21 | + expect(() => encodeBase64([1, 2, 3], 5)).toThrow("Illegal length: 5"); |
| 22 | + }); |
28 | 23 | }); |
29 | 24 |
|
30 | | -it("Decode Base64", () => { |
31 | | - expect(decodeBase64("..CA.uOD/eaGAOmJB.yMBv.", 16)).toEqual([ |
32 | | - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
33 | | - 0x0c, 0x0d, 0x0e, 0x0f, |
34 | | - ]); |
| 25 | +describe("decodeBase64", () => { |
| 26 | + it("should decode base64 string to byte array", () => { |
| 27 | + expect(decodeBase64("..CA.uOD/eaGAOmJB.yMBv.", 16)).toEqual([ |
| 28 | + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
| 29 | + 0x0c, 0x0d, 0x0e, 0x0f, |
| 30 | + ]); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should handle invalid characters gracefully", () => { |
| 34 | + const result = decodeBase64("invalid@char", 5); |
| 35 | + |
| 36 | + expect(result.length).toBeLessThanOrEqual(5); |
| 37 | + }); |
35 | 38 | }); |
0 commit comments