import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import * as yaml from 'js-yaml';
import { ColdImportManifestSchema } from '../src/modules/sync/cold-import/manifest.schema';

/**
 * push 断流告警开关(monitoring.push_lag_alert)。
 *
 * 【为什么需要】监控的触发条件是"曾经推过"(有 push SyncLog),对**联调期**宿主不成立:
 * 推几次就停是正常节奏,不是"链路挂了、数据在漏"。2026-08-01 FRIDAY 就连报了数轮
 * critical(28.9h / 29.9h 无推送),而它根本还没正式上线推送 —— 这类噪音会把真告警淹掉。
 *
 * 【为什么不用"把阈值调大"糊弄】那样语义是"容忍 100000 小时不推",读代码的人无法分辨
 * 这是"故意关掉"还是"填错了"。显式布尔让意图留在 yaml 里,且宿主上线时删一行即恢复。
 */
const readManifest = (host: string) =>
  ColdImportManifestSchema.parse(
    yaml.load(readFileSync(join(__dirname, `../data/${host}/manifest.yaml`), 'utf-8')),
  );

describe('push_lag_alert', () => {
  test('⭐ FRIDAY 联调期显式关闭 —— 上线推送后要删掉这行', () => {
    expect(readManifest('friday').monitoring?.push_lag_alert).toBe(false);
  });

  test('不配 → 默认 true(既有宿主行为不变,不会静默失去监控)', () => {
    const parsed = ColdImportManifestSchema.parse({
      host_name: 'x',
      tenant_id: 't1',
      amount_unit: 'yuan',
      timezone: 'UTC',
      assemblers: [{ file: 'assemblers/x.yaml' }],
      tables: [{ table: 't', file: 't.csv' }],
      monitoring: { push_lag_error_hours: 26 },
    });
    expect(parsed.monitoring?.push_lag_alert).toBe(true);
  });

  test('整个 monitoring 段缺失也不报错(jvs-dw 是 pull 宿主,压根不涉及 push 断流)', () => {
    const parsed = ColdImportManifestSchema.parse({
      host_name: 'x',
      tenant_id: 't1',
      amount_unit: 'yuan',
      timezone: 'UTC',
      assemblers: [{ file: 'assemblers/x.yaml' }],
      tables: [{ table: 't', file: 't.csv' }],
    });
    expect(parsed.monitoring).toBeUndefined();
  });
});
