import { NodeRegistry } from '../../src/core/node-register';
import { BaseNode } from '../../src/core/node';
import { INodeContext, INodeOutput, NodeStatus } from '../../src/types';
import { describe, test, expect, beforeEach, jest } from '@jest/globals';

// Create a mock node implementation for testing
class TestNode extends BaseNode {
  id = 'test-node';
  
  async execute(context: INodeContext): Promise<INodeOutput> {
    return {
      status: NodeStatus.COMPLETED,
      output: { result: 'success' }
    };
  }
}

describe('NodeRegistry', () => {
  let registry: NodeRegistry;
  const testNode = new TestNode();
  
  beforeEach(() => {
    registry = new NodeRegistry();
    // Clear mocks/spies if needed
    jest.clearAllMocks();
  });
  
  test('should register a node', () => {
    // Spy on console.log
    const consoleSpy = jest.spyOn(console, 'log');
    
    registry.register(testNode);
    
    expect(registry.has('test-node')).toBe(true);
    expect(consoleSpy).toHaveBeenCalledWith(
      expect.stringContaining('已注册')
    );
  });
  
  test('should get a registered node', () => {
    registry.register(testNode);
    
    const retrievedNode = registry.get('test-node');
    
    expect(retrievedNode).toBe(testNode);
  });
  
  test('should return undefined for non-existent node', () => {
    const nonExistentNode = registry.get('non-existent-node');
    
    expect(nonExistentNode).toBeUndefined();
  });
  
  test('should check if a node exists', () => {
    registry.register(testNode);
    
    expect(registry.has('test-node')).toBe(true);
    expect(registry.has('non-existent-node')).toBe(false);
  });
  
  test('should list all registered node IDs', () => {
    const anotherNode = new TestNode();
    Object.defineProperty(anotherNode, 'id', { value: 'another-node' });
    
    registry.register(testNode);
    registry.register(anotherNode);
    
    const nodeIds = registry.listNodeIds();
    
    expect(nodeIds).toContain('test-node');
    expect(nodeIds).toContain('another-node');
    expect(nodeIds.length).toBe(2);
  });
  
  test('should log warning when registering a node with duplicate ID', () => {
    const consoleSpy = jest.spyOn(console, 'warn');
    
    registry.register(testNode);
    registry.register(testNode); // Register the same node again
    
    expect(consoleSpy).toHaveBeenCalledWith(
      expect.stringContaining('已存在')
    );
  });
}); 