You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

31 lines
1.1 KiB

import { describe, expect, test } from "bun:test";
import { captchaConsume, captchaCreate } from "./store";
describe("captcha store", () => {
test("consume succeeds once then fails", () => {
const { captchaId } = captchaCreate("ab12");
expect(captchaConsume(captchaId, "ab12")).toBe(true);
expect(captchaConsume(captchaId, "ab12")).toBe(false);
});
test("wrong answer invalidates challenge", () => {
const { captchaId } = captchaCreate("ab12");
expect(captchaConsume(captchaId, "xxxx")).toBe(false);
expect(captchaConsume(captchaId, "ab12")).toBe(false);
});
test("trim + lowercase on user input", () => {
const { captchaId } = captchaCreate("ab12");
expect(captchaConsume(captchaId, " AB12 ")).toBe(true);
});
test("length mismatch fails without throwing", () => {
const { captchaId } = captchaCreate("ab12");
expect(captchaConsume(captchaId, "x")).toBe(false);
});
test("expired challenge fails", () => {
const { captchaId } = captchaCreate("ab12", { ttlMs: -1000 });
expect(captchaConsume(captchaId, "ab12")).toBe(false);
});
});