|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { utf8ToBase64 } from "@hpcc-js/util"; |
| 3 | + |
| 4 | +describe("utf8ToBase64", () => { |
| 5 | + it("encodes ASCII input", () => { |
| 6 | + expect(utf8ToBase64("hello world")) |
| 7 | + .toEqual("aGVsbG8gd29ybGQ="); |
| 8 | + }); |
| 9 | + |
| 10 | + it("treats nullish values as empty string", () => { |
| 11 | + expect(utf8ToBase64(undefined as unknown as string)).toEqual(""); |
| 12 | + expect(utf8ToBase64(null as unknown as string)).toEqual(""); |
| 13 | + }); |
| 14 | + |
| 15 | + it("check frequest characters", () => { |
| 16 | + expect(utf8ToBase64("hello world")).toEqual(btoa("hello world")); |
| 17 | + expect(utf8ToBase64("g@s*!")).toEqual(btoa("g@s*!")); |
| 18 | + expect(utf8ToBase64("~@:!$%^&*()_-;'#***")).toEqual(btoa("~@:!$%^&*()_-;'#***")); |
| 19 | + expect(utf8ToBase64("!$%^&*()_-;'#:@~")).toEqual(btoa("!$%^&*()_-;'#:@~")); |
| 20 | + // expect(utf8ToBase64("¬!£$%^&*()_-;'#:@~")).toEqual(btoa("¬!£$%^&*()_-;'#:@~")); |
| 21 | + }); |
| 22 | + |
| 23 | + describe("incremental ASCII coverage", () => { |
| 24 | + const printableAscii = Array.from({ length: 95 }, (_, i) => String.fromCharCode(32 + i)).join(""); |
| 25 | + const checkpoints = [1, 5, 10, 20, 32, 48, 64, 80, 95]; |
| 26 | + const cases = checkpoints.map((len) => [len, printableAscii.slice(0, len)] as const); |
| 27 | + |
| 28 | + it.each(cases)("encodes first %i printable ASCII chars", (_length, sample) => { |
| 29 | + expect(utf8ToBase64(sample)).toEqual(btoa(sample)); |
| 30 | + }); |
| 31 | + }); |
| 32 | +}); |
0 commit comments