📄 src/web3/utils/hash.test.ts
D-OPEN SOVEREIGN
1import { describe, it, expect } from 'vitest';
2import { hashUsernameToCoords } from './hash';
3
4describe('hashUsernameToCoords', () => {
5  it('returns 4 points', () => {
6    const points = hashUsernameToCoords('alice', 200, 100);
7    expect(points).toHaveLength(4);
8  });
9
10  it('is deterministic for the same username and dimensions', () => {
11    const a = hashUsernameToCoords('alice', 200, 100);
12    const b = hashUsernameToCoords('alice', 200, 100);
13    expect(a).toEqual(b);
14  });
15
16  it('produces different coordinates for different usernames', () => {
17    const a = hashUsernameToCoords('alice', 200, 100);
18    const b = hashUsernameToCoords('bob', 200, 100);
19    expect(a).not.toEqual(b);
20  });
21
22  it('keeps every point within the padded container bounds', () => {
23    const width = 200;
24    const height = 100;
25    const padding = 16;
26    const points = hashUsernameToCoords('someone', width, height, padding);
27
28    for (const { x, y } of points) {
29      expect(x).toBeGreaterThanOrEqual(padding);
30      expect(x).toBeLessThanOrEqual(width - padding);
31      expect(y).toBeGreaterThanOrEqual(padding);
32      expect(y).toBeLessThanOrEqual(height - padding);
33    }
34  });
35
36  it('does not throw when padding leaves no inner space', () => {
37    const points = hashUsernameToCoords('tiny', 20, 20, 16);
38    expect(points).toHaveLength(4);
39    for (const { x, y } of points) {
40      expect(Number.isFinite(x)).toBe(true);
41      expect(Number.isFinite(y)).toBe(true);
42    }
43  });
44});
45