1package tools
 2
 3import (
 4	"runtime"
 5	"slices"
 6	"testing"
 7)
 8
 9func TestGetSafeReadOnlyCommands(t *testing.T) {
10	commands := getSafeReadOnlyCommands()
11
12	// Check that we have some commands
13	if len(commands) == 0 {
14		t.Fatal("Expected some safe commands, got none")
15	}
16
17	// Check for cross-platform commands that should always be present
18	crossPlatformCommands := []string{"echo", "hostname", "whoami", "git status", "go version"}
19	for _, cmd := range crossPlatformCommands {
20		found := slices.Contains(commands, cmd)
21		if !found {
22			t.Errorf("Expected cross-platform command %q to be in safe commands", cmd)
23		}
24	}
25
26	if runtime.GOOS == "windows" {
27		// Check for Windows-specific commands
28		windowsCommands := []string{"dir", "type", "Get-Process"}
29		for _, cmd := range windowsCommands {
30			found := slices.Contains(commands, cmd)
31			if !found {
32				t.Errorf("Expected Windows command %q to be in safe commands on Windows", cmd)
33			}
34		}
35
36		// Check that Unix commands are NOT present on Windows
37		unixCommands := []string{"ls", "pwd", "ps"}
38		for _, cmd := range unixCommands {
39			found := slices.Contains(commands, cmd)
40			if found {
41				t.Errorf("Unix command %q should not be in safe commands on Windows", cmd)
42			}
43		}
44	} else {
45		// Check for Unix-specific commands
46		unixCommands := []string{"ls", "pwd", "ps"}
47		for _, cmd := range unixCommands {
48			found := slices.Contains(commands, cmd)
49			if !found {
50				t.Errorf("Expected Unix command %q to be in safe commands on Unix", cmd)
51			}
52		}
53
54		// Check that Windows-specific commands are NOT present on Unix
55		windowsOnlyCommands := []string{"dir", "Get-Process", "systeminfo"}
56		for _, cmd := range windowsOnlyCommands {
57			found := slices.Contains(commands, cmd)
58			if found {
59				t.Errorf("Windows-only command %q should not be in safe commands on Unix", cmd)
60			}
61		}
62	}
63}
64
65func TestPlatformSpecificSafeCommands(t *testing.T) {
66	// Test that the function returns different results on different platforms
67	commands := getSafeReadOnlyCommands()
68
69	hasWindowsCommands := false
70	hasUnixCommands := false
71
72	for _, cmd := range commands {
73		if cmd == "dir" || cmd == "Get-Process" || cmd == "systeminfo" {
74			hasWindowsCommands = true
75		}
76		if cmd == "ls" || cmd == "ps" || cmd == "df" {
77			hasUnixCommands = true
78		}
79	}
80
81	if runtime.GOOS == "windows" {
82		if !hasWindowsCommands {
83			t.Error("Expected Windows commands on Windows platform")
84		}
85		if hasUnixCommands {
86			t.Error("Did not expect Unix commands on Windows platform")
87		}
88	} else {
89		if hasWindowsCommands {
90			t.Error("Did not expect Windows-only commands on Unix platform")
91		}
92		if !hasUnixCommands {
93			t.Error("Expected Unix commands on Unix platform")
94		}
95	}
96}