import { expect, test } from "vitest" import { formatBearerCredential, JsonWebToken } from "#Source/credential/index.ts" test("JsonWebToken.signToken creates a compact JWT string", async () => { const jwt = new JsonWebToken({ secret: "credential-secret", expiresIn: 3_600 }) const token = await jwt.signToken("user-1") expect(token.split(".")).toHaveLength(3) expect(typeof token).toBe("string") }) test("JsonWebToken.parseToken returns userId for valid tokens and undefined for invalid input", async () => { const jwt = new JsonWebToken({ secret: "credential-secret", expiresIn: 3_600 }) const token = await jwt.signToken("user-1") await expect(jwt.parseToken(token)).resolves.toBe("user-1") await expect(jwt.parseToken(formatBearerCredential(token))).resolves.toBeUndefined() await expect(jwt.parseToken(undefined)).resolves.toBeUndefined() await expect(jwt.parseToken("broken.token.value")).resolves.toBeUndefined() await expect( new JsonWebToken({ secret: "other-secret", expiresIn: 3_600 }).parseToken(token), ).resolves.toBeUndefined() })