Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

64.9 KB
Raw Blame
import type { DatabaseOpsError } from '@service-storage/databases';
import {
  cleanup,
  fireEvent,
  render,
  screen,
  waitFor,
  within,
} from '@solidjs/testing-library';
import userEvent from '@testing-library/user-event';
import { err, ok, okAsync, type Result, ResultAsync } from 'neverthrow';
import { createSignal } from 'solid-js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { LaneKey } from '../../../lib/core/database-sql/generated/types';
import {
  type OptionEditing,
  OptionEditingContext,
} from '../context/option-editing';
import type { DatabaseViewColumn } from '../core/database-view';
import type { DatabaseRow } from '../core/table';
import { boardLane } from '../tests/board-layout';
import { BoardLaneHeader } from './board-lane-header';
import { DatabaseBoard } from './database-board';
import { GridCell } from './grid-cell';

let menuStyles: HTMLStyleElement;
beforeEach(() => {
  vi.spyOn(window, 'scrollTo').mockImplementation(() => {});
  // JSDOM reports an empty animation name; presence expects CSS's default none.
  menuStyles = document.createElement('style');
  // Include the dialog overlay so its presence also finishes on close.
  menuStyles.textContent = '* { animation-name: none; }';
  document.head.append(menuStyles);
});
afterEach(() => {
  cleanup();
  menuStyles.remove();
  vi.restoreAllMocks();
});

