修复 consume 缺行不建行导致 sentTotal 丢失的问题

This commit is contained in:
yuming
2026-08-15 17:40:31 +08:00
parent 8d63b47577
commit 07ee98f838
2 changed files with 20 additions and 3 deletions
+12
View File
@@ -4,6 +4,7 @@ const { useTempDb } = require('./helper')
useTempDb() // 必须在 require quota/db 之前
const quota = require('../src/quota')
const db = require('../src/db')
test('未记录过的用户余额为 0', () => {
assert.strictEqual(quota.getBalance('u_new'), 0)
@@ -49,3 +50,14 @@ test('各用户额度互不影响', () => {
assert.strictEqual(quota.getBalance('a'), 3)
assert.strictEqual(quota.getBalance('b'), 1)
})
test('consume 对从未 grant 过的 openid 也要建行,不丢失 sentTotal', () => {
quota.consume('u6', 3)
// 仅断言 getBalance 为 0 不足以证明建行了(缺行时本来就返回 0),
// 必须直接查表,确认行确实被创建且 sentTotal 记录了这次消费
assert.strictEqual(quota.getBalance('u6'), 0)
const row = db.prepare('SELECT * FROM subscribe_quota WHERE openid = ?').get('u6')
assert.ok(row, 'u6 对应的记录行应当被创建')
assert.strictEqual(row.balance, 0)
assert.strictEqual(row.sentTotal, 3)
})