feat(shared): lift recovery-code primitives into shared crypto module

This commit is contained in:
byGalax
2026-05-15 21:54:57 +02:00
parent d9f3c6c562
commit eef884c782
7 changed files with 96 additions and 1 deletions
@@ -0,0 +1,29 @@
import { describe, expect, it, beforeAll } from 'vitest';
import { setCryptoBackend } from './backend';
import { makeWasmTestBackend } from './testBackend';
import { generateRecoveryCode, normalizeRecoveryCode, RECOVERY_CODE_LEN } from './recoveryCode';
beforeAll(async () => { setCryptoBackend(await makeWasmTestBackend()); });
describe('recoveryCode', () => {
it('generates a 24-char alphabet-restricted code grouped 4×6 with dashes', async () => {
const code = await generateRecoveryCode();
expect(code).toHaveLength(27);
const groups = code.split('-');
expect(groups).toHaveLength(4);
for (const g of groups) expect(g).toMatch(/^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/);
});
it('normalizeRecoveryCode strips dashes/spaces and uppercases', () => {
expect(normalizeRecoveryCode(' abcdef-ghjklm-npqrst-uvwxyz ')).toBe('ABCDEFGHJKLMNPQRSTUVWXYZ');
});
it('normalizeRecoveryCode drops characters outside the alphabet', () => {
expect(normalizeRecoveryCode('AB1OD-EF0H1J')).toBe('ABDEFHJ');
});
it('RECOVERY_CODE_LEN matches alphabet length', () => {
expect(RECOVERY_CODE_LEN).toBe(24);
});
});