Files
smarthash/test/test.browser.ts

77 lines
2.5 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();
});
tap.test('sha256 produces consistent results across environments', async () => {
// Test that our implementation produces the same hash as Node.js crypto
const testHash = await smarthash.sha256FromString('test');
const expectedHash = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08';
expect(testHash).toEqual(expectedHash);
// Test with different string
const testHash2 = await smarthash.sha256FromString('hello world');
const expectedHash2 = 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
expect(testHash2).toEqual(expectedHash2);
});
export default tap.start();