269 lines
8.8 KiB
TypeScript
269 lines
8.8 KiB
TypeScript
import {
|
|
ArtworkStatus,
|
|
AuditStatus,
|
|
createDefaultTraceLayout,
|
|
TraceCodeStatus,
|
|
} from '@artveris/shared';
|
|
import { PublicTraceService } from './public-trace.service';
|
|
|
|
function createHarness(options: {
|
|
artworkStatus?: ArtworkStatus;
|
|
showOutboundTimeline?: boolean;
|
|
withOutbound?: boolean;
|
|
traceLayout?: any;
|
|
} = {}) {
|
|
const outbound = options.withOutbound
|
|
? {
|
|
outTime: new Date('2026-07-16T08:00:00.000Z'),
|
|
remark: 'outbound note',
|
|
images: [
|
|
{
|
|
id: 1n,
|
|
fileKey: 'outbound/1.jpg',
|
|
fileName: '1.jpg',
|
|
mimeType: 'image/jpeg',
|
|
size: 1024n,
|
|
width: 800,
|
|
height: 600,
|
|
sortOrder: 0,
|
|
},
|
|
],
|
|
_count: { images: 1 },
|
|
}
|
|
: null;
|
|
const code = {
|
|
id: 1n,
|
|
shortCode: '482731000001',
|
|
artworkId: 10,
|
|
status: TraceCodeStatus.INACTIVE,
|
|
queryCount: 0,
|
|
firstActivatedAt: null,
|
|
lastQueriedAt: null,
|
|
batch: { batchNo: 'B1' },
|
|
artwork: {
|
|
id: 10,
|
|
archiveNo: 'A1',
|
|
status: ArtworkStatus.DRAFT,
|
|
auditStatus: AuditStatus.APPROVED,
|
|
deletedAt: null,
|
|
},
|
|
outbound,
|
|
};
|
|
const artwork = {
|
|
...code.artwork,
|
|
status: options.artworkStatus ?? ArtworkStatus.DRAFT,
|
|
title: '草稿作品',
|
|
artist: null,
|
|
images: [],
|
|
showcaseImages: [
|
|
{
|
|
key: 'artwork/10/showcase/detail.jpg',
|
|
name: '笔触细节',
|
|
description: '近距离展示作品笔触与材质',
|
|
type: 'image',
|
|
},
|
|
],
|
|
dynamicFields: null,
|
|
traceLayout: options.traceLayout ?? null,
|
|
showOutboundTimeline: options.showOutboundTimeline ?? false,
|
|
template: { fields: [] },
|
|
traceNodes: [],
|
|
certificates: [],
|
|
};
|
|
const prisma = {
|
|
traceCode: {
|
|
findUnique: jest.fn().mockImplementation(async ({ select }: any) => {
|
|
if (select && Object.keys(select).length === 1 && select.id) return { id: code.id };
|
|
return code;
|
|
}),
|
|
updateMany: jest.fn().mockResolvedValue({ count: 1 }),
|
|
update: jest.fn(),
|
|
findUniqueOrThrow: jest.fn().mockResolvedValue({
|
|
...code,
|
|
status: TraceCodeStatus.ACTIVE,
|
|
queryCount: 1,
|
|
firstActivatedAt: new Date('2026-07-19T08:00:00.000Z'),
|
|
}),
|
|
},
|
|
artwork: { findFirst: jest.fn().mockResolvedValue(artwork) },
|
|
queryLog: { create: jest.fn().mockResolvedValue({}) },
|
|
blockchainProof: { findUnique: jest.fn() },
|
|
outboundImage: { findMany: jest.fn(), count: jest.fn() },
|
|
$transaction: jest.fn(),
|
|
};
|
|
const redis = {
|
|
get: jest.fn().mockResolvedValue(null),
|
|
set: jest.fn().mockResolvedValue('OK'),
|
|
};
|
|
const storage = { getUrl: jest.fn((key: string) => `/uploads/${key}`) };
|
|
const settings = { get: jest.fn().mockResolvedValue(null) };
|
|
const service = new PublicTraceService(prisma as any, storage as any, redis as any, settings as any);
|
|
return { service, prisma };
|
|
}
|
|
|
|
describe('PublicTraceService draft visibility', () => {
|
|
it('returns INVALID for a draft artwork even when it is approved', async () => {
|
|
const { service, prisma } = createHarness();
|
|
|
|
await expect(service.trace('482731000001', { ip: '127.0.0.1' })).resolves.toEqual({ status: 'INVALID' });
|
|
expect(prisma.traceCode.updateMany).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('does not expose an approved draft through preview', async () => {
|
|
const { service } = createHarness();
|
|
|
|
await expect(service.preview('482731000001', 'inactive')).resolves.toEqual({ status: 'INVALID' });
|
|
});
|
|
|
|
it('does not expose outbound images for an approved draft', async () => {
|
|
const { service, prisma } = createHarness();
|
|
|
|
await expect(service.outboundImages('482731000001', 1, 20, true)).resolves.toEqual({ status: 'INVALID' });
|
|
expect(prisma.outboundImage.findMany).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('does not look up chain detail for an item that is not publicly visible', async () => {
|
|
const { service, prisma } = createHarness();
|
|
|
|
await expect(service.captureChainDetail('482731000001')).resolves.toEqual({ complete: false, detail: null });
|
|
expect(prisma.blockchainProof.findUnique).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('PublicTraceService outbound timeline visibility', () => {
|
|
it.each([
|
|
{ showOutboundTimeline: false, expectedSystemNodes: 0 },
|
|
{ showOutboundTimeline: true, expectedSystemNodes: 1 },
|
|
])(
|
|
'keeps outbound data while producing $expectedSystemNodes system timeline node(s)',
|
|
async ({ showOutboundTimeline, expectedSystemNodes }) => {
|
|
const { service } = createHarness({
|
|
artworkStatus: ArtworkStatus.ACTIVE,
|
|
showOutboundTimeline,
|
|
withOutbound: true,
|
|
});
|
|
|
|
const result = await service.preview('482731000001', 'inactive');
|
|
|
|
expect(result.status).toBe('OK');
|
|
expect((result as any).outbound).toEqual(
|
|
expect.objectContaining({
|
|
outTime: new Date('2026-07-16T08:00:00.000Z'),
|
|
remark: 'outbound note',
|
|
}),
|
|
);
|
|
expect(
|
|
(result as any).timeline.filter((node: { system: string | null }) => node.system === 'OUTBOUND_RECORD'),
|
|
).toHaveLength(expectedSystemNodes);
|
|
},
|
|
);
|
|
});
|
|
|
|
describe('PublicTraceService artwork showcase', () => {
|
|
it('returns showcase image titles, descriptions and public URLs', async () => {
|
|
const { service } = createHarness({ artworkStatus: ArtworkStatus.ACTIVE });
|
|
|
|
const result = await service.preview('482731000001', 'inactive');
|
|
|
|
expect((result as any).artwork.showcaseImages).toEqual([
|
|
{
|
|
name: '笔触细节',
|
|
description: '近距离展示作品笔触与材质',
|
|
type: 'image',
|
|
url: '/uploads/artwork/10/showcase/detail.jpg',
|
|
},
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('PublicTraceService module layout', () => {
|
|
it('returns the legacy default order for artwork without a saved layout', async () => {
|
|
const { service } = createHarness({ artworkStatus: ArtworkStatus.ACTIVE });
|
|
|
|
const result = await service.preview('482731000001', 'inactive');
|
|
|
|
expect((result as any).artwork.traceLayout.modules.map((module: any) => module.type)).toEqual([
|
|
'HERO_CAROUSEL',
|
|
'AUTHENTICITY_STATUS',
|
|
'PRODUCT_INTRO',
|
|
'SHOWCASE',
|
|
'CERTIFICATES',
|
|
'TIMELINE',
|
|
'BLOCKCHAIN',
|
|
]);
|
|
});
|
|
|
|
it('filters hidden modules and converts custom module media keys to public URLs', async () => {
|
|
const traceLayout = createDefaultTraceLayout();
|
|
traceLayout.modules[1] = { ...traceLayout.modules[1], visible: false };
|
|
traceLayout.modules.push({
|
|
id: 'custom-story',
|
|
type: 'IMAGE_TEXT',
|
|
visible: true,
|
|
title: '创作故事',
|
|
config: {
|
|
heading: '釉色灵感',
|
|
body: '源于山水。',
|
|
imagePosition: 'LEFT',
|
|
image: {
|
|
key: 'artwork/10/story.jpg',
|
|
name: '创作手稿',
|
|
description: '艺术家手稿',
|
|
type: 'image',
|
|
},
|
|
},
|
|
});
|
|
const { service } = createHarness({
|
|
artworkStatus: ArtworkStatus.ACTIVE,
|
|
traceLayout,
|
|
});
|
|
|
|
const result = await service.preview('482731000001', 'inactive');
|
|
const modules = (result as any).artwork.traceLayout.modules;
|
|
|
|
expect(modules.some((module: any) => module.type === 'AUTHENTICITY_STATUS')).toBe(false);
|
|
expect(modules.find((module: any) => module.id === 'custom-story').config.image).toEqual({
|
|
name: '创作手稿',
|
|
description: '艺术家手稿',
|
|
type: 'image',
|
|
url: '/uploads/artwork/10/story.jpg',
|
|
});
|
|
});
|
|
|
|
it('keeps first-scan activation and query logging when the authenticity module is hidden', async () => {
|
|
const traceLayout = createDefaultTraceLayout();
|
|
traceLayout.modules[1] = { ...traceLayout.modules[1], visible: false };
|
|
const { service, prisma } = createHarness({
|
|
artworkStatus: ArtworkStatus.ACTIVE,
|
|
traceLayout,
|
|
});
|
|
|
|
const result = await service.trace('482731000001', { ip: '127.0.0.1' });
|
|
|
|
expect(result.status).toBe('OK');
|
|
expect((result as any).antiCounterfeit.firstQuery).toBe(true);
|
|
expect(prisma.traceCode.updateMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: expect.objectContaining({ status: TraceCodeStatus.INACTIVE }),
|
|
}),
|
|
);
|
|
expect(prisma.queryLog.create).toHaveBeenCalled();
|
|
expect(
|
|
(result as any).artwork.traceLayout.modules.some(
|
|
(module: any) => module.type === 'AUTHENTICITY_STATUS',
|
|
),
|
|
).toBe(false);
|
|
});
|
|
|
|
it('keeps active preview free of activation, counting and query-log side effects', async () => {
|
|
const { service, prisma } = createHarness({ artworkStatus: ArtworkStatus.ACTIVE });
|
|
|
|
const result = await service.preview('482731000001', 'active');
|
|
|
|
expect(result.status).toBe('OK');
|
|
expect(prisma.traceCode.updateMany).not.toHaveBeenCalled();
|
|
expect(prisma.traceCode.update).not.toHaveBeenCalled();
|
|
expect(prisma.queryLog.create).not.toHaveBeenCalled();
|
|
});
|
|
});
|