fix: 移除消费者出证每日上限并保留单件防重
This commit is contained in:
@@ -2,7 +2,7 @@ import { DepositCertificateService } from './deposit-certificate.service';
|
||||
import { DepositProvider } from './deposit.provider';
|
||||
|
||||
const settings = {
|
||||
certificateEnabled: 'true', certificateDailyLimit: '100', blockchainDriver: 'tianhe',
|
||||
certificateEnabled: 'true', blockchainDriver: 'tianhe',
|
||||
tianheEnv: 'test', tianheAppId: 'app', tianheAppKey: 'key', tianheWalletAddress: '0x' + 'a'.repeat(40),
|
||||
};
|
||||
function fixture(state = 'PENDING') {
|
||||
@@ -78,16 +78,31 @@ describe('Consumer certificate lifecycle', () => {
|
||||
await expect(service.request('123456000001')).rejects.toThrow();
|
||||
expect(prisma.$transaction).not.toHaveBeenCalled();
|
||||
});
|
||||
it('uses an atomic NONE claim so a stale concurrent transaction cannot overwrite the request or consume quota', async () => {
|
||||
const { service, prisma } = fixture('NONE');
|
||||
const tx = {
|
||||
systemSetting: { upsert: jest.fn(), update: jest.fn() },
|
||||
$queryRaw: jest.fn().mockResolvedValue([{ value: '2026-09-20:1' }]),
|
||||
blockchainProof: { updateMany: jest.fn().mockResolvedValue({ count: 0 }) },
|
||||
};
|
||||
prisma.$transaction.mockImplementation(async callback => callback(tx));
|
||||
it('ignores obsolete daily settings and claims an item without any quota transaction', async () => {
|
||||
const { service, prisma, config } = fixture('NONE');
|
||||
config.getAllRaw.mockResolvedValue({ ...settings, certificateDailyLimit: '0', certificateDailyQuota: '2026-09-20:999999' } as any);
|
||||
await service.request('123456000001');
|
||||
expect(tx.blockchainProof.updateMany).toHaveBeenCalledWith(expect.objectContaining({ where: { id: 1, certificateState: 'NONE', status: 'ONCHAIN' } }));
|
||||
expect(tx.systemSetting.update).not.toHaveBeenCalled();
|
||||
expect(prisma.blockchainProof.updateMany).toHaveBeenCalledWith(expect.objectContaining({
|
||||
where: { id: 1, certificateState: 'NONE', status: 'ONCHAIN' },
|
||||
data: expect.objectContaining({ certificateState: 'PENDING' }),
|
||||
}));
|
||||
expect(prisma.$transaction).not.toHaveBeenCalled();
|
||||
});
|
||||
it('concurrent first clicks preserve the winning request snapshot and operation id', async () => {
|
||||
const { service, prisma, row } = fixture('NONE');
|
||||
let writes = 0;
|
||||
prisma.blockchainProof.updateMany.mockImplementation(async ({ where, data }: any) => {
|
||||
if (row.certificateState !== where.certificateState) return { count: 0 };
|
||||
Object.assign(row, data);
|
||||
writes++;
|
||||
return { count: 1 };
|
||||
});
|
||||
const results = await Promise.all(Array.from({ length: 10 }, () => service.request('123456000001')));
|
||||
const saved = row.certificateRequest;
|
||||
expect(writes).toBe(1);
|
||||
expect(results.every(result => result.status === 'PENDING')).toBe(true);
|
||||
await service.request('123456000001');
|
||||
expect(row.certificateRequest).toBe(saved);
|
||||
expect(writes).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user