Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions apps/scan/src/services/db/resources/origin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

const db = vi.hoisted(() => ({
ogImageUpsert: vi.fn(),
resourceOriginFindUnique: vi.fn(),
resourceOriginUpsert: vi.fn(),
transaction: vi.fn(),
}));

vi.mock('@x402scan/scan-db', () => ({
scanDb: {
$transaction: db.transaction,
},
}));

import { upsertOrigin } from './origin';

interface MockTransactionClient {
ogImage: {
upsert: typeof db.ogImageUpsert;
};
resourceOrigin: {
findUnique: typeof db.resourceOriginFindUnique;
upsert: typeof db.resourceOriginUpsert;
};
}

type MockTransactionCallback = (
transactionClient: MockTransactionClient
) => unknown;

describe('upsertOrigin', () => {
beforeEach(() => {
vi.clearAllMocks();

db.resourceOriginUpsert.mockResolvedValue({ id: 'origin-1' });
db.ogImageUpsert.mockResolvedValue({});
db.resourceOriginFindUnique.mockResolvedValue({
id: 'origin-1',
ogImages: [],
});
db.transaction.mockImplementation((callback: MockTransactionCallback) =>
Promise.resolve(
callback({
ogImage: {
upsert: db.ogImageUpsert,
},
resourceOrigin: {
findUnique: db.resourceOriginFindUnique,
upsert: db.resourceOriginUpsert,
},
})
)
);
});

it('upserts each OG image URL once per origin', async () => {
await upsertOrigin({
origin: 'https://example.com',
ogImages: [
{
url: 'https://cdn.example.com/og.png',
height: 100,
width: 200,
title: 'First image',
},
{
url: 'https://cdn.example.com/og.png',
height: 300,
width: 400,
title: 'Updated image',
},
],
});

expect(db.ogImageUpsert).toHaveBeenCalledTimes(1);
expect(db.ogImageUpsert).toHaveBeenCalledWith({
where: {
originId_url: {
originId: 'origin-1',
url: 'https://cdn.example.com/og.png',
},
},
update: {
description: undefined,
height: 300,
title: 'Updated image',
width: 400,
},
create: {
description: undefined,
height: 300,
originId: 'origin-1',
title: 'Updated image',
url: 'https://cdn.example.com/og.png',
width: 400,
},
});
});
});
8 changes: 7 additions & 1 deletion apps/scan/src/services/db/resources/origin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ export const upsertOrigin = async (

const originId = upsertedOrigin.id;

const uniqueOgImages = [
...new Map(
origin.ogImages.map(ogImage => [ogImage.url, ogImage])
).values(),
];

await Promise.all(
origin.ogImages.map(({ url, height, width, title, description }) =>
uniqueOgImages.map(({ url, height, width, title, description }) =>
tx.ogImage.upsert({
where: {
originId_url: {
Expand Down