~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/hydraulophone

An unofficial YouTube Music app for iPhone, iPad, and Mac.

0
0
main
79 lines · 2.4 KB Raw History
import Foundation
import Testing

@testable import HydraulophoneCore

struct LyricsWordTests {
  @Test
  func activeWordUsesStartInclusiveEndExclusiveIntervalsAndLeavesGapsUnhighlighted() {
    let line = LyricsLine(
      id: 1,
      text: "One two three",
      startTime: 0,
      endTime: 10,
      words: [
        LyricsWord(text: "One ", startTime: 1, endTime: 3),
        LyricsWord(text: "two ", startTime: 4, endTime: 6),
        LyricsWord(text: "three", startTime: 7, endTime: 9),
      ]
    )

    #expect(line.words.count == 3)
    #expect(line.activeWordIndex(at: 0) == nil)
    #expect(line.activeWordIndex(at: 1) == 0)
    #expect(line.activeWordIndex(at: 2.999) == 0)
    #expect(line.activeWordIndex(at: 3) == nil)
    #expect(line.activeWordIndex(at: 4) == 1)
    #expect(line.activeWordIndex(at: 6) == nil)
    #expect(line.activeWordIndex(at: 7) == 2)
    #expect(line.activeWordIndex(at: 9) == nil)
    #expect(line.activeWordIndex(at: 10) == nil)
  }

  @Test
  func nonFinitePlaybackPositionsNeverSelectAWord() {
    let line = LyricsLine(
      id: 1,
      text: "Word",
      startTime: 0,
      endTime: 2,
      words: [LyricsWord(text: "Word", startTime: 0, endTime: 2)]
    )

    #expect(line.activeWordIndex(at: -.infinity) == nil)
    #expect(line.activeWordIndex(at: -1) == nil)
    #expect(line.activeWordIndex(at: .nan) == nil)
    #expect(line.activeWordIndex(at: .infinity) == nil)
    #expect(line.activeWordIndex(at: 0) == 0)
    #expect(line.activeWordIndex(at: 2) == nil)
  }

  @Test
  func invalidWordDataKeepsPlainLineTextAndDropsWordHighlighting() {
    let mismatchedText = LyricsLine(
      id: 1,
      text: "First second",
      startTime: 0,
      endTime: 4,
      words: [
        LyricsWord(text: "First ", startTime: 0, endTime: 2),
        LyricsWord(text: "wrong", startTime: 2, endTime: 4),
      ]
    )
    #expect(mismatchedText.text == "First second")
    #expect(mismatchedText.words.isEmpty)
    #expect(mismatchedText.activeWordIndex(at: 1) == nil)

    for word in [
      LyricsWord(text: "Word", startTime: .nan, endTime: 2),
      LyricsWord(text: "Word", startTime: 1, endTime: 1),
      LyricsWord(text: "Word", startTime: 1, endTime: .infinity),
      LyricsWord(text: "Word", startTime: -1, endTime: 2),
    ] {
      let line = LyricsLine(id: 2, text: "Word", words: [word])
      #expect(line.text == "Word")
      #expect(line.words.isEmpty)
      #expect(line.activeWordIndex(at: 1) == nil)
    }
  }
}