describe('database board', () => {
  it('draws a lane per person and moves a card to another person', async () => {
    const owner: DatabaseViewColumn = {
      id: 'owner',
      name: 'Owner',
      dataType: 'ENTITY',
      specificEntityType: 'USER',
      isMultiSelect: false,
      options: [],
      writable: true,
    };
    const onMove = vi.fn();
    render(() => (
      <DatabaseBoard
        rows={[
          {
            rowId: 'launch',
            cells: { title: 'Launch project', owner: 'macro|[email protected]' },
          },
          { rowId: 'loose', cells: { title: 'Loose end', owner: null } },
        ]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          owner,
        ]}
        board={{
          lanes: [
            { key: { kind: 'none' }, hidden: false, cards: ['loose'] },
            {
              key: { kind: 'user', id: 'macro|[email protected]' },
              hidden: false,
              cards: [],
            },
            {
              key: { kind: 'user', id: 'macro|[email protected]' },
              hidden: false,
              cards: ['launch'],
            },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'owner',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={owner}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={onMove}
        onCreate={vi.fn(async () => true)}
      />
    ));

    expect(
      screen
        .getAllByRole('region')
        .filter((lane) => lane.hasAttribute('data-kanban-lane'))
        .map((lane) => lane.getAttribute('aria-label'))
    ).toEqual(['[email protected] lane', '[email protected] lane']);
    expect(screen.queryByRole('button', { name: 'New group' })).toBeNull();
    const trigger = screen.getByRole('button', { name: 'Move Launch project' });
    trigger.focus();
    fireEvent.keyDown(trigger, { key: 'Enter' });
    fireEvent.keyDown(
      await screen.findByRole('menuitem', { name: '[email protected]' }),
      { key: 'Enter' }
    );
    await waitFor(() =>
      expect(onMove).toHaveBeenCalledWith(
        'launch',
        { kind: 'user', id: 'macro|[email protected]' },
        undefined
      )
    );
  });

  it('draws the lanes the board lays out, in its order, and leaves hidden lanes out', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [
        { id: 'todo', label: 'To do', color: null },
        { id: 'done', label: 'Done', color: null },
        { id: 'archived', label: 'Archived', color: null },
      ],
      writable: true,
    };
    render(() => (
      <DatabaseBoard
        rows={[
          {
            rowId: 'launch',
            cells: { title: 'Launch project', status: 'To do' },
          },
          {
            rowId: 'unassigned',
            cells: { title: 'Unassigned record', status: null },
          },
          { rowId: 'old', cells: { title: 'Old record', status: 'Archived' } },
        ]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
            {
              key: { kind: 'option', id: 'todo' },
              hidden: false,
              cards: ['launch'],
            },
            {
              key: { kind: 'option', id: 'archived' },
              hidden: true,
              cards: ['old'],
            },
            { key: { kind: 'none' }, hidden: false, cards: ['unassigned'] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
      />
    ));
    expect(
      screen
        .getAllByRole('region')
        .filter((lane) => lane.hasAttribute('data-kanban-lane'))
        .map((lane) => lane.getAttribute('aria-label'))
    ).toEqual(['Done lane', 'To do lane']);
    expect(
      screen.queryByRole('button', { name: 'Open Unassigned record' })
    ).toBeNull();
    expect(
      screen.queryByRole('button', { name: 'Open Old record' })
    ).toBeNull();
    expect(
      within(boardLane('Done lane')).getByText('Drop a record here')
    ).toBeTruthy();
  });

  it("shows the layout's card fields in order, skipping the title and empty fields", () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    render(() => (
      <DatabaseBoard
        rows={[
          {
            rowId: 'launch',
            cells: {
              title: 'Launch project',
              status: 'To do',
              owner: 'Ada',
              team: 'Design',
              notes: 'Bring draft',
              due: null,
            },
          },
        ]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
          {
            id: 'owner',
            name: 'Owner',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          {
            id: 'team',
            name: 'Team',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          {
            id: 'notes',
            name: 'Notes',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          {
            id: 'due',
            name: 'Due',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
        ]}
        board={{
          lanes: [
            {
              key: { kind: 'option', id: 'todo' },
              hidden: false,
              cards: ['launch'],
            },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: ['notes', 'title', 'due', 'owner'],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
      />
    ));
    const card = screen.getByRole('button', { name: 'Open Launch project' });
    expect(within(card).getByText('Launch project')).toBeTruthy();
    expect(within(card).queryByText('Design')).toBeNull();
    expect(
      Array.from(
        card.closest('article')!.querySelectorAll('[data-card-field]'),
        (element) => element.getAttribute('title')
      )
    ).toEqual(['Notes: Bring draft', 'Owner: Ada']);
  });

  it.each(['STRING', 'NUMBER'] as const)(
    'reserves usable editor space for empty %s card fields',
    async (dataType) => {
      const title: DatabaseViewColumn = {
        id: 'title',
        name: 'Name',
        dataType: 'STRING',
        isMultiSelect: false,
        options: [],
        writable: true,
      };
      const status: DatabaseViewColumn = {
        ...title,
        id: 'status',
        name: 'Status',
        dataType: 'SELECT_STRING',
        options: [{ id: 'todo', label: 'To do', color: null }],
      };
      const value: DatabaseViewColumn = {
        ...title,
        id: 'value',
        name: 'Value',
        dataType,
      };
      const onWrite = vi.fn(async () => true);
      render(() => (
        <DatabaseBoard
          rows={[
            {
              rowId: 'record',
              cells: { title: 'Empty field', status: 'To do', value: null },
            },
          ]}
          columns={[title, status, value]}
          groupColumn={status}
          board={{
            lanes: [
              {
                key: { kind: 'option', id: 'todo' },
                hidden: false,
                cards: ['record'],
              },
            ],
          }}
          layout={{
            kind: 'board',
            title: 'title',
            groupBy: 'status',
            lanes: [],
            cardFields: ['value'],
            hideEmptyLanes: false,
          }}
          canEdit
          rowPending={() => false}
          onOpen={vi.fn()}
          onCreate={vi.fn(async () => true)}
          renderCell={(row, column, options) => (
            <GridCell
              {...options}
              column={column()}
              value={row().cells[column().id]}
              canEdit
              onWrite={onWrite}
              onAddOption={vi.fn(async () => true)}
            />
          )}
        />
      ));
      const field = screen.getByLabelText('Value');
      expect(field.classList.contains('min-w-16')).toBe(true);
      const display = within(field).getByRole('button', {
        name: 'Value: —. Click to edit',
      });
      // JSDOM has no layout; the board's reserved display area is 64 by 24.
      vi.spyOn(display, 'getBoundingClientRect').mockReturnValue(
        new DOMRect(0, 0, 64, 24)
      );
      fireEvent.click(display);
      const editor = within(field).getByRole('textbox', { name: 'Edit Value' });
      const cell = field.firstElementChild as HTMLDivElement;
      expect(cell.style.width).toBe('64px');
      expect(cell.style.height).toBe('24px');
      fireEvent.input(editor, {
        target: { value: dataType === 'NUMBER' ? '123.45' : 'Readable' },
      });
      expect(cell.style.width).toBe('64px');
      fireEvent.keyDown(editor, { key: 'Enter' });
      await waitFor(() =>
        expect(onWrite).toHaveBeenCalledExactlyOnceWith(
          dataType === 'NUMBER' ? 123.45 : 'Readable'
        )
      );
      expect(cell.style.width).toBe('');
      expect(field.classList.contains('min-w-16')).toBe(true);
    }
  );

  it.each([false, true])(
    'keeps an invalid card-field draft after another field saves (refresh columns: %s)',
    async (refreshColumns) => {
      const title: DatabaseViewColumn = {
        id: 'title',
        name: 'Name',
        dataType: 'STRING',
        isMultiSelect: false,
        options: [],
        writable: true,
      };
      const [columns, setColumns] = createSignal<DatabaseViewColumn[]>([
        title,
        {
          ...title,
          id: 'status',
          name: 'Status',
          dataType: 'SELECT_STRING',
          options: [{ id: 'todo', label: 'To do', color: null }],
        },
        { ...title, id: 'amount', name: 'Amount', dataType: 'NUMBER' },
        { ...title, id: 'notes', name: 'Notes' },
      ]);
      const [rows, setRows] = createSignal<DatabaseRow[]>([
        {
          rowId: 'record',
          cells: {
            title: 'Keep my draft',
            status: 'To do',
            amount: 12,
            notes: 'Original notes',
          },
        },
      ]);
      const onWrite = vi.fn(
        async (
          rowId: string,
          columnId: string,
          value: DatabaseRow['cells'][string]
        ) => {
          setRows((current) =>
            current.map((row) => ({
              ...row,
              cells: {
                ...row.cells,
                ...(row.rowId === rowId ? { [columnId]: value } : {}),
              },
            }))
          );
          if (refreshColumns)
            setColumns((current) =>
              current.map((column) => ({
                ...column,
                options: column.options.map((option) => ({ ...option })),
              }))
            );
          return true;
        }
      );
      render(() => (
        <DatabaseBoard
          rows={rows()}
          columns={columns()}
          groupColumn={columns()[1]}
          board={{
            lanes: [
              {
                key: { kind: 'option', id: 'todo' },
                hidden: false,
                cards: ['record'],
              },
            ],
          }}
          layout={{
            kind: 'board',
            title: 'title',
            groupBy: 'status',
            lanes: [],
            cardFields: ['amount', 'notes'],
            hideEmptyLanes: false,
          }}
          canEdit
          rowPending={() => false}
          onOpen={vi.fn()}
          onCreate={vi.fn(async () => true)}
          renderCell={(row, column, options) => (
            <GridCell
              {...options}
              column={column()}
              value={row().cells[column().id]}
              canEdit
              onWrite={(value) => onWrite(row().rowId, column().id, value)}
              onAddOption={vi.fn(async () => true)}
            />
          )}
        />
      ));
      await userEvent.click(
        screen.getByRole('button', { name: /^Amount: 12\./ })
      );
      const invalidInput = screen.getByRole('textbox', { name: 'Edit Amount' });
      await userEvent.clear(invalidInput);
      await userEvent.type(invalidInput, 'invalid{Enter}');
      expect(invalidInput.getAttribute('aria-invalid')).toBe('true');
      await userEvent.click(
        screen.getByRole('button', { name: /^Notes: Original notes\./ })
      );
      const notesInput = screen.getByRole('textbox', { name: 'Edit Notes' });
      await userEvent.clear(notesInput);
      await userEvent.type(notesInput, 'Saved notes{Enter}');
      await waitFor(() =>
        expect(onWrite).toHaveBeenCalledExactlyOnceWith(
          'record',
          'notes',
          'Saved notes'
        )
      );
      expect(
        screen.getByRole('button', { name: /^Notes: Saved notes\./ })
      ).toBeTruthy();
      expect(screen.getByRole('textbox', { name: 'Edit Amount' })).toBe(
        invalidInput
      );
      expect((invalidInput as HTMLInputElement).value).toBe('invalid');
      expect(invalidInput.getAttribute('aria-invalid')).toBe('true');
      expect(screen.queryByRole('alert')).toBeNull();
      invalidInput.focus();
      expect(screen.getByRole('alert').textContent).toBe(
        'Enter a valid number'
      );
      await userEvent.clear(invalidInput);
      await userEvent.type(invalidInput, '24{Enter}');
      await waitFor(() =>
        expect(onWrite).toHaveBeenLastCalledWith('record', 'amount', 24)
      );
      expect(onWrite).toHaveBeenCalledTimes(2);
    }
  );

  it('heads each card with its title column, an entity as its chip, never repeated among the fields', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    render(() => (
      <DatabaseBoard
        rows={[
          {
            rowId: 'launch',
            cells: {
              title: 'Launch project',
              status: 'To do',
              owner: 'macro|[email protected]',
            },
          },
          {
            rowId: 'unowned',
            cells: { title: 'Tidy backlog', status: 'To do', owner: null },
          },
        ]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
          {
            id: 'owner',
            name: 'Owner',
            dataType: 'ENTITY',
            specificEntityType: 'USER',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
        ]}
        board={{
          lanes: [
            {
              key: { kind: 'option', id: 'todo' },
              hidden: false,
              cards: ['launch', 'unowned'],
            },
          ],
        }}
        layout={{
          kind: 'board',
          groupBy: 'status',
          title: 'owner',
          lanes: [],
          cardFields: ['owner', 'title'],
          hideEmptyLanes: false,
        }}
        renderMentionValue={(id, type) => (
          <span data-testid="mention">{`${type} ${id}`}</span>
        )}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
      />
    ));
    const owned = screen.getByRole('button', { name: 'Open Linked record' });
    expect(within(owned).getByRole('heading', { level: 3 }).textContent).toBe(
      'USER macro|[email protected]'
    );
    expect(within(owned).getByTestId('mention')).toBeTruthy();
    expect(
      Array.from(
        owned.closest('article')!.querySelectorAll('[data-card-field]'),
        (element) => element.getAttribute('title')
      )
    ).toEqual(['Name: Launch project']);
    const unowned = screen.getByRole('button', { name: 'Open Unnamed' });
    expect(within(unowned).getByRole('heading', { level: 3 }).textContent).toBe(
      'Unnamed'
    );
    expect(
      Array.from(
        unowned.closest('article')!.querySelectorAll('[data-card-field]'),
        (element) => element.getAttribute('title')
      )
    ).toEqual(['Name: Tidy backlog']);
  });

  it('creates and opens a record when the card title is not text it can type into', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    const onCreate = vi.fn(async () => true);
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
          {
            id: 'due',
            name: 'Due',
            dataType: 'DATE',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          groupBy: 'status',
          title: 'due',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={onCreate}
      />
    ));
    const lane = boardLane('To do lane');
    fireEvent.click(within(lane).getByRole('button', { name: 'New record' }));
    expect(within(lane).queryByRole('textbox')).toBeNull();
    fireEvent.click(within(lane).getByRole('button', { name: 'Add record' }));
    expect(onCreate).toHaveBeenCalledExactlyOnceWith(
      { kind: 'option', id: 'todo' },
      '',
      expect.any(String),
      { open: true }
    );
  });

  it("hands a lane's new record to the host's form instead of an inline card", () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    const onCreate = vi.fn(async () => true);
    const onCompose = vi.fn();
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          groupBy: 'status',
          title: 'title',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={onCreate}
        onCompose={onCompose}
      />
    ));
    const lane = boardLane('To do lane');
    fireEvent.click(within(lane).getByRole('button', { name: 'New record' }));
    expect(onCompose).toHaveBeenCalledExactlyOnceWith({
      kind: 'option',
      id: 'todo',
    });
    expect(within(lane).queryByRole('textbox')).toBeNull();
    expect(onCreate).not.toHaveBeenCalled();
  });

  it("moves a card from its Move menu into the chosen lane's option", async () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [
        { id: 'todo', label: 'To do', color: null },
        { id: 'done', label: 'Done', color: null },
      ],
      writable: true,
    };
    const onMove = vi.fn();
    render(() => (
      <DatabaseBoard
        rows={[
          {
            rowId: 'launch',
            cells: { title: 'Launch project', status: 'To do' },
          },
        ]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'none' }, hidden: false, cards: [] },
            {
              key: { kind: 'option', id: 'todo' },
              hidden: false,
              cards: ['launch'],
            },
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={onMove}
        onCreate={vi.fn(async () => true)}
      />
    ));
    const trigger = screen.getByRole('button', { name: 'Move Launch project' });
    trigger.focus();
    fireEvent.keyDown(trigger, { key: 'Enter' });
    fireEvent.keyDown(await screen.findByRole('menuitem', { name: 'Done' }), {
      key: 'Enter',
    });
    await waitFor(() =>
      expect(onMove).toHaveBeenCalledWith(
        'launch',
        { kind: 'option', id: 'done' },
        undefined
      )
    );
    await waitFor(() =>
      expect(screen.getByRole('status').textContent).toBe(
        'Launch project moved to Done.'
      )
    );
    trigger.focus();
    fireEvent.keyDown(trigger, { key: 'Enter' });
    expect(screen.queryByRole('menuitem', { name: 'No status' })).toBeNull();
  });

  it('hides a lane from its lane menu, the lane without an option as null', async () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    const onHideLane = vi.fn();
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'none' }, hidden: false, cards: [] },
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onHideLane={onHideLane}
        onCreate={vi.fn(async () => true)}
      />
    ));
    const todoMenu = screen.getByRole('button', { name: 'To do lane menu' });
    todoMenu.focus();
    fireEvent.keyDown(todoMenu, { key: 'Enter' });
    fireEvent.keyDown(
      await screen.findByRole('menuitem', { name: 'Hide lane' }),
      { key: 'Enter' }
    );
    await waitFor(() =>
      expect(onHideLane).toHaveBeenCalledWith({ kind: 'option', id: 'todo' })
    );
    expect(
      screen.queryByRole('menuitemcheckbox', { name: 'Hide empty lanes' })
    ).toBeNull();
    expect(
      screen.queryByRole('button', { name: 'No status lane menu' })
    ).toBeNull();
  });

  it('has no lane menu when the view cannot change', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
      />
    ));
    expect(
      screen.queryByRole('button', { name: 'To do lane menu' })
    ).toBeNull();
    expect(
      screen.getByRole('button', { name: 'Add record to To do' })
    ).toBeTruthy();
  });

  it("offers an option's editor on its lane header only to editors with option editing", () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    const [canEdit, setCanEdit] = createSignal(true);
    render(() => (
      <OptionEditingContext.Provider
        value={{
          update: vi.fn(() => okAsync(undefined)),
          remove: vi.fn(() => okAsync(undefined)),
        }}
      >
        <DatabaseBoard
          rows={[]}
          columns={[
            {
              id: 'title',
              name: 'Name',
              dataType: 'STRING',
              isMultiSelect: false,
              options: [],
              writable: true,
            },
            status,
          ]}
          board={{
            lanes: [
              { key: { kind: 'none' }, hidden: false, cards: [] },
              { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
            ],
          }}
          layout={{
            kind: 'board',
            title: 'title',
            groupBy: 'status',
            lanes: [],
            cardFields: [],
            hideEmptyLanes: false,
          }}
          groupColumn={status}
          canEdit={canEdit()}
          rowPending={() => false}
          onOpen={vi.fn()}
          onMove={vi.fn()}
          onCreate={vi.fn(async () => true)}
        />
      </OptionEditingContext.Provider>
    ));
    expect(
      within(boardLane('To do lane')).getByRole('button', {
        name: 'Rename To do',
      })
    ).toBeTruthy();
    expect(screen.queryByRole('region', { name: 'No status lane' })).toBeNull();
    setCanEdit(false);
    expect(screen.queryByRole('button', { name: 'Rename To do' })).toBeNull();
    expect(
      screen.queryByRole('button', { name: 'Change To do color' })
    ).toBeNull();
  });

  it('has no option editor without option editing', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
      />
    ));
    expect(screen.queryByRole('button', { name: 'Rename To do' })).toBeNull();
    expect(
      screen.queryByRole('button', { name: 'Change To do color' })
    ).toBeNull();
  });

  it('reorders lanes with Alt+Arrow, naming every lane in the new order', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [
        { id: 'todo', label: 'To do', color: null },
        { id: 'done', label: 'Done', color: null },
      ],
      writable: true,
    };
    const onLaneOrderChange = vi.fn();
    const onMove = vi.fn();
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
            { key: { kind: 'none' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={onMove}
        onLaneOrderChange={onLaneOrderChange}
        onCreate={vi.fn(async () => true)}
      />
    ));
    fireEvent.keyDown(
      screen.getByRole('button', { name: 'Reorder Done lane' }),
      {
        key: 'ArrowRight',
        altKey: true,
      }
    );
    expect(onLaneOrderChange).toHaveBeenCalledWith([
      { kind: 'option', id: 'todo' },
      { kind: 'option', id: 'done' },
      { kind: 'none' },
    ]);
    expect(onMove).not.toHaveBeenCalled();
  });

  it('keeps a failed new-group draft and lets the user retry it', async () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    const onAddGroup = vi.fn(
      async (_label: string): Promise<Result<void, DatabaseOpsError>> =>
        ok(undefined)
    );
    onAddGroup.mockResolvedValueOnce(
      err({
        code: 'NETWORK_ERROR',
        message: 'Connection unavailable',
        refusal: null,
      })
    );
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
        onAddGroup={onAddGroup}
      />
    ));
    fireEvent.click(screen.getByRole('button', { name: 'New group' }));
    const input = screen.getByRole('textbox', {
      name: 'New group name',
    }) as HTMLInputElement;
    fireEvent.input(input, { target: { value: 'In review' } });
    fireEvent.click(screen.getByRole('button', { name: 'Add group' }));
    await waitFor(() =>
      expect(screen.getByRole('alert').textContent).toBe(
        'Your change could not be sent. Check your connection.'
      )
    );
    expect(input.value).toBe('In review');
    fireEvent.click(screen.getByRole('button', { name: 'Add group' }));
    await waitFor(() => expect(onAddGroup).toHaveBeenCalledTimes(2));
    expect(onAddGroup).toHaveBeenLastCalledWith('In review');
  });

  it('refuses a new group named like an existing option', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    const onAddGroup = vi.fn(
      async (_label: string): Promise<Result<void, DatabaseOpsError>> =>
        ok(undefined)
    );
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
        onAddGroup={onAddGroup}
      />
    ));
    fireEvent.click(screen.getByRole('button', { name: 'New group' }));
    fireEvent.input(screen.getByRole('textbox', { name: 'New group name' }), {
      target: { value: 'to DO' },
    });
    fireEvent.click(screen.getByRole('button', { name: 'Add group' }));
    expect(screen.getByRole('alert').textContent).toBe(
      'A group with this name already exists.'
    );
    expect(onAddGroup).not.toHaveBeenCalled();
  });

  it('refuses a new group matching an existing option label once both are trimmed', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'done', label: 'Done ', color: null }],
      writable: true,
    };
    const onAddGroup = vi.fn(
      async (_label: string): Promise<Result<void, DatabaseOpsError>> =>
        ok(undefined)
    );
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
        onAddGroup={onAddGroup}
      />
    ));
    fireEvent.click(screen.getByRole('button', { name: 'New group' }));
    fireEvent.input(screen.getByRole('textbox', { name: 'New group name' }), {
      target: { value: '  DONE  ' },
    });
    fireEvent.click(screen.getByRole('button', { name: 'Add group' }));
    expect(screen.getByRole('alert').textContent).toBe(
      'A group with this name already exists.'
    );
    expect(onAddGroup).not.toHaveBeenCalled();
  });

  it('validates numeric groups and sends their canonical labels', async () => {
    const amount: DatabaseViewColumn = {
      id: 'amount',
      name: 'Amount',
      dataType: 'SELECT_NUMBER',
      isMultiSelect: false,
      options: [{ id: 'two', label: '2', color: null }],
      writable: true,
    };
    const onAddGroup = vi.fn(
      async (_label: string): Promise<Result<void, DatabaseOpsError>> =>
        ok(undefined)
    );
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          amount,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'two' }, hidden: false, cards: [] },
            { key: { kind: 'none' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'amount',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={amount}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
        onAddGroup={onAddGroup}
      />
    ));
    fireEvent.click(screen.getByRole('button', { name: 'New group' }));
    fireEvent.input(screen.getByRole('textbox', { name: 'New group name' }), {
      target: { value: 'not a number' },
    });
    fireEvent.click(screen.getByRole('button', { name: 'Add group' }));
    expect(screen.getByRole('alert').textContent).toContain('valid number');
    expect(onAddGroup).not.toHaveBeenCalled();
    fireEvent.input(screen.getByRole('textbox', { name: 'New group name' }), {
      target: { value: '1.0' },
    });
    fireEvent.click(screen.getByRole('button', { name: 'Add group' }));
    await waitFor(() => expect(onAddGroup).toHaveBeenCalledWith('1'));
  });

  it("Enter saves a card into its lane's option and opens an empty card below it", () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [
        { id: 'todo', label: 'To do', color: null },
        { id: 'done', label: 'Done', color: null },
      ],
      writable: true,
    };
    const onCreate = vi.fn(async () => true);
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={onCreate}
      />
    ));
    const lane = boardLane('Done lane');
    fireEvent.click(within(lane).getByRole('button', { name: 'New record' }));
    const first = within(lane).getByRole('textbox', {
      name: 'New record title',
    }) as HTMLTextAreaElement;
    expect(document.activeElement).toBe(first);
    fireEvent.input(first, { target: { value: 'First idea' } });
    fireEvent.keyDown(first, { key: 'Enter' });
    expect(onCreate).toHaveBeenCalledWith(
      { kind: 'option', id: 'done' },
      'First idea',
      expect.any(String)
    );
    const next = within(lane).getByRole('textbox', {
      name: 'New record title',
    }) as HTMLTextAreaElement;
    expect(next).not.toBe(first);
    expect(next.value).toBe('');
    expect(document.activeElement).toBe(next);
    fireEvent.input(next, { target: { value: 'Second idea' } });
    fireEvent.keyDown(next, { key: 'Enter' });
    expect(onCreate).toHaveBeenLastCalledWith(
      { kind: 'option', id: 'done' },
      'Second idea',
      expect.any(String)
    );
    expect(onCreate).toHaveBeenCalledTimes(2);
    expect(within(boardLane('To do lane')).queryByRole('textbox')).toBeNull();
  });

  it('creates a card in the lane without an option with no option', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'todo', label: 'To do', color: null }],
      writable: true,
    };
    const onCreate = vi.fn(async () => true);
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'none' }, hidden: false, cards: [] },
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={onCreate}
      />
    ));
    expect(
      screen.queryByRole('button', { name: 'Add record to No status' })
    ).toBeNull();
    expect(onCreate).not.toHaveBeenCalled();
  });

  it('Shift+Enter saves a card and asks to open its record', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'done', label: 'Done', color: null }],
      writable: true,
    };
    const onCreate = vi.fn(async () => true);
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={onCreate}
      />
    ));
    const lane = boardLane('Done lane');
    fireEvent.click(within(lane).getByRole('button', { name: 'New record' }));
    const input = within(lane).getByRole('textbox');
    fireEvent.input(input, { target: { value: 'Needs detail' } });
    fireEvent.keyDown(input, { key: 'Enter', shiftKey: true });
    expect(onCreate).toHaveBeenCalledWith(
      { kind: 'option', id: 'done' },
      'Needs detail',
      expect.any(String),
      { open: true }
    );
    expect(within(lane).queryByRole('textbox')).toBeNull();
  });

  it('Escape cancels a new card and returns focus to its lane', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'done', label: 'Done', color: null }],
      writable: true,
    };
    const onCreate = vi.fn(async () => true);
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={onCreate}
      />
    ));
    const lane = boardLane('Done lane');
    fireEvent.click(within(lane).getByRole('button', { name: 'New record' }));
    const input = within(lane).getByRole('textbox', {
      name: 'New record title',
    });
    fireEvent.input(input, { target: { value: 'Not this one' } });
    fireEvent.keyDown(input, { key: 'Escape' });
    expect(within(lane).queryByRole('textbox')).toBeNull();
    expect(onCreate).not.toHaveBeenCalled();
    expect(document.activeElement).toBe(
      within(lane).getByRole('button', { name: 'New record' })
    );
  });

  it('blurring a typed card saves it and blurring an empty card cancels it', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'done', label: 'Done', color: null }],
      writable: true,
    };
    const onCreate = vi.fn(async () => true);
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={onCreate}
      />
    ));
    const lane = boardLane('Done lane');
    fireEvent.click(within(lane).getByRole('button', { name: 'New record' }));
    fireEvent.blur(within(lane).getByRole('textbox'));
    expect(within(lane).queryByRole('textbox')).toBeNull();
    expect(onCreate).not.toHaveBeenCalled();
    fireEvent.click(within(lane).getByRole('button', { name: 'New record' }));
    const input = within(lane).getByRole('textbox');
    fireEvent.input(input, { target: { value: 'Typed then left' } });
    fireEvent.blur(input);
    expect(onCreate).toHaveBeenCalledWith(
      { kind: 'option', id: 'done' },
      'Typed then left',
      expect.any(String)
    );
    expect(within(lane).queryByRole('textbox')).toBeNull();
  });

  it('n on a focused card or Enter on a lane header adds a card to that lane', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [
        { id: 'todo', label: 'To do', color: null },
        { id: 'done', label: 'Done', color: null },
      ],
      writable: true,
    };
    render(() => (
      <DatabaseBoard
        rows={[
          {
            rowId: 'launch',
            cells: { title: 'Launch project', status: 'To do' },
          },
        ]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            {
              key: { kind: 'option', id: 'todo' },
              hidden: false,
              cards: ['launch'],
            },
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
      />
    ));
    const todo = boardLane('To do lane');
    const card = within(todo).getByRole('button', {
      name: 'Open Launch project',
    });
    card.focus();
    fireEvent.keyDown(card, { key: 'n' });
    expect(document.activeElement).toBe(
      within(todo).getByRole('textbox', { name: 'New record title' })
    );
    fireEvent.keyDown(document.activeElement!, { key: 'Escape' });
    const done = boardLane('Done lane');
    const header = within(done).getByRole('button', { name: 'Done lane' });
    header.focus();
    fireEvent.keyDown(header, { key: 'Enter' });
    expect(document.activeElement).toBe(
      within(done).getByRole('textbox', { name: 'New record title' })
    );
  });

  it('keeps a new-card title after a failed write and unrelated row updates', async () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'done', label: 'Done', color: null }],
      writable: true,
    };
    const [rows, setRows] = createSignal<DatabaseRow[]>([]);
    const [cards, setCards] = createSignal<string[]>([]);
    const onCreate = vi.fn(async () => false);
    render(() => (
      <DatabaseBoard
        rows={rows()}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            {
              key: { kind: 'option', id: 'done' },
              hidden: false,
              cards: cards(),
            },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={onCreate}
      />
    ));
    fireEvent.click(screen.getByRole('button', { name: 'Add record to Done' }));
    const input = screen.getByRole('textbox', {
      name: 'New record title',
    }) as HTMLTextAreaElement;
    fireEvent.input(input, { target: { value: 'Remember this draft' } });
    setRows([
      { rowId: 'other', cells: { title: 'Other record', status: 'Done' } },
    ]);
    setCards(['other']);
    expect(
      screen.getByRole('button', { name: 'Open Other record' })
    ).toBeTruthy();
    expect(
      (
        screen.getByRole('textbox', {
          name: 'New record title',
        }) as HTMLTextAreaElement
      ).value
    ).toBe('Remember this draft');
    fireEvent.keyDown(input, { key: 'Enter' });
    await waitFor(() =>
      expect(onCreate).toHaveBeenCalledWith(
        { kind: 'option', id: 'done' },
        'Remember this draft',
        expect.any(String)
      )
    );
    await waitFor(() =>
      expect(
        screen
          .getAllByRole('textbox', { name: 'New record title' })
          .map((field) => (field as HTMLTextAreaElement).value)
      ).toEqual(['Remember this draft', ''])
    );
  });

  it('shows a submitted card in place while it saves and keeps its title when the save fails', async () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [{ id: 'done', label: 'Done', color: null }],
      writable: true,
    };
    const [pending, setPending] = createSignal(new Set<string>());
    let complete: (saved: boolean) => void = () => {};
    const onCreate = vi.fn(
      (_lane: LaneKey, _title: string, intentId: string) => {
        setPending((ids) => new Set(ids).add(intentId));
        return new Promise<boolean>((resolve) => {
          complete = (saved) => {
            setPending(
              (ids) => new Set([...ids].filter((id) => id !== intentId))
            );
            resolve(saved);
          };
        });
      }
    );
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        createPending={(id) => pending().has(id)}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={onCreate}
      />
    ));
    const lane = boardLane('Done lane');
    fireEvent.click(within(lane).getByRole('button', { name: 'New record' }));
    const input = screen.getByRole('textbox', { name: 'New record title' });
    fireEvent.input(input, { target: { value: 'First idea' } });
    fireEvent.keyDown(input, { key: 'Enter' });
    const saving = within(lane).getByRole('status', {
      name: 'Saving new record',
    });
    expect(saving.textContent).toContain('First idea');
    const next = within(lane).getByRole('textbox', {
      name: 'New record title',
    });
    expect(
      saving.compareDocumentPosition(next) & Node.DOCUMENT_POSITION_FOLLOWING
    ).toBeTruthy();
    expect(within(lane).getByText('1')).toBeTruthy();
    complete(false);
    await waitFor(() =>
      expect(
        screen
          .getAllByRole('textbox', { name: 'New record title' })
          .map((field) => (field as HTMLTextAreaElement).value)
      ).toEqual(['First idea', ''])
    );
  });

  it('starts a card in the first lane from the host controls', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [
        { id: 'todo', label: 'To do', color: null },
        { id: 'done', label: 'Done', color: null },
      ],
      writable: true,
    };
    let addCard: () => boolean = () => false;
    render(() => (
      <DatabaseBoard
        rows={[]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
            { key: { kind: 'option', id: 'todo' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit
        rowPending={() => false}
        onOpen={vi.fn()}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
        controlsRef={(controls) => {
          addCard = controls.addCard;
        }}
      />
    ));
    expect(addCard()).toBe(true);
    expect(
      within(boardLane('Done lane')).getByRole('textbox', {
        name: 'New record title',
      })
    ).toBeTruthy();
  });

  it('opens records for viewers without exposing move, create or lane controls', () => {
    const status: DatabaseViewColumn = {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      isMultiSelect: false,
      options: [
        { id: 'todo', label: 'To do', color: null },
        { id: 'done', label: 'Done', color: null },
      ],
      writable: true,
    };
    const onOpen = vi.fn();
    render(() => (
      <DatabaseBoard
        rows={[
          {
            rowId: 'launch',
            cells: { title: 'Launch project', status: 'To do' },
          },
        ]}
        columns={[
          {
            id: 'title',
            name: 'Name',
            dataType: 'STRING',
            isMultiSelect: false,
            options: [],
            writable: true,
          },
          status,
        ]}
        board={{
          lanes: [
            {
              key: { kind: 'option', id: 'todo' },
              hidden: false,
              cards: ['launch'],
            },
            { key: { kind: 'option', id: 'done' }, hidden: false, cards: [] },
          ],
        }}
        layout={{
          kind: 'board',
          title: 'title',
          groupBy: 'status',
          lanes: [],
          cardFields: [],
          hideEmptyLanes: false,
        }}
        groupColumn={status}
        canEdit={false}
        rowPending={() => false}
        onOpen={onOpen}
        onMove={vi.fn()}
        onCreate={vi.fn(async () => true)}
        onAddGroup={vi.fn(async () => ok(undefined))}
      />
    ));
    fireEvent.click(
      screen.getByRole('button', { name: 'Open Launch project' })
    );
    expect(onOpen).toHaveBeenCalledWith('launch');
    expect(
      screen.queryByRole('button', { name: 'Move Launch project' })
    ).toBeNull();
    expect(screen.queryByRole('button', { name: 'New record' })).toBeNull();
    expect(
      screen.queryByRole('button', { name: 'Add record to Done' })
    ).toBeNull();
    expect(screen.queryByRole('button', { name: 'New group' })).toBeNull();
    expect(within(boardLane('Done lane')).getByText('No records')).toBeTruthy();
  });
});

