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