Skip to content

Add end-to-end integration tests for node reference autocomplete flow #155

Closed

Description

@malibio

Problem Statement

While individual components are tested (Issues #151, #152), the complete user flow from typing "@" to inserting a node reference is untested. Integration bugs between ContentEditableController, NodeAutocomplete, and EventBus could break the feature silently.

Untested Flow:

  1. User types "@test" in editor
  2. Autocomplete modal appears at cursor
  3. Results filter as user types
  4. User selects with Enter
  5. Reference inserted in markdown format: [Title](nodespace://id)
  6. EventBus emits 'nodeReferenceSelected' event
  7. Modal closes and focus returns to editor

Why This Matters

Real Bugs This Would Catch:

  • Autocomplete doesn't appear when "@" typed
  • Results don't filter with query updates
  • Enter key creates new node instead of selecting reference
  • Reference inserted in wrong format
  • Focus lost after selection
  • EventBus events not fired

Current Risk: Full integration chain has never been automatically verified.

Files Involved

Components:

  • src/lib/design/components/contentEditableController.ts (trigger detection)
  • src/lib/design/components/base-node.svelte (autocomplete integration)
  • src/lib/components/ui/node-autocomplete/node-autocomplete.svelte
  • src/lib/services/eventBus.ts

Test File:

  • src/tests/integration/autocomplete-flow.test.ts

Acceptance Criteria

Create integration test file with 12+ test cases:

Complete Flow Tests

  • Trigger Detection (3 tests)

    • Typing "@" shows autocomplete
    • Typing " @" (space before) shows autocomplete
    • Typing "text@" (mid-word) doesn't show autocomplete
  • Query Filtering (3 tests)

    • Empty "@" shows all results
    • "@test" filters to matching nodes
    • "@xyz" with no matches shows empty state
  • Selection Flow (4 tests)

    • Enter key inserts reference and closes modal
    • Click on result inserts reference
    • Escape closes without inserting
    • Typing continues updates query
  • EventBus Integration (2 tests)

    • 'triggerDetected' event fired when "@" typed
    • 'nodeReferenceSelected' event fired on selection
    • Events contain correct nodeId and title
  • Focus Management (2 tests)

    • Focus stays in editor while modal open
    • Focus returns to editor after selection
    • Cursor positioned correctly after insertion
  • Content Integrity (2 tests)

    • Reference inserted in correct markdown format
    • Existing content preserved around insertion
    • Undo/redo works correctly

Implementation Guide

// src/tests/integration/autocomplete-flow.test.ts
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/svelte';
import userEvent from '@testing-library/user-event';
import BaseNode from '$lib/design/components/base-node.svelte';
import { eventBus } from '$lib/services/eventBus';

describe('Node Reference Autocomplete - Complete Flow', () => {
  beforeEach(() => {
    eventBus.reset();
  });

  describe('Trigger Detection', () => {
    it('should show autocomplete when @ typed', async () => {
      const user = userEvent.setup();

      const { container } = render(BaseNode, {
        props: {
          nodeId: 'test-node',
          nodeType: 'text',
          content: '',
          autoFocus: true
        }
      });

      const editor = container.querySelector('[contenteditable]');
      expect(editor).toBeInTheDocument();

      // Type "@"
      await user.type(editor, '@');

      // Autocomplete should appear
      expect(screen.getByRole('listbox')).toBeInTheDocument();
      expect(screen.getByLabelText('Node reference autocomplete')).toBeInTheDocument();
    });

    it('should filter results as user types query', async () => {
      const user = userEvent.setup();

      const { container } = render(BaseNode, {
        props: {
          nodeId: 'test-node',
          nodeType: 'text',
          content: '',
          autoFocus: true
        }
      });

      const editor = container.querySelector('[contenteditable]');

      // Type "@test"
      await user.type(editor, '@test');

      // Results should be filtered
      const listbox = screen.getByRole('listbox');
      expect(listbox).toBeInTheDocument();

      // Should show filtered results (mock data contains "Test Node")
      expect(screen.getByText(/Test Node/i)).toBeInTheDocument();
    });
  });

  describe('Selection and Insertion', () => {
    it('should insert reference when Enter pressed', async () => {
      const user = userEvent.setup();

      const { container } = render(BaseNode, {
        props: {
          nodeId: 'test-node',
          nodeType: 'text',
          content: '',
          autoFocus: true
        }
      });

      const editor = container.querySelector('[contenteditable]');

      // Type "@" to trigger
      await user.type(editor, '@');

      // Wait for autocomplete to appear
      expect(screen.getByRole('listbox')).toBeInTheDocument();

      // Press Enter to select first result
      await user.keyboard('{Enter}');

      // Reference should be inserted in markdown format
      const content = editor.textContent;
      expect(content).toMatch(/\[.*\]\(nodespace:\/\/.*\)/);
    });

    it('should close modal after selection', async () => {
      const user = userEvent.setup();

      const { container } = render(BaseNode, {
        props: {
          nodeId: 'test-node',
          nodeType: 'text',
          content: '',
          autoFocus: true
        }
      });

      const editor = container.querySelector('[contenteditable]');

      await user.type(editor, '@');
      expect(screen.getByRole('listbox')).toBeInTheDocument();

      await user.keyboard('{Enter}');

      // Modal should close
      expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
    });

    it('should close without inserting on Escape', async () => {
      const user = userEvent.setup();

      const { container } = render(BaseNode, {
        props: {
          nodeId: 'test-node',
          nodeType: 'text',
          content: 'Before @',
          autoFocus: true
        }
      });

      const editor = container.querySelector('[contenteditable]');

      await user.type(editor, '@');
      expect(screen.getByRole('listbox')).toBeInTheDocument();

      await user.keyboard('{Escape}');

      // Modal closed, content unchanged
      expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
      expect(editor.textContent).toBe('Before @');
    });
  });

  describe('EventBus Integration', () => {
    it('should emit triggerDetected event when @ typed', async () => {
      const user = userEvent.setup();
      const eventHandler = vi.fn();

      eventBus.subscribe('trigger:detected', eventHandler);

      const { container } = render(BaseNode, {
        props: {
          nodeId: 'test-node',
          nodeType: 'text',
          content: '',
          autoFocus: true
        }
      });

      const editor = container.querySelector('[contenteditable]');

      await user.type(editor, '@');

      expect(eventHandler).toHaveBeenCalledWith(
        expect.objectContaining({
          type: 'trigger:detected',
          nodeId: 'test-node'
        })
      );
    });

    it('should emit nodeReferenceSelected event on selection', async () => {
      const user = userEvent.setup();
      const eventHandler = vi.fn();

      eventBus.subscribe('node:reference-selected', eventHandler);

      const { container } = render(BaseNode, {
        props: {
          nodeId: 'test-node',
          nodeType: 'text',
          content: '',
          autoFocus: true
        }
      });

      const editor = container.querySelector('[contenteditable]');

      await user.type(editor, '@');
      await user.keyboard('{Enter}');

      expect(eventHandler).toHaveBeenCalledWith(
        expect.objectContaining({
          type: 'node:reference-selected',
          nodeId: expect.any(String),
          nodeTitle: expect.any(String)
        })
      );
    });
  });

  describe('Focus Management', () => {
    it('should maintain editor focus during autocomplete', async () => {
      const user = userEvent.setup();

      const { container } = render(BaseNode, {
        props: {
          nodeId: 'test-node',
          nodeType: 'text',
          content: '',
          autoFocus: true
        }
      });

      const editor = container.querySelector('[contenteditable]');

      await user.type(editor, '@');

      // Autocomplete visible but editor still focused
      expect(screen.getByRole('listbox')).toBeInTheDocument();
      expect(document.activeElement).toBe(editor);
    });

    it('should return focus to editor after selection', async () => {
      const user = userEvent.setup();

      const { container } = render(BaseNode, {
        props: {
          nodeId: 'test-node',
          nodeType: 'text',
          content: '',
          autoFocus: true
        }
      });

      const editor = container.querySelector('[contenteditable]');

      await user.type(editor, '@');
      await user.keyboard('{Enter}');

      // Focus back in editor
      expect(document.activeElement).toBe(editor);
    });
  });
});

Dependencies

Requires:

Blocks:

Definition of Done

  • 12+ integration test cases passing
  • Complete flow tested: trigger → filter → select → insert
  • EventBus events verified
  • Focus management verified
  • Content integrity verified
  • Tests run with bun run test

Estimated Effort

4-5 hours - Complex integration testing with multiple components

Priority

High - Critical user workflow must be verified

Activity

  1. self-assigned this
    on Oct 5, 2025
  2. added a commit that references this issue on Oct 5, 2025
    d12302e
  3. added 4 commits that reference this issue on Feb 4, 2026
    ffe55d0
    64ac099
    3668720
    6cc969e
  4. added 2 commits that reference this issue on Feb 26, 2026
    da71480
    20a276e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions