"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const hashing_js_1 = require("./hashing.js");
(0, vitest_1.describe)('hashing', () => {
    (0, vitest_1.describe)('sha256Base64', () => {
        const testString = 'This is a test';
        const expectedHash = 'x74e2QL7jdTUiZfGRS9dflCfvNvigIsWvPTtzkwH0U4=';
        (0, vitest_1.it)('hashes a string correctly', async () => {
            const hash = await (0, hashing_js_1.sha256Base64)(testString);
            (0, vitest_1.expect)(hash).toEqual(expectedHash);
        });
        (0, vitest_1.it)('hashes a node.js buffer correctly', async () => {
            const buffer = Buffer.from(testString);
            const hash = await (0, hashing_js_1.sha256Base64)(buffer);
            (0, vitest_1.expect)(hash).toEqual(expectedHash);
        });
        (0, vitest_1.it)('hashes a Uint8Array correctly', async () => {
            const uint8Array = new TextEncoder().encode(testString);
            const hash = await (0, hashing_js_1.sha256Base64)(uint8Array);
            (0, vitest_1.expect)(hash).toEqual(expectedHash);
        });
        (0, vitest_1.it)('hashes a Uint16Array correctly', async () => {
            const uint8Array = new TextEncoder().encode(testString);
            // before we convert (the underlying buffer) to a Uint16Array, we need to make sure that the length is a multiple of 2
            (0, vitest_1.expect)(uint8Array.length % 2).toEqual(0);
            // create a new Uint16Array that is a view into the same underlying buffer
            const uint16Array = new Uint16Array(uint8Array.buffer, uint8Array.byteOffset, uint8Array.length / 2);
            const hash = await (0, hashing_js_1.sha256Base64)(uint16Array);
            (0, vitest_1.expect)(hash).toEqual(expectedHash);
        });
        (0, vitest_1.it)('hashes an ArrayBuffer correctly', async () => {
            const uint8Array = new TextEncoder().encode(testString);
            // Create a new array buffer that is the same length as the uint8Array.  We don't use the underlying buffer
            // of the uint8Array because it might be longer. Instead, we create a new array buffer that is the same length.
            const arrayBuffer = new ArrayBuffer(uint8Array.length);
            // copy the uint8Array into the arrayBuffer
            const arrayBufferView = new Uint8Array(arrayBuffer);
            arrayBufferView.set(uint8Array);
            const hash = await (0, hashing_js_1.sha256Base64)(arrayBuffer);
            (0, vitest_1.expect)(hash).toEqual(expectedHash);
            // also test the view
            const hashView = await (0, hashing_js_1.sha256Base64)(arrayBufferView);
            (0, vitest_1.expect)(hashView).toEqual(expectedHash);
        });
        (0, vitest_1.it)('hashes a Uint8Array correctly, even when the underlying buffer is longer', async () => {
            const uint8Array = new TextEncoder().encode(testString);
            // Create a new array buffer that is longer than the uint8Array.
            const arrayBuffer = new ArrayBuffer(uint8Array.length + 2);
            // Copy the uint8Array into the arrayBuffer, starting at index 1. Set the first and last bytes to some fixed values.
            const arrayBufferView = new Uint8Array(arrayBuffer);
            arrayBufferView[0] = 1;
            arrayBufferView[arrayBufferView.length - 1] = 2;
            arrayBufferView.set(uint8Array, 1);
            // Create a new uint8Array that has the same content as the uint8Array, but has an underlying buffer that is longer.
            const uint8ArraySlice = new Uint8Array(arrayBuffer, 1, uint8Array.length);
            // Verify that the underlying buffer of the uint8ArraySlice is referencially the same as `arrayBuffer`.
            (0, vitest_1.expect)(uint8ArraySlice.buffer).toEqual(arrayBuffer);
            const hash = await (0, hashing_js_1.sha256Base64)(uint8ArraySlice);
            (0, vitest_1.expect)(hash).toEqual(expectedHash);
        });
    });
});
//# sourceMappingURL=hashing.test.js.map