@@ -0,0 +1,37 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import sharp from 'sharp';
|
||||
import { addWatermark } from './watermark.util';
|
||||
|
||||
describe('addWatermark', () => {
|
||||
it('在图片上叠加水印并输出有效 JPEG', async () => {
|
||||
const base = await sharp({
|
||||
create: { width: 400, height: 300, channels: 3, background: { r: 200, g: 200, b: 200 } },
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
|
||||
const out = await addWatermark(base, '仅供溯源核验使用');
|
||||
const meta = await sharp(out).metadata();
|
||||
expect(meta.format).toBe('jpeg');
|
||||
expect(meta.width).toBe(400);
|
||||
expect(meta.height).toBe(300);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { parseUserAgent, extractIp } from './ua.util';
|
||||
|
||||
describe('parseUserAgent', () => {
|
||||
it('识别移动端 iOS', () => {
|
||||
const r = parseUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)');
|
||||
expect(r.deviceType).toBe('Mobile');
|
||||
expect(r.os).toBe('iOS');
|
||||
});
|
||||
|
||||
it('识别 PC Windows', () => {
|
||||
const r = parseUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
|
||||
expect(r.deviceType).toBe('PC');
|
||||
expect(r.os).toBe('Windows');
|
||||
});
|
||||
|
||||
it('空 UA 返回 Unknown', () => {
|
||||
expect(parseUserAgent(undefined).deviceType).toBe('Unknown');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractIp', () => {
|
||||
it('优先取 x-forwarded-for 首个', () => {
|
||||
expect(extractIp({ 'x-forwarded-for': '1.2.3.4, 5.6.7.8' })).toBe('1.2.3.4');
|
||||
});
|
||||
|
||||
it('回退到 fallback 并去除 ipv6 前缀', () => {
|
||||
expect(extractIp({}, '::ffff:9.9.9.9')).toBe('9.9.9.9');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user