74 lines
2.8 KiB
TypeScript
74 lines
2.8 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
|
|
describe("tree-shake", () => {
|
|
it("sideEffects is false", { timeout: 5000 }, () => {
|
|
const pkg = JSON.parse(
|
|
readFileSync(join(__dirname, "..", "package.json"), "utf8")
|
|
) as { sideEffects?: boolean };
|
|
expect(pkg.sideEffects).toBe(false);
|
|
});
|
|
|
|
it("exposes per-feature subpath exports", { timeout: 5000 }, () => {
|
|
const pkg = JSON.parse(
|
|
readFileSync(join(__dirname, "..", "package.json"), "utf8")
|
|
) as { exports?: Record<string, unknown> };
|
|
const keys = Object.keys(pkg.exports ?? {});
|
|
// Expect: ".", "./hash", "./mac", "./kdf", "./encoding", "./errors", "./package.json"
|
|
expect(keys.length).toBeGreaterThanOrEqual(6);
|
|
expect(keys).toContain("./hash");
|
|
expect(keys).toContain("./mac");
|
|
expect(keys).toContain("./kdf");
|
|
expect(keys).toContain("./encoding");
|
|
expect(keys).toContain("./errors");
|
|
});
|
|
|
|
it("hash subpath export works", { timeout: 30000 }, async () => {
|
|
const { sha256 } = await import("../src/hash/sha2/sha2.js");
|
|
expect(sha256(new Uint8Array([1, 2, 3])).length).toBe(32);
|
|
});
|
|
|
|
it("mac subpath export works", { timeout: 30000 }, async () => {
|
|
const { hmac } = await import("../src/mac/hmac/hmac.js");
|
|
expect(
|
|
hmac("SHA-256", new Uint8Array(32), new Uint8Array([1])).length
|
|
).toBe(32);
|
|
});
|
|
|
|
it("kdf subpath export works", { timeout: 30000 }, async () => {
|
|
const { hkdf } = await import("../src/kdf/hkdf/hkdf.js");
|
|
expect(
|
|
hkdf("SHA-256", new Uint8Array(32), {
|
|
salt: new Uint8Array(16),
|
|
info: new Uint8Array(0),
|
|
length: 32,
|
|
}).length
|
|
).toBe(32);
|
|
});
|
|
|
|
it("encoding subpath export works", { timeout: 30000 }, async () => {
|
|
const { toHex, fromHex } = await import("../src/encoding/hex/hex.js");
|
|
expect(fromHex(toHex(new Uint8Array([1, 2, 3])))).toEqual(
|
|
new Uint8Array([1, 2, 3])
|
|
);
|
|
});
|
|
|
|
it("errors subpath export works", { timeout: 30000 }, async () => {
|
|
const { HashError, HASH_CODES } = await import(
|
|
"../src/errors/index.js"
|
|
);
|
|
expect(HASH_CODES["KDF.INVALID_PARAMS"]).toBe(
|
|
"HASH.KDF.INVALID_PARAMS"
|
|
);
|
|
expect(HASH_CODES["ALGORITHM.UNSUPPORTED"]).toBe(
|
|
"HASH.ALGORITHM.UNSUPPORTED"
|
|
);
|
|
expect(HASH_CODES["ENCODING.INVALID_INPUT"]).toBe(
|
|
"HASH.ENCODING.INVALID_INPUT"
|
|
);
|
|
const err = new HashError(HASH_CODES["ENCODING.INVALID_INPUT"], "test");
|
|
expect(err).toBeInstanceOf(HashError);
|
|
});
|
|
});
|