返回 DeepSeek-Reasonix
devinfo_test.go
根目录 / desktop / devinfo_test.go
1 package main
2
3 import "testing"
4
5 func TestParseCPUModel(t *testing.T) {
6 cpuinfo := "processor\t: 0\nvendor_id\t: GenuineIntel\nmodel name\t: Intel(R) Core(TM) i7-12700K\nflags\t: fpu vme\n"
7 if got := parseCPUModel(cpuinfo); got != "Intel(R) Core(TM) i7-12700K" {
8 t.Errorf("parseCPUModel = %q", got)
9 }
10 if got := parseCPUModel("no such field"); got != "" {
11 t.Errorf("parseCPUModel on garbage = %q, want empty", got)
12 }
13 }
14
15 func TestParseMemTotalBytes(t *testing.T) {
16 meminfo := "MemTotal: 32652284 kB\nMemFree: 1234 kB\n"
17 if got := parseMemTotalBytes(meminfo); got != 32652284*1024 {
18 t.Errorf("parseMemTotalBytes = %d", got)
19 }
20 if got := parseMemTotalBytes("MemFree: 1 kB"); got != 0 {
21 t.Errorf("parseMemTotalBytes without MemTotal = %d, want 0", got)
22 }
23 }
24
25 func TestParseOSReleasePrettyName(t *testing.T) {
26 osRelease := "NAME=\"Ubuntu\"\nPRETTY_NAME=\"Ubuntu 24.04.1 LTS\"\nID=ubuntu\n"
27 if got := parseOSReleasePrettyName(osRelease); got != "Ubuntu 24.04.1 LTS" {
28 t.Errorf("parseOSReleasePrettyName = %q", got)
29 }
30 }
31
32 func TestParseOSReleaseTechnicalFields(t *testing.T) {
33 fields := parseOSRelease("# comment\nID=fedora\nVERSION_ID=\"40\"\nPRETTY_NAME='Fedora Linux 40'\nBROKEN\n")
34 if fields["ID"] != "fedora" || fields["VERSION_ID"] != "40" || fields["PRETTY_NAME"] != "Fedora Linux 40" {
35 t.Fatalf("os-release fields = %#v", fields)
36 }
37 }
38
39 func TestParseOSReleaseDistributionMatrix(t *testing.T) {
40 tests := []struct {
41 name, body, id, version string
42 }{
43 {name: "ubuntu", body: "ID=ubuntu\nVERSION_ID=22.04\n", id: "ubuntu", version: "22.04"},
44 {name: "debian", body: "ID=debian\nVERSION_ID=12\n", id: "debian", version: "12"},
45 {name: "fedora", body: "ID=fedora\nVERSION_ID=40\n", id: "fedora", version: "40"},
46 {name: "arch", body: "ID=arch\n", id: "arch"},
47 {name: "unknown", body: "NAME=Custom Linux\n", id: ""},
48 {name: "malformed", body: "BROKEN\n=bad\n", id: ""},
49 }
50 for _, test := range tests {
51 t.Run(test.name, func(t *testing.T) {
52 fields := parseOSRelease(test.body)
53 if fields["ID"] != test.id || fields["VERSION_ID"] != test.version {
54 t.Fatalf("fields = %#v", fields)
55 }
56 })
57 }
58 }
59
60 func TestLinuxSessionTypeUsesBoundedBuckets(t *testing.T) {
61 tests := []struct {
62 name string
63 env map[string]string
64 want string
65 }{
66 {name: "wayland", env: map[string]string{"XDG_SESSION_TYPE": "Wayland"}, want: "wayland"},
67 {name: "x11 fallback", env: map[string]string{"DISPLAY": ":0"}, want: "x11"},
68 {name: "remote wins", env: map[string]string{"SSH_CONNECTION": "present", "XDG_SESSION_TYPE": "wayland"}, want: "remote"},
69 {name: "unknown", env: map[string]string{"XDG_SESSION_TYPE": "tty"}, want: "unknown"},
70 }
71 for _, tt := range tests {
72 t.Run(tt.name, func(t *testing.T) {
73 if got := linuxSessionType(func(key string) string { return tt.env[key] }); got != tt.want {
74 t.Fatalf("linuxSessionType() = %q, want %q", got, tt.want)
75 }
76 })
77 }
78 }
79
80 func TestCollectDeviceInfoSane(t *testing.T) {
81 d := collectDeviceInfo()
82 if d.Cores < 1 {
83 t.Errorf("Cores = %d", d.Cores)
84 }
85 if d.OSVersion == "" {
86 t.Error("OSVersion empty")
87 }
88 }
89
89 lines GO