Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: cover SongLinks picker options, label freshness, and removal directly

[._.]/ Adam Eivyea02efc07154
diff --git a/client/src/components/songbook/SongLinks.jsx b/client/src/components/songbook/SongLinks.jsx
index d082ba1687..10a544cb9f 100644
--- a/client/src/components/songbook/SongLinks.jsx
+++ b/client/src/components/songbook/SongLinks.jsx
@@ -94,7 +94,7 @@ export function SongLinksEditor({ links, onChange }) {
const linked = useMemo(() => new Set(links.map(songLinkKey)), [links]);
const options = useMemo(() => (
(recordsByType[type] || [])
- .filter((r) => r?.id && !linked.has(`${type}:${r.id}`))
+ .filter((r) => r?.id && !linked.has(songLinkKey({ type, id: r.id })))
.map((r) => ({ id: r.id, title: r.title || r.id }))
), [recordsByType, type, linked]);
@@ -127,25 +127,27 @@ export function SongLinksEditor({ links, onChange }) {
{links.length > 0 && (
<ul className="space-y-1 mb-2">
- {links.map((link) => (
- <li
- key={songLinkKey(link)}
- className="flex items-center gap-2 px-3 py-2 bg-port-card border border-port-border rounded-lg text-sm"
- >
- <span className="shrink-0 text-xs text-gray-500">{songLinkTypeLabel(link.type)}</span>
- <span className="flex-1 min-w-0 truncate text-gray-200">
- {linkText(link, freshTitles.get(songLinkKey(link)))}
- </span>
- <button
- type="button"
- onClick={() => remove(link)}
- className="p-1 shrink-0 text-gray-500 hover:text-port-error"
- aria-label={`Remove link to ${linkText(link, freshTitles.get(songLinkKey(link)))}`}
+ {links.map((link) => {
+ const key = songLinkKey(link);
+ const text = linkText(link, freshTitles.get(key));
+ return (
+ <li
+ key={key}
+ className="flex items-center gap-2 px-3 py-2 bg-port-card border border-port-border rounded-lg text-sm"
>
- <X size={14} />
- </button>
- </li>
- ))}
+ <span className="shrink-0 text-xs text-gray-500">{songLinkTypeLabel(link.type)}</span>
+ <span className="flex-1 min-w-0 truncate text-gray-200">{text}</span>
+ <button
+ type="button"
+ onClick={() => remove(link)}
+ className="p-1 shrink-0 text-gray-500 hover:text-port-error"
+ aria-label={`Remove link to ${text}`}
+ >
+ <X size={14} />
+ </button>
+ </li>
+ );
+ })}
</ul>
)}
diff --git a/client/src/components/songbook/SongLinks.test.jsx b/client/src/components/songbook/SongLinks.test.jsx
new file mode 100644
index 0000000000..b45844806a
--- /dev/null
+++ b/client/src/components/songbook/SongLinks.test.jsx
@@ -0,0 +1,119 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { render, screen, fireEvent, waitFor, within } from '@testing-library/react';
+import { MemoryRouter } from 'react-router';
+
+// Mock the api barrel — the editor loads both picker lists on mount.
+const api = vi.hoisted(() => ({ listRounds: vi.fn(), listTracks: vi.fn() }));
+vi.mock('../../services/api', () => api);
+
+import { SongLinkChips, SongLinksEditor } from './SongLinks.jsx';
+
+// Invented fixture data only (privacy convention).
+const ROUNDS = [{ id: 'r1', title: 'Example Round' }, { id: 'r2', title: 'Second Round' }];
+const TRACKS = [{ id: 't1', title: 'Example Track' }];
+
+const renderEditor = (links, onChange = vi.fn()) => {
+ render(<MemoryRouter><SongLinksEditor links={links} onChange={onChange} /></MemoryRouter>);
+ return onChange;
+};
+
+describe('SongLinkChips', () => {
+ it('renders nothing for an empty or absent list', () => {
+ const { container, rerender } = render(<MemoryRouter><SongLinkChips links={[]} /></MemoryRouter>);
+ expect(container.textContent).toBe('');
+ rerender(<MemoryRouter><SongLinkChips links={undefined} /></MemoryRouter>);
+ expect(container.textContent).toBe('');
+ });
+
+ it('falls back through fresh title → stored label → raw id, never blank', () => {
+ render(
+ <MemoryRouter>
+ <SongLinkChips links={[
+ { type: 'round', id: 'r1', label: 'Example Round' },
+ { type: 'track', id: 't1', label: '' },
+ ]}
+ />
+ </MemoryRouter>,
+ );
+ expect(screen.getByRole('link', { name: /Example Round/ })).toBeTruthy();
+ // No label stored (a link made before the field carried one) → the id shows.
+ expect(screen.getByRole('link', { name: /t1/ })).toBeTruthy();
+ });
+});
+
+describe('SongLinksEditor', () => {
+ beforeEach(() => {
+ api.listRounds.mockReset().mockResolvedValue({ rounds: ROUNDS });
+ api.listTracks.mockReset().mockResolvedValue(TRACKS);
+ });
+
+ it('hides already-linked records from the picker options', async () => {
+ renderEditor([{ type: 'round', id: 'r1', label: 'Example Round' }]);
+ const target = await screen.findByLabelText('Record to link');
+ // Placeholder + the one unlinked round (r1 is already linked).
+ await waitFor(() => expect(within(target).getAllByRole('option')).toHaveLength(2));
+ expect(within(target).queryByRole('option', { name: 'Example Round' })).toBeNull();
+ expect(within(target).getByRole('option', { name: 'Second Round' })).toBeTruthy();
+ });
+
+ it('switches the option list with the type select and resets the pending target', async () => {
+ renderEditor([]);
+ const target = await screen.findByLabelText('Record to link');
+ await waitFor(() => expect(within(target).getAllByRole('option')).toHaveLength(3));
+ fireEvent.change(target, { target: { value: 'r1' } });
+ expect(target.value).toBe('r1');
+
+ fireEvent.change(screen.getByLabelText('Link type'), { target: { value: 'track' } });
+ // The round id would be meaningless under `track` — the pending pick clears.
+ expect(screen.getByLabelText('Record to link').value).toBe('');
+ expect(within(screen.getByLabelText('Record to link')).getByRole('option', { name: 'Example Track' })).toBeTruthy();
+ });
+
+ // The stored label is a snapshot from link time. When the target IS present
+ // locally its CURRENT title wins, so a renamed record doesn't read stale.
+ it('prefers the target record\'s current title over a stale stored label', async () => {
+ renderEditor([{ type: 'round', id: 'r1', label: 'Old Name' }]);
+ expect(await screen.findByText('Example Round')).toBeTruthy();
+ expect(screen.queryByText('Old Name')).toBeNull();
+ });
+
+ // …but a link whose target is missing on THIS machine (Rounds/Tracks are not
+ // brain records and need not exist on every peer) still shows its name.
+ it('keeps showing the stored label when the target is not on this machine', async () => {
+ renderEditor([{ type: 'round', id: 'elsewhere', label: 'Remote Round' }]);
+ expect(await screen.findByText('Remote Round')).toBeTruthy();
+ });
+
+ it('removes a link through onChange without mutating the passed array', async () => {
+ const links = [{ type: 'round', id: 'r1', label: 'Example Round' }];
+ const onChange = renderEditor(links);
+ fireEvent.click(await screen.findByRole('button', { name: 'Remove link to Example Round' }));
+ expect(onChange).toHaveBeenCalledWith([]);
+ expect(links).toHaveLength(1);
+ });
+
+ it('disables Add until a record is picked', async () => {
+ const onChange = renderEditor([]);
+ const add = await screen.findByRole('button', { name: 'Add link' });
+ expect(add.disabled).toBe(true);
+
+ fireEvent.change(screen.getByLabelText('Link type'), { target: { value: 'track' } });
+ fireEvent.change(await screen.findByLabelText('Record to link'), { target: { value: 't1' } });
+ expect(add.disabled).toBe(false);
+
+ fireEvent.click(add);
+ // The label is captured from the picked record, not left blank.
+ expect(onChange).toHaveBeenCalledWith([{ type: 'track', id: 't1', label: 'Example Track' }]);
+ });
+
+ // `null` (not fetched / failed) must not read the same as `[]` (fetched, this
+ // install genuinely has none) — but both leave the picker unusable, so the
+ // message has to say which.
+ it('distinguishes "loading" from "this install has none"', async () => {
+ api.listRounds.mockResolvedValue({ rounds: [] });
+ renderEditor([]);
+ const target = await screen.findByLabelText('Record to link');
+ await waitFor(() => expect(target.textContent).toContain('No rounds available'));
+ expect(target.disabled).toBe(true);
+ });
+});
diff --git a/client/src/components/songbook/constants.js b/client/src/components/songbook/constants.js
index 66fb15e9a7..d2aea45c36 100644
--- a/client/src/components/songbook/constants.js
+++ b/client/src/components/songbook/constants.js
@@ -81,12 +81,11 @@ export const SONG_FORMATS = ['chordpro', 'tab', 'plain', 'drum'];
// The record kinds a song can link to. Mirrors `songLinkTypeEnum` in
// server/lib/brainValidation.js; `path` is the detail route the chip navigates
-// to (`${path}/${id}`), and `listAll` names the api.js helper the edit-mode
-// picker loads its options from. Parity with the server enum is asserted in
+// to (`${path}/${id}`). Parity with the server enum is asserted in
// constants.test.js.
export const SONG_LINK_TYPES = [
- { id: 'round', label: 'Round', path: '/rounds', listAll: 'listRounds' },
- { id: 'track', label: 'Track', path: '/music/tracks', listAll: 'listTracks' },
+ { id: 'round', label: 'Round', path: '/rounds' },
+ { id: 'track', label: 'Track', path: '/music/tracks' },
];
// Display label for a stored link type. A type this client doesn't know (a song