Backend tests: - CPU, memory, disk, network collector tests (existing) - Added temperature, processes, system, AMD GPU collector tests - All tests use mock filesystem data Frontend tests: - Added Vitest with jsdom environment - Tests for formatters (formatBytes, formatUptime, etc.) - Tests for theme store 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package collectors
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestSystemCollector(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
procPath := filepath.Join(tmpDir, "proc")
|
|
|
|
if err := os.MkdirAll(procPath, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Mock /proc/version (format: "Linux version 6.1.0-test ...")
|
|
versionContent := "Linux version 6.1.0-test (gcc) #1 SMP PREEMPT\n"
|
|
if err := os.WriteFile(filepath.Join(procPath, "version"), []byte(versionContent), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Mock /proc/uptime
|
|
if err := os.WriteFile(filepath.Join(procPath, "uptime"), []byte("12345.67 98765.43\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
collector := NewSystemCollector(procPath)
|
|
stats, err := collector.Collect()
|
|
if err != nil {
|
|
t.Fatalf("Collect failed: %v", err)
|
|
}
|
|
|
|
// Hostname uses os.Hostname(), so we can't mock it easily
|
|
// Just check it's not empty
|
|
if stats.Hostname == "" {
|
|
t.Error("Expected non-empty hostname")
|
|
}
|
|
|
|
if stats.Kernel != "6.1.0-test" {
|
|
t.Errorf("Expected kernel '6.1.0-test', got '%s'", stats.Kernel)
|
|
}
|
|
|
|
if stats.Uptime != 12345 {
|
|
t.Errorf("Expected uptime 12345, got %d", stats.Uptime)
|
|
}
|
|
}
|
|
|
|
func TestSystemCollector_MissingFiles(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
collector := NewSystemCollector(tmpDir)
|
|
|
|
stats, err := collector.Collect()
|
|
// Should not crash, may have errors but returns partial data
|
|
if err != nil {
|
|
t.Logf("Error (may be acceptable): %v", err)
|
|
}
|
|
|
|
// Should have some default values or empty strings
|
|
t.Logf("Stats with missing files: hostname=%s, kernel=%s, uptime=%d",
|
|
stats.Hostname, stats.Kernel, stats.Uptime)
|
|
}
|