Files
website/src/lib/server/osu.test.ts

63 lines
2.1 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
vi.mock('$app/env/private', () => ({ OSU_CLIENT_ID: '1', OSU_CLIENT_SECRET: 'test' }));
import { getOsuMaps } from './osu';
it('falls back deliberately, merges IDs, reuses OAuth and caches matching results', async () => {
const fetchFn = vi.fn(async (url: string | URL | Request) => {
if (String(url).includes('/oauth/token'))
return Response.json({ access_token: 'token', expires_in: 3600 });
const query = new URL(String(url)).searchParams.get('q');
return Response.json({
beatmapsets: [
{
id: 1,
title: 'Unique Song',
artist: query === 'unique song' ? 'Artist' : 'Wrong Performer'
}
]
});
});
const track = {
id: '6PUzxtIHkv346yP89NzP9X',
name: 'Unique Song',
artists: [{ name: 'Artist' }],
album: { name: '', images: [] }
};
const results = await Promise.all([getOsuMaps(fetchFn, track), getOsuMaps(fetchFn, track)]);
expect(results[0]).toHaveLength(1);
expect(results[0][0].score).toBe(100);
expect(results[1]).toEqual(results[0]);
await getOsuMaps(fetchFn, track);
expect(fetchFn).toHaveBeenCalledTimes(3);
expect(fetchFn.mock.calls.filter(([url]) => String(url).includes('/oauth/token'))).toHaveLength(
1
);
});
describe('parseBeatmapset', () => {
it('keeps the shown fields, fixes preview URLs and sorts difficulties', async () => {
const { parseBeatmapset } = await import('./osu');
const set = parseBeatmapset({
id: 7,
title: 'T',
artist: 'A',
creator: 'mapper',
status: 'ranked',
preview_url: '//b.ppy.sh/preview/7.mp3',
covers: { card: 'https://assets.ppy.sh/c.jpg', list: 'javascript:alert(1)' },
beatmaps: [
{ id: 2, mode: 'osu', difficulty_rating: 5.123, total_length: 90, version: 'Hard' },
{ id: 1, mode: 'osu', difficulty_rating: 2, total_length: 90, version: 'Easy' },
{ id: 3, mode: 'bogus' }
]
});
expect(set).toMatchObject({
id: 7,
creator: 'mapper',
preview_url: 'https://b.ppy.sh/preview/7.mp3'
});
expect(set?.covers?.list).toBeUndefined();
expect(set?.beatmaps?.map((d) => d.stars)).toEqual([2, 5.12]);
expect(parseBeatmapset({ id: 'x' })).toBeNull();
});
});