// A modem's code (modemCode.ts): six characters derived from the project and // the modem, the same wherever it is derived; read back as people type it. import { describe, expect, it } from 'vitest'; import { MODEM_CODE_ALPHABET, MODEM_CODE_LENGTH, modemCode, modemCodeOf, modemCodeText } from '../src/index.js'; const uuid = (number: number) => `00000000-0000-4000-8000-${number.toString(16).padStart(12, '0')}`; describe("a modem's code", () => { it('is six characters one does not misread, the same for the same modem', () => { const code = modemCode(uuid(1), uuid(2)); expect(code).toMatch(new RegExp(`^[${MODEM_CODE_ALPHABET}]{${MODEM_CODE_LENGTH}}$`)); expect(MODEM_CODE_ALPHABET).not.toMatch(/[01IO]/); expect(modemCode(uuid(1), uuid(2))).toBe(code); // Pinned: the editor and server F must derive the same code from the same project and modem. expect(code).toBe('VKUZ5Q'); }); it('differs for a copy of the project and for another modem, and spreads over every character', () => { expect(modemCode(uuid(1), uuid(2))).not.toBe(modemCode(uuid(3), uuid(2))); expect(modemCode(uuid(1), uuid(2))).not.toBe(modemCode(uuid(1), uuid(3))); const codes = Array.from({ length: 20_000 }, (_, index) => modemCode(uuid(index), uuid(7))); // 2^30 codes: 20 000 projects meet a handful of times at most. expect(new Set(codes).size).toBeGreaterThan(19_990); for (let position = 0; position < MODEM_CODE_LENGTH; position += 1) { const counts = new Map(); for (const code of codes) counts.set(code[position]!, (counts.get(code[position]!) ?? 0) + 1); expect(counts.size, `characters at ${position}`).toBe(MODEM_CODE_ALPHABET.length); // Each character about 625 times; none below half of that. expect(Math.min(...counts.values()), `rarest character at ${position}`).toBeGreaterThan(300); } }); it('is read as typed, without spaces, dashes and small letters, and written in two groups', () => { expect(modemCodeOf(' k7f-3qx ')).toBe('K7F3QX'); expect(modemCodeOf('K7F 3QX')).toBe('K7F3QX'); for (const text of ['', 'K7F3Q', 'K7F3QXY', 'K7F3Q0', 'K7F3QO', 'K7F3Q1', 'K7F3QI', 'K7F3Q!']) expect(modemCodeOf(text), text).toBeUndefined(); expect(modemCodeText('K7F3QX')).toBe('K7F-3QX'); }); });