test.build/core/adapters/security.adapter.test.ts

test.build/core/adapters/security.adapter.test.ts is a file in Codebase Testing. 33 lines of code and 0 definitions.

import { type TestKey, testKey } from "./security.fixture.ts";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { isSignedBy, signText } from "@banes-lab/build-scripts/core/adapters/security.adapter.ts";
import { signingFailed } from "@banes-lab/build-scripts/configuration/strings/site.strings.ts";

const TEXT = "Contact: mailto:a@example.test\n";

let key: TestKey | null = null;

const encoded = function encoded(): string {
    if (key === null) {
        throw new Error("security adapter test: no key");
    }
    return key.encoded;
};

beforeAll(() => {
    key = testKey();
});

afterAll(() => {
    key?.dispose();
});

describe("signText and isSignedBy", () => {
    it("signs the text, exports the public key, and that key verifies the signed text", () => {
        const security = signText(encoded(), TEXT);
        expect(security.signed).toContain(TEXT.trim());
        expect(isSignedBy(security.publicKey, security.signed)).toBe(true);
    });

    it("refuses a changed text and a text with no signature", () => {
        const security = signText(encoded(), TEXT);
        expect(isSignedBy(security.publicKey, security.signed.replace("a@example.test", "b@example.test"))).toBe(false);
        expect(isSignedBy(security.publicKey, TEXT)).toBe(false);
    });

    it("fails loudly on a key that does not import", () => {
        expect(() => signText("bm90IGEga2V5", TEXT)).toThrow(signingFailed("import"));
    });
});