release / web (push) Failing after 1m16s
release / go (push) Successful in 1m48s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m0s
release / android (push) Successful in 4m34s
release / Build signed APK (push) Successful in 5m9s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
Test data and example comments named real artists, records and songs, and carried real MusicBrainz ids. One mapping swaps them for invented ones throughout (Fields of Ontario, Plastic Apes, Marlo Davies, ...), keeping what each case tests: sort order, index letter, escaping, title keys, artist-prefix stripping. A test username is no longer the operator's name. The cover-art and Last.fm connection checks still query real releases; they reach live services and need something that exists there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
213 lines
7.6 KiB
TypeScript
213 lines
7.6 KiB
TypeScript
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||
import { render, screen, fireEvent, waitFor } from '@testing-library/svelte';
|
||
import { tick } from 'svelte';
|
||
import { writable } from 'svelte/store';
|
||
import { mockQuery } from '../../test-utils/query';
|
||
|
||
const invalidateMock = vi.fn();
|
||
vi.mock('@tanstack/svelte-query', async (orig) => {
|
||
const actual = (await orig()) as Record<string, unknown>;
|
||
return { ...actual, useQueryClient: () => ({ invalidateQueries: invalidateMock }) };
|
||
});
|
||
|
||
vi.mock('#lib/api/notifications.js', async (orig) => {
|
||
const actual = (await orig()) as Record<string, unknown>;
|
||
return {
|
||
badgeLabel: actual.badgeLabel,
|
||
readAllCutoff: actual.readAllCutoff,
|
||
createUnreadCountQuery: vi.fn(),
|
||
createNotificationsQuery: vi.fn(),
|
||
markNotificationRead: vi.fn().mockResolvedValue(undefined),
|
||
markAllNotificationsRead: vi.fn().mockResolvedValue(undefined)
|
||
};
|
||
});
|
||
|
||
import NotificationBell from './NotificationBell.svelte';
|
||
import { goto } from '$app/navigation';
|
||
import {
|
||
badgeLabel,
|
||
createNotificationsQuery,
|
||
createUnreadCountQuery,
|
||
markAllNotificationsRead,
|
||
markNotificationRead,
|
||
readAllCutoff,
|
||
type NotificationItem
|
||
} from '#lib/api/notifications.js';
|
||
|
||
function item(over: Partial<NotificationItem> = {}): NotificationItem {
|
||
return {
|
||
id: 'n1',
|
||
kind: 'new_music',
|
||
title: 'New album',
|
||
body: 'Fields of Ontario – Geomantic is in the library.',
|
||
link: '/albums/al-1',
|
||
created_at: new Date().toISOString(),
|
||
read_at: null,
|
||
...over
|
||
};
|
||
}
|
||
|
||
function setInbox(items: NotificationItem[], unread = items.filter((i) => !i.read_at).length) {
|
||
(createUnreadCountQuery as ReturnType<typeof vi.fn>).mockReturnValue(mockQuery({ data: unread }));
|
||
(createNotificationsQuery as ReturnType<typeof vi.fn>).mockReturnValue(
|
||
mockQuery({ data: { items, unread_count: unread } })
|
||
);
|
||
}
|
||
|
||
async function openPanel() {
|
||
await fireEvent.click(screen.getByRole('button', { name: /notifications/i }));
|
||
}
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks();
|
||
});
|
||
|
||
describe('badgeLabel', () => {
|
||
test('is empty at zero, the count to nine, then 9+', () => {
|
||
expect(badgeLabel(0)).toBe('');
|
||
expect(badgeLabel(1)).toBe('1');
|
||
expect(badgeLabel(9)).toBe('9');
|
||
expect(badgeLabel(10)).toBe('9+');
|
||
expect(badgeLabel(250)).toBe('9+');
|
||
});
|
||
});
|
||
|
||
describe('NotificationBell', () => {
|
||
test('shows the unread count on the badge and in the label', () => {
|
||
setInbox([item(), item({ id: 'n2' }), item({ id: 'n3' })]);
|
||
render(NotificationBell);
|
||
expect(screen.getByTestId('notification-badge')).toHaveTextContent('3');
|
||
expect(screen.getByRole('button', { name: 'Notifications, 3 unread' })).toBeInTheDocument();
|
||
});
|
||
|
||
test('caps the badge at 9+', () => {
|
||
setInbox([item()], 42);
|
||
render(NotificationBell);
|
||
expect(screen.getByTestId('notification-badge')).toHaveTextContent('9+');
|
||
});
|
||
|
||
test('no badge when everything is read', () => {
|
||
setInbox([item({ read_at: new Date().toISOString() })]);
|
||
render(NotificationBell);
|
||
expect(screen.queryByTestId('notification-badge')).toBeNull();
|
||
expect(screen.getByRole('button', { name: 'Notifications' })).toBeInTheDocument();
|
||
});
|
||
|
||
test('lists each kind with the server-rendered title and body, unread marked', async () => {
|
||
setInbox([
|
||
item({ id: 'a', kind: 'new_music', title: 'New album', body: 'Geomantic is in the library.' }),
|
||
item({
|
||
id: 'b',
|
||
kind: 'tracks_missing',
|
||
title: '4 tracks went missing',
|
||
body: 'Their files are no longer where the library expects them.',
|
||
link: '/collection/missing',
|
||
read_at: new Date().toISOString()
|
||
})
|
||
]);
|
||
render(NotificationBell);
|
||
await openPanel();
|
||
|
||
const rows = screen.getAllByTestId('notification-item');
|
||
expect(rows).toHaveLength(2);
|
||
expect(rows[0]).toHaveTextContent('New album');
|
||
expect(rows[0]).toHaveTextContent('Geomantic is in the library.');
|
||
expect(rows[0]).toHaveAttribute('data-unread', 'true');
|
||
expect(rows[1]).toHaveTextContent('4 tracks went missing');
|
||
expect(rows[1]).not.toHaveAttribute('data-unread');
|
||
});
|
||
|
||
test('clicking an unread item marks it read and opens its subject', async () => {
|
||
setInbox([item({ id: 'n7', link: '/albums/al-7' })]);
|
||
render(NotificationBell);
|
||
await openPanel();
|
||
await fireEvent.click(screen.getByTestId('notification-item'));
|
||
|
||
await waitFor(() => expect(goto).toHaveBeenCalledWith('/albums/al-7'));
|
||
expect(markNotificationRead).toHaveBeenCalledWith('n7');
|
||
expect(invalidateMock).toHaveBeenCalledWith({ queryKey: ['notifications'] });
|
||
expect(screen.queryByRole('dialog', { name: 'Notifications' })).toBeNull();
|
||
});
|
||
|
||
test('clicking a read item only opens it', async () => {
|
||
setInbox([item({ read_at: new Date().toISOString(), link: '/collection/missing' })]);
|
||
render(NotificationBell);
|
||
await openPanel();
|
||
await fireEvent.click(screen.getByTestId('notification-item'));
|
||
|
||
await waitFor(() => expect(goto).toHaveBeenCalledWith('/collection/missing'));
|
||
expect(markNotificationRead).not.toHaveBeenCalled();
|
||
});
|
||
|
||
test('opening the panel marks what it shows read, up to the newest notice', async () => {
|
||
setInbox([
|
||
item({ id: 'new', created_at: '2026-10-23T12:00:00.123456Z' }),
|
||
item({ id: 'older', created_at: '2026-10-23T11:00:00Z' })
|
||
]);
|
||
render(NotificationBell);
|
||
expect(markAllNotificationsRead).not.toHaveBeenCalled();
|
||
|
||
await openPanel();
|
||
|
||
await waitFor(() =>
|
||
expect(markAllNotificationsRead).toHaveBeenCalledWith('2026-10-23T12:00:00.124Z')
|
||
);
|
||
expect(markAllNotificationsRead).toHaveBeenCalledTimes(1);
|
||
expect(invalidateMock).toHaveBeenCalledWith({ queryKey: ['notifications'] });
|
||
});
|
||
|
||
test('a notice read on opening still looks new until the panel closes', async () => {
|
||
const page = (items: NotificationItem[]) => ({
|
||
data: { items, unread_count: 0 },
|
||
isPending: false,
|
||
isError: false,
|
||
refetch: () => {}
|
||
});
|
||
const list = writable(page([item({ id: 'n1' })]));
|
||
(createUnreadCountQuery as ReturnType<typeof vi.fn>).mockReturnValue(mockQuery({ data: 1 }));
|
||
(createNotificationsQuery as ReturnType<typeof vi.fn>).mockReturnValue(list);
|
||
render(NotificationBell);
|
||
await openPanel();
|
||
await waitFor(() => expect(markAllNotificationsRead).toHaveBeenCalled());
|
||
|
||
// The refetch after marking brings the row back read.
|
||
list.set(page([item({ id: 'n1', read_at: new Date().toISOString() })]));
|
||
await tick();
|
||
expect(screen.getByTestId('notification-item')).toHaveAttribute('data-unread', 'true');
|
||
|
||
await openPanel(); // closes
|
||
await openPanel(); // opens again
|
||
expect(screen.getByTestId('notification-item')).not.toHaveAttribute('data-unread');
|
||
});
|
||
|
||
test('opening an inbox with nothing unread marks nothing', async () => {
|
||
setInbox([item({ read_at: new Date().toISOString() })]);
|
||
render(NotificationBell);
|
||
await openPanel();
|
||
expect(markAllNotificationsRead).not.toHaveBeenCalled();
|
||
});
|
||
|
||
test('an empty inbox says so', async () => {
|
||
setInbox([]);
|
||
render(NotificationBell);
|
||
await openPanel();
|
||
expect(screen.getByText('Nothing waiting for you.')).toBeInTheDocument();
|
||
expect(markAllNotificationsRead).not.toHaveBeenCalled();
|
||
});
|
||
});
|
||
|
||
describe('readAllCutoff', () => {
|
||
test('is a millisecond past the newest notice, so its microseconds are covered', () => {
|
||
expect(
|
||
readAllCutoff([
|
||
item({ created_at: '2026-10-23T11:00:00Z' }),
|
||
item({ created_at: '2026-10-23T12:00:00.123456Z' })
|
||
])
|
||
).toBe('2026-10-23T12:00:00.124Z');
|
||
});
|
||
|
||
test('is undefined with nothing to mark', () => {
|
||
expect(readAllCutoff([])).toBeUndefined();
|
||
});
|
||
});
|