Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
import * as sdkDb from '../src/services/pluginWorkloadSdkDb.js'
import { checkRateLimit } from '../src/services/pluginWorkloadSdkQuotaTracker.js'
vi.mock('../src/db.js', () => ({
pool: {
query: vi.fn().mockResolvedValue({ rows: [], rowCount: 0 }),
connect: vi.fn(),
},
withTransaction: vi.fn(),
}))
vi.mock('../src/services/pluginWorkloadSdkDb.js', async () => {
const actual = await vi.importActual<typeof import('../src/services/pluginWorkloadSdkDb.js')>(
'../src/services/pluginWorkloadSdkDb.js'
)
return {
...actual,
countRecentInvocations: vi.fn(),
}
})
const grant = (
overrides: Partial<sdkDb.PluginWorkloadSdkGrant> = {}
): sdkDb.PluginWorkloadSdkGrant => ({
id: 'g1',
recipeNamespace: 'sandbox-recipes',
recipeName: 'sdk-recipe',
capabilityFamily: 'promptBridge',
provider: 'zai',
allowedModels: [],
allowedEventTypes: [],
allowedTargetRefs: [],
allowedUserRefs: [],
allowedCallers: ['api'],
quotaLimits: { maxRequestsPerRun: 10 },
modelPolicies: {},
promptTargets: [],
defaultTargetRef: null,
policyState: 'active',
policyRevision: 0,
revocationId: null,
createdAt: '',
updatedAt: '',
...overrides,
})
// The former `consumeQuota` describe block was removed with the function
// itself (issue #348, plan §1.5 — deleted outright, not stubbed, so any
// missed production caller is a compile error).
// ─── Issue #348 (plan D3) — platform per-minute defaults ─────────────────
//
// RED-FIRST (plan §6.6): these tests assert the POST-change ceilings
// (promptBridge 600/min, clientNotifications 750/min, sourced from
// config.pluginSdkPromptBridgeRlPerMin / config.pluginSdkNotificationsRlPerMin).
// Each case is POSITIVE (at the ceiling → allowed) AND NEGATIVE (one over → denied
// with quota_exceeded). Against pre-change code the hardcoded defaults are 60/120,
// so the exactly-at-the-ceiling cases FAIL until Phase 1 step 1.4 lands. Do not
// weaken them to pass early.
//
// Plan D3.3 (per-run leg inert): after Phase 1, `consumeQuota` is DELETED
// (decision §3.2 — delete, don't stub), so a mocked-db assertion here would
// have no symbol to call. The per-run-inert behavior is asserted end-to-end
// in the real-Postgres regression suite instead
// (test/pluginWorkloadSdkSteplessQuota.realPostgres.integration.test.ts,
// plan D4 Tests 1 and 3), where the enforcement path itself — not a mock —
// proves the per-run cap never denies and never resets anything.
describe('checkRateLimit — platform per-minute rate limits (issue #348)', () => {
beforeEach(() => {
vi.mocked(sdkDb.countRecentInvocations).mockReset()
})
it('allows promptBridge at exactly 600/minute (positive) and denies at 601 (negative, platform default)', async () => {
const noOverrides = grant({ quotaLimits: {} })
// POSITIVE: at the ceiling → allowed.
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(600)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'promptBridge', noOverrides)
).resolves.toEqual({ ok: true })
// NEGATIVE: one over the ceiling → denied.
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(601)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'promptBridge', noOverrides)
).resolves.toMatchObject({
ok: false,
error: 'quota_exceeded',
retryable: false,
message: expect.stringContaining('600/minute'),
})
})
it('allows clientNotifications at exactly 750/minute (positive) and denies at 751 (negative), narrowed to the eventType', async () => {
const noOverrides = grant({ capabilityFamily: 'clientNotifications', quotaLimits: {} })
// POSITIVE: at the ceiling → allowed.
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(750)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'clientNotifications', noOverrides, {
eventType: 'lead.followup.due',
})
).resolves.toEqual({ ok: true })
expect(sdkDb.countRecentInvocations).toHaveBeenCalledWith(
'sandbox-recipes',
'sdk-recipe',
'clientNotifications',
{ detail: 'lead.followup.due' }
)
// NEGATIVE: one over the ceiling → denied.
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(751)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'clientNotifications', noOverrides, {
eventType: 'lead.followup.due',
})
).resolves.toMatchObject({
ok: false,
error: 'quota_exceeded',
retryable: false,
message: expect.stringContaining('750/minute'),
})
})
it('frees the recipe once the trailing window drains (deny at 601, allow at 0)', async () => {
const noOverrides = grant({ quotaLimits: {} })
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(601)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'promptBridge', noOverrides)
).resolves.toMatchObject({ ok: false, error: 'quota_exceeded' })
// The window is derived from the invocation audit trail (Postgres now()),
// so a drained window is simply a lower count — no counter to reset.
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(0)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'promptBridge', noOverrides)
).resolves.toEqual({ ok: true })
})
it('lets a grant-level maxInvocationsPerMinute override win over the platform default', async () => {
// Decision §3.1: ENV replaces only the hardcoded fallback constants — a
// grant override still wins (`grant value ?? config default`).
const withOverride = grant({ quotaLimits: { maxInvocationsPerMinute: 5 } })
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(5)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'promptBridge', withOverride)
).resolves.toEqual({ ok: true })
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(6)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'promptBridge', withOverride)
).resolves.toMatchObject({
ok: false,
error: 'quota_exceeded',
message: expect.stringContaining('5/minute'),
})
})
it('lets a grant-level maxNotificationsPerMinute override win over the platform default', async () => {
const withOverride = grant({
capabilityFamily: 'clientNotifications',
quotaLimits: { maxNotificationsPerMinute: 9 },
})
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(9)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'clientNotifications', withOverride, {
eventType: 'lead.followup.due',
})
).resolves.toEqual({ ok: true })
vi.mocked(sdkDb.countRecentInvocations).mockResolvedValue(10)
await expect(
checkRateLimit('sandbox-recipes', 'sdk-recipe', 'clientNotifications', withOverride, {
eventType: 'lead.followup.due',
})
).resolves.toMatchObject({
ok: false,
error: 'quota_exceeded',
message: expect.stringContaining('9/minute'),
})
})
})