import { describe, expect, it } from 'vitest';
import { alertValue, canAct, ruleCondition, thresholdText } from './alert';

describe('alert domain helpers', () => {
  it('keeps trigger evidence and duration explicit', () => {
    const event = { triggerValue: 96, threshold: 80, thresholdHigh: 0, operator: 'gt', unit: 'km/h', durationSec: 60 };
    expect(alertValue(event)).toBe('96 km/h');
    expect(thresholdText(event)).toBe('> 80 km/h，持续 60 秒');
  });

  it('enforces valid workflow transitions', () => {
    expect(canAct('unprocessed', 'acknowledge')).toBe(true);
    expect(canAct('processing', 'acknowledge')).toBe(false);
    expect(canAct('recovered', 'close')).toBe(true);
    expect(canAct('closed', 'ignore')).toBe(false);
  });

  it('renders boolean rules without numeric fiction', () => {
    expect(ruleCondition({ valueType: 'boolean', booleanThreshold: true, metric: 'alarm_active', durationSec: 0 } as never)).toBe('协议告警位 是');
  });

  it('renders range and state-change semantics', () => {
    expect(thresholdText({ threshold: 20, thresholdHigh: 80, operator: 'between', unit: '%', durationSec: 30 })).toBe('区间内 20–80 %，持续 30 秒');
    expect(ruleCondition({ valueType: 'boolean', metric: 'alarm_active', operator: 'changed', durationSec: 0 } as never)).toBe('协议告警位 状态变化');
  });
});
