import { test } from 'node:test' import assert from 'node:assert/strict' import { createHash } from 'node:crypto' import { hashStudyPassword, verifyStudyPassword, studyPasswordNeedsRehash } from '../server/study-helpers.js' test('new hashes are salted scrypt and verify', () => { const a = hashStudyPassword('correct horse') const b = hashStudyPassword('correct horse') assert.match(a, /^scrypt\$[0-9a-f]+\$[0-9a-f]+$/) assert.notEqual(a, b) assert.ok(verifyStudyPassword('correct horse', a)) assert.ok(!verifyStudyPassword('wrong', a)) assert.ok(!studyPasswordNeedsRehash(a)) }) test('legacy SHA-256 hashes still verify and are flagged for rehash', () => { const legacy = createHash('sha256').update('study-user:hunter2hunter2').digest('hex') assert.ok(verifyStudyPassword('hunter2hunter2', legacy)) assert.ok(!verifyStudyPassword('nope', legacy)) assert.ok(studyPasswordNeedsRehash(legacy)) }) test('malformed stored values never verify', () => { for (const bad of ['', undefined, null, 'scrypt$', 'scrypt$zz$', 'abc']) { assert.ok(!verifyStudyPassword('x', bad)) } })