d3ro-voice/server/supabase/functions/_shared/embedding-quota.fake.ts

25 lines
1.2 KiB
TypeScript

// Test double for EmbeddingUsageStore (not a test file itself).
import { EMBEDDING_QUOTA_FEATURE, type EmbeddingUsageStore } from './embedding-quota.ts'
import type { PlanQuotaTier } from './core-contract.generated.ts'
/** In-memory daily_usage keyed by date; `today` is the date increments land on. */
export class MemoryUsageStore implements EmbeddingUsageStore {
rows = new Map<string, number>()
failIncrement = false
constructor(public tier: PlanQuotaTier, public today: string) {}
readTier(): Promise<PlanQuotaTier> {
return Promise.resolve(this.tier)
}
increment(_userId: string, feature: string, amount: number): Promise<number> {
if (this.failIncrement) return Promise.reject(new Error('down'))
if (feature !== EMBEDDING_QUOTA_FEATURE) return Promise.reject(new Error(`unexpected feature ${feature}`))
const next = (this.rows.get(this.today) ?? 0) + amount
this.rows.set(this.today, next)
return Promise.resolve(next)
}
sumBetween(_userId: string, _feature: string, fromDate: string, beforeDate: string): Promise<number> {
let sum = 0
for (const [date, count] of this.rows) if (date >= fromDate && date < beforeDate) sum += count
return Promise.resolve(sum)
}
}