Merge pull request #35907 from overleaf/mg-ai-usage-event

Fire a 'ai-usage-limit-reached' event in backend

GitOrigin-RevId: b43babdd2a0146086cee5ebd2bde83f8daaf1bb5
This commit is contained in:
Malik Glossop
2026-09-17 11:50:38 +00:00
committed by Copybot
parent e567abe71a
commit 42feabe643
5 changed files with 431 additions and 11 deletions
@@ -143,6 +143,62 @@ describe('FeatureUsageRateLimiter', function () {
})
})
describe('_recordFirstLimitReach', function () {
beforeEach(async function (ctx) {
ctx.periodStart = new Date()
await UserFeatureUsage.create({
_id: ctx.userId,
features: {
[MOCKED_FEATURE_NAME]: { usage: 101, periodStart: ctx.periodStart },
},
})
})
it('returns true for the first reach of the period', async function (ctx) {
expect(
await ctx.FeatureUsageRateLimiter._recordFirstLimitReach(
ctx.userId,
ctx.periodStart
)
).to.be.true
})
it('returns false for later reaches in the same period', async function (ctx) {
await ctx.FeatureUsageRateLimiter._recordFirstLimitReach(
ctx.userId,
ctx.periodStart
)
expect(
await ctx.FeatureUsageRateLimiter._recordFirstLimitReach(
ctx.userId,
ctx.periodStart
)
).to.be.false
expect(
await ctx.FeatureUsageRateLimiter._recordFirstLimitReach(
ctx.userId,
ctx.periodStart
)
).to.be.false
})
it('returns true again once the period resets to a new periodStart', async function (ctx) {
await ctx.FeatureUsageRateLimiter._recordFirstLimitReach(
ctx.userId,
ctx.periodStart
)
const newPeriodStart = new Date(ctx.periodStart.getTime() + 1000)
expect(
await ctx.FeatureUsageRateLimiter._recordFirstLimitReach(
ctx.userId,
newPeriodStart
)
).to.be.true
})
})
describe('getRemainingFeatureUses', function () {
beforeEach(function (ctx) {
ctx._getAllowanceStub.resolves(100)