Suite of AT Protocol TypeScript libraries built on web standards
1import bases from "../util.ts";
2import { fromString, type SupportedEncodings, toString } from "../string.ts";
3import { assert, assertEquals, assertThrows } from "@std/assert";
4
5const supportedBases = Object.keys(bases) as SupportedEncodings[];
6
7Deno.test("fromString creates a Uint8Array from a string", () => {
8 const str = "hello world";
9 const arr = new TextEncoder().encode(str);
10
11 assertEquals(fromString(str), arr);
12});
13
14supportedBases.filter((base) => base !== "base256emoji").forEach((base) => {
15 Deno.test(`fromString creates a Uint8Array from a ${base} string`, () => {
16 const arr = Uint8Array.from([0, 1, 2, 3]);
17 const str = toString(arr, base);
18
19 assertEquals(fromString(str, base), arr);
20 });
21});
22
23Deno.test("fromString creates a Uint8Array from a base64 string with non-printable utf8 characters", () => {
24 const str = "AAECA6q7zA";
25 const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]);
26
27 assertEquals(fromString(str, "base64"), arr);
28});
29
30Deno.test("fromString creates a Uint8Array from an ascii string", () => {
31 const str = [
32 String.fromCharCode(0),
33 String.fromCharCode(1),
34 String.fromCharCode(2),
35 String.fromCharCode(3),
36 String.fromCharCode(4),
37 ].join("");
38 const arr = Uint8Array.from([0, 1, 2, 3, 4]);
39
40 assertEquals(fromString(str, "ascii"), arr);
41});
42
43Deno.test("fromString throws when an unknown base is passed", () => {
44 const str = "hello world";
45
46 // @ts-expect-error 'derp' is not a valid encoding
47 assertThrows(() => fromString(str, "derp"), /Unsupported encoding/);
48});
49
50Deno.test("fromString returns Uint8Array", () => {
51 const a = fromString("derp");
52 const slice = a.slice();
53
54 // node slice is a copy operation, Uint8Array slice is a no-copy operation
55 assert(slice.buffer !== a.buffer);
56});
57
58Deno.test("toString creates a String from a Uint8Array", () => {
59 const str = "hello world";
60 const arr = new TextEncoder().encode(str);
61
62 assertEquals(toString(arr), str);
63});
64
65Deno.test("toString creates a hex string from a Uint8Array with non-printable utf8 characters", () => {
66 const str = "00010203aabbcc";
67 const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]);
68
69 assertEquals(toString(arr, "base16"), str);
70});
71
72Deno.test("toString creates a base32 string from a Uint8Array with non-printable utf8 characters", () => {
73 const str = "aaaqea5kxpga";
74 const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]);
75
76 assertEquals(toString(arr, "base32"), str);
77});
78
79Deno.test("toString creates a base36 string from a Uint8Array with non-printable utf8 characters", () => {
80 const str = "0e52zorf0";
81 const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]);
82
83 assertEquals(toString(arr, "base36"), str);
84});
85
86Deno.test("toString creates a base64 string from a Uint8Array with non-printable utf8 characters", () => {
87 const str = "AAECA6q7zA";
88 const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]);
89
90 assertEquals(toString(arr, "base64"), str);
91});
92
93Deno.test("toString creates an ascii string from a Uint8Array", () => {
94 const str = [
95 String.fromCharCode(0),
96 String.fromCharCode(1),
97 String.fromCharCode(2),
98 String.fromCharCode(3),
99 String.fromCharCode(4),
100 ].join("");
101 const arr = Uint8Array.from([0, 1, 2, 3, 4]);
102
103 assertEquals(toString(arr, "ascii"), str);
104});
105
106Deno.test("toString throws when an unknown base is passed", () => {
107 const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]);
108
109 // @ts-expect-error 'derp' is not a valid encoding
110 assertThrows(() => toString(arr, "derp"), /Unsupported encoding/);
111});