feat: Merge isohash functionality into smarthash for cross-environment hash support
- Updated test files to use new tapbundle import from @git.zone/tstest. - Created a new plan for merging isohash into smarthash, detailing objectives and implementation steps. - Added browser-specific tests for SHA256 hashing functions in test/test.browser.ts. - Implemented browser-compatible hashing functions in ts_web/index.ts using Web Crypto API. - Introduced plugins for environment detection and JSON handling in ts_web/plugins.ts. - Ensured that existing smarthash functionality remains intact and consistent across environments.
This commit is contained in:
65
test/test.browser.ts
Normal file
65
test/test.browser.ts
Normal file
@ -0,0 +1,65 @@
|
||||
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();
|
Reference in New Issue
Block a user