import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { resolveOnlyPatientIds } from '../src/modules/sync/cold-import/clickhouse-source.service';

/**
 * PAC_COHORT_ONLY_PATIENT(定向重摄名单)解析。
 *
 * 背景:污染修复要按名单重摄 3 万个患者,逗号串约 275KB > 环境变量 128KB(MAX_ARG_STRLEN)
 *   上限会 E2BIG,故支持 `@file`(每行一个 id)。
 * ⚠️ 本函数还是"是否定向模式"的判定源 —— cold-import 据它决定**不推进增量游标**
 *   (定向轮只覆盖名单内患者,推进全局水位会把名单外患者的 DW 新写入永久埋掉)。
 */
describe('PAC_COHORT_ONLY_PATIENT 解析', () => {
  const ORIG = process.env.PAC_COHORT_ONLY_PATIENT;
  let tmpFile = '';

  afterEach(() => {
    if (ORIG === undefined) delete process.env.PAC_COHORT_ONLY_PATIENT;
    else process.env.PAC_COHORT_ONLY_PATIENT = ORIG;
    if (tmpFile && fs.existsSync(tmpFile)) fs.unlinkSync(tmpFile);
    tmpFile = '';
  });

  test('未设置 → 空数组(非定向模式,游标照常推进)', () => {
    delete process.env.PAC_COHORT_ONLY_PATIENT;
    expect(resolveOnlyPatientIds()).toEqual([]);
  });

  test('空串 / 纯空白 → 空数组(不误判成定向模式)', () => {
    process.env.PAC_COHORT_ONLY_PATIENT = '   ';
    expect(resolveOnlyPatientIds()).toEqual([]);
  });

  test('单个 id', () => {
    process.env.PAC_COHORT_ONLY_PATIENT = '1855960';
    expect(resolveOnlyPatientIds()).toEqual(['1855960']);
  });

  test('逗号列表 → 去空白、丢空项', () => {
    process.env.PAC_COHORT_ONLY_PATIENT = '1855960, 1855959 ,,1855956,';
    expect(resolveOnlyPatientIds()).toEqual(['1855960', '1855959', '1855956']);
  });

  test('@file → 逐行读(大名单绕开 128KB 环境变量上限)', () => {
    tmpFile = path.join(os.tmpdir(), `pids-${Date.now()}.txt`);
    fs.writeFileSync(tmpFile, '1855960\n1855959\n\n  1855956  \n');
    process.env.PAC_COHORT_ONLY_PATIENT = `@${tmpFile}`;
    expect(resolveOnlyPatientIds()).toEqual(['1855960', '1855959', '1855956']);
  });

  test('@file 三万行量级 → 全部读出(真实修复场景规模)', () => {
    tmpFile = path.join(os.tmpdir(), `pids-big-${Date.now()}.txt`);
    const ids = Array.from({ length: 30626 }, (_, i) => String(1000000 + i));
    fs.writeFileSync(tmpFile, ids.join('\n'));
    process.env.PAC_COHORT_ONLY_PATIENT = `@${tmpFile}`;
    const got = resolveOnlyPatientIds();
    expect(got).toHaveLength(30626);
    expect(got[0]).toBe('1000000');
    expect(got[30625]).toBe('1030625');
  });

  test('@file 文件不存在 → 抛错(不静默退化成全量重摄)', () => {
    process.env.PAC_COHORT_ONLY_PATIENT = '@/no/such/pids.txt';
    expect(() => resolveOnlyPatientIds()).toThrow(/不存在/);
  });
});
