(feat): better connection management, utility functions & tests

This commit is contained in:
Alois 2026-04-14 01:37:18 +02:00
commit 822dfea577
14 changed files with 448 additions and 39 deletions

View file

@ -0,0 +1,39 @@
import { describe, expect, test } from "bun:test";
import { READY_STATE } from "@tensamin/ttp-core";
import { TAuthClient, generateKeyPair, getFriendlyReadyState } from "../src/index";
describe("index helpers", () => {
test("generateKeyPair returns base64 keys with expected length", () => {
const pair = generateKeyPair();
const priv = Buffer.from(pair.private, "base64");
const pub = Buffer.from(pair.public, "base64");
expect(priv.length).toBe(56);
expect(pub.length).toBe(56);
});
test("getFriendlyReadyState maps numeric state to key", () => {
for (const [name, value] of Object.entries(READY_STATE)) {
expect(getFriendlyReadyState(value)).toBe(name);
}
});
test("generateLink builds auth URL with and without challenge", () => {
const client = Object.create(TAuthClient.prototype) as TAuthClient;
client.frontendUrl = "https://tauth.example.com/login";
client.identifier = "my-app";
client.redirectUrl = new URL("https://app.example.com/callback");
const withoutChallenge = client.generateLink();
const withChallenge = client.generateLink("abcdef");
const parsedWithout = new URL(withoutChallenge);
const parsedWith = new URL(withChallenge);
expect(parsedWithout.searchParams.get("identifier")).toBe("my-app");
expect(parsedWithout.searchParams.get("redirect")).toBe(
"https://app.example.com/callback",
);
expect(parsedWith.searchParams.get("challenge")).toBe("abcdef");
});
});