Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { LabelStudio, Sidebar, VideoView } from "@humansignal/frontend-test/helpers/LSF";
import {
  timelineVideoConfig,
  timelineVideoData,
  singleTimelineRegionResult,
  multipleTimelineRegionsResult,
  overlappingTimelineRegionsResult,
  multipleTimelineRegionsLongResult,
} from "../../data/video_segmentation/timeline_regions";
import { TWO_FRAMES_TIMEOUT } from "../utils/constants";
 
// This test suite has exhibited flakiness in CI environments, so we are using retries
// while we work on improving CI stability for visual comparisons.
const suiteConfig = {
  retries: {
    runMode: 3, // Retry 3 times in CI (headless mode)
    openMode: 0, // No retries in local development (interactive mode)
  },
};
 
describe("Video Timeline Region Loop Playback", suiteConfig, () => {
  beforeEach(() => {
    LabelStudio.addFeatureFlagsOnPageLoad({
      fflag_fix_front_optic_1608_improve_video_frame_seek_precision_short: true,
    });
  });
 
  describe("Single Region Playback", () => {
    it("Should play selected region from beginning to end and stop", () => {
      LabelStudio.params()
        .config(timelineVideoConfig)
        .data(timelineVideoData)
        .withResult(singleTimelineRegionResult)
        .init();
 
      LabelStudio.waitForObjectsReady();
      Sidebar.hasRegions(1);
 
      // Position cursor outside the region (frame 20)
      VideoView.clickAtFrame(20);
      VideoView.waitForFrame(20);
 
      // Select the region
      Sidebar.toggleRegionSelection(0);
      Sidebar.hasSelectedRegions(1);
 
      // Start playback
      VideoView.play();
 
      // Verify video moved back to region and played to the end (frame 15)
      VideoView.verifyPlayingRange(
        singleTimelineRegionResult[0].value.ranges[0].end,
        singleTimelineRegionResult[0].value.ranges[0].end,
      );
 
      // Video should be paused after region end
      VideoView.pauseButton.should("not.exist");
      VideoView.playButton.should("be.visible");
 
      // Verify current frame is at region end
      VideoView.getCurrentFrame().should("be.eq", 15);
    });
 
    it("Should play from current position if cursor is within region", () => {
      LabelStudio.params()
        .config(timelineVideoConfig)
        .data(timelineVideoData)
        .withResult(singleTimelineRegionResult)
        .init();
 
      LabelStudio.waitForObjectsReady();
      Sidebar.hasRegions(1);
 
      // Select the region (frames 5-15)
      Sidebar.toggleRegionSelection(0);
      Sidebar.hasSelectedRegions(1);
 
      // Position cursor within the region (frame 10)
      VideoView.setCurrentFrame(10);
      VideoView.waitForFrame(10);
 
      // Start playback
      VideoView.play();
 
      // Should start from frame 10 and play to frame 15
      VideoView.getCurrentFrame().should("be.greaterThan", 9);
 
      VideoView.waitForFrame(15);
 
      // Video should stop at region end
      VideoView.playButton.should("be.visible");
    });
  });
 
  describe("Multiple Regions Playback", () => {
    it("Should play from earliest region start to latest region end", () => {
      LabelStudio.params()
        .config(timelineVideoConfig)
        .data(timelineVideoData)
        .withResult(multipleTimelineRegionsResult)
        .init();
 
      LabelStudio.waitForObjectsReady();
      Sidebar.hasRegions(2);
 
      // Position cursor outside the regions
      VideoView.clickAtFrame(25);
      VideoView.waitForFrame(25);
 
      // Select both regions
      Sidebar.toggleRegionSelection(0);
      Sidebar.hasSelectedRegions(1);
      Sidebar.toggleRegionSelection(1, true); // Select both regions
      Sidebar.hasSelectedRegions(2);
 
      // Start playback
      VideoView.play();
 
      // Should play from first region to the end of the second region
      VideoView.verifyPlayingRange(
        multipleTimelineRegionsResult[0].value.ranges[0].end,
        multipleTimelineRegionsResult[1].value.ranges[0].end,
      );
 
      VideoView.playButton.should("be.visible");
    });
 
    it("Should handle overlapping regions correctly", () => {
      LabelStudio.params()
        .config(timelineVideoConfig)
        .data(timelineVideoData)
        .withResult(overlappingTimelineRegionsResult)
        .init();
 
      LabelStudio.waitForObjectsReady();
      Sidebar.hasRegions(2);
 
      VideoView.clickAtFrame(25);
 
      // Select both overlapping regions (8-12 and 10-18)
      Sidebar.toggleRegionSelection(0);
      Sidebar.hasSelectedRegions(1);
      Sidebar.toggleRegionSelection(1, true); // Select both regions
      Sidebar.hasSelectedRegions(2);
 
      VideoView.play();
 
      // Should play from first region to the end of the second region
      VideoView.verifyPlayingRange(
        overlappingTimelineRegionsResult[0].value.ranges[0].end,
        overlappingTimelineRegionsResult[1].value.ranges[0].end,
      );
 
      VideoView.playButton.should("be.visible");
    });
  });
 
  describe("Loop Timeline Region Feature", () => {
    it("Should loop region playback when loop option is enabled", () => {
      LabelStudio.params()
        .config(timelineVideoConfig)
        .data(timelineVideoData)
        .withResult(singleTimelineRegionResult)
        .init();
 
      LabelStudio.waitForObjectsReady();
      Sidebar.hasRegions(1);
 
      // Enable loop timeline region option
      VideoView.toggleConfigModal();
      VideoView.enableLoopTimelineRegion();
      VideoView.isLoopTimelineRegionEnabled();
      VideoView.toggleConfigModal(); // Close modal
 
      // Select the region
      Sidebar.toggleRegionSelection(0);
      Sidebar.hasSelectedRegions(1);
 
      // Start playback
      VideoView.play();
 
      // Wait for first loop (frames 5-15)
      VideoView.waitForFrame(5);
 
      VideoView.waitForFrame(15);
 
      // Should loop back to start instead of stopping
      VideoView.waitForFrame(5);
 
      VideoView.waitForFrame(15);
 
      // Video should still be playing (not stopped)
      VideoView.pauseButton.should("be.visible");
 
      // Manually pause to end test
      VideoView.pause();
    });
 
    it("Should persist loop timeline region setting between page reloads", () => {
      // First session - enable loop setting
      LabelStudio.params()
        .config(timelineVideoConfig)
        .data(timelineVideoData)
        .withResult(singleTimelineRegionResult)
        .init();
 
      LabelStudio.waitForObjectsReady();
 
      VideoView.toggleConfigModal();
      VideoView.enableLoopTimelineRegion();
      VideoView.isLoopTimelineRegionEnabled();
      VideoView.toggleConfigModal();
 
      cy.window().then((win) => {
        // trigger saving to localStorage
        win.LabelStudio.destroyAll();
        const localStorage = JSON.parse(JSON.stringify(win.localStorage));
 
        // Reinitialize LSF (simulate page reload)
        LabelStudio.params()
          .config(timelineVideoConfig)
          .data(timelineVideoData)
          .withResult(singleTimelineRegionResult)
          .localStorageItems(localStorage)
          .init();
 
        LabelStudio.waitForObjectsReady();
 
        // Check that setting is still enabled
        VideoView.toggleConfigModal();
        VideoView.isLoopTimelineRegionEnabled();
 
        VideoView.disableLoopTimelineRegion();
        VideoView.isLoopTimelineRegionDisabled();
        VideoView.toggleConfigModal();
      });
 
      cy.window().then((win) => {
        // trigger saving to localStorage
        win.LabelStudio.destroyAll();
        const localStorage = JSON.parse(JSON.stringify(win.localStorage));
        // Reinitialize LSF (simulate page reload)
        LabelStudio.params()
          .config(timelineVideoConfig)
          .data(timelineVideoData)
          .withResult(singleTimelineRegionResult)
          .localStorageItems(localStorage)
          .init();
 
        LabelStudio.waitForObjectsReady();
 
        // Check that setting is still disabled
        VideoView.toggleConfigModal();
        VideoView.isLoopTimelineRegionDisabled();
        VideoView.toggleConfigModal();
      });
    });
  });
 
  describe("Edge Cases", () => {
    it("Should handle region selection changes during playback", () => {
      LabelStudio.params()
        .config(timelineVideoConfig)
        .data(timelineVideoData)
        .withResult(multipleTimelineRegionsLongResult)
        .init();
 
      LabelStudio.waitForObjectsReady();
      Sidebar.hasRegions(2);
 
      // Start with first region selected
      Sidebar.toggleRegionSelection(1);
      cy.wait(TWO_FRAMES_TIMEOUT);
      VideoView.play();
 
      VideoView.verifyPlayingRange(20, 15, true);
 
      // Change selection to second region during playback
      Sidebar.toggleRegionSelection(0); // Select another region
 
      VideoView.verifyPlayingRange(9, 10);
      cy.wait(TWO_FRAMES_TIMEOUT);
      VideoView.getCurrentFrame().should("be.eq", 10);
    });
 
    it("Should handle no regions selected gracefully", () => {
      LabelStudio.params()
        .config(timelineVideoConfig)
        .data(timelineVideoData)
        .withResult(singleTimelineRegionResult)
        .init();
 
      LabelStudio.waitForObjectsReady();
      Sidebar.hasRegions(1);
      Sidebar.hasSelectedRegions(0); // No regions selected by default
 
      // Try to play without selected regions
      VideoView.clickAtFrame(2);
      VideoView.play();
 
      VideoView.verifyPlayingRange(10, 40, true);
      VideoView.pause();
    });
  });
});