Files
moonmix/model/tabs_test.mbt
T
nandiandClaude Opus 5.5 e16cc1b2ad Name Chrome streams after their browser tabs
A browser extension reports audible tabs to moonmix-tabs-host, which matches
them to PipeWire streams by time and publishes the names for the mixer; streams
of one tab share a row. Ambiguous streams keep the browser's name. Without the
extension, a browser's only running stream shows its MPRIS title.

The smoke test no longer claims real audio hardware.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 19:26:29 -08:00

192 lines
5.4 KiB
MoonBit

///|
fn tab(id : Int32, title : String, audible~ : Bool = true) -> @model.BrowserTab {
{ id, title, audible, }
}
///|
/// Names by serial, sorted for comparison.
fn names(t : @model.TabTracker) -> String {
let out = t.matches().map(fn(m) { "\{m.0}=\{m.2}" })
out.sort_by(@model.compare_text)
out.join(" ")
}
///|
fn fresh() -> @model.TabTracker {
let t = @model.TabTracker::new()
t.set_streams([], 0L, initial=true)
t.set_tabs([], 0L)
t
}
///|
test "tabs: matched by when they start, also long afterwards" {
let t = fresh()
t.set_streams(["1"], 1000L)
t.set_tabs([tab(10, "A")], 1300L)
t.settle(1500L)
inspect(names(t), content="")
t.set_streams(["1", "2"], 5000L)
t.set_tabs([tab(10, "A"), tab(11, "B")], 5400L)
t.settle(9000L)
inspect(names(t), content="1=A 2=B")
// A quiet passage and a new title do not lose the match.
t.set_tabs([tab(10, "A2", audible=false), tab(11, "B")], 10000L)
t.settle(20000L)
inspect(names(t), content="1=A2 2=B")
}
///|
test "tabs: starting together is ambiguous" {
let t = fresh()
t.set_streams(["1", "2"], 1000L)
t.set_tabs([tab(10, "A"), tab(11, "B")], 1300L)
t.settle(20000L)
inspect(names(t), content="")
}
///|
test "tabs: already playing at start" {
let t = @model.TabTracker::new()
t.set_streams(["1", "2"], 0L, initial=true)
t.set_tabs([tab(10, "A"), tab(11, "B")], 0L)
t.settle(20000L)
inspect(names(t), content="")
// With a single tab there is no doubt.
let t = @model.TabTracker::new()
t.set_streams(["1", "2"], 0L, initial=true)
t.set_tabs([tab(10, "A")], 0L)
t.settle(20000L)
inspect(names(t), content="1=A 2=A")
}
///|
test "tabs: a second stream of the only tab joins it" {
let t = fresh()
t.set_streams(["1"], 1000L)
t.set_tabs([tab(10, "A")], 1300L)
t.settle(4000L)
t.set_streams(["1", "2"], 5000L)
t.settle(6000L)
inspect(names(t), content="1=A")
t.settle(9000L)
inspect(names(t), content="1=A 2=A")
}
///|
test "tabs: media and Web Audio of one tab, next to another tab" {
let t = fresh()
t.set_streams(["1"], 1000L)
t.set_tabs([tab(10, "A")], 1300L)
t.settle(4000L)
t.set_streams(["1", "2"], 9000L)
t.set_streams(["1", "2", "3"], 9100L)
t.set_tabs([tab(10, "A"), tab(11, "B")], 9300L)
t.settle(10550L)
t.settle(11000L)
inspect(names(t), content="1=A 2=B 3=B")
}
///|
test "tabs: a contradicted elimination is dropped for good" {
let t = fresh()
t.set_streams(["1"], 1000L)
t.set_tabs([tab(10, "A")], 1300L)
t.settle(4000L)
// Stream 2 is a silent tab's; B is heard only much later.
t.set_streams(["1", "2"], 5000L)
t.settle(9000L)
inspect(names(t), content="1=A 2=A")
t.set_tabs([tab(10, "A"), tab(11, "B")], 20000L)
t.settle(24000L)
inspect(names(t), content="1=A")
t.settle(30000L)
inspect(names(t), content="1=A")
}
///|
test "tabs: a stopped stream is forgotten, a closed tab too" {
let t = fresh()
t.set_streams(["1"], 1000L)
t.set_tabs([tab(10, "A")], 1300L)
t.settle(4000L)
t.set_streams([], 5000L)
inspect(names(t), content="")
t.set_streams(["1"], 6000L)
t.set_tabs([tab(11, "B")], 6300L)
t.settle(9000L)
inspect(names(t), content="1=B")
t.set_tabs([], 10000L)
inspect(names(t), content="")
}
///|
test "tabs: no elimination next to another profile" {
let t = @model.TabTracker::new()
t.set_streams(["1"], 0L, initial=true)
t.set_tabs([tab(10, "A")], 0L)
t.settle(9000L)
inspect(names(t), content="1=A")
t.settle(9500L, eliminate=false)
inspect(names(t), content="")
}
///|
test "tabs: messages and files" {
let parsed = @model.parse_tabs(
"{\"tabs\":[{\"id\":7,\"title\":\"a \\\"b\\\" é\",\"audible\":true}]}",
)
assert_true(parsed == Some([tab(7, "a \"b\" é")]))
assert_true(@model.parse_tabs("nope") is None)
assert_true(@model.parse_tabs("{\"tabs\":[{\"id\":\"7\"}]}") is None)
let file : @model.TabFile = {
browser: 5,
streams: [("84", 7, "x\n\"y\""), ("85", 8, "z")],
}
assert_true(@model.TabFile::parse(file.to_text()) == Some(file))
assert_false(file.to_text().contains("\n"))
assert_true(@model.TabFile::parse("{}") is None)
let other : @model.TabFile = { browser: 5, streams: [("85", 9, "w")], }
let merged = @model.merge_tab_files([file, other])
inspect(merged.length(), content="1")
inspect(merged.get("84").unwrap().key, content="5:7")
let titles = @model.parse_media_titles("12\tSong\nbad\n")
assert_true(titles.get(12) == Some("Song"))
inspect(titles.length(), content="1")
}
///|
test "tabs: rows are named and grouped" {
let s = @model.State::new()
for id in [60, 61, 62] {
let _ = s.apply(
Upsert(
@model.Node::new(
id,
"Stream/Output/Audio",
app_name="Google Chrome",
media_name="Playback",
serial="\{id + 100}",
parent_pid=5,
volumes=[1.0],
),
),
)
}
let name : @model.TabName = { key: "5:1", title: "Music", }
assert_true(s.apply(TabNames({ "160": name, "162": name })))
assert_false(s.apply(TabNames({ "160": name, "162": name })))
// Three running streams: a browser-wide title says nothing about one.
let _ = s.apply(MediaTitles({ 5: "Song" }))
let rows = @model.rows(s, Playback)
inspect(
rows
.map(fn(r) { "\{r.title}/\{r.subtitle}/\{r.members.length()}" })
.join(", "),
content="Music/Google Chrome/2, Google Chrome/Playback/1",
)
let _ = s.apply(Remove(60))
let _ = s.apply(Remove(62))
inspect(@model.rows(s, Playback)[0].subtitle, content="Song")
}