Files
moonfetch/moonfetch_test.mbt
2026-11-19 10:03:28 +00:00

215 lines
5.8 KiB
MoonBit

///|
test "parse_os_release debian" {
let sample =
#|PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
#|NAME="Debian GNU/Linux"
#|VERSION_ID="12"
#|ID=debian
let res = @lib.parse_os_release(sample)
assert_eq(res.pretty_name, "Debian GNU/Linux 12 (bookworm)")
assert_eq(res.id, "debian")
assert_eq(res.name, "Debian GNU/Linux")
assert_eq(res.version_id, "12")
}
///|
test "parse_os_release fallback pretty_name" {
let sample =
#|NAME="Alpine Linux"
#|ID=alpine
#|VERSION_ID=3.20
let res = @lib.parse_os_release(sample)
assert_eq(res.pretty_name, "Alpine Linux 3.20")
assert_eq(res.id, "alpine")
}
///|
test "parse_cpuinfo" {
let sample =
#|processor : 0
#|model name : AMD EPYC 7763 64-Core Processor
#|cpu MHz : 2445.406
#|
#|processor : 1
#|model name : AMD EPYC 7763 64-Core Processor
#|cpu MHz : 2445.406
let cpu = @lib.parse_cpuinfo(sample)
assert_eq(cpu.model, "AMD EPYC 7763 64-Core Processor")
assert_eq(cpu.cores, 2)
assert_eq(cpu.mhz, "2445.406")
}
///|
test "parse_meminfo with MemAvailable" {
let sample =
#|MemTotal: 16384000 kB
#|MemFree: 2000000 kB
#|MemAvailable: 12288000 kB
let mem = @lib.parse_meminfo(sample)
assert_eq(mem.total_kib, 16384000UL)
assert_eq(mem.available_kib, 12288000UL)
assert_eq(mem.used_kib, 4096000UL)
}
///|
test "parse_meminfo fallback without MemAvailable" {
let sample =
#|MemTotal: 8000000 kB
#|MemFree: 1000000 kB
#|Buffers: 500000 kB
#|Cached: 2500000 kB
let mem = @lib.parse_meminfo(sample)
assert_eq(mem.available_kib, 4000000UL)
assert_eq(mem.used_kib, 4000000UL)
}
///|
test "parse_uptime_seconds and format_uptime" {
assert_eq(@lib.parse_uptime_seconds("45.67 80.12\n"), 45)
assert_eq(@lib.format_uptime(45), "45 secs")
assert_eq(@lib.format_uptime(65), "1 min")
assert_eq(@lib.format_uptime(3665), "1 hour, 1 min")
assert_eq(@lib.format_uptime(90120), "1 day, 1 hour, 2 mins")
}
///|
test "format_usage" {
let one_gib : UInt64 = 1073741824UL
let four_gib : UInt64 = 4UL * one_gib
assert_eq(@lib.format_usage(one_gib, four_gib), "1.00 GiB / 4.00 GiB (25%)")
assert_eq(@lib.format_usage(0UL, 0UL), "Unknown")
}
///|
test "count_dpkg_installed and count_apk_installed" {
let dpkg =
#|Package: bash
#|Status: install ok installed
#|
#|Package: removed-pkg
#|Status: deinstall ok config-files
#|
#|Package: curl
#|Status: install ok installed
assert_eq(@lib.count_dpkg_installed(dpkg), 2)
let apk =
#|P:busybox
#|V:1.36.1
#|P:musl
#|V:1.2.4
assert_eq(@lib.count_apk_installed(apk), 2)
}
///|
test "cli parse_args" {
let opts = @lib.parse_args([
"--logo", "arch", "--no-color", "--structure", "title:os:memory",
])
assert_eq(opts.logo, Some("arch"))
assert_eq(opts.no_color, true)
assert_eq(opts.no_logo, false)
assert_eq(opts.structure, ["title", "os", "memory"])
assert_eq(opts.error, None)
let err_opts = @lib.parse_args(["--unknown-flag"])
assert_true(err_opts.error is Some(_))
let no_logo_opts = @lib.parse_args(["--logo=none", "--json"])
assert_eq(no_logo_opts.no_logo, true)
assert_eq(no_logo_opts.json, true)
}
///|
test "parse_config_content" {
let cfg =
#|# sample config
#|logo = "moonbit"
#|no_color = true
#|structure = "title,os,kernel"
let opts = @lib.parse_config_content(cfg, @lib.CliOptions::default())
assert_eq(opts.logo, Some("moonbit"))
assert_eq(opts.no_color, true)
assert_eq(opts.structure, ["title", "os", "kernel"])
}
///|
test "visible_len and render_fetch no_color" {
assert_eq(@lib.visible_len("\u001b[1;31mhello\u001b[0m"), 5)
let info : @lib.SystemInfo = {
username: "alice",
hostname: "box",
os_id: "debian",
os_name: "Debian 12 x86_64",
host_model: "KVM",
kernel: "Linux 6.1",
arch: "x86_64",
uptime: "10 mins",
uptime_secs: 600,
packages: "100 (dpkg)",
shell: "bash 5.2",
terminal: "xterm (80x24)",
de_wm: "Headless / TTY",
cpu: "Test CPU (4)",
memory: "1.00 GiB / 4.00 GiB (25%)",
mem_used_kib: 1048576UL,
mem_total_kib: 4194304UL,
disk: "2.00 GiB / 10.00 GiB (20%)",
disk_used_bytes: 2147483648UL,
disk_total_bytes: 10737418240UL,
local_ip: "10.0.0.2",
locale: "C.UTF-8",
}
let opts = @lib.parse_args([
"--no-logo", "--no-color", "--structure", "title:separator:os:kernel",
])
let out = @lib.render_fetch(info, opts)
assert_eq(
out, "alice@box\n---------\nOS: Debian 12 x86_64\nKernel: Linux 6.1",
)
let json_out = info.to_json()
assert_true(json_out.contains("\"username\": \"alice\""))
assert_true(json_out.contains("\"os\": \"Debian 12 x86_64\""))
}
///|
test "ubuntu logo multi-color and no_color" {
let info : @lib.SystemInfo = {
username: "alice",
hostname: "box",
os_id: "ubuntu",
os_name: "Ubuntu 26.04.1 LTS x86_64",
host_model: "KVM",
kernel: "Linux 6.12",
arch: "x86_64",
uptime: "10 mins",
uptime_secs: 600,
packages: "100 (dpkg)",
shell: "bash 5.2",
terminal: "xterm (80x24)",
de_wm: "Headless / TTY",
cpu: "Test CPU (4)",
memory: "1.00 GiB / 4.00 GiB (25%)",
mem_used_kib: 1048576UL,
mem_total_kib: 4194304UL,
disk: "2.00 GiB / 10.00 GiB (20%)",
disk_used_bytes: 2147483648UL,
disk_total_bytes: 10737418240UL,
local_ip: "10.0.0.2",
locale: "C.UTF-8",
}
let colored = @lib.render_fetch(
info,
@lib.parse_args(["--structure", "title"]),
)
assert_true(colored.contains("\u001b[1;31m"))
assert_true(colored.contains("\u001b[1;37m"))
assert_true(!colored.contains("$1") && !colored.contains("$2"))
let plain = @lib.render_fetch(
info,
@lib.parse_args(["--no-color", "--structure", "title"]),
)
assert_true(!plain.contains("\u001b["))
assert_true(!plain.contains("$1") && !plain.contains("$2"))
}