// @vitest-environment happy-dom
//
// FileTree's status column, fed by a mocked cambium-core `ComputeStatusResponse`
// run through `buildChipMap` — the same shape useFileControl.js's engine effect
// produces. No react-testing-library in this repo (see fileStatus.test.js-era
// pure-function style elsewhere); this renders the real component with
// react-dom/client directly, which happy-dom (already a devDependency, used by
// serverPins.test.js/nativeMirror.test.js) supports without adding anything new.
import { act } from 'react';
import { createRoot } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { FileTree } from './FileTree';
import { buildChipMap } from '../data/statusChips';

const ROOT_ID = 'root::';

let container;

beforeEach(() => {
  container = document.createElement('div');
  document.body.appendChild(container);
});

afterEach(() => {
  document.body.removeChild(container);
  container = null;
});

function renderTree(statuses) {
  const nodes = [
    {
      id: ROOT_ID,
      type: 'folder',
      name: 'Workspace',
      isRoot: true,
      expanded: true,
      children: [
        {
          id: `${ROOT_ID}Assets/gone.txt`,
          type: 'file',
          name: 'gone.txt',
          size: '--',
        },
      ],
    },
  ];
  const root = createRoot(container);
  act(() => {
    root.render(
      <FileTree
        nodes={nodes}
        setNodes={() => {}}
        mode="workspace"
        selectedIds={new Set()}
        anchorId={null}
        onSelect={() => {}}
        sort={{ column: 'name', direction: 'asc' }}
        onSort={() => {}}
        scrollTop={0}
        onScroll={() => {}}
        visible
        rootId={ROOT_ID}
        statuses={statuses}
        onStatusClick={() => {}}
        pulsePaths={null}
        deletedFolders={null}
        onContextMenuRow={() => {}}
        onOpenFile={() => {}}
        dropTargetId={null}
        versionFractionOf={null}
      />
    );
  });
  return root;
}

describe('FileTree renders chips from a mocked engine response', () => {
  it('shows a markedForDelete row as a red chip with the deleted strikethrough class', () => {
    // Mocked cambium-core ComputeStatusResponse.statuses entry.
    const engineEntries = [{ path: 'Assets/gone.txt', status: 'markedForDelete', detail: null }];
    const chips = buildChipMap(engineEntries, { pullingPaths: null, serverReady: true });

    const root = renderTree(chips);
    try {
      const chipEl = container.querySelector('.status-chip--red');
      expect(chipEl).not.toBeNull();
      expect(chipEl.querySelector('.status-chip-label').textContent).toBe('Marked for delete');

      const rowEl = container.querySelector('.tree-node--deleted');
      expect(rowEl).not.toBeNull();
      expect(rowEl.textContent).toContain('gone.txt');
    } finally {
      act(() => root.unmount());
    }
  });

  it('renders no chip and no deleted class for a file with no engine entry', () => {
    const chips = buildChipMap([], { pullingPaths: null, serverReady: true });
    const root = renderTree(chips);
    try {
      expect(container.querySelector('.status-chip')).toBeNull();
      expect(container.querySelector('.tree-node--deleted')).toBeNull();
    } finally {
      act(() => root.unmount());
    }
  });
});
