feat: 支持消费者按实物点击生成天河链存证证书
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import { DepositCertificateService } from './deposit-certificate.service';
|
||||
import { DepositProvider } from './deposit.provider';
|
||||
|
||||
const settings = {
|
||||
certificateEnabled: 'true', certificateDailyLimit: '100', blockchainDriver: 'tianhe',
|
||||
tianheEnv: 'test', tianheAppId: 'app', tianheAppKey: 'key', tianheWalletAddress: '0x' + 'a'.repeat(40),
|
||||
};
|
||||
function fixture(state = 'PENDING') {
|
||||
const row: any = {
|
||||
id: 1, traceCodeId: 1n, status: 'ONCHAIN', txHash: '0x' + 'c'.repeat(64), chainName: '天河链',
|
||||
certificateState: state, certificateAttempts: 0, certificateLeaseUntil: null,
|
||||
certificateRequest: { version: 1, environment: 'test', appId: 'app', address: settings.tianheWalletAddress, operateId: 'stable-op', text: 'frozen-evidence' },
|
||||
};
|
||||
const prisma = {
|
||||
blockchainProof: {
|
||||
updateMany: jest.fn().mockResolvedValue({ count: 1 }),
|
||||
findUniqueOrThrow: jest.fn().mockResolvedValue(row),
|
||||
},
|
||||
traceCode: { findUnique: jest.fn().mockResolvedValue({ shortCode: '123456000001', status: 'ACTIVE', artwork: { status: 'ACTIVE', auditStatus: 'APPROVED' }, proofs: [row] }) },
|
||||
$transaction: jest.fn(),
|
||||
};
|
||||
const config = { getAllRaw: jest.fn().mockResolvedValue(settings), get: jest.fn().mockResolvedValue('true') };
|
||||
return { row, prisma, config, service: new DepositCertificateService(prisma as any, config as any) };
|
||||
}
|
||||
describe('Consumer certificate lifecycle', () => {
|
||||
afterEach(() => jest.restoreAllMocks());
|
||||
it('GET only reads persisted state and never starts a job or calls Tianhe', async () => {
|
||||
const { service, prisma, config } = fixture('NONE');
|
||||
expect(await service.status('123456000001')).toMatchObject({ status: 'NONE', url: null });
|
||||
expect(prisma.blockchainProof.updateMany).not.toHaveBeenCalled();
|
||||
expect(prisma.$transaction).not.toHaveBeenCalled();
|
||||
expect(config.getAllRaw).not.toHaveBeenCalled();
|
||||
});
|
||||
it('returns an existing shared certificate without consulting credentials or submitting', async () => {
|
||||
const { service, row, config } = fixture('READY');
|
||||
row.certificateUrl = 'https://test.tichain.tianhecloud.com/#/certificate?address=' + settings.tianheWalletAddress + '&txHash=0x' + 'a'.repeat(64) + '&digest=' + 'b'.repeat(64);
|
||||
expect(await service.request('123456000001')).toMatchObject({ status: 'READY', url: row.certificateUrl });
|
||||
expect(config.getAllRaw).not.toHaveBeenCalled();
|
||||
});
|
||||
it('does not send when another worker owns the lease', async () => {
|
||||
const { service, row, prisma } = fixture();
|
||||
prisma.blockchainProof.updateMany.mockResolvedValue({ count: 0 });
|
||||
const submit = jest.spyOn(DepositProvider.prototype, 'submit');
|
||||
await service.process(row);
|
||||
expect(submit).not.toHaveBeenCalled();
|
||||
});
|
||||
it('a submission timeout changes to query-only, preserving the exact operation id', async () => {
|
||||
const { service, row, prisma } = fixture();
|
||||
const submit = jest.spyOn(DepositProvider.prototype, 'submit').mockRejectedValue(new Error('timeout'));
|
||||
await service.process(row);
|
||||
expect(submit).toHaveBeenCalledWith(settings.tianheWalletAddress, 'stable-op', 'frozen-evidence');
|
||||
expect(prisma.blockchainProof.updateMany.mock.calls[1][0].data.certificateState).toBe('WAITING');
|
||||
row.certificateState = 'WAITING';
|
||||
jest.spyOn(DepositProvider.prototype, 'query').mockResolvedValue({ operateId: 'stable-op', status: 2, createTime: '', txHash: '' });
|
||||
const cert = jest.spyOn(DepositProvider.prototype, 'certificate');
|
||||
await service.process(row);
|
||||
expect(submit).toHaveBeenCalledTimes(1);
|
||||
expect(cert).not.toHaveBeenCalled();
|
||||
});
|
||||
it('recovers a crashed SUBMITTING worker by querying, not resubmitting', async () => {
|
||||
const { service, row } = fixture('SUBMITTING');
|
||||
const submit = jest.spyOn(DepositProvider.prototype, 'submit');
|
||||
const query = jest.spyOn(DepositProvider.prototype, 'query').mockResolvedValue({ status: 2 });
|
||||
await service.process(row);
|
||||
expect(submit).not.toHaveBeenCalled(); expect(query).toHaveBeenCalledWith(settings.tianheWalletAddress, 'stable-op');
|
||||
});
|
||||
it('stops before network calls if the wallet or environment changed', async () => {
|
||||
const { service, row, config, prisma } = fixture();
|
||||
config.getAllRaw.mockResolvedValue({ ...settings, tianheEnv: 'production' });
|
||||
const submit = jest.spyOn(DepositProvider.prototype, 'submit');
|
||||
await service.process(row);
|
||||
expect(submit).not.toHaveBeenCalled();
|
||||
expect(prisma.blockchainProof.updateMany.mock.calls[1][0].data.certificateState).toBe('UNKNOWN');
|
||||
});
|
||||
it('rejects inactive, disabled and unapproved items before creation', async () => {
|
||||
const { service, prisma } = fixture('NONE');
|
||||
prisma.traceCode.findUnique.mockResolvedValue({ status: 'INACTIVE' } as any);
|
||||
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));
|
||||
await service.request('123456000001');
|
||||
expect(tx.blockchainProof.updateMany).toHaveBeenCalledWith(expect.objectContaining({ where: { id: 1, certificateState: 'NONE', status: 'ONCHAIN' } }));
|
||||
expect(tx.systemSetting.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user