v4TimelineRowHeightCache.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // M5④ 虚拟滚动核心:v4 timeline 行高缓存单测(纯数据结构)。
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. DEFAULT_ROW_HEIGHT_ESTIMATE_PX,
  5. TimelineRowHeightCache,
  6. } from "@/v4/timelineRowHeightCache.js";
  7. describe("TimelineRowHeightCache", () => {
  8. it("未测量行回落估计值;测量后 estimate 返回真实高度(行卸载重挂保缓存)", () => {
  9. const cache = new TimelineRowHeightCache();
  10. expect(cache.estimate(1)).toBe(DEFAULT_ROW_HEIGHT_ESTIMATE_PX);
  11. expect(cache.estimate(1, 100)).toBe(100);
  12. cache.set(1, 237);
  13. expect(cache.estimate(1)).toBe(237);
  14. expect(cache.get(1)).toBe(237);
  15. // 其他行不受影响
  16. expect(cache.estimate(2)).toBe(DEFAULT_ROW_HEIGHT_ESTIMATE_PX);
  17. });
  18. it("rowId 未知(越界索引)回落估计值", () => {
  19. const cache = new TimelineRowHeightCache();
  20. expect(cache.estimate(undefined)).toBe(DEFAULT_ROW_HEIGHT_ESTIMATE_PX);
  21. expect(cache.estimate(undefined, 48)).toBe(48);
  22. });
  23. it("重复写入以最新测量为准(流式行持续长高)", () => {
  24. const cache = new TimelineRowHeightCache();
  25. cache.set(7, 72);
  26. cache.set(7, 340);
  27. expect(cache.estimate(7)).toBe(340);
  28. expect(cache.size).toBe(1);
  29. });
  30. it("HLP01:以稳定 turnId 缓存,补到更早 header 后仍复用测高", () => {
  31. const cache = new TimelineRowHeightCache();
  32. cache.set("turn-long", 480);
  33. expect(cache.estimate("turn-long")).toBe(480);
  34. });
  35. it("拒绝无效高度(0/负数/NaN 不覆盖已有测量)", () => {
  36. const cache = new TimelineRowHeightCache();
  37. cache.set(3, 120);
  38. cache.set(3, 0);
  39. cache.set(3, -5);
  40. cache.set(3, Number.NaN);
  41. expect(cache.estimate(3)).toBe(120);
  42. });
  43. it("超上限按写入序淘汰最旧行;重复写入刷新淘汰顺序", () => {
  44. const cache = new TimelineRowHeightCache(3);
  45. cache.set(1, 10);
  46. cache.set(2, 20);
  47. cache.set(3, 30);
  48. // 刷新 rowId=1 的活跃度,随后插入第 4 项应淘汰 rowId=2
  49. cache.set(1, 11);
  50. cache.set(4, 40);
  51. expect(cache.get(2)).toBeUndefined();
  52. expect(cache.get(1)).toBe(11);
  53. expect(cache.get(3)).toBe(30);
  54. expect(cache.get(4)).toBe(40);
  55. expect(cache.size).toBe(3);
  56. });
  57. it("clear 全量重置(会话切换防 rowId 串号)", () => {
  58. const cache = new TimelineRowHeightCache();
  59. cache.set(1, 100);
  60. cache.clear();
  61. expect(cache.size).toBe(0);
  62. expect(cache.estimate(1)).toBe(DEFAULT_ROW_HEIGHT_ESTIMATE_PX);
  63. });
  64. });