Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3import {
4 createComposerScrollGestureState,
5 recordComposerScrollGestureEvent,
6} from "./composerScrollGesture";
7import { createUpwardScrollDetector, isTimelineScrollTarget } from "./timelineScrollTarget";
8
9class ScrollElement extends EventTarget {
10 scrollTop = 0;
11 scrollHeight = 100;
12 clientHeight = 100;
13 overflowY = "visible";
14 overscrollBehaviorY = "auto";
15
16 constructor(readonly parentElement: ScrollElement | null = null) {
17 super();
18 }
19
20 contains(target: ScrollElement): boolean {
21 return (
22 target === this || (target.parentElement !== null && this.contains(target.parentElement))
23 );
24 }
25}
26
27function targetsTimeline(target: EventTarget | null, timeline: ScrollElement, deltaY: number) {
28 return isTimelineScrollTarget(target, timeline as unknown as HTMLElement, deltaY);
29}
30
31function setup() {
32 const timeline = Object.assign(new ScrollElement(), {
33 overflowY: "auto",
34 scrollHeight: 1500,
35 clientHeight: 500,
36 scrollTop: 1000,
37 });
38 const group = Object.assign(new ScrollElement(timeline), {
39 overflowY: "auto",
40 scrollHeight: 300,
41 scrollTop: 80,
42 });
43 return { timeline, group, content: new ScrollElement(group) };
44}
45
46beforeEach(() => {
47 vi.stubGlobal("Element", ScrollElement);
48 vi.stubGlobal("getComputedStyle", (element: ScrollElement) => element);
49});
50afterEach(() => vi.unstubAllGlobals());
51
52describe("timeline scroll targets", () => {
53 it.each([-30, 30])("keeps a nested tool group's scroll out of the timeline: %i", (deltaY) => {
54 const { timeline, group, content } = setup();
55 expect(targetsTimeline(content, timeline, deltaY)).toBe(false);
56 expect(targetsTimeline(group, timeline, deltaY)).toBe(false);
57 });
58
59 it.each([
60 { scrollTop: 0, deltaY: -30 },
61 { scrollTop: 200, deltaY: 30 },
62 ])("allows chaining only past the matching edge: %j", ({ scrollTop, deltaY }) => {
63 const { timeline, group, content } = setup();
64 group.scrollTop = scrollTop;
65 expect(targetsTimeline(content, timeline, deltaY)).toBe(true);
66 expect(targetsTimeline(content, timeline, -deltaY)).toBe(false);
67 });
68
69 it("does not accumulate nested scrolling toward composer collapse", () => {
70 const { timeline, group, content } = setup();
71 const state = createComposerScrollGestureState();
72 const record = (target: ScrollElement, now: number, deltaPx: number) =>
73 recordComposerScrollGestureEvent(state, {
74 now,
75 deltaPx,
76 collapseThresholdPx: 24,
77 collapseEligible: targetsTimeline(target, timeline, -deltaPx),
78 canScrollInGestureDirection: timeline.scrollTop > 0,
79 scrollsTowardLogicalEnd: false,
80 });
81
82 expect(record(timeline, 0, 20)).toBe(false);
83 expect(record(content, 20, 30)).toBe(false);
84 group.scrollTop = 0;
85 expect(record(content, 40, 10)).toBe(false);
86 expect(record(content, 60, 14)).toBe(true);
87 });
88});
89
90describe("timeline scroll follow opt-out", () => {
91 it("detects iframe-only upward scrolling after following streaming content", () => {
92 const scrolledUp = createUpwardScrollDetector({ top: 1000, height: 1500 });
93 expect(scrolledUp({ top: 1000, height: 1800 })).toBe(false);
94 expect(scrolledUp({ top: 1300, height: 1800 })).toBe(false);
95 expect(scrolledUp({ top: 1200, height: 1700 })).toBe(false);
96
97 // Sandboxed frames deliver no wheel or touch event to the parent timeline.
98 expect(scrolledUp({ top: 1199.5, height: 1700 })).toBe(false);
99 expect(scrolledUp({ top: 1199, height: 1700 })).toBe(false);
100 expect(scrolledUp({ top: 1198.5, height: 1700 })).toBe(true);
101 });
102
103 it("preserves upward new-turn positioning, then detects the reader leaving that anchor", () => {
104 const scrolledUp = createUpwardScrollDetector({ top: 1400, height: 2400 });
105 // The caller must distinguish its animated positioning from iframe scroll chaining.
106 const positioning = { top: 1200, height: 2400, programmatic: true };
107 expect(scrolledUp(positioning)).toBe(false);
108 expect(scrolledUp({ ...positioning, top: 1100 })).toBe(false);
109
110 expect(scrolledUp({ top: 1100, height: 2400 })).toBe(false);
111 expect(scrolledUp({ top: 950, height: 2400 })).toBe(true);
112 });
113});