| 133 | } |
| 134 | |
| 135 | func TestActionCacheSortedEntries(t *testing.T) { |
| 136 | // Create temporary directory for testing |
| 137 | tmpDir := testutil.TempDir(t, "test-*") |
| 138 | |
| 139 | // Create cache and add entries in non-alphabetical order |
| 140 | cache := NewActionCache(tmpDir) |
| 141 | cache.Set("zzz/last-action", "v1", "sha111") |
| 142 | cache.Set("actions/checkout", "v5", "sha222") |
| 143 | cache.Set("mmm/middle-action", "v2", "sha333") |
| 144 | cache.Set("actions/setup-node", "v4", "sha444") |
| 145 | cache.Set("aaa/first-action", "v3", "sha555") |
| 146 | |
| 147 | // Save to disk |
| 148 | err := cache.Save() |
| 149 | if err != nil { |
| 150 | t.Fatalf("Failed to save cache: %v", err) |
| 151 | } |
| 152 | |
| 153 | // Read the file content |
| 154 | cachePath := filepath.Join(tmpDir, ".github", "aw", CacheFileName) |
| 155 | data, err := os.ReadFile(cachePath) |
| 156 | if err != nil { |
| 157 | t.Fatalf("Failed to read cache file: %v", err) |
| 158 | } |
| 159 | |
| 160 | content := string(data) |
| 161 | |
| 162 | // Verify that entries appear in alphabetical order by checking their positions |
| 163 | entries := []string{ |
| 164 | "aaa/first-action@v3", |
| 165 | "actions/checkout@v5", |
| 166 | "actions/setup-node@v4", |
| 167 | "mmm/middle-action@v2", |
| 168 | "zzz/last-action@v1", |
| 169 | } |
| 170 | |
| 171 | lastPos := -1 |
| 172 | for _, entry := range entries { |
| 173 | pos := indexOf(content, entry) |
| 174 | if pos == -1 { |
| 175 | t.Errorf("Entry %s not found in cache file", entry) |
| 176 | continue |
| 177 | } |
| 178 | if pos < lastPos { |
| 179 | t.Errorf("Entry %s appears before previous entry (not sorted)", entry) |
| 180 | } |
| 181 | lastPos = pos |
| 182 | } |
| 183 | |
| 184 | // Also verify the file is valid JSON |
| 185 | var loadedCache ActionCache |
| 186 | err = json.Unmarshal(data, &loadedCache) |
| 187 | if err != nil { |
| 188 | t.Fatalf("Saved cache is not valid JSON: %v", err) |
| 189 | } |
| 190 | |
| 191 | // Verify all entries are present |
| 192 | if len(loadedCache.Entries) != 5 { |