diff --git a/src/tools/hash-text/hash-text.e2e.spec.ts b/src/tools/hash-text/hash-text.e2e.spec.ts new file mode 100644 index 00000000..c9fc26ec --- /dev/null +++ b/src/tools/hash-text/hash-text.e2e.spec.ts @@ -0,0 +1,58 @@ +import { Buffer } from 'node:buffer'; +import { expect, test } from '@playwright/test'; + +test.describe('Tool - Hash text', () => { + test('updates the added hashes when the digest encoding or input changes', async ({ page }) => { + await page.goto('/hash-text?encoding=Hex'); + await page.getByPlaceholder('Your string to hash...').fill('hello world'); + + const algorithms = [ + 'ADLER32', + 'CRC32', + 'BLAKE2B', + 'BLAKE2S', + 'BLAKE3', + 'KECCAK', + 'XXHASH32', + 'XXHASH64', + 'XXHASH3', + 'XXHASH128', + 'SM3', + 'WHIRLPOOL', + ]; + const digestInput = (algorithm: string) => + page + .locator('.n-input-group') + .filter({ has: page.getByText(algorithm, { exact: true }) }) + .getByRole('textbox'); + + await expect(digestInput('ADLER32')).toHaveValue('1a0b045d'); + const hexDigests = new Map(); + for (const algorithm of algorithms) { + await expect(digestInput(algorithm)).toHaveValue(/^[0-9a-f]+$/); + hexDigests.set(algorithm, await digestInput(algorithm).inputValue()); + } + + const encodings = [ + { + label: 'Binary (base 2)', + encode: (bytes: Buffer) => [...bytes].map((byte) => byte.toString(2).padStart(8, '0')).join(''), + }, + { label: 'Base64 (base 64)', encode: (bytes: Buffer) => bytes.toString('base64') }, + { label: 'Base64url (base 64 with url safe chars)', encode: (bytes: Buffer) => bytes.toString('base64url') }, + { label: 'Hexadecimal (base 16)', encode: (bytes: Buffer) => bytes.toString('hex') }, + ]; + let currentLabel = 'Hexadecimal (base 16)'; + for (const { label, encode } of encodings) { + await page.getByText(currentLabel, { exact: true }).and(page.locator('span')).click(); + await page.getByText(label, { exact: true }).and(page.locator('.c-select-dropdown-option')).click(); + for (const algorithm of algorithms) { + await expect(digestInput(algorithm)).toHaveValue(encode(Buffer.from(hexDigests.get(algorithm)!, 'hex'))); + } + currentLabel = label; + } + + await page.getByPlaceholder('Your string to hash...').fill('abc'); + await expect(digestInput('ADLER32')).toHaveValue('024d0127'); + }); +}); diff --git a/src/tools/hash-text/hash-text.vue b/src/tools/hash-text/hash-text.vue index df2bec8d..1b419619 100644 --- a/src/tools/hash-text/hash-text.vue +++ b/src/tools/hash-text/hash-text.vue @@ -70,7 +70,7 @@ const defaultHashWasmValues = { const hashWasmValues = computedAsync(async () => { const encodingValue = encoding.value; const clearTextValue = clearText.value; - const ret = defaultHashWasmValues; + const ret = { ...defaultHashWasmValues }; for (const algo of algoWasmNames) { ret[algo] = formatHexWithEncoding(await algosWasm[algo](clearTextValue), encodingValue); }