Skip to content
markpaper

src/ws/userSet.test.ts

v0.3.0 · 1.5 KB

Download file
import { describe, expect, it } from 'vitest';
import { diffUserSets, planUserSlots } from './userSet.js';

const a = '0x' + 'a'.repeat(40);
const b = '0x' + 'b'.repeat(40);
const c = '0x' + 'c'.repeat(40);

describe('planUserSlots', () => {
  it('gives slots by input priority and sends the overflow to REST', () => {
    expect(planUserSlots([c, a, b], 2)).toEqual({ ws: [a, c], poll: [b], invalid: [] });
  });

  it('dedupes in lowercase and sorts the WS set deterministically', () => {
    const upperA = '0x' + 'A'.repeat(40);
    const p1 = planUserSlots([b, upperA, a], 5);
    const p2 = planUserSlots([a, b], 5);
    expect(p1.ws).toEqual([a, b]);
    expect(p1.ws).toEqual(p2.ws);
  });

  it('never plans placeholders or malformed addresses', () => {
    expect(planUserSlots(['0xYOUR_ADDRESS', '0x...', a], 10)).toEqual({ ws: [a], poll: [], invalid: ['0xYOUR_ADDRESS', '0x...'] });
  });

  it('handles zero / fractional / negative capacity', () => {
    expect(planUserSlots([a, b], 0)).toEqual({ ws: [], poll: [a, b], invalid: [] });
    expect(planUserSlots([a, b], 1.9).ws).toEqual([a]);
    expect(planUserSlots([a], -1).poll).toEqual([a]);
  });
});

describe('diffUserSets', () => {
  it('computes additions and removals', () => {
    expect(diffUserSets([a, b], [b, c])).toEqual({ add: [c], remove: [a], unchanged: false });
  });

  it('same membership in another order or case is a no-op', () => {
    expect(diffUserSets([a, b], [b.toUpperCase().replace('0X', '0x'), a])).toEqual({ add: [], remove: [], unchanged: true });
  });
});
All files