import { expect, it } from "bun:test"; import { jwt } from "../jwt"; it("encodes and decodes a jwt token", async () => { const secret = "secret"; const payload = { hello: "world" }; const token = await jwt.encode({ payload, secret }); const decoded = await jwt.decode({ token, secret }); expect(decoded).toEqual({ valid: true, header: jwt.DEFAULT_HEADER, payload, }); }); it("sets valid to false if the signature is invalid", async () => { const secret = "secret"; const payload = { hello: "world" }; const token = await jwt.encode({ payload, secret }); const decoded = await jwt.decode({ token, secret: "wrong" }); expect(decoded).toEqual({ valid: false, header: jwt.DEFAULT_HEADER, payload, }); }); it(`sets valid to false if secret isn't passed`, async () => { const secret = "secret"; const payload = { hello: "world" }; const token = await jwt.encode({ payload, secret }); const decoded = await jwt.decode({ token }); expect(decoded).toEqual({ valid: false, header: jwt.DEFAULT_HEADER, payload, }); }); it("throws an error if the token is invalid", async () => { await expect( jwt.decode({ token: "invalid", secret: "secret" }) ).rejects.toThrow(jwt.ERROR.INVALID_JWT_FORMAT); });