This commit is contained in:
37
src/lib/server/osu.test.ts
Normal file
37
src/lib/server/osu.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { expect, it, vi } from 'vitest';
|
||||
vi.mock('$env/dynamic/private', () => ({ env: { 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 as typeof fetch, track),
|
||||
getOsuMaps(fetchFn as typeof fetch, track)
|
||||
]);
|
||||
expect(results[0]).toHaveLength(1);
|
||||
expect(results[0][0].score).toBe(100);
|
||||
expect(results[1]).toEqual(results[0]);
|
||||
await getOsuMaps(fetchFn as typeof fetch, track);
|
||||
expect(fetchFn).toHaveBeenCalledTimes(3);
|
||||
expect(fetchFn.mock.calls.filter(([url]) => String(url).includes('/oauth/token'))).toHaveLength(
|
||||
1
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user