intentActions.test.ts 3.04 KB
/**
 * 阶段 3 的等价性契约:四个 action 从「改 store 里的 seq」换成「改业务字段 + 发意图」。
 *
 * 原来 seq 与业务字段在同一次 set 里落地;现在分成 set + dispatch 两步,
 * 因此要钉住两件事:
 *   1. 业务字段的连带清空没丢(spotlight / selection / members / drawerOpen / analysisError);
 *   2. 每次调用都发一次意图,重复点同一行也能重新执行(原来靠 seq += 1)。
 */
import { afterEach, describe, expect, it } from 'vitest';
import { useHeatmapStore } from './useHeatmapStore';
import type { CasePin, SpotlightOpen, SpotlightView } from './useHeatmapStore';
import {
  getAnalysisRerunVersion,
  getCaseFocusVersion,
  getHotspotFocusVersion,
  getSpotlightOpenVersion,
  resetIntentsForTests,
} from './intentBus';

const reset = () => {
  useHeatmapStore.setState({
    activeHotspot: null,
    casePin: null,
    spotlight: null,
    analysisError: '上次的失败',
    selection: null,
    members: null,
    drawerOpen: true,
  });
  resetIntentsForTests();
};

afterEach(reset);

describe('store action 的意图等价性', () => {
  it('pickHotspot:清高亮 + 收聚焦 + 发一次黑点定位意图', () => {
    const view: SpotlightView = {
      kind: 'districts',
      source: '测试聚焦',
      cellM: 200,
      units: [],
      base: useHeatmapStore.getState().filter,
      active: null,
      note: null,
    };
    useHeatmapStore.setState({ spotlight: view });
    useHeatmapStore.getState().pickHotspot(7);

    expect(useHeatmapStore.getState().activeHotspot).toBe(7);
    expect(useHeatmapStore.getState().spotlight).toBeNull();
    expect(getHotspotFocusVersion()).toBe(1);
  });

  it('rerunAnalysis:清失败原文 + 发一次重算意图', () => {
    useHeatmapStore.getState().rerunAnalysis();

    expect(useHeatmapStore.getState().analysisError).toBeNull();
    expect(getAnalysisRerunVersion()).toBe(1);
  });

  it('focusCasePoint:挪图钉 + 收聚焦 + 发一次案件定位意图', () => {
    const pin: CasePin = { lng: 114.06, lat: 22.54, address: '测试地址' };
    useHeatmapStore.getState().focusCasePoint(pin);

    expect(useHeatmapStore.getState().casePin).toEqual(pin);
    expect(useHeatmapStore.getState().spotlight).toBeNull();
    expect(getCaseFocusVersion()).toBe(1);
  });

  it('openSpotlight:按当前筛选快照 + 收抽屉 + 发一次聚焦意图', () => {
    const view: SpotlightOpen = {
      kind: 'districts',
      source: '测试聚焦',
      cellM: 200,
      units: [],
    };
    useHeatmapStore.getState().openSpotlight(view);

    const st = useHeatmapStore.getState();
    expect(st.spotlight?.base).toEqual(st.filter);
    expect(st.selection).toBeNull();
    expect(st.members).toBeNull();
    expect(st.drawerOpen).toBe(false);
    expect(getSpotlightOpenVersion()).toBe(1);
  });

  it('连点同一行两次:版本 +2,与原来 seq 自增逐次对应', () => {
    useHeatmapStore.getState().pickHotspot(3);
    useHeatmapStore.getState().pickHotspot(3);
    expect(getHotspotFocusVersion()).toBe(2);
  });
});