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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import { AudioView, LabelStudio } from "@humansignal/frontend-test/helpers/LSF";
 
const config = `
<View>
  <Audio name="audio" value="$url" hotkey="space" sync="text" />
  <Header value="Sentiment"/>
  <ParagraphLabels name="label" toName="text">
    <Label value="General: Positive" background="#00ff00"/>
    <Label value="General: Negative" background="#ff0000"/>
    <Label value="Company: Positive" background="#7dff7d"/>
    <Label value="Company: Negative" background="#ff7d7d"/>
    <Label value="External: Positive" background="#4bff4b"/>
    <Label value="External: Negative" background="#ff4b4b"/>
  </ParagraphLabels>
  <View style="height: 400px; overflow-y: auto">
    <Header value="Transcript"/>
    <Paragraphs audioUrl="$url" contextscroll="true" sync="audio" name="text" value="$text" layout="dialogue" textKey="text" nameKey="author" showplayer="true" />
  </View>
</View>
`;
 
const configWithScroll = `
<View>
    <Audio name="audio" value="$url"
           hotkey="space" sync="text"/>
    <Header value="Transcript"/>
    <Paragraphs audioUrl="$url"
                sync="audio"
                name="text"
                value="$text"
                layout="dialogue"
                textKey="text"
                nameKey="author"
                contextscroll="true"
                granularity="paragraph"/>
     <View style="position: sticky">
      <Header value="Sentiment Labels"/>
      <ParagraphLabels name="label" toName="text">
        <Label value="General: Positive" background="#00ff00"/>
        <Label value="General: Negative" background="#ff0000"/>
        <Label value="Company: Positive" background="#7dff7d"/>
        <Label value="Company: Negative" background="#ff7d7d"/>
        <Label value="External: Positive" background="#4bff4b"/>
        <Label value="External: Negative" background="#ff4b4b"/>
      </ParagraphLabels>
    </View>
</View>
`;
 
const data = {
  url: "/public/files/opossum_intro.webm",
  text: [
    {
      end: 3,
      text: "Dont you hate that?",
      start: 1,
      author: "Mia Wallace",
    },
    {
      text: "Hate what?",
      start: 3,
      author: "Vincent Vega:",
      duration: 1,
    },
    {
      text: "Uncomfortable silences. Why do we feel its necessary to yak about nonsense in order to be comfortable?",
      author: "Mia Wallace:",
      start: 4,
      end: 6,
    },
    {
      text: "I dont know. Thats a good question.",
      start: 6,
      end: 8,
      author: "Vincent Vega:",
    },
    {
      text: "Thats when you know you found somebody really special. When you can just shut the door closed a minute, and comfortably share silence.",
      author: "Mia Wallace:",
      start: 8,
      end: 10,
    },
    {
      text: "Thats when you know you found somebody really special. When you can just shut the door closed a minute, and comfortably share silence.",
      author: "Mia Wallace:",
      start: 10,
      end: 12,
    },
    {
      text: "Thats when you know you found somebody really special. When you can just shut the door closed a minute, and comfortably share silence.",
      author: "Mia Wallace:",
      start: 12,
      end: 16,
    },
    {
      text: "Thats when you know you found somebody really special. When you can just shut the door closed a minute, and comfortably share silence.",
      author: "Mia Wallace:",
      start: 16,
      end: 20,
    },
  ],
};
 
const annotations = [
  {
    value: {
      start: "0",
      end: "0",
      startOffset: 0,
      endOffset: 4,
      text: "Dont",
      paragraphlabels: ["General: Negative"],
    },
    id: "RcHv5CdYBt",
    from_name: "label",
    to_name: "text",
    type: "paragraphlabels",
    origin: "manual",
  },
  {
    value: {
      start: "0",
      end: "0",
      startOffset: 9,
      endOffset: 13,
      text: "hate",
      paragraphlabels: ["General: Positive"],
    },
    id: "eePG7PVYH7",
    from_name: "label",
    to_name: "text",
    type: "paragraphlabels",
    origin: "manual",
  },
];
 
