Files
shd.build/src/routes/images/news/[id]/server.test.ts
T
jb dc08eefce6
Validate and deploy production / validate (push) Successful in 23s
Validate and deploy staging / validate (push) Successful in 28s
Validate and deploy production / deploy (push) Successful in 49s
Validate and deploy staging / deploy (push) Successful in 28s
feat: link official news to Ubisoft
2026-08-01 22:26:52 -04:00

55 lines
1.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
get: vi.fn(),
fetchThumbnail: vi.fn(),
}));
vi.mock('$lib/server/database', () => ({
getDatabase: vi.fn(async () => ({
prepare: vi.fn(async () => ({ get: mocks.get })),
})),
}));
vi.mock('$lib/server/ubisoft-news-client', () => ({
fetchUbisoftNewsThumbnail: mocks.fetchThumbnail,
}));
import { GET } from './+server';
const articleId = '9890c398-0522-4e8c-8fdb-78de9c66deb6';
const sourceUrl = 'https://staticctf.ubisoft.com/news/cover.jpg';
describe('Ubisoft News image proxy', () => {
beforeEach(() => {
mocks.get.mockReset().mockResolvedValue({ thumbnail_url: sourceUrl });
mocks.fetchThumbnail.mockReset().mockResolvedValue({
body: new Uint8Array([1, 2, 3]),
contentType: 'image/jpeg',
});
});
it('serves a same-origin cached image and honors its ETag', async () => {
const first = await GET({
params: { id: articleId },
request: new Request(`http://localhost/images/news/${articleId}`),
} as never);
expect(first.status).toBe(200);
expect(first.headers.get('content-type')).toBe('image/jpeg');
expect(first.headers.get('cross-origin-resource-policy')).toBe(
'same-origin',
);
const etag = first.headers.get('etag');
expect(etag).toBeTruthy();
const second = await GET({
params: { id: articleId },
request: new Request(`http://localhost/images/news/${articleId}`, {
headers: { 'if-none-match': String(etag) },
}),
} as never);
expect(second.status).toBe(304);
expect(mocks.fetchThumbnail).toHaveBeenCalledTimes(1);
});
});