Files
atlas-seller-kit/apps/api/test/product-image-derivative-history.unit.test.ts

57 lines
3.4 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { ProductImageDerivativeHistoryService } from '../src/media/product-image-derivative-history.service.js';
const workspaceId = '30cf54b7-06a3-4e5f-a5fc-d74165115094';
const productId = '733d0d73-3d39-4a98-ac0a-5c0e521d8998';
describe('商品图片处理记录', () => {
it('只读取当前商品成功的 3:4 与水印结果,并重新签发预览地址', async () => {
const query = vi.fn(async (sql: string, values: unknown[]) => {
expect(values).toEqual([productId]);
if (sql.includes('FROM atlas.products')) return { rows: [{ id: productId }] };
expect(sql).toContain("WHERE product_id = $1 AND status = 'succeeded'");
expect(sql).toContain('LIMIT 100');
return { rows: [
{
id: 'watermark-task', kind: 'watermark', source_asset_id: 'source-1',
output_asset_id: 'output-1', created_at: new Date('2026-09-27T08:00:00Z'),
content_type: 'image/jpeg', byte_size: '1200', width_px: 1200, height_px: 1600,
asset_created_at: new Date('2026-09-27T08:01:00Z'),
},
{
id: 'ratio-task', kind: 'aspect_ratio_3_4', source_asset_id: 'source-2',
output_asset_id: 'output-2', created_at: new Date('2026-09-27T07:00:00Z'),
content_type: 'image/jpeg', byte_size: '1100', width_px: 1200, height_px: 1600,
asset_created_at: new Date('2026-09-27T07:01:00Z'),
},
] };
});
const workspaceTransaction = vi.fn(async (_id: string, work: (tx: { query: typeof query }) => Promise<unknown>) => work({ query }));
const accessUrl = vi.fn(async (_workspace: string, assetId: string) => ({
url: `https://example.test/${assetId}`, expires_at: '2026-09-27T08:10:00Z',
}));
const service = new ProductImageDerivativeHistoryService(
{ workspaceTransaction } as unknown as ConstructorParameters<typeof ProductImageDerivativeHistoryService>[0],
{ accessUrl } as unknown as ConstructorParameters<typeof ProductImageDerivativeHistoryService>[1],
);
const records = await service.list(workspaceId, productId);
expect(workspaceTransaction).toHaveBeenCalledWith(workspaceId, expect.any(Function));
expect(records).toMatchObject([
{ kind: 'watermark', output_asset: { id: 'output-1', preview_url: 'https://example.test/output-1' } },
{ kind: 'aspect_ratio_3_4', output_asset: { id: 'output-2', preview_url: 'https://example.test/output-2' } },
]);
expect(accessUrl).toHaveBeenCalledTimes(2);
});
it('商品不在当前工作空间时返回 404,不查询图片记录', async () => {
const query = vi.fn(async () => ({ rows: [] }));
const workspaceTransaction = vi.fn(async (_id: string, work: (tx: { query: typeof query }) => Promise<unknown>) => work({ query }));
const service = new ProductImageDerivativeHistoryService(
{ workspaceTransaction } as unknown as ConstructorParameters<typeof ProductImageDerivativeHistoryService>[0],
{} as ConstructorParameters<typeof ProductImageDerivativeHistoryService>[1],
);
await expect(service.list(workspaceId, productId)).rejects.toMatchObject({ status: 404 });
expect(query).toHaveBeenCalledOnce();
});
});