/**
 * cold-import 成组完整性 — findIncompleteGroups
 *
 * 规则(用户定):同 manifest group 的表在网页上传时要么全在、要么整组不在;部分在 → 拒(半组无意义)。
 * 独立表(无 group)缺失不影响(装载层宽容跳过)。
 *
 * 跑:pnpm --filter @pac/service test -- cold-import-groups
 */
import { findIncompleteGroups } from '../src/modules/admin/import-upload.service';

// 模拟 FRIDAY manifest.tables 的关键子集:treatment_plan 组(头+行)+ 若干独立表
const TABLES = [
  { file: 'customer_basic_info.csv' }, // 独立
  { file: 'appointment_base.csv' }, // 独立
  { file: 'customer_treat_plan.csv', group: 'treatment_plan' },
  { file: 'customer_treat_plan_item.csv', group: 'treatment_plan' },
];

describe('findIncompleteGroups', () => {
  it('组内全在 → 不报(通过)', () => {
    const got = findIncompleteGroups(
      TABLES,
      new Set(['customer_basic_info.csv', 'customer_treat_plan.csv', 'customer_treat_plan_item.csv']),
    );
    expect(got).toEqual([]);
  });

  it('整组都不在 → 不报(宽容:该主体不导)', () => {
    const got = findIncompleteGroups(TABLES, new Set(['customer_basic_info.csv']));
    expect(got).toEqual([]);
  });

  it('组内部分在(缺行表)→ 报不完整', () => {
    const got = findIncompleteGroups(
      TABLES,
      new Set(['customer_basic_info.csv', 'customer_treat_plan.csv']), // 缺 _item
    );
    expect(got).toHaveLength(1);
    expect(got[0]!.group).toBe('treatment_plan');
    expect(got[0]!.missing).toEqual(['customer_treat_plan_item.csv']);
    expect(got[0]!.present).toEqual(['customer_treat_plan.csv']);
  });

  it('独立表缺失不影响(无 group 不参与成组)', () => {
    const got = findIncompleteGroups(
      TABLES,
      new Set(['customer_treat_plan.csv', 'customer_treat_plan_item.csv']), // 缺两张独立表,但组齐
    );
    expect(got).toEqual([]);
  });
});
