//go:build linux

package stats

import (
	"strings"
	"testing"
)

func TestParseKB(t *testing.T) {
	if got := parseKB("MemTotal:       16384000 kB"); got != 16384000 {
		t.Fatalf("got %d", got)
	}
	if parseKB("short") != 0 {
		t.Fatal("invalid line should return 0")
	}
	if parseKB("MemAvailable: 0 kB") != 0 {
		t.Fatal("zero kB should parse as 0")
	}
}

func TestParseMeminfo(t *testing.T) {
	content := strings.Join([]string{
		"MemTotal:       16384000 kB",
		"MemFree:         8192000 kB",
		"MemAvailable:    4096000 kB",
	}, "\n")
	total, avail := parseMeminfo(strings.NewReader(content))
	if total != 16384000*1024 {
		t.Fatalf("total %d", total)
	}
	if avail != 4096000*1024 {
		t.Fatalf("avail %d", avail)
	}
}

func TestParseMeminfoMissingTotal(t *testing.T) {
	content := "MemAvailable: 4096000 kB\n"
	total, avail := parseMeminfo(strings.NewReader(content))
	if total != 0 || avail != 0 {
		t.Fatalf("missing MemTotal: total=%d avail=%d", total, avail)
	}
}
