hitTest.test.ts 4.05 KB
import { describe, expect, it } from 'vitest';
import { hitTest, type HitLayers } from './hitTest';
import { useHeatmapStore } from '@/store/useHeatmapStore';
import type { BucketHit } from './ClusterLayer';

const PX = { x: 100, y: 100 };
const BUCKET: BucketHit = {
  gx: 1,
  gy: 2,
  count: 3,
  lng: 114,
  lat: 22.6,
  screenX: 100,
  screenY: 100,
  radius: 12,
};
const SPOT_UNIT = {
  idx: 0,
  label: '粤海街道',
  count: 12,
  roster: { source: '聚焦 · 粤海街道', where: {} },
};
const SPOT = {
  kind: 'streets' as const,
  source: '在地图上看前 6',
  base: { from: 0, to: 0, districts: [], keyword: '' },
  cellM: 100,
  active: null,
  units: [SPOT_UNIT],
  note: '',
};

/** 四层都不命中;各测试按需覆盖某一层,避免每个用例重建整份 store 状态 */
function baseState() {
  return useHeatmapStore.getState();
}
function baseLayers(): HitLayers {
  return {
    pin: { hitClose: () => false },
    spot: { hitAt: () => -1 },
    hotspot: { hitAt: () => -1 },
    cluster: { hitAt: () => null },
  };
}
function normalState() {
  return { ...baseState(), analysisOn: false, spotlight: null, casePin: null };
}

describe('hitTest 四层命中优先级', () => {
  it('卡片关闭叉最优先:命中时不再问下面三层', () => {
    const calls: string[] = [];
    const layers: HitLayers = {
      pin: { hitClose: () => true },
      spot: { hitAt: () => (calls.push('spot'), 0) },
      hotspot: { hitAt: () => (calls.push('hotspot'), 0) },
      cluster: { hitAt: () => (calls.push('cluster'), BUCKET) },
    };
    expect(hitTest(layers, normalState(), PX)).toEqual({ kind: 'pinClose' });
    expect(calls).toEqual([]);
  });

  it('聚焦形状压过黑点与数字桶', () => {
    const state = { ...normalState(), spotlight: SPOT };
    const layers: HitLayers = {
      ...baseLayers(),
      spot: { hitAt: () => 0 },
      hotspot: { hitAt: () => 3 },
      cluster: { hitAt: () => BUCKET },
    };
    expect(hitTest(layers, state, PX)).toEqual({ kind: 'spot', index: 0, unit: SPOT_UNIT });
  });

  it('黑点压过数字桶', () => {
    const layers: HitLayers = {
      ...baseLayers(),
      hotspot: { hitAt: () => 7 },
      cluster: { hitAt: () => BUCKET },
    };
    expect(hitTest(layers, normalState(), PX)).toEqual({ kind: 'hotspot', index: 7 });
  });

  it('常态下命中聚合桶', () => {
    const layers: HitLayers = { ...baseLayers(), cluster: { hitAt: () => BUCKET } };
    expect(hitTest(layers, normalState(), PX)).toEqual({ kind: 'bucket', hit: BUCKET });
  });

  it('降噪 / 聚焦 / 定位期间数字层收起:连命中都不问,返回空白', () => {
    for (const state of [
      { ...normalState(), analysisOn: true },
      { ...normalState(), spotlight: SPOT },
      { ...normalState(), casePin: { lng: 114, lat: 22.6, address: '南山区·测试路' } },
      { ...normalState(), layerVisible: { ...normalState().layerVisible, cluster: false } },
    ]) {
      let asked = false;
      const layers: HitLayers = {
        ...baseLayers(),
        cluster: {
          hitAt: () => {
            asked = true;
            return BUCKET;
          },
        },
      };
      expect(hitTest(layers, state, PX)).toBeNull();
      expect(asked).toBe(false);
    }
  });

  it('四层都没命中返回 null,调用方据此清选择', () => {
    expect(hitTest(baseLayers(), normalState(), PX)).toBeNull();
  });

  it('命中坐标原样传给每一层', () => {
    const seen: Array<[number, number]> = [];
    const layers: HitLayers = {
      pin: {
        hitClose: (x, y) => {
          seen.push([x, y]);
          return false;
        },
      },
      spot: {
        hitAt: (x, y) => {
          seen.push([x, y]);
          return -1;
        },
      },
      hotspot: {
        hitAt: (x, y) => {
          seen.push([x, y]);
          return -1;
        },
      },
      cluster: null,
    };
    hitTest(layers, normalState(), { x: 12, y: 34 });
    // 常态没有聚焦时不问 spot 层,只有关闭叉与黑点两层被调用
    expect(seen).toEqual([
      [12, 34],
      [12, 34],
    ]);
  });
});