|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { createPreauthConnectionBudget } from "./preauth-connection-budget.js"; |
3 | 3 |
|
4 | 4 | describe("createPreauthConnectionBudget", () => { |
| 5 | + afterEach(() => { |
| 6 | + vi.unstubAllEnvs(); |
| 7 | + }); |
| 8 | + |
5 | 9 | it("caps connections with a finite configured limit", () => { |
6 | 10 | const budget = createPreauthConnectionBudget(2); |
7 | 11 |
|
@@ -30,4 +34,23 @@ describe("createPreauthConnectionBudget", () => { |
30 | 34 | } |
31 | 35 | expect(budget.acquire(undefined)).toBe(false); |
32 | 36 | }); |
| 37 | + |
| 38 | + it("accepts strict plus-signed env limits", () => { |
| 39 | + vi.stubEnv("OPENCLAW_MAX_PREAUTH_CONNECTIONS_PER_IP", "+02"); |
| 40 | + const budget = createPreauthConnectionBudget(); |
| 41 | + |
| 42 | + expect(budget.acquire("127.0.0.1")).toBe(true); |
| 43 | + expect(budget.acquire("127.0.0.1")).toBe(true); |
| 44 | + expect(budget.acquire("127.0.0.1")).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it("ignores non-decimal env limits", () => { |
| 48 | + vi.stubEnv("OPENCLAW_MAX_PREAUTH_CONNECTIONS_PER_IP", "0x2"); |
| 49 | + const budget = createPreauthConnectionBudget(); |
| 50 | + |
| 51 | + for (let i = 0; i < 32; i += 1) { |
| 52 | + expect(budget.acquire("127.0.0.1")).toBe(true); |
| 53 | + } |
| 54 | + expect(budget.acquire("127.0.0.1")).toBe(false); |
| 55 | + }); |
33 | 56 | }); |
0 commit comments