success = ref(false)

a = sine(duration=5., 440.)
a = metadata.map(id="a", update=false, (fun (_) -> [("source", "a")]), a)

b = sine(duration=5., 880.)
b =
  metadata.map(
    id="b",
    update=false,
    (fun (_) -> [("source", "b"), ("liq_cross_start_duration", "1.1")]),
    b
  )

c = sine(duration=5., 440.)
c = metadata.map(id="c", update=false, (fun (_) -> [("source", "c")]), c)

s = sequence([a, b, c])
s = crossfade(start_duration=5., end_duration=4., s)

track_count = ref(0)

def on_metadata(m) =
  ref.incr(track_count)

  if track_count() == 1 then if m["source"] != "a" then test.fail() end end

  if
    track_count() == 2
  then
    if m["source"] != "b" then test.fail() end
    if s.end_duration() != 4. then test.fail() end
    if s.start_duration() != 1.1 then test.fail() end
  end

  if
    track_count() == 3
  then
    if m["source"] != "c" then test.fail() end
    if s.end_duration() != 4. then test.fail() end
    if s.start_duration() != 5. then test.fail() end
    success := true
  end
end

s.on_metadata(on_metadata)
clock.assign_new(sync="none", [s])

def on_stop() =
  if success() then test.pass() else test.fail() end
end

output.dummy(fallible=true, on_stop=on_stop, s)
