65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
![]() |
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||
|
import * as smarthash from '../ts_web/index.js';
|
||
|
|
||
|
tap.test('sha256FromString should work in browser environment', async () => {
|
||
|
const testHash = await smarthash.sha256FromString('test');
|
||
|
const testHash2 = await smarthash.sha256FromString('testString');
|
||
|
const testHash3 = await smarthash.sha256FromString('test');
|
||
|
|
||
|
expect(testHash).toEqual(testHash3);
|
||
|
expect(testHash).not.toEqual(testHash2);
|
||
|
expect(testHash).toBeTypeofString();
|
||
|
expect(testHash).toHaveLength(64); // SHA256 hash is 64 hex characters
|
||
|
});
|
||
|
|
||
|
tap.test('sha256FromBuffer should work with Uint8Array in browser', async () => {
|
||
|
const encoder = new TextEncoder();
|
||
|
const buffer = encoder.encode('test');
|
||
|
|
||
|
const hashFromBuffer = await smarthash.sha256FromBuffer(buffer);
|
||
|
const hashFromString = await smarthash.sha256FromString('test');
|
||
|
|
||
|
expect(hashFromBuffer).toEqual(hashFromString);
|
||
|
});
|
||
|
|
||
|
tap.test('sha265FromObject should produce reproducible hashes', async () => {
|
||
|
const hash1 = await smarthash.sha265FromObject({
|
||
|
hithere: 1,
|
||
|
wow: 'two',
|
||
|
});
|
||
|
|
||
|
const hash2 = await smarthash.sha265FromObject({
|
||
|
wow: 'two',
|
||
|
hithere: 1,
|
||
|
});
|
||
|
|
||
|
const hash3 = await smarthash.sha265FromObject({
|
||
|
wow: 'twoe',
|
||
|
hithere: 1,
|
||
|
});
|
||
|
|
||
|
expect(hash1).toEqual(hash2);
|
||
|
expect(hash1).not.toEqual(hash3);
|
||
|
});
|
||
|
|
||
|
tap.test('sha256FromStringSync should throw in browser environment', async () => {
|
||
|
expect(() => {
|
||
|
smarthash.sha256FromStringSync('test');
|
||
|
}).toThrow();
|
||
|
});
|
||
|
|
||
|
tap.test('sha256FromStream should throw in browser environment', async () => {
|
||
|
expect(() => {
|
||
|
smarthash.sha256FromStream(null);
|
||
|
}).toThrow();
|
||
|
});
|
||
|
|
||
|
tap.test('sha256FromFile should throw in browser environment', async () => {
|
||
|
await expect(smarthash.sha256FromFile('./test.txt')).rejects.toThrow();
|
||
|
});
|
||
|
|
||
|
tap.test('md5FromString should throw in browser environment', async () => {
|
||
|
await expect(smarthash.md5FromString('test')).rejects.toThrow();
|
||
|
});
|
||
|
|
||
|
export default tap.start();
|