From be77a03aa00bd679c856d2f76687a8883e1a5db3 Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Sun, 31 May 2026 17:20:10 -0300 Subject: [PATCH] =?UTF-8?q?fix(quota):=20await=20getQuotaStore()=20in=20en?= =?UTF-8?q?force=20=E2=80=94=20quota=20never=20enforced/recorded=20(fail-o?= =?UTF-8?q?pen)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getQuotaStore() is async; enforce.ts used it without await, so store was a Promise and store.peek/store.consume threw 'not a function' → enforceQuotaShare failed open on every request and recordConsumption never wrote. Production quota was a silent no-op (unit tests passed because they inject a sync mock store). Await it + guard. --- src/lib/quota/enforce.ts | 4 +-- tests/unit/quota-enforce-await-store.test.ts | 32 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/unit/quota-enforce-await-store.test.ts diff --git a/src/lib/quota/enforce.ts b/src/lib/quota/enforce.ts index d951d1516d..249c62c2cc 100644 --- a/src/lib/quota/enforce.ts +++ b/src/lib/quota/enforce.ts @@ -103,7 +103,7 @@ export async function enforceQuotaShare(input: EnforceInput): Promise). enforce.ts + * called it WITHOUT await, so `store` was a Promise and `store.peek` / `store.consume` + * threw "peek is not a function" → enforceQuotaShare failed open on EVERY request and + * recordConsumption never recorded. The existing enforce unit tests inject a sync mock + * store, so they passed while production was a no-op. This guard asserts every + * getQuotaStore() call in enforce.ts is awaited. + */ +import test from "node:test"; +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; + +test("enforce.ts awaits every getQuotaStore() call (it is async)", () => { + const p = join( + fileURLToPath(import.meta.url), + "..", + "..", + "..", + "src/lib/quota/enforce.ts" + ); + const src = readFileSync(p, "utf8"); + const totalCalls = (src.match(/getQuotaStore\(\)/g) || []).length; + const awaitedCalls = (src.match(/await\s+getQuotaStore\(\)/g) || []).length; + assert.ok(totalCalls > 0, "expected enforce.ts to call getQuotaStore()"); + assert.equal( + awaitedCalls, + totalCalls, + `every getQuotaStore() must be awaited — found ${totalCalls} call(s), ${awaitedCalls} awaited` + ); +});