heatEngine.test.ts 5.51 KB
/**
 * 热力层实现切换契约。
 *
 * `?heat=official` 才启用百度内置 BMapGL.Heatmap;缺省 / 拼错 / 别的值一律走自绘层,
 * 避免线上误带一个参数就把多轮微调好的视觉替换掉。
 */
import { afterEach, describe, expect, it } from 'vitest';
import { officialHeatConfig, useOfficialHeatLayer } from './index';
import { DEFAULT_OFFICIAL_CONFIG } from './BaiduNativeHeatLayer';

const original = window.location.search;

function setSearch(qs: string): void {
  window.history.replaceState(null, '', `${window.location.pathname}${qs}`);
}

afterEach(() => {
  setSearch(original);
});

describe('热力实现 URL 开关', () => {
  it('无参数时默认自绘层', () => {
    setSearch('');
    expect(useOfficialHeatLayer()).toBe(false);
  });

  it('?heat=official 切到百度官方 WebGL 层', () => {
    setSearch('?heat=official');
    expect(useOfficialHeatLayer()).toBe(true);
  });

  it('其它值、大小写不符、参数拼错都不误切', () => {
    for (const qs of ['?heat=custom', '?heat=Official', '?heatmap=official', '?heat=']) {
      setSearch(qs);
      // useOfficialHeatLayer 只是读 URL 的普通函数(return heatKind() === 'official'),
      // use 前缀让 rules-of-hooks 误判成 hook;这里在循环里逐个 URL 断言是合法的。
      // eslint-disable-next-line react-hooks/rules-of-hooks
      expect(useOfficialHeatLayer()).toBe(false);
    }
  });
});

describe('官方层调参默认值(demo 原始点口径)', () => {
  it('默认原始点 + 2000米半径 + max=375(z11 视野标定,盖过数字白盘)', () => {
    setSearch('');
    expect(officialHeatConfig()).toEqual({
      size: 2000,
      opacity: 0.85,
      maxScale: 1,
      raw: true,
      mode: 'raw',
      unit: 'm',
      limit: 35000,
      rawMax: 375,
      spread: 0.45,
    });
    expect(DEFAULT_OFFICIAL_CONFIG.unit).toBe('m');
    // max=1 等于关掉归一化,3.5 万点叠加必成纯红地毯(截图实证)
    expect(DEFAULT_OFFICIAL_CONFIG.rawMax).toBeGreaterThan(1);
    // 半径从 demo 的 800 放大到 2000(面积 ×6.25),阈值必须同比抬高,
    // 否则每像素被更多点覆盖又烧成红海。
    expect(DEFAULT_OFFICIAL_CONFIG.rawMax / 60).toBeCloseTo(
      (DEFAULT_OFFICIAL_CONFIG.size / 800) ** 2,
      1,
    );
  });

  it('heatData=bucket 回退到旧的聚合桶口径(32px 半径),size 按像素夹紧', () => {
    setSearch('?heatData=bucket');
    const cfg = officialHeatConfig();
    expect(cfg.raw).toBe(false);
    expect(cfg.mode).toBe('bucket');
    expect(cfg.unit).toBe('px');
    expect(cfg.size).toBe(32);

    setSearch('?heatData=bucket&heatSize=999');
    expect(officialHeatConfig().size).toBe(120);
  });

  it('heatMode=bucketDots:与 raw 共用已验证标定,只有数据来源不同', () => {
    setSearch('?heatMode=bucketDots');
    const c = officialHeatConfig();
    expect(c.mode).toBe('bucketDots');
    expect(c.raw).toBe(true);
    expect(c.unit).toBe('m');
    // 4000/140 是「每案放大成 N 点」错误假设下的模拟落点,真实数据直接烧成红海;
    // 必须与 raw 共用 2000米/375(用户截图确认有过渡、有主次),保证只换数据源不换标定。
    expect(c.size).toBe(DEFAULT_OFFICIAL_CONFIG.size);
    expect(c.rawMax).toBe(DEFAULT_OFFICIAL_CONFIG.rawMax);
    expect(c.spread).toBe(0.45);

    setSearch('?heatMode=bucketDots&heatSpread=0.8&heatRawMax=200&heatLimit=50000');
    const tuned = officialHeatConfig();
    expect(tuned.spread).toBe(0.8);
    expect(tuned.rawMax).toBe(200);
    expect(tuned.limit).toBe(50000);

    setSearch('?heatMode=bucketDots&heatSpread=99');
    expect(officialHeatConfig().spread).toBe(1);
    setSearch('?heatMode=bucketDots&heatSpread=-1');
    expect(officialHeatConfig().spread).toBe(0.05);
  });

  it('heatMode 优先于旧 heatData;未知模式回落 raw', () => {
    setSearch('?heatData=bucket&heatMode=bucketDots');
    expect(officialHeatConfig().mode).toBe('bucketDots');
    setSearch('?heatData=bucket&heatMode=oops');
    expect(officialHeatConfig().mode).toBe('raw');
  });

  it('旋钮都能从 URL 读,并各自夹紧到安全范围', () => {
    setSearch(
      '?heat=official&heatSize=1200&heatOpacity=0.6&heatMax=1.5&heatLimit=20000&heatRawMax=120',
    );
    expect(officialHeatConfig()).toEqual({
      size: 1200,
      opacity: 0.6,
      maxScale: 1.5,
      raw: true,
      mode: 'raw',
      unit: 'm',
      limit: 20000,
      rawMax: 120,
      spread: 0.45,
    });

    setSearch('?heatSize=99999&heatOpacity=-3&heatMax=100&heatLimit=999999&heatRawMax=9999');
    const c = officialHeatConfig();
    expect(c.size).toBe(5000);
    expect(c.opacity).toBe(0.1);
    expect(c.maxScale).toBe(5);
    expect(c.limit).toBe(100000);
    expect(c.rawMax).toBe(500);
  });

  it('rawMax 可单独收紧/放宽归一化阈值,且不改变半径与倍率', () => {
    setSearch('?heatRawMax=30');
    const low = officialHeatConfig();
    expect(low.rawMax).toBe(30);
    expect(low.size).toBe(2000);
    expect(low.maxScale).toBe(1);

    setSearch('?heatRawMax=1');
    expect(officialHeatConfig().rawMax).toBe(1); // 允许极端值,供 A/B 复现饱和态
  });

  it('非数字、空值、拼错都回落到默认,不会误带极端参数', () => {
    for (const qs of [
      '?heatSize=abc',
      '?heatOpacity=',
      '?heatMax=NaN',
      '?heatLimit=oops',
      '?heatRawMax=abc',
      '?size=10',
    ]) {
      setSearch(qs);
      expect(officialHeatConfig()).toEqual(DEFAULT_OFFICIAL_CONFIG);
    }
  });
});