v4TimelineRowHeightCache.test.ts 2.6 KB

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