|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/linuxdeepin/go-lib/log" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSmartMirror_makeChoice(t *testing.T) { |
| 14 | + if os.Getenv("NO_TEST_NETWORK") == "1" { |
| 15 | + t.Skip("Skipping test due to NO_TEST_NETWORK=1") |
| 16 | + } |
| 17 | + logger.SetLogLevel(log.LevelDebug) |
| 18 | + // Create temporary test directory |
| 19 | + tempDir := t.TempDir() |
| 20 | + defer func() { |
| 21 | + for i := 0; i < 5; i++ { |
| 22 | + err := os.RemoveAll(tempDir) |
| 23 | + if err == nil { |
| 24 | + break |
| 25 | + } |
| 26 | + if i < 4 { |
| 27 | + time.Sleep(100 * time.Millisecond) |
| 28 | + } else { |
| 29 | + t.Logf("warn: Failed to remove temp directory after 5 attempts: %v", err) |
| 30 | + } |
| 31 | + } |
| 32 | + }() |
| 33 | + stateDirectory = tempDir |
| 34 | + |
| 35 | + // Prepare test data |
| 36 | + testSources := []map[string]interface{}{ |
| 37 | + { |
| 38 | + "id": "default", |
| 39 | + "name": "official", |
| 40 | + // NOTE: Intentionally using an incorrect CDN address here |
| 41 | + "url": "https://x-cdn-community-packages.deepin.com/", |
| 42 | + "weight": 100, |
| 43 | + }, |
| 44 | + { |
| 45 | + "id": "TUNA", |
| 46 | + "name": "[CN] Tsinghua University", |
| 47 | + "url": "http://mirrors.tuna.tsinghua.edu.cn/deepin/", |
| 48 | + "name_locale": map[string]string{"zh_CN": "[CN] 清华大学", "zh_TW": "[CN] 清華大學"}, |
| 49 | + "weight": 60000, |
| 50 | + "country": "CN", |
| 51 | + "adjust_delay": 0, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + // Write mirrors.json |
| 56 | + mirrorsData, _ := json.MarshalIndent(testSources, "", " ") |
| 57 | + err := os.WriteFile(filepath.Join(tempDir, "mirrors.json"), mirrorsData, 0644) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Failed to create test mirrors.json: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + // Write empty config file |
| 63 | + configData := []byte(`{"enable":true}`) |
| 64 | + err = os.WriteFile(filepath.Join(tempDir, "smartmirror_config.json"), configData, 0644) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("Failed to create test config: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + // Write quality data file |
| 70 | + qualityData := map[string]interface{}{ |
| 71 | + "https://x-cdn-community-packages.deepin.com/": map[string]int{ |
| 72 | + "detect_count": 50, |
| 73 | + "access_count": 50, |
| 74 | + "failed_count": 5, |
| 75 | + "average_delay": 150, |
| 76 | + }, |
| 77 | + "http://mirrors.tuna.tsinghua.edu.cn/deepin/": map[string]int{ |
| 78 | + "detect_count": 80, |
| 79 | + "access_count": 80, |
| 80 | + "failed_count": 2, |
| 81 | + "average_delay": 110, |
| 82 | + }, |
| 83 | + } |
| 84 | + qualityJSON, _ := json.MarshalIndent(qualityData, "", " ") |
| 85 | + err = os.WriteFile(filepath.Join(tempDir, "smartmirror_quality.json"), qualityJSON, 0644) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("Failed to create test quality.json: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + // Create SmartMirror instance |
| 91 | + s := newSmartMirror(nil) |
| 92 | + originalURL := "https://community-packages.deepin.com/beige/pool/main/f/fish/fish_3.7.1-1deepin1_amd64.deb" |
| 93 | + officialMirror := "https://community-packages.deepin.com/" |
| 94 | + |
| 95 | + t.Logf("Loaded sources count: %d", len(s.sources)) |
| 96 | + for i, source := range s.sources { |
| 97 | + t.Logf("Source[%d]: %s - %s", i, source.Name, source.Url) |
| 98 | + } |
| 99 | + |
| 100 | + result := s.makeChoice(originalURL, officialMirror) |
| 101 | + t.Logf("Result URL: %s", result) |
| 102 | + |
| 103 | + // Verify result is not empty |
| 104 | + if result == "" { |
| 105 | + t.Error("makeChoice returned empty result") |
| 106 | + } |
| 107 | +} |
0 commit comments