Files
it-tools/src/tools/bcrypt/bcrypt.models.test.ts
T
lionel-roweandShareVB 102cfce37b Use async versions of bcrypt methods to avoid freezing browser tab (#204)
* Use async versions of bcrypt methods to avoid freezing browser tab

* Update bcryptjs to 3.0.3 and remove setInterval workaround

* extract tools strings enhancements

* i18n plugin optimization
---------

Co-authored-by: ShareVB <sharevb@gmail.com>
2025-11-13 00:55:29 +01:00

54 lines
1.8 KiB
TypeScript

import { compare, hash } from 'bcryptjs';
import { assert, describe, expect, test } from 'vitest';
import { installAbortSignalPolyfill } from 'abort-signal-polyfill';
import { type Update, bcryptWithProgressUpdates } from './bcrypt.models';
// Install the polyfill
installAbortSignalPolyfill();
// simplified polyfill for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fromAsync
async function fromAsync<T>(iter: AsyncIterable<T>) {
const out: T[] = [];
for await (const val of iter) {
out.push(val);
}
return out;
}
function checkProgressAndGetResult<T>(updates: Update<T>[]) {
const first = updates.at(0);
const penultimate = updates.at(-2);
const last = updates.at(-1);
const allExceptLast = updates.slice(0, -1);
expect(allExceptLast.every(x => x.kind === 'progress')).toBeTruthy();
expect(first).toEqual({ kind: 'progress', progress: 0 });
expect(penultimate).toEqual({ kind: 'progress', progress: 1 });
assert(last != null && last.kind === 'success');
return last;
}
describe('bcrypt models', () => {
describe(bcryptWithProgressUpdates.name, () => {
test('with bcrypt hash function', async () => {
const updates = await fromAsync(bcryptWithProgressUpdates(hash, ['abc', 5]));
const result = checkProgressAndGetResult(updates);
expect(result.value).toMatch(/^\$2[ab]\$05\$.{53}$/);
expect(result.timeTakenMs).toBeGreaterThan(0);
});
test('with bcrypt compare function', async () => {
const updates = await fromAsync(
bcryptWithProgressUpdates(compare, ['abc', '$2a$05$FHzYelm8Qn.IhGP.N8V1TOWFlRTK.8cphbxZSvSFo9B6HGscnQdhy']),
);
const result = checkProgressAndGetResult(updates);
expect(result.value).toBe(true);
expect(result.timeTakenMs).toBeGreaterThan(0);
});
});
});