describe('board lane header', () => {
  const column: DatabaseViewColumn = {
    id: 'status',
    name: 'Status',
    dataType: 'SELECT_STRING',
    isMultiSelect: false,
    options: [
      { id: 'todo', label: 'To do', color: null },
      { id: 'done', label: 'Done', color: null },
    ],
    writable: true,
  };
  function renderHeader(editing: OptionEditing, groupColumn = column) {
    return render(() => (
      <OptionEditingContext.Provider value={editing}>
        <BoardLaneHeader
          column={groupColumn}
          option={groupColumn.options[0]}
          label={groupColumn.options[0].label}
          count={2}
          canEdit
          icon={<span />}
          onHide={vi.fn()}
        >
          {groupColumn.options[0].label}
        </BoardLaneHeader>
      </OptionEditingContext.Provider>
    ));
  }

  it('saves a rename once across Enter and blur, then restores name focus', async () => {
    let complete!: () => void;
    const saved = new Promise<void>((resolve) => {
      complete = resolve;
    });
    const editing = {
      update: vi.fn(() => ResultAsync.fromSafePromise(saved)),
      remove: vi.fn(() => okAsync(undefined)),
    };
    renderHeader(editing);
    await userEvent.click(screen.getByRole('button', { name: 'Rename To do' }));
    const input = screen.getByRole('textbox', { name: 'Group name' });
    expect(document.activeElement).toBe(input);
    fireEvent.input(input, { target: { value: 'In progress' } });
    fireEvent.keyDown(input, { key: 'Enter' });
    fireEvent.blur(input);
    expect(editing.update).toHaveBeenCalledExactlyOnceWith('status', 'todo', {
      label: 'In progress',
    });
    expect((input as HTMLInputElement).readOnly).toBe(true);
    complete();
    await waitFor(() =>
      expect(document.activeElement).toBe(
        screen.getByRole('button', { name: 'Rename To do' })
      )
    );
  });

  it('cancels a rename with Escape without saving on blur', async () => {
    const editing = {
      update: vi.fn(() => okAsync(undefined)),
      remove: vi.fn(() => okAsync(undefined)),
    };
    renderHeader(editing);
    const trigger = screen.getByRole('button', { name: 'Rename To do' });
    await userEvent.click(trigger);
    expect(trigger.isConnected).toBe(true);
    expect(trigger.getAttribute('aria-hidden')).toBe('true');
    expect(trigger.tabIndex).toBe(-1);
    const input = screen.getByRole('textbox', { name: 'Group name' });
    fireEvent.input(input, { target: { value: 'Discarded' } });
    fireEvent.keyDown(input, { key: 'Escape' });
    fireEvent.blur(input);
    await waitFor(() => expect(document.activeElement).toBe(trigger));
    expect(trigger.hasAttribute('aria-hidden')).toBe(false);
    expect(trigger.tabIndex).toBe(0);
    expect(editing.update).not.toHaveBeenCalled();
  });

  it.each([
    ['SELECT_STRING', 'DONE', 'Another option already has this name.'],
    ['SELECT_NUMBER', 'not a number', 'Enter a number.'],
  ] as const)(
    'keeps invalid %s names in the editor without writing',
    async (dataType, name, message) => {
      const editing = {
        update: vi.fn(() => okAsync(undefined)),
        remove: vi.fn(() => okAsync(undefined)),
      };
      renderHeader(editing, { ...column, dataType });
      await userEvent.click(
        screen.getByRole('button', { name: 'Rename To do' })
      );
      const input = screen.getByRole('textbox', { name: 'Group name' });
      fireEvent.input(input, { target: { value: name } });
      fireEvent.keyDown(input, { key: 'Enter' });
      expect(screen.getByRole('alert').textContent).toBe(message);
      expect((input as HTMLInputElement).value).toBe(name);
      expect(editing.update).not.toHaveBeenCalled();
    }
  );

  it('requires delete confirmation, supports cancellation and reopening, and blocks repeat deletion', async () => {
    let complete!: () => void;
    const deleted = new Promise<void>((resolve) => {
      complete = resolve;
    });
    const editing = {
      update: vi.fn(() => okAsync(undefined)),
      remove: vi.fn(() => ResultAsync.fromSafePromise(deleted)),
    };
    renderHeader(editing);
    const openConfirmation = async () => {
      await userEvent.click(
        screen.getByRole('button', { name: 'To do lane menu' })
      );
      await userEvent.click(
        await screen.findByRole('menuitem', { name: 'Delete group' })
      );
      return screen.findByRole('dialog', { name: 'Delete “To do”?' });
    };
    const dialog = await openConfirmation();
    expect(dialog.textContent).toContain('Records will not be deleted.');
    expect(editing.remove).not.toHaveBeenCalled();
    await userEvent.click(
      within(dialog).getByRole('button', { name: 'Cancel' })
    );
    const reopened = await openConfirmation();
    await userEvent.click(
      within(reopened).getByRole('button', { name: 'Delete group' })
    );
    expect(editing.remove).toHaveBeenCalledExactlyOnceWith('status', 'todo');
    expect(
      within(reopened)
        .getByRole('button', { name: 'Deleting…' })
        .hasAttribute('disabled')
    ).toBe(true);
    expect(
      within(reopened)
        .getByRole('button', { name: 'Cancel' })
        .hasAttribute('disabled')
    ).toBe(true);
    complete();
    await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
  });
});