import { describe, it, expect, beforeEach } from 'vitest';
import { draggableListItem } from './draggable-list-item.js';
import { mountHost, connectTrait, resetDOM } from '../test-helpers.js';

// Minimal smoke coverage. The draggable-list-item trait is the pointer
// driver for the apps/tasks/ DnD model. The cases
// below exercise only its own surface (schema, attribute lifecycle,
// id assignment, idempotent disconnect). Full pointer-flow tests need a
// laid-out DOM with sibling droppables and live elementFromPoint —
// those belong with the playground integration suite, not the trait
// gate.

describe('draggable-list-item', () => {
  beforeEach(resetDOM);

  it('schema shape is defined', () => {
    expect(draggableListItem.schema.name).toBe('draggable-list-item');
    expect(draggableListItem.schema.category).toBe('motion-positioning');
    expect(draggableListItem.schema.description.length).toBeGreaterThanOrEqual(11);
    expect(draggableListItem.schema.events).toContain('dnd-lift');
    expect(draggableListItem.schema.events).toContain('dnd-drop');
  });

  it('connect assigns an id + sets the host attribute', () => {
    const host = mountHost('div');
    connectTrait(draggableListItem, host);
    expect(host.getAttribute('data-draggable-list-item-id')).toBeTruthy();
  });

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

  it('reconnect with the same host does not throw', () => {
    const host = mountHost('div');
    const inst = draggableListItem();
    inst.connect(host);
    inst.disconnect(host);
    expect(() => {
      const inst2 = draggableListItem();
      inst2.connect(host);
      inst2.disconnect(host);
    }).not.toThrow();
  });
});
