import { describe, it, expect, beforeEach } from 'vitest';
import { keyboardReorderable } from './keyboard-reorderable.js';
import { mountHost, connectTrait, resetDOM } from '../test-helpers.js';

// Minimal smoke coverage. Keyboard-reorderable is the WCAG 2.5.7 +
// ARIA 1.1 alternative to draggable-list-item for the
// apps/tasks/ DnD model. Cases below verify
// only the trait's own surface (schema, attribute lifecycle, id
// assignment, tabindex injection). Multi-container keyboard flow
// belongs in the playground integration suite.

describe('keyboard-reorderable', () => {
  beforeEach(resetDOM);

  it('schema shape is defined', () => {
    expect(keyboardReorderable.schema.name).toBe('keyboard-reorderable');
    expect(keyboardReorderable.schema.category).toBe('keyboard-navigation');
    expect(keyboardReorderable.schema.description.length).toBeGreaterThanOrEqual(11);
    expect(keyboardReorderable.schema.events).toContain('dnd-lift');
    expect(keyboardReorderable.schema.events).toContain('dnd-drop');
  });

  it('connect assigns an id + ensures tabindex', () => {
    const host = mountHost('div');
    connectTrait(keyboardReorderable, host);
    expect(host.getAttribute('data-keyboard-reorderable-id')).toBeTruthy();
    expect(host.getAttribute('tabindex')).toBe('0');
  });

  it('connect respects pre-existing tabindex', () => {
    const host = mountHost('div', { tabindex: '-1' });
    connectTrait(keyboardReorderable, host);
    expect(host.getAttribute('tabindex')).toBe('-1');
  });

  it('disconnect clears its attributes', () => {
    const host = mountHost('div');
    const inst = keyboardReorderable();
    inst.connect(host);
    host.setAttribute('data-keyboard-reorderable-lifting', '');
    inst.disconnect(host);
    expect(host.hasAttribute('data-keyboard-reorderable-lifting')).toBe(false);
    expect(host.hasAttribute('data-keyboard-reorderable-id')).toBe(false);
  });
});
