import * as bcrypt from 'bcrypt';
import { compareHash, compareStoredHighEntropyValue, hashHighEntropyValue, hashValue } from './hash.util';

describe('hash utilities', () => {
  it('hashes and verifies passwords with bcrypt', async () => {
    const hash = await hashValue('secret', 4);
    await expect(compareHash('secret', hash)).resolves.toBe(true);
    await expect(compareHash('wrong', hash)).resolves.toBe(false);
  });

  it('uses deterministic secret-keyed hashes for high entropy tokens', async () => {
    const stored = hashHighEntropyValue('token', 'pepper');
    expect(stored).toMatch(/^sha256:[0-9a-f]{64}$/);
    await expect(compareStoredHighEntropyValue('token', stored, 'pepper')).resolves.toBe(true);
    await expect(compareStoredHighEntropyValue('other', stored, 'pepper')).resolves.toBe(false);
  });

  it('supports legacy bcrypt token hashes', async () => {
    const stored = await bcrypt.hash('legacy-token', 4);
    await expect(compareStoredHighEntropyValue('legacy-token', stored, 'unused')).resolves.toBe(true);
  });
});
