Skip to content

Commit 444bf79

Browse files
committed
frontend: add unit tests for base58.ts
1 parent 9efc6a1 commit 444bf79

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

frontend/src/base58.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { encodeBase58Flickr } from './base58';
3+
4+
// Alphabet used by encodeBase58Flickr for reference:
5+
// '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
6+
7+
describe('encodeBase58Flickr', () => {
8+
it('encodes empty input as "1"', () => {
9+
const input = new Uint8Array([]);
10+
expect(encodeBase58Flickr(input)).toBe('1');
11+
});
12+
13+
it('encodes a single zero byte as "1"', () => {
14+
const input = new Uint8Array([0x00]);
15+
expect(encodeBase58Flickr(input)).toBe('1');
16+
});
17+
18+
it('encodes multiple zero bytes as a single "1" (value is zero)', () => {
19+
const input = new Uint8Array([0x00, 0x00]);
20+
// value remains 0n -> early return
21+
expect(encodeBase58Flickr(input)).toBe('1');
22+
});
23+
24+
it('preserves leading zeroes as leading "1" characters', () => {
25+
const input = new Uint8Array([0x00, 0xff]);
26+
// 0xff -> base58 Flickr is '5p' and one leading zero -> '1' prefix
27+
expect(encodeBase58Flickr(input)).toBe('15p');
28+
});
29+
30+
it('encodes a non-zero single byte correctly', () => {
31+
const input = new Uint8Array([0xff]);
32+
// 255 -> 58*4=232 remainder 23 -> indices [4,23] => '5p'
33+
expect(encodeBase58Flickr(input)).toBe('5p');
34+
});
35+
36+
it('encodes multiple bytes without leading zeros', () => {
37+
const input = new Uint8Array([0x01, 0x02]); // 258 decimal
38+
// 258 -> 58*4=232 remainder 26 -> indices [4,26] => '5s'
39+
expect(encodeBase58Flickr(input)).toBe('5s');
40+
});
41+
42+
it('handles multiple leading zeroes (prefix with multiple 1s)', () => {
43+
const input = new Uint8Array([0x00, 0x00, 0x01]);
44+
// value=1 -> '2', with two leading zeros -> '112'
45+
expect(encodeBase58Flickr(input)).toBe('112');
46+
});
47+
48+
it('encodes 58 correctly (two digits)', () => {
49+
const input = new Uint8Array([0x3a]); // 58 decimal
50+
// 58 -> digits [0,1] => '21' with Flickr alphabet
51+
expect(encodeBase58Flickr(input)).toBe('21');
52+
});
53+
54+
it('encodes 256 correctly and with leading zeros', () => {
55+
const noLeading = new Uint8Array([0x01, 0x00]); // 256 decimal -> '5q'
56+
expect(encodeBase58Flickr(noLeading)).toBe('5q');
57+
58+
const withLeading = new Uint8Array([0x00, 0x00, 0x01, 0x00]); // two leading zeros + 256 -> '115q'
59+
expect(encodeBase58Flickr(withLeading)).toBe('115q');
60+
});
61+
62+
it('encodes with multiple leading zeros before 0xff (255)', () => {
63+
const input = new Uint8Array([0x00, 0x00, 0xff]);
64+
// base value 255 => '5p', two leading zeros -> prefix '11'
65+
expect(encodeBase58Flickr(input)).toBe('115p');
66+
});
67+
});

0 commit comments

Comments
 (0)