import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { buildRowsFromPayload } from '../src/processor/index.js';
import { config } from '../src/config/config.js';

describe('Feature Toggle: Loop Name Auto Generation', () => {
  const basePayload = {
    ts_ms: 1700000000000,
    hotel_id: 1001,
    room_id: '8001',
    device_id: 'dev_001',
    direction: '上报',
    cmd_word: '0x36',
    frame_id: 1,
    udp_raw: '00',
    sys_lock_status: 0,
    report_count: 0,
    fault_count: 0,
    device_list: [
      { dev_type: 1, dev_addr: 10, dev_loop: 1, dev_data: 100 }
    ]
  };

  let originalConfigValue;

  beforeEach(() => {
    originalConfigValue = config.enableLoopNameAutoGeneration;
  });

  afterEach(() => {
    config.enableLoopNameAutoGeneration = originalConfigValue;
  });

  it('should generate loop_name when flag is true', () => {
    config.enableLoopNameAutoGeneration = true;
    const rows = buildRowsFromPayload(basePayload);
    // Expect format: [1强电继电器（输出状态）-10-1]
    // The exact name depends on the map, but it should contain brackets and numbers
    expect(rows[0].loop_name).toBeDefined();
    expect(rows[0].loop_name).toMatch(/^\[1.*-10-1\]$/);
  });

  it('should NOT generate loop_name when flag is false', () => {
    config.enableLoopNameAutoGeneration = false;
    const rows = buildRowsFromPayload(basePayload);
    expect(rows[0].loop_name).toBeNull();
  });
});
