import type { Node } from '@xyflow/svelte';

import { describe, expect, it } from 'vitest';

import {
  createTinyflowNodeNormalizer,
  DEFAULT_NODE_DRAG_HANDLE,
} from './nodeInteraction';

function createNode(patch: Partial<Node> = {}): Node {
  return {
    data: {},
    id: 'node-1',
    position: { x: 0, y: 0 },
    type: 'customNode',
    ...patch,
  };
}

describe('createTinyflowNodeNormalizer', () => {
  it('节点已经规范化时保留引用', () => {
    const normalizer = createTinyflowNodeNormalizer();
    const node = createNode({ dragHandle: DEFAULT_NODE_DRAG_HANDLE });

    expect(normalizer(node)).toBe(node);
  });

  it('需要补齐交互属性时采用写时复制且不修改输入', () => {
    const normalizer = createTinyflowNodeNormalizer();
    const node = createNode();

    const normalized = normalizer(node);

    expect(normalized).not.toBe(node);
    expect(normalized.dragHandle).toBe(DEFAULT_NODE_DRAG_HANDLE);
    expect(node.dragHandle).toBeUndefined();
  });

  it('只读节点已经包含限制属性时保留引用', () => {
    const normalizer = createTinyflowNodeNormalizer({ readonly: true } as any);
    const node = createNode({
      connectable: false,
      draggable: false,
      selectable: false,
    });

    expect(normalizer(node)).toBe(node);
  });
});
