import { describe, expect, it } from 'vitest';
import { formatShanghaiDateTime, formatZhNumber } from './formatters';

describe('formatZhNumber', () => {
  it('formats Chinese locale numbers with independent precision caches', () => {
    expect(formatZhNumber(1234.56)).toBe('1,235');
    expect(formatZhNumber(1234.56, 1)).toBe('1,234.6');
    expect(formatZhNumber(1234.56, 2)).toBe('1,234.56');
    expect(formatZhNumber(1234.56, 1)).toBe('1,234.6');
  });

  it('preserves Intl handling for non-finite numeric values', () => {
    expect(formatZhNumber(Number.NaN, 2)).toBe('NaN');
  });
});

describe('formatShanghaiDateTime', () => {
  it('renders explicit timestamps in Shanghai and preserves local business values', () => {
    expect(formatShanghaiDateTime('2026-07-18T02:33:00Z')).toBe('2026-07-18 10:33:00');
    expect(formatShanghaiDateTime('2026-07-18T02:33:00+08:00')).toBe('2026-07-18 02:33:00');
    expect(formatShanghaiDateTime('2026-07-18 02:33:00')).toBe('2026-07-18 02:33:00');
  });

  it('converts timezone-less UTC service timestamps only when the source contract says UTC', () => {
    expect(formatShanghaiDateTime('2026-07-18 02:33:00', { sourceZone: 'utc' })).toBe('2026-07-18 10:33:00');
    expect(formatShanghaiDateTime('', { sourceZone: 'utc' })).toBe('—');
    expect(formatShanghaiDateTime(undefined, { empty: '尚未上报' })).toBe('尚未上报');
  });
});
