import { expect, it, vi } from 'vitest'; import { render } from 'vitest-browser-svelte'; import { userEvent } from 'vitest/browser'; import NotesPanel from './NotesPanel.svelte'; import type { NoteCard } from '#lib/server/notes.ts'; import type { NoteType } from '#lib/server/note-types.ts'; const types: NoteType[] = [ { id: 'people', name: 'Person', pluralName: 'People', accent: 'blue-screen', revision: 0, hasTemplate: false } ]; function note(id: string, title: string, typeId: string | null): NoteCard { return { id, title, typeId, typeName: null, accent: 'blue-screen', summary: '', body: null, bodyText: '', revision: 0, image: null }; } it('lists notes whose type is missing under Unfiled and opens the chosen note', async () => { const onset = vi.fn(); await render(NotesPanel, { projectId: 'project', types, notes: [ note('one', 'Maren', 'people'), note('two', 'Lost type', 'gone'), note('three', 'No type', null) ], selectedId: null, noteScenes: [], onset, onscene: vi.fn() }); const groups = Array.from(document.querySelectorAll('.group')); expect(groups.map((group) => group.querySelector('h3')?.textContent?.trim())).toEqual([ 'People 1', 'Unfiled 2' ]); expect(groups[1].textContent).toContain('Lost type'); expect(groups[1].textContent).toContain('No type'); await userEvent.click( Array.from(groups[1].querySelectorAll('button')).find((button) => button.textContent?.includes('Lost type') )! ); expect(onset).toHaveBeenCalledWith('two'); }); it('opens and cancels the new-note form with the first available type selected', async () => { await render(NotesPanel, { projectId: 'project', types, notes: [], selectedId: null, noteScenes: [], onset: vi.fn(), onscene: vi.fn() }); const add = Array.from(document.querySelectorAll('button')).find( (button) => button.textContent?.trim() === 'Add' )!; await userEvent.click(add); expect(document.querySelector('select[aria-label="Note type"]')?.value).toBe( 'people' ); expect(document.querySelector('input[aria-label="Note title"]')).not.toBeNull(); await userEvent.click( Array.from(document.querySelectorAll('button')).find( (button) => button.textContent?.trim() === 'Cancel' )! ); expect(document.querySelector('input[aria-label="Note title"]')).toBeNull(); }); it('renders full-size link for preserved image and plain band for display-only image', async () => { const preservedNote: NoteCard = { ...note('n1', 'Preserved Note', 'people'), image: { noteId: 'n1', imageId: '11111111-1111-1111-1111-111111111111', mime: 'image/png', bytes: 5000, width: 200, height: 150, alt: 'Portrait description', originalName: 'portrait.png', createdAt: new Date() } }; await render(NotesPanel, { projectId: 'p1', types, notes: [preservedNote], selectedId: 'n1', noteScenes: [], onset: vi.fn(), onscene: vi.fn() }); const link = document.querySelector('a.image-band'); expect(link).not.toBeNull(); expect(link?.href).toContain( '/api/projects/p1/images/11111111-1111-1111-1111-111111111111?original=1' ); expect(link?.target).toBe('_blank'); expect(link?.rel).toContain('noopener'); const img = document.querySelector('img.image-media'); expect(img?.src).toContain('/api/projects/p1/images/11111111-1111-1111-1111-111111111111'); expect(img?.alt).toBe('Portrait description'); expect(img?.decoding).toBe('async'); expect(img?.loading).toBe('lazy'); const altInput = document.querySelector('#note-image-alt'); expect(altInput?.value).toBe('Portrait description'); const buttons = Array.from(document.querySelectorAll('button')); expect(buttons.some((b) => b.textContent?.trim() === 'Replace')).toBe(true); expect(buttons.some((b) => b.textContent?.trim() === 'Remove')).toBe(true); }); it('renders plain band without link when image has bytes === 0 (display-only)', async () => { const displayOnlyNote: NoteCard = { ...note('n2', 'Display Only', 'people'), image: { noteId: 'n2', imageId: '22222222-2222-2222-2222-222222222222', mime: 'image/jpeg', bytes: 0, width: 100, height: 100, alt: '', originalName: 'pic.jpg', createdAt: new Date() } }; await render(NotesPanel, { projectId: 'p1', types, notes: [displayOnlyNote], selectedId: 'n2', noteScenes: [], onset: vi.fn(), onscene: vi.fn() }); expect(document.querySelector('a.image-band')).toBeNull(); expect(document.querySelector('div.image-band')).not.toBeNull(); expect(document.querySelector('img.image-media')?.loading).toBe('lazy'); }); it('renders missing image notice when image_id is null', async () => { const missingNote: NoteCard = { ...note('n3', 'Missing Image Note', 'people'), image: { noteId: 'n3', imageId: null, mime: 'image/png', bytes: 0, width: 0, height: 0, alt: '', originalName: 'lost.png', createdAt: new Date() } }; await render(NotesPanel, { projectId: 'p1', types, notes: [missingNote], selectedId: 'n3', noteScenes: [], onset: vi.fn(), onscene: vi.fn() }); expect(document.querySelector('.image-missing')?.textContent).toContain( 'This note names an image the instance does not have.' ); expect(document.querySelector('img.image-media')).toBeNull(); }); it('renders dashed well and respects preserveOriginals flag', async () => { const emptyNote = note('n4', 'Empty Note', 'people'); const { unmount } = await render(NotesPanel, { projectId: 'p1', types, notes: [emptyNote], selectedId: 'n4', noteScenes: [], preserveOriginals: true, onset: vi.fn(), onscene: vi.fn() }); expect(document.querySelector('.image-drop-well')).not.toBeNull(); expect(document.querySelector('.image-preserve-toggle')).not.toBeNull(); expect(document.querySelector('.image-preserve-toggle input')?.checked).toBe( true ); unmount(); await render(NotesPanel, { projectId: 'p1', types, notes: [emptyNote], selectedId: 'n4', noteScenes: [], preserveOriginals: false, onset: vi.fn(), onscene: vi.fn() }); expect(document.querySelector('.image-drop-well')).not.toBeNull(); expect(document.querySelector('.image-preserve-toggle')).toBeNull(); });