97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
|
|
import { describe, expect, test } from 'bun:test';
|
|||
|
|
import {
|
|||
|
|
bytesBin,
|
|||
|
|
laneCountBin,
|
|||
|
|
peerHash,
|
|||
|
|
safeAttribute,
|
|||
|
|
UnsafeAttributeError,
|
|||
|
|
} from '../src/index.ts';
|
|||
|
|
|
|||
|
|
describe('peerHash', () => {
|
|||
|
|
test('produces stable 8-char hex', () => {
|
|||
|
|
const a = peerHash('alice@example.com');
|
|||
|
|
const b = peerHash('alice@example.com');
|
|||
|
|
expect(a).toBe(b);
|
|||
|
|
expect(a).toMatch(/^[0-9a-f]{8}$/);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
test('different addresses produce different hashes', () => {
|
|||
|
|
expect(peerHash('alice@example.com')).not.toBe(peerHash('bob@example.com'));
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
test('never echoes the address', () => {
|
|||
|
|
const addr = 'alice@example.com';
|
|||
|
|
expect(peerHash(addr).includes('alice')).toBe(false);
|
|||
|
|
expect(peerHash(addr).includes('@')).toBe(false);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe('bytesBin', () => {
|
|||
|
|
test('bins by order of magnitude', () => {
|
|||
|
|
expect(bytesBin(0)).toBe('≤4KB');
|
|||
|
|
expect(bytesBin(4096)).toBe('≤4KB');
|
|||
|
|
expect(bytesBin(4097)).toBe('4–64KB');
|
|||
|
|
expect(bytesBin(64 * 1024)).toBe('4–64KB');
|
|||
|
|
expect(bytesBin(64 * 1024 + 1)).toBe('64KB–1MB');
|
|||
|
|
expect(bytesBin(1024 * 1024)).toBe('64KB–1MB');
|
|||
|
|
expect(bytesBin(10 * 1024 * 1024)).toBe('1–10MB');
|
|||
|
|
expect(bytesBin(100 * 1024 * 1024)).toBe('10–100MB');
|
|||
|
|
expect(bytesBin(1024 * 1024 * 1024)).toBe('100MB–1GB');
|
|||
|
|
expect(bytesBin(2 * 1024 * 1024 * 1024)).toBe('≥1GB');
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
test('handles invalid input', () => {
|
|||
|
|
expect(bytesBin(-1)).toBe('unknown');
|
|||
|
|
expect(bytesBin(NaN)).toBe('unknown');
|
|||
|
|
expect(bytesBin(Infinity)).toBe('unknown');
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe('laneCountBin', () => {
|
|||
|
|
test('snaps to {1, 4, 16, 64}', () => {
|
|||
|
|
expect(laneCountBin(1)).toBe(1);
|
|||
|
|
expect(laneCountBin(2)).toBe(4);
|
|||
|
|
expect(laneCountBin(4)).toBe(4);
|
|||
|
|
expect(laneCountBin(5)).toBe(16);
|
|||
|
|
expect(laneCountBin(16)).toBe(16);
|
|||
|
|
expect(laneCountBin(32)).toBe(64);
|
|||
|
|
expect(laneCountBin(128)).toBe(64);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe('safeAttribute', () => {
|
|||
|
|
test('rejects PII-flavoured keys', () => {
|
|||
|
|
expect(() => safeAttribute('shade.peer.address', 'x')).toThrow(UnsafeAttributeError);
|
|||
|
|
expect(() => safeAttribute('shade.bytes.exact', 1)).toThrow(UnsafeAttributeError);
|
|||
|
|
expect(() => safeAttribute('shade.plaintext', 'x')).toThrow(UnsafeAttributeError);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
test('rejects address-like values', () => {
|
|||
|
|
expect(() => safeAttribute('custom.tag', 'alice@example.com')).toThrow(UnsafeAttributeError);
|
|||
|
|
expect(() => safeAttribute('custom.tag', 'device:abc-123')).toThrow(UnsafeAttributeError);
|
|||
|
|
expect(() => safeAttribute('custom.tag', 'did:web:example.com')).toThrow(UnsafeAttributeError);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
test('rejects oversized strings', () => {
|
|||
|
|
expect(() => safeAttribute('ok', 'x'.repeat(257))).toThrow(UnsafeAttributeError);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
test('accepts safe values', () => {
|
|||
|
|
expect(safeAttribute('shade.bytes.bin', '4–64KB')).toEqual({
|
|||
|
|
key: 'shade.bytes.bin',
|
|||
|
|
value: '4–64KB',
|
|||
|
|
});
|
|||
|
|
expect(safeAttribute('shade.lane.count', 4)).toEqual({ key: 'shade.lane.count', value: 4 });
|
|||
|
|
expect(safeAttribute('shade.retry.count', 0)).toEqual({ key: 'shade.retry.count', value: 0 });
|
|||
|
|
expect(safeAttribute('shade.error.code', 'SHADE_TIMEOUT')).toEqual({
|
|||
|
|
key: 'shade.error.code',
|
|||
|
|
value: 'SHADE_TIMEOUT',
|
|||
|
|
});
|
|||
|
|
// hashes pass through
|
|||
|
|
expect(safeAttribute('shade.peer.hash', 'abcdef01')).toEqual({
|
|||
|
|
key: 'shade.peer.hash',
|
|||
|
|
value: 'abcdef01',
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
});
|