Files
chandra/lib/battery/battery_wbtest.mbt
T
Claude 88915842c2
CI / test (push) Failing after 18s
Battery panel module: charge, charging state, time left, low-battery warning
- New `battery` panel module (components/battery, logic in lib/battery): a battery icon filled to
  the charge with the percentage and time remaining ("73% 2:05"; time to full while charging).
  The fill turns green when charging or full, amber at 20 % and below, and at 10 % the whole
  button turns red and a "Battery low" popup opens by itself, once per discharge spell. A click
  opens the details (charge, state, time left in words). Without a battery it shows a plug.
- Right-click "Module settings": Show percentage, Show time remaining, and Without a battery
  (Show a plug / Show nothing). Saved per module in state.edn like the clocks' settings.
- New `battery` capability (wit/capabilities.wit: read -> status {percent, charge-state,
  secs-left}). Served by the wasmer stub (host ints 7-9), host-wasm and wasmhost (publish-battery).
- Host side (compositor/shell/battery.mbt): reads /sys/class/power_supply, every battery
  together (energy_* / power_now, or charge_* / current_now; capacity when that is all there
  is; device-scope batteries like mice are skipped). The `system` snapshot the Power tab uses
  now reads the battery through the same code. Time left follows a running average of the
  rate, starting over when the state changes.
- A fresh panel gets the battery module before the clock only on a machine with a battery.
- wasm_stub: MAX_INST 512 -> 2048. The host test binary creates ~70 compositors without
  unloading their modules; at 512 the last tests' panels came up half empty.
- Tests: sysfs reading against a fake power supply (energy and charge units, two packs,
  charging, full, capacity only, no battery), the running average, the module in the panel
  (text, settings, click, the critical warning, the plug), the rendering logic, and a
  host-wasm test of the module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hky51dmaUqiYW746Gz
2026-11-25 19:53:42 +00:00

124 lines
4.2 KiB
MoonBit

///|
fn test_tokens() -> @theme.Tokens {
let c = @ui.color(10, 20, 30, 255)
{
name: "t",
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: 6,
spacing: 6,
panel_height: 44,
}
}
///|
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
}
///|
fn has_red(ops : Array[@types.DrawOp]) -> Bool {
ops.iter().any(op => op is FillRect(f) && f.color.r == b'\xEB' && f.color.g == b'\x40')
}
///|
let size : @types.Size = { w: 104, h: 34 }
///|
test "time formats" {
assert_eq(short_time(0), "")
assert_eq(short_time(59), "0:00")
assert_eq(short_time(7500), "2:05")
assert_eq(long_time(35 * 60 + 20), "35 min")
assert_eq(long_time(7500), "2 h 05 min")
assert_eq(long_time(-1), "")
}
///|
test "panel shows percent and time, each switchable" {
let c = BatteryConfig::new()
let r = { percent: 73, state: state_discharging, secs_left: 7500 }
assert_eq(texts(render(size, test_tokens(), r, c)), ["73% 2:05"])
assert_true(c.set("time", "off"))
assert_false(c.set("time", "off"))
assert_eq(texts(render(size, test_tokens(), r, c)), ["73%"])
assert_true(c.set("percent", "off"))
assert_eq(texts(render(size, test_tokens(), r, c)), [])
assert_true(c.set("time", "on"))
assert_eq(texts(render(size, test_tokens(), r, c)), ["2:05"])
assert_eq(c.items().map(i => i.value), ["off", "on", "Show a plug"])
assert_false(c.set("bogus", "on"))
// Too narrow for text: icon only.
c.show_percent = true
assert_eq(texts(render({ w: 34, h: 34 }, test_tokens(), r, c)), [])
}
///|
test "charging shows time to full; full and unknown times show percent only" {
let c = BatteryConfig::new()
let charging = { percent: 40, state: state_charging, secs_left: 3600 }
assert_eq(texts(render(size, test_tokens(), charging, c)), ["40% 1:00"])
assert_eq(charging.describe(), "Charging, full in 1 h 00 min")
let full = { percent: 100, state: state_full, secs_left: 0 }
assert_eq(texts(render(size, test_tokens(), full, c)), ["100%"])
assert_eq(full.describe(), "Fully charged, plugged in")
let unknown = { percent: 55, state: state_discharging, secs_left: 0 }
assert_eq(texts(render(size, test_tokens(), unknown, c)), ["55%"])
assert_eq(unknown.describe(), "Discharging")
assert_eq({ percent: 80, state: state_not_charging, secs_left: 0 }.describe(), "Plugged in, not charging")
}
///|
test "low and critical levels; charging is never low" {
assert_eq({ percent: 21, state: state_discharging, secs_left: 0 }.level(), 0)
assert_eq({ percent: 20, state: state_discharging, secs_left: 0 }.level(), 1)
assert_eq({ percent: 10, state: state_discharging, secs_left: 0 }.level(), 2)
assert_eq({ percent: 5, state: state_charging, secs_left: 0 }.level(), 0)
let c = BatteryConfig::new()
let crit = { percent: 8, state: state_discharging, secs_left: 600 }
assert_true(has_red(render(size, test_tokens(), crit, c)))
assert_false(has_red(render(size, test_tokens(), { ..crit, percent: 50 }, c)))
let (_, _, ops) = render_popup(test_tokens(), crit, warning=true)
assert_eq(texts(ops), ["Battery low: 8%", "Discharging, 10 min left", "Plug in the computer to keep working."])
}
///|
test "the low-battery warning opens once per discharge spell" {
let lw = LowWarning::new()
let r = { percent: 11, state: state_discharging, secs_left: 0 }
assert_false(lw.update(r))
assert_true(lw.update({ ..r, percent: 10 }))
assert_false(lw.update({ ..r, percent: 9 }))
assert_false(lw.update({ ..r, percent: 9, state: state_charging }))
assert_true(lw.update({ ..r, percent: 9 }))
}
///|
test "without a battery: a plug, or nothing" {
let c = BatteryConfig::new()
let none = { percent: -1, state: state_none, secs_left: 0 }
let ops = render(size, test_tokens(), none, c)
assert_true(ops.length() > 0)
assert_eq(texts(ops), [])
assert_true(c.set("no-battery", "Show nothing"))
assert_eq(render(size, test_tokens(), none, c).length(), 0)
assert_eq(none.describe(), "No battery: on mains power")
}