优化电子凭证核销码生成
This commit is contained in:
@@ -1,30 +1,58 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { generateKuaishouIndustryVoucherCode } from '../../../repositories/kuaishou-industry-voucher-repo.js'
|
||||
import { resolveKuaishouIndustryVoucherValidity } from './voucher-service.js'
|
||||
|
||||
test('generateKuaishouIndustryVoucherCode creates non-numeric stable voucher code', () => {
|
||||
let requestedSize = 0
|
||||
const code = generateKuaishouIndustryVoucherCode((size) => {
|
||||
requestedSize = size
|
||||
return Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
})
|
||||
|
||||
assert.equal(requestedSize, 10)
|
||||
assert.equal(code.length, 19)
|
||||
assert.match(code, /^KSV[0-9ABCDEFGHJKMNPQRSTVWXYZ]{16}$/)
|
||||
assert.doesNotMatch(code, /^\d+$/)
|
||||
assert.doesNotMatch(code, /[ILOU]/)
|
||||
})
|
||||
|
||||
test('generateKuaishouIndustryVoucherCode changes with random bytes', () => {
|
||||
const first = generateKuaishouIndustryVoucherCode(() => Buffer.alloc(10, 0))
|
||||
const second = generateKuaishouIndustryVoucherCode(() => Buffer.alloc(10, 1))
|
||||
|
||||
assert.notEqual(first, second)
|
||||
})
|
||||
|
||||
test('resolveKuaishouIndustryVoucherValidity uses certExpDays when explicit times are zero', () => {
|
||||
const nowMs = 1783394218325
|
||||
const validity = resolveKuaishouIndustryVoucherValidity({
|
||||
certActualStartTime: 0,
|
||||
certActualEndTime: 0,
|
||||
certStartTime: 0,
|
||||
certEndTime: 0,
|
||||
certExpDays: 3,
|
||||
}, nowMs)
|
||||
const validity = resolveKuaishouIndustryVoucherValidity(
|
||||
{
|
||||
certActualStartTime: 0,
|
||||
certActualEndTime: 0,
|
||||
certStartTime: 0,
|
||||
certEndTime: 0,
|
||||
certExpDays: 3,
|
||||
},
|
||||
nowMs,
|
||||
)
|
||||
|
||||
assert.equal(validity.validStartTime, nowMs)
|
||||
assert.equal(validity.validEndTime, nowMs + 3 * 86_400_000)
|
||||
})
|
||||
|
||||
test('resolveKuaishouIndustryVoucherValidity prefers actual certificate time range', () => {
|
||||
const validity = resolveKuaishouIndustryVoucherValidity({
|
||||
certActualStartTime: 1000,
|
||||
certActualEndTime: 5000,
|
||||
certStartTime: 2000,
|
||||
certEndTime: 6000,
|
||||
certExpDays: 3,
|
||||
}, 9000)
|
||||
const validity = resolveKuaishouIndustryVoucherValidity(
|
||||
{
|
||||
certActualStartTime: 1000,
|
||||
certActualEndTime: 5000,
|
||||
certStartTime: 2000,
|
||||
certEndTime: 6000,
|
||||
certExpDays: 3,
|
||||
},
|
||||
9000,
|
||||
)
|
||||
|
||||
assert.equal(validity.validStartTime, 1000)
|
||||
assert.equal(validity.validEndTime, 5000)
|
||||
|
||||
Reference in New Issue
Block a user