38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { ConfigService } from '@nestjs/config';
|
|
import { BlockchainService } from './blockchain.service';
|
|
|
|
function makeService(): BlockchainService {
|
|
const config = {
|
|
get: (key: string) => (key === 'blockchain.driver' ? 'mock' : undefined),
|
|
} as unknown as ConfigService;
|
|
return new BlockchainService(config);
|
|
}
|
|
|
|
describe('BlockchainService.hashData', () => {
|
|
const svc = makeService();
|
|
|
|
it('相同输入产生相同哈希', () => {
|
|
const a = svc.hashData({ title: '青花瓷', n: 1 });
|
|
const b = svc.hashData({ title: '青花瓷', n: 1 });
|
|
expect(a).toBe(b);
|
|
expect(a).toMatch(/^0x[0-9a-f]{64}$/);
|
|
});
|
|
|
|
it('不同输入产生不同哈希', () => {
|
|
const a = svc.hashData({ title: '青花瓷' });
|
|
const b = svc.hashData({ title: '霁红釉' });
|
|
expect(a).not.toBe(b);
|
|
});
|
|
});
|
|
|
|
describe('BlockchainService.attest (mock)', () => {
|
|
it('返回完整链上字段', async () => {
|
|
const svc = makeService();
|
|
const result = await svc.attest({ dataHash: '0xabc' });
|
|
expect(result.txHash).toMatch(/^0x[0-9a-f]{64}$/);
|
|
expect(result.blockHash).toMatch(/^0x[0-9a-f]{64}$/);
|
|
expect(result.blockHeight).toBeGreaterThan(0);
|
|
expect(result.explorerUrl).toContain(result.txHash);
|
|
});
|
|
});
|