|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "runtime" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestNodeToolGetDownloadOptions(t *testing.T) { |
| 9 | + manager, err := NewManager() |
| 10 | + if err != nil { |
| 11 | + t.Fatalf("Failed to create manager: %v", err) |
| 12 | + } |
| 13 | + |
| 14 | + nodeTool := NewNodeTool(manager) |
| 15 | + options := nodeTool.GetDownloadOptions() |
| 16 | + |
| 17 | + // Test that download options are returned (FileExtension is used for temp file naming) |
| 18 | + if options.FileExtension == "" { |
| 19 | + t.Errorf("Expected FileExtension to be non-empty") |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestNodeToolDownloadURLPlatformSpecific(t *testing.T) { |
| 24 | + manager, err := NewManager() |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("Failed to create manager: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + nodeTool := NewNodeTool(manager) |
| 30 | + |
| 31 | + // Get download URL for a test version |
| 32 | + url := nodeTool.getDownloadURL("20.19.5") |
| 33 | + |
| 34 | + // Verify platform-specific URL behavior |
| 35 | + if runtime.GOOS == "windows" { |
| 36 | + if !endsWith(url, ExtZip) { |
| 37 | + t.Errorf("Expected Windows URL to end with %s, got %s", ExtZip, url) |
| 38 | + } |
| 39 | + } else { |
| 40 | + if !endsWith(url, ExtTarGz) { |
| 41 | + t.Errorf("Expected non-Windows URL to end with %s, got %s", ExtTarGz, url) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestNodeToolAutomaticArchiveDetection(t *testing.T) { |
| 47 | + // Test that automatic archive detection works for different file types |
| 48 | + testCases := []struct { |
| 49 | + filename string |
| 50 | + expectedType string |
| 51 | + }{ |
| 52 | + {"node-v20.19.5-win-x64.zip", "zip"}, |
| 53 | + {"node-v20.19.5-linux-x64.tar.gz", "tar.gz"}, |
| 54 | + {"node-v20.19.5-darwin-arm64.tar.gz", "tar.gz"}, |
| 55 | + } |
| 56 | + |
| 57 | + for _, tc := range testCases { |
| 58 | + t.Run(tc.filename, func(t *testing.T) { |
| 59 | + // Test that detectArchiveType works correctly with the filename directly |
| 60 | + detectedType := detectArchiveType(tc.filename) |
| 61 | + if detectedType != tc.expectedType { |
| 62 | + t.Errorf("Expected archive type %s for %s, got %s", tc.expectedType, tc.filename, detectedType) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Helper function to check if a string ends with a suffix |
| 69 | +func endsWith(s, suffix string) bool { |
| 70 | + return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix |
| 71 | +} |
0 commit comments