describe("Sync: Audio Paragraphs", () => {
  beforeEach(() => {
    LabelStudio.addFeatureFlagsOnPageLoad({
      ff_front_dev_2715_audio_3_280722_short: true,
      fflag_feat_front_lsdv_e_278_contextual_scrolling_short: true,
      fflag_feat_front_bros_199_enable_select_all_in_ner_phrase_short: false,
    });
 
    // expect uncaught exception for fast play/pause
    cy.on("uncaught:exception", () => false);
  });
 
  it("Play/pause is synced between audio and paragraphs when interacting with audio interface", () => {
    LabelStudio.params().config(config).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    cy.log("Audio is starting at 0");
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.equal(0);
    });
 
    AudioView.playButton.click();
    cy.wait(100);
 
    cy.log("Audio is playing");
    cy.get("audio").then(([audio]) => {
      expect(audio.paused).to.equal(false);
    });
 
    AudioView.pauseButton.click();
    cy.wait(100);
 
    cy.log("Audio is played and now paused");
    cy.get("audio").then(([audio]) => {
      expect(audio.paused).to.equal(true);
    });
  });
 
  it("Seeking is synced between audio and paragraphs when interacting with audio interface", () => {
    LabelStudio.params().config(config).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    AudioView.clickAt(100, 0);
    cy.log("Seek by clicking on some point in the audio timeline");
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.be.greaterThan(0);
    });
 
    AudioView.clickAt(0, 0);
    cy.log("Seek to beginning by clicking on the first point in the audio timeline");
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.equal(0);
    });
 
    AudioView.clickAt(300, 0);
    cy.log("Seek by clicking on some point further in the audio timeline");
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.be.greaterThan(0);
    });
  });
 
  it("Playback speed is synced between audio and paragraph audio when interacting with audio interface", () => {
    LabelStudio.params().config(config).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    // Get the main audio player and first paragraph audio
    cy.get('[data-testid="waveform-audio"]').then(([mainAudio]) => {
      cy.get(".lsf-paragraphs audio").then(([paragraphAudio]) => {
        const mainAudioElement = mainAudio as HTMLAudioElement;
        const paragraphAudioElement = paragraphAudio as HTMLAudioElement;
 
        expect(mainAudioElement.currentTime).to.equal(paragraphAudioElement.currentTime);
        expect(mainAudioElement.currentTime).to.equal(0);
        expect(mainAudioElement.playbackRate).to.equal(paragraphAudioElement.playbackRate);
        expect(mainAudioElement.playbackRate).to.equal(1);
      });
    });
 
    // Set playback speed before playing
    AudioView.setPlaybackSpeedInput(1.5, false); // false = audio-only, don't check video
    AudioView.playButton.click();
    cy.wait(1000);
 
    // Check sync during playback
    cy.get('[data-testid="waveform-audio"]').then(([mainAudio]) => {
      cy.get(".lsf-paragraphs audio").then(([paragraphAudio]) => {
        const mainAudioElement = mainAudio as HTMLAudioElement;
        const paragraphAudioElement = paragraphAudio as HTMLAudioElement;
 
        expect(mainAudioElement.playbackRate).to.equal(paragraphAudioElement.playbackRate);
        expect(mainAudioElement.playbackRate).to.equal(1.5);
      });
    });
 
    // Change speed during playback
    AudioView.setPlaybackSpeedInput(1, false); // false = audio-only, don't check video
    cy.wait(1000);
 
    // Check sync after speed change
    cy.get('[data-testid="waveform-audio"]').then(([mainAudio]) => {
      cy.get(".lsf-paragraphs audio").then(([paragraphAudio]) => {
        const mainAudioElement = mainAudio as HTMLAudioElement;
        const paragraphAudioElement = paragraphAudio as HTMLAudioElement;
 
        expect(mainAudioElement.playbackRate).to.equal(paragraphAudioElement.playbackRate);
        expect(mainAudioElement.playbackRate).to.equal(1);
      });
    });
 
    // Check final sync
    cy.get('[data-testid="waveform-audio"]').then(([mainAudio]) => {
      cy.get(".lsf-paragraphs audio").then(([paragraphAudio]) => {
        const mainAudioElement = mainAudio as HTMLAudioElement;
        const paragraphAudioElement = paragraphAudio as HTMLAudioElement;
 
        expect(mainAudioElement.currentTime).to.be.closeTo(paragraphAudioElement.currentTime, 0.4);
      });
    });
  });
 
  it("Play/pause is synced between audio and paragraphs when interacting with paragraph interface", () => {
    LabelStudio.params().config(config).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    cy.log("Audio is starting at 0");
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.equal(0);
    });
 
    cy.get('[data-testid="phrase:0"]').siblings('button[aria-label="play"]').click();
    cy.wait(100);
 
    cy.log("Audio is playing");
    cy.get("audio").then(([audio]) => {
      expect(audio.paused).to.equal(false);
    });
 
    cy.get('[data-testid="phrase:0"]').siblings('button[aria-label="pause"]').click();
 
    cy.log("Audio is played and now paused");
    cy.get("audio").then(([audio]) => {
      expect(audio.paused).to.equal(true);
    });
  });
 
  it("Audio clip is played when selecting the play button next to a paragraph segment", () => {
    LabelStudio.params().config(config).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.equal(0);
    });
 
    cy.get('[data-testid="phrase:0"]').siblings('button[aria-label="play"]').click();
    cy.wait(100);
 
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.not.equal(0);
    });
 
    cy.get('[data-testid="phrase:1"]').siblings('button[aria-label="play"]').click();
    cy.wait(100);
 
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.not.equal(0);
    });
  });
 
  it("Playback button states continually change over time according to the paragraph segment which is being played", () => {
    LabelStudio.params().config(config).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.equal(0);
    });
 
    AudioView.playButton.click();
 
    // Plays the first paragraph segment when the audio interface is played
    cy.get('[data-testid="phrase:0"]').siblings('button[aria-label="pause"]').should("exist");
    cy.get('[data-testid="phrase:1"]').siblings('button[aria-label="play"]').should("exist");
    cy.get('[data-testid="phrase:2"]').siblings('button[aria-label="play"]').should("exist");
    cy.get('[data-testid="phrase:3"]').siblings('button[aria-label="play"]').should("exist");
    cy.get('[data-testid="phrase:4"]').siblings('button[aria-label="play"]').should("exist");
 
    cy.wait(2000);
 
    // Plays the second paragraph segment when the audio progresses to the second paragraph segment
    cy.get('[data-testid="phrase:1"]').siblings('button[aria-label="pause"]').should("exist");
    cy.get('[data-testid="phrase:0"]').siblings('button[aria-label="play"]').should("exist");
    cy.get('[data-testid="phrase:2"]').siblings('button[aria-label="play"]').should("exist");
    cy.get('[data-testid="phrase:3"]').siblings('button[aria-label="play"]').should("exist");
    cy.get('[data-testid="phrase:4"]').siblings('button[aria-label="play"]').should("exist");
 
    cy.wait(2000);
 
    // Plays the third paragraph segment when the audio progresses to the third paragraph segment
    cy.get('[data-testid="phrase:2"]').siblings('button[aria-label="pause"]').should("exist");
    cy.get('[data-testid="phrase:0"]').siblings('button[aria-label="play"]').should("exist");
    cy.get('[data-testid="phrase:1"]').siblings('button[aria-label="play"]').should("exist");
    cy.get('[data-testid="phrase:3"]').siblings('button[aria-label="play"]').should("exist");
    cy.get('[data-testid="phrase:4"]').siblings('button[aria-label="play"]').should("exist");
  });
 
  it("Check if paragraph is scrolling automatically following the audio", () => {
    LabelStudio.params().config(configWithScroll).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    // Inject CSS to force scrolling in test environment
    // cy.get("head").invoke(
    //   "append",
    //   `<style>
    //     [data-testid="phrases-wrapper"] {
    //       max-height: 120px !important;
    //       overflow-y: auto !important;
    //     }
    //   </style>`,
    // );
 
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.equal(0);
    });
 
    AudioView.playButton.click();
    cy.wait(5100);
 
    cy.get('[data-testid="phrases-wrapper"]').then(($el) => {
      expect($el[0].scrollTop).to.be.greaterThan(100);
    });
  });
 
  it("Paragraph should automatically scroll if user seeks audio player", () => {
    LabelStudio.params().config(configWithScroll).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.equal(0);
    });
 
    AudioView.playButton.click();
    cy.wait(3000);
 
    AudioView.clickAt(0, 0);
    cy.wait(1000);
 
    AudioView.pauseButton.click();
    cy.wait(1000);
 
    cy.get('[data-testid="phrases-wrapper"]').then(($el) => {
      const scrollTop = $el[0].scrollTop;
      // Expect small padding for visual breathing room (should be greater than 0 but less than 20)
      expect(scrollTop).to.be.greaterThan(0);
      expect(scrollTop).to.be.lessThan(20);
    });
  });
 
  it("Paragraph shouldnt automatically scroll if user disable the auto-scroll toggle", () => {
    LabelStudio.params().config(configWithScroll).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.equal(0);
    });
 
    cy.get('[data-testid="auto-scroll-toggle"]').click();
    AudioView.playButton.click();
    cy.wait(5000);
 
    cy.get('[data-testid="phrases-wrapper"]').then(($el) => {
      expect($el[0].scrollTop).to.equal(0);
    });
  });
 
  it("Paragraph shouldnt automatically scroll if user manually scroll and the current paragraph is not in the screen", () => {
    LabelStudio.params().config(configWithScroll).data(data).withResult(annotations).init();
 
    LabelStudio.waitForObjectsReady();
    AudioView.isReady();
 
    cy.get("audio").then(([audio]) => {
      expect(audio.currentTime).to.equal(0);
    });
 
    AudioView.playButton.click();
    cy.wait(100);
 
    cy.get('[data-testid="phrases-wrapper"]').then(($el) => {
      $el[0].scrollTo(0, 1000);
      const wheelEvt = new WheelEvent("wheel", { deltaY: 1200 });
      $el[0].dispatchEvent(wheelEvt);
    });
 
    cy.wait(5000);
 
    cy.get('[data-testid="phrases-wrapper"]').then(($el) => {
      expect($el[0].scrollTop).to.be.greaterThan(190);
    });
  });
});