dims.test.ts 6.41 KB
import { describe, expect, it } from 'vitest';
import {
  ROAD_UNRESOLVED,
  bandOfHour,
  buildRoadIndex,
  countForShare,
  hoursOfBand,
  nightShareOf,
  roadOfAddress,
  sumTop,
  TIME_BANDS,
} from './dims';

/**
 * 道路名与时段档位的单测。
 *
 * 路名解析是「自由文本 → 榜单」的入口,写错的后果不是报错,而是榜上多出一条不存在的路、
 * 或者某条真路被切成两半(各自都不起眼)。所以每种地址写法都钉一条断言,
 * 用例全部取内置样本与真实模板「省-市-区-具体地址」里的实际长相。
 */
describe('roadOfAddress', () => {
  it('路名后面跟着店铺、社区这些字,只取到后缀为止', () => {
    expect(roadOfAddress('东园路汽配城')).toBe('东园路');
    expect(roadOfAddress('宝安大道医院')).toBe('宝安大道');
    expect(roadOfAddress('泰然九路路口')).toBe('泰然九路');
    expect(roadOfAddress('翠幽路天桥')).toBe('翠幽路');
  });

  it('整条地址就是一条路名', () => {
    expect(roadOfAddress('解放路')).toBe('解放路');
    expect(roadOfAddress('章阁路')).toBe('章阁路');
  });

  it('长后缀优先:「大道」不能被读成「大」+ 短后缀', () => {
    expect(roadOfAddress('深南大道建材市场')).toBe('深南大道');
    expect(roadOfAddress('塘家大道路口')).toBe('塘家大道');
  });

  it('行政街道不是路段:吞进路名会冒出一条不存在的路还霸榜', () => {
    expect(roadOfAddress('西乡街道海山路18 号')).toBe('海山路');
    expect(roadOfAddress('粤海街道科苑南路 3000 号')).toBe('科苑南路');
  });

  it('地址层级、门牌不算路名的一部分', () => {
    expect(roadOfAddress('南山区科技园南路-18 号')).toBe('科技园南路');
    expect(roadOfAddress('宝安大道-2 栋 3 层')).toBe('宝安大道');
    expect(roadOfAddress('宝龙工业区公园路')).toBe('公园路');
  });

  it('国道、高速这一类编号写法要留得住', () => {
    expect(roadOfAddress('S308省道建材市场')).toBe('S308省道');
    expect(roadOfAddress('梅观高速出口')).toBe('梅观高速');
    expect(roadOfAddress('水官高速公路收费站')).toBe('水官高速公路');
  });

  it('「无名路」读得出后缀、读不出是哪条路,按读不出算', () => {
    expect(roadOfAddress('无名路544号')).toBe('');
    expect(roadOfAddress('未命名道路12 号')).toBe('');
  });

  it('读不出路名的地址返回空串,不硬凑一个名字', () => {
    expect(roadOfAddress('深圳市华强北商圈附近')).toBe('');
    expect(roadOfAddress('大梅沙海滨公园汽修厂')).toBe('');
    expect(roadOfAddress('')).toBe('');
  });

  it('一条地址只取第一个路名:一行案件归一条路,总数才不会被重复计数', () => {
    expect(roadOfAddress('深南大道与福华一路交叉口')).toBe('深南大道');
  });
});

describe('buildRoadIndex', () => {
  it('下标 0 固定是「读不出路名」那一档,同一条路复用同一个下标', () => {
    const { names, ofDetail } = buildRoadIndex([
      '东园路汽配城',
      '无名路544号',
      '东乐路汽配城',
      '东园路 3 号',
    ]);
    expect(ofDetail[1]).toBe(ROAD_UNRESOLVED);
    expect(names[0]).toBe('');
    expect(ofDetail[0]).toBe(ofDetail[3]);
    expect(ofDetail[0]).not.toBe(ofDetail[2]);
    expect(names[ofDetail[0]]).toBe('东园路');
    expect(names[ofDetail[2]]).toBe('东乐路');
  });

  it('全部读不出时字典里只留那一档', () => {
    const { names, ofDetail } = buildRoadIndex(['华强北商圈附近', '无名路1 号']);
    expect(names).toEqual(['']);
    expect(Array.from(ofDetail)).toEqual([ROAD_UNRESOLVED, ROAD_UNRESOLVED]);
  });
});

describe('时段档位', () => {
  it('六档首尾相接,铺满 24 小时且不重叠', () => {
    expect(TIME_BANDS[0].from).toBe(0);
    expect(TIME_BANDS[TIME_BANDS.length - 1].to).toBe(24);
    for (let i = 1; i < TIME_BANDS.length; i += 1) {
      expect(TIME_BANDS[i].from).toBe(TIME_BANDS[i - 1].to);
    }
    for (let h = 0; h < 24; h += 1) {
      const b = TIME_BANDS[bandOfHour(h)];
      expect(h).toBeGreaterThanOrEqual(b.from);
      expect(h).toBeLessThan(b.to);
    }
  });

  it('档位里的小时列表与档位边界一致(清单按它取数,必须和图上那一格同一口径)', () => {
    expect(hoursOfBand(TIME_BANDS[0])).toEqual([0, 1, 2, 3, 4, 5]);
    expect(hoursOfBand(TIME_BANDS[5])).toEqual([22, 23]);
  });

  it('夜间档跨零点:晚上 10 点到次日早上 6 点', () => {
    expect(TIME_BANDS.map((b) => b.label)).toEqual([
      '凌晨',
      '早间',
      '午间',
      '下午',
      '晚间',
      '深夜',
    ]);
    const hist = new Array(24).fill(0);
    hist[23] = 1;
    hist[0] = 1;
    hist[9] = 2;
    expect(nightShareOf(hist)).toBeCloseTo(0.5);
    // 全区都没有案子时不能算成 0/0 = NaN,界面要读的是「没有数据」
    expect(nightShareOf(new Array(24).fill(0))).toBe(0);
  });
});

describe('countForShare', () => {
  it('累计到半数、八成各需要几条', () => {
    const sorted = [50, 30, 15, 5];
    expect(countForShare(sorted, 0.5)).toBe(1);
    expect(countForShare(sorted, 0.8)).toBe(2);
    expect(countForShare(sorted, 1)).toBe(4);
  });

  it('空表与全零不给假数字', () => {
    expect(countForShare([], 0.5)).toBe(0);
    expect(countForShare([0, 0], 0.5)).toBe(0);
  });
});

describe('sumTop', () => {
  it('前 N 条合计;N 超过条目数时按全部算', () => {
    const sorted = [50, 30, 15, 5];
    expect(sumTop(sorted, 1)).toBe(50);
    expect(sumTop(sorted, 3)).toBe(95);
    expect(sumTop(sorted, 10)).toBe(100);
    expect(sumTop([], 3)).toBe(0);
  });

  // 面板上「前 3 个区占多少」与「半数案件来自几处」是并排读的两个数,
  // 后者数出来的那几处加起来必须真能覆盖到那个比例,否则两块自相矛盾
  it('覆盖账与头部合计对得上:凑到某比例的那几条之和不低于该比例', () => {
    const sorted = [420, 260, 180, 90, 50];
    const total = sumTop(sorted, sorted.length);
    for (const share of [0.5, 0.8, 1]) {
      const n = countForShare(sorted, share);
      expect(sumTop(sorted, n) / total).toBeGreaterThanOrEqual(share - 1e-9);
      // 少一处就不该够 —— 这一条保证「几处」不是随便报的保守数字
      if (n > 1) expect(sumTop(sorted, n - 1) / total).toBeLessThan(share);
    }
  });
});