Files
minstrel/web/src/lib/api/likes.test.ts
T
bvandeusenandClaude Opus 5.5 698ff23105 fix(likes): the web marks every copy of a liked song, not only the one tapped (#8126)
A like is on the song: the server likes every copy sharing its song_key,
but the web's optimistic update marked only the tapped track, so the
same song on another album or a compilation showed an empty heart until
the liked ids were fetched again. A track like or unlike now answers 200
with the ids of every copy it changed, the asked one first, and the web
marks them all. Clients that ignore the body (Android) are unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 11:58:13 -05:00

107 lines
4.2 KiB
TypeScript

import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest';
import { QueryClient } from '@tanstack/svelte-query';
vi.mock('./client', () => ({
api: {
get: vi.fn(),
post: vi.fn().mockResolvedValue(null),
del: vi.fn().mockResolvedValue(null)
}
}));
import { likeEntity, unlikeEntity } from './likes';
import { qk } from './queries';
import { api } from './client';
let client: QueryClient;
beforeEach(() => {
vi.clearAllMocks();
client = new QueryClient();
client.setQueryData(qk.likedIds(), {
track_ids: ['t1'],
album_ids: [],
artist_ids: []
});
});
afterEach(() => client.clear());
describe('likes mutations', () => {
test('likeEntity track adds id to track_ids in cache (optimistic)', async () => {
await likeEntity(client, 'track', 't2');
const cached = client.getQueryData(qk.likedIds()) as
| { track_ids: string[] } | undefined;
expect(cached?.track_ids).toContain('t2');
expect(api.post).toHaveBeenCalledWith('/api/likes/tracks/t2', undefined);
});
test('likeEntity album hits albums endpoint and adds to album_ids', async () => {
await likeEntity(client, 'album', 'al1');
const cached = client.getQueryData(qk.likedIds()) as
| { album_ids: string[] } | undefined;
expect(cached?.album_ids).toContain('al1');
expect(api.post).toHaveBeenCalledWith('/api/likes/albums/al1', undefined);
});
test('likeEntity artist adds to artist_ids', async () => {
await likeEntity(client, 'artist', 'ar1');
const cached = client.getQueryData(qk.likedIds()) as
| { artist_ids: string[] } | undefined;
expect(cached?.artist_ids).toContain('ar1');
expect(api.post).toHaveBeenCalledWith('/api/likes/artists/ar1', undefined);
});
test('unlikeEntity removes id from cache', async () => {
await unlikeEntity(client, 'track', 't1');
const cached = client.getQueryData(qk.likedIds()) as
| { track_ids: string[] } | undefined;
expect(cached?.track_ids).not.toContain('t1');
expect(api.del).toHaveBeenCalledWith('/api/likes/tracks/t1');
});
test('likeEntity rolls back on error', async () => {
(api.post as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('boom'));
try {
await likeEntity(client, 'track', 't2');
} catch { /* expected */ }
const cached = client.getQueryData(qk.likedIds()) as
| { track_ids: string[] } | undefined;
expect(cached?.track_ids).not.toContain('t2');
});
test('a failed like reverts only itself, not a like made while it was out', async () => {
let fail!: (e: Error) => void;
(api.post as ReturnType<typeof vi.fn>)
.mockImplementationOnce(() => new Promise((_, reject) => { fail = reject; }))
.mockResolvedValueOnce(null);
const first = likeEntity(client, 'track', 'tA');
await likeEntity(client, 'track', 'tB');
fail(new Error('boom'));
await expect(first).rejects.toThrow('boom');
const cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined;
expect(cached?.track_ids).toContain('tB');
expect(cached?.track_ids).not.toContain('tA');
expect(cached?.track_ids).toContain('t1');
});
test('a like marks every copy of the song the server names, and an unlike clears them', async () => {
(api.post as ReturnType<typeof vi.fn>).mockResolvedValueOnce({ track_ids: ['t2', 't2-best-of'] });
await likeEntity(client, 'track', 't2');
let cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined;
expect(cached?.track_ids).toEqual(['t1', 't2', 't2-best-of']);
(api.del as ReturnType<typeof vi.fn>).mockResolvedValueOnce({ track_ids: ['t2-best-of', 't2'] });
await unlikeEntity(client, 'track', 't2-best-of');
cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined;
expect(cached?.track_ids).toEqual(['t1']);
});
test('a failed unlike puts back only its own like', async () => {
(api.del as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('boom'));
await expect(unlikeEntity(client, 'track', 't1')).rejects.toThrow('boom');
const cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined;
expect(cached?.track_ids).toEqual(['t1']);
});
});