Files
chandra/lib/gamelib/gamelib_test.mbt
T
Claude 1154437050
CI / test (push) Successful in 2m32s
Gamerig Library: fit the screen without scrolling, draw over the wallpaper
The grid picks the roomiest card density that fits (fewer description and hint lines, narrower
cards, then name and buttons only) before it would scroll. The module now draws over the
desktop wallpaper with glassy panels and cards; Background setting Wallpaper (default) or Solid.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgvjzw8hHirxRsXSsE
2026-11-26 03:14:18 +00:00

192 lines
6.8 KiB
MoonBit

///| A /api/state answer like gamerig's server.py gives.
let sample : String =
#|{"system": {"gpu": "NVIDIA GeForce GTX 1650", "resolution": "1920x1080", "display": ":0",
#| "audio": "PulseAudio (Selkies)", "audio_ok": true, "hostname": "rig"},
#| "apps": [
#| {"id": "steam", "name": "Steam", "category": "games", "featured": true, "icon": "Play",
#| "description": "Your Steam library, store and friends.", "installed": true, "running": false,
#| "modes": [{"id": "default", "label": "Launch"}, {"id": "bigpicture", "label": "Big Picture"}]},
#| {"id": "lutris", "name": "Lutris", "category": "launchers", "icon": "Folder",
#| "description": "Open gaming platform.", "installed": false, "install_hint": "apt-get install lutris"},
#| {"id": "terminal", "name": "Terminal", "category": "tools", "icon": "Terminal",
#| "description": "Interactive shell inside the rig.", "installed": true, "running": true, "pid": 42},
#| {"name": "no id"}
#| ]}
///|
fn sample_state() -> @gamelib.State {
match @gamelib.parse_state(sample) {
Ok(s) => s
Err(e) => abort(e)
}
}
///|
fn tokens() -> @theme.Tokens {
let c = @ui.color(230, 230, 230, 255)
{
name: "dark",
palette: {
desktop_top: c,
desktop_bottom: c,
surface: c,
surface_hover: c,
surface_popup: c,
foreground: c,
foreground_muted: c,
accent: c,
accent_foreground: c,
border: c,
},
font_size: 14,
radius: 8,
spacing: 8,
panel_height: 40,
}
}
///|
fn texts(ops : Array[@types.DrawOp]) -> Array[String] {
let out : Array[String] = []
for op in ops {
if op is Text(t) {
out.push(t.text)
}
}
out
}
///|
test "parse_state reads the rig, the games and their extra modes" {
let s = sample_state()
assert_eq(s.rig.gpu, "NVIDIA GeForce GTX 1650")
assert_true(s.rig.connected)
assert_true(s.rig.audio_ok)
assert_eq(s.games.map(g => g.id), ["steam", "lutris", "terminal"])
assert_eq(s.modes, [{ game: "steam", id: "bigpicture", label: "Big Picture" }])
assert_eq(s.games[1].install_hint, "apt-get install lutris")
assert_eq(s.games[2].status(), Running)
assert_true(@gamelib.parse_state("nope") is Err(_))
assert_eq(@gamelib.error_message("{\"error\": \"unknown app\"}"), "unknown app")
}
///|
test "filtering: categories, running, search words, featured first" {
let g = sample_state().games
assert_eq(@gamelib.count_in(g, All), 3)
assert_eq(@gamelib.count_in(g, Games), 1)
assert_eq(@gamelib.count_in(g, Running), 1)
assert_eq(@gamelib.filter_games(g, All, "shell rig").map(x => x.id), ["terminal"])
assert_eq(@gamelib.filter_games(g, All, "").map(x => x.id), ["steam", "lutris", "terminal"])
assert_eq(@gamelib.filter_games(g, Tools, "steam").length(), 0)
}
///|
test "wrap and ellipsize keep to the width" {
let lines = @gamelib.wrap("one two three four five six seven", 60, 10, 2)
assert_eq(lines.length(), 2)
for l in lines {
assert_true(@gamelib.approx_width(l, 10) <= 60)
}
assert_true(lines[1].has_suffix("…"))
assert_eq(@gamelib.ellipsize("short", 100, 10), "short")
}
///|
test "grid: featured cards span two columns, rows wrap" {
let g = sample_state().games
let (cells, total, cols) = @gamelib.grid_layout(g, 900)
assert_eq(cols, 3)
// steam (featured) two columns, lutris next to it, terminal on the next row.
assert_eq(cells[0].3 > cells[1].3, true)
assert_eq(cells[1].2, 0)
assert_true(cells[2].2 > 0)
assert_true(total >= cells[2].2 + cells[2].4)
// One column: nothing spans.
let (one, _, c1) = @gamelib.grid_layout(g, 300)
assert_eq(c1, 1)
assert_eq(one[0].3, one[1].3)
}
///|
test "render shows the sidebar, the cards and the rig; clicks launch, stop and filter" {
let lib = @gamelib.Library::new()
let s = sample_state()
let size : @types.Size = { w: 1600, h: 1000 }
let ops = lib.render(s.games, s.modes, s.rig, size, tokens())
let t = texts(ops)
for want in ["Gamerig", "Library", "Games", "Running Apps", "Rig Status", "Steam", "Big Picture", "Switch to", "Stop", "Not installed", "All apps"] {
assert_true(t.contains(want), msg="missing " + want)
}
assert_true(t.iter().any(x => x.has_prefix("NVIDIA GeForce")))
let click = (h : @gamelib.Hit) => {
for reg in lib.regions {
if reg.1 == h {
let p : @types.Point = { x: reg.0.x + 2, y: reg.0.y + 2 }
ignore(lib.handle(PointerDown(p)))
return lib.handle(PointerUp(p)).1
}
}
abort("no region")
}
assert_eq(click(Button("steam", "bigpicture")), Launch("steam", "bigpicture"))
assert_eq(click(Button("steam", "default")), Launch("steam", "default"))
assert_eq(click(StopButton("terminal")), Stop("terminal"))
assert_eq(click(RunningRow("terminal")), Focus("terminal"))
assert_eq(click(RefreshButton), Refresh)
assert_eq(click(Cat(Tools)), NoAction)
assert_eq(lib.category, Tools)
let t = texts(lib.render(s.games, s.modes, s.rig, size, tokens()))
assert_false(t.contains("Steam"))
assert_true(t.contains("Terminal"))
}
///|
test "typing filters, Enter launches the selection, Escape clears" {
let lib = @gamelib.Library::new()
let s = sample_state()
let size : @types.Size = { w: 1280, h: 720 }
ignore(lib.render(s.games, s.modes, s.rig, size, tokens()))
for k in ["l", "u", "t"] {
ignore(lib.handle(KeyDown(k)))
}
assert_eq(lib.query, "lut")
ignore(lib.render(s.games, s.modes, s.rig, size, tokens()))
ignore(lib.handle(KeyDown("Right")))
assert_eq(lib.selected, "lutris")
assert_eq(lib.handle(KeyDown("Enter")).1, Launch("lutris", "default"))
ignore(lib.handle(KeyDown("Backspace")))
assert_eq(lib.query, "lu")
ignore(lib.handle(KeyDown("Escape")))
assert_eq(lib.query, "")
}
///|
test "disconnected: says it is waiting, and the error" {
let lib = @gamelib.Library::new()
let rig = { ..@gamelib.Rig::unknown(), error: "Library daemon not reachable" }
let t = texts(lib.render([], [], rig, { w: 1280, h: 720 }, tokens()))
assert_true(t.contains("Waiting for the library daemon…"))
assert_true(t.contains("Library daemon not reachable"))
}
///|
test "nine games fit a 1280x720 desktop without scrolling, and the wallpaper shows through" {
let lib = @gamelib.Library::new()
let base = sample_state().games[1]
let games = Array::makei(9, i => { ..base, id: "g" + i.to_string(), featured: i < 2 })
let ops = lib.render(games, [], @gamelib.Rig::unknown(), { w: 1280, h: 720 }, tokens())
assert_true(lib.content_h <= lib.view_h, msg="grid \{lib.content_h} > view \{lib.view_h}")
// No opaque full-screen background in the default Wallpaper mode.
match ops[0] {
FillRect(f) => assert_true(f.rect.h != 720 || f.color.a.to_int() < 255)
_ => ()
}
assert_true(lib.set_config("background", "Solid"))
let ops = lib.render(games, [], @gamelib.Rig::unknown(), { w: 1280, h: 720 }, tokens())
match ops[0] {
FillRect(f) => assert_eq(f.color.a.to_int(), 255)
_ => fail("no background")
}
}