New Captcha Package added

This commit is contained in:
2026-07-25 13:38:18 +05:30
parent d0aded0392
commit 8b728a3e5d
157 changed files with 12092 additions and 1913 deletions
+20
View File
@@ -0,0 +1,20 @@
import { describe, expect, test } from "bun:test";
import { evaluateCaptchaRisk, shouldRequireCaptcha, validCaptchaGrant } from "../src/policy.ts";
describe("adaptive CAPTCHA policy", () => {
test("raises risk from automation signals", () => {
const result = evaluateCaptchaRisk({ failedAttempts: 3, completionMs: 200, suspiciousHeaders: true });
expect(result.challenge).toBe(true);
expect(result.reasons).toContain("too-fast");
});
test("honours explicit policy actions", () => {
expect(shouldRequireCaptcha("signup", { alwaysForActions: ["signup"] }).challenge).toBe(true);
expect(shouldRequireCaptcha("health", { neverForActions: ["health"] }).challenge).toBe(false);
});
test("accepts an unexpired route grant", () => {
const grant = validCaptchaGrant([{ action: "page", routeGroup: "/reports", provider: "self-hosted", expiresAt: 200 }], "page", 100, "/reports");
expect(grant?.provider).toBe("self-hosted");
});
});