(feat): better connection management, utility functions & tests
This commit is contained in:
parent
b8889812c5
commit
822dfea577
14 changed files with 448 additions and 39 deletions
39
test/index-helpers.test.ts
Normal file
39
test/index-helpers.test.ts
Normal 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");
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue