|
4 | 4 | package extract |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "encoding/hex" |
7 | 8 | "os" |
8 | 9 | "path/filepath" |
9 | 10 | "sync" |
@@ -170,3 +171,137 @@ func TestBundle_Ensure_ConcurrentAccess(t *testing.T) { |
170 | 171 | require.NoError(t, err) |
171 | 172 | assert.Equal(t, []byte("concurrent data"), got) |
172 | 173 | } |
| 174 | + |
| 175 | +// --- Direct tests for computeHash and isValid --- |
| 176 | + |
| 177 | +func TestBundle_ComputeHash_Stability(t *testing.T) { |
| 178 | + t.Parallel() |
| 179 | + |
| 180 | + files := []File{ |
| 181 | + {Name: "a.txt", Content: []byte("aaa"), Mode: 0o644}, |
| 182 | + } |
| 183 | + b := NewBundle("v1", files) |
| 184 | + |
| 185 | + hash1 := b.computeHash() |
| 186 | + hash2 := b.computeHash() |
| 187 | + |
| 188 | + assert.Equal(t, hash1, hash2, "same bundle should produce the same hash") |
| 189 | + assert.Len(t, hash1, 64, "SHA-256 hex digest should be 64 characters") |
| 190 | + |
| 191 | + // Verify it is valid hex. |
| 192 | + _, err := hex.DecodeString(hash1) |
| 193 | + require.NoError(t, err, "hash should be valid hex") |
| 194 | +} |
| 195 | + |
| 196 | +func TestBundle_ComputeHash_Sensitivity(t *testing.T) { |
| 197 | + t.Parallel() |
| 198 | + |
| 199 | + baseline := NewBundle("v1", []File{ |
| 200 | + {Name: "a.txt", Content: []byte("hello"), Mode: 0o644}, |
| 201 | + }) |
| 202 | + baseHash := baseline.computeHash() |
| 203 | + |
| 204 | + tests := []struct { |
| 205 | + name string |
| 206 | + build func() *Bundle |
| 207 | + }{ |
| 208 | + { |
| 209 | + name: "different version", |
| 210 | + build: func() *Bundle { |
| 211 | + return NewBundle("v2", []File{ |
| 212 | + {Name: "a.txt", Content: []byte("hello"), Mode: 0o644}, |
| 213 | + }) |
| 214 | + }, |
| 215 | + }, |
| 216 | + { |
| 217 | + name: "different content", |
| 218 | + build: func() *Bundle { |
| 219 | + return NewBundle("v1", []File{ |
| 220 | + {Name: "a.txt", Content: []byte("world"), Mode: 0o644}, |
| 221 | + }) |
| 222 | + }, |
| 223 | + }, |
| 224 | + { |
| 225 | + name: "different filename", |
| 226 | + build: func() *Bundle { |
| 227 | + return NewBundle("v1", []File{ |
| 228 | + {Name: "b.txt", Content: []byte("hello"), Mode: 0o644}, |
| 229 | + }) |
| 230 | + }, |
| 231 | + }, |
| 232 | + } |
| 233 | + |
| 234 | + for _, tt := range tests { |
| 235 | + t.Run(tt.name, func(t *testing.T) { |
| 236 | + t.Parallel() |
| 237 | + other := tt.build() |
| 238 | + assert.NotEqual(t, baseHash, other.computeHash(), |
| 239 | + "hash should differ when %s changes", tt.name) |
| 240 | + }) |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +func TestBundle_ComputeHash_EmptyBundle(t *testing.T) { |
| 245 | + t.Parallel() |
| 246 | + |
| 247 | + b1 := NewBundle("v1", nil) |
| 248 | + b2 := NewBundle("v2", nil) |
| 249 | + |
| 250 | + hash1 := b1.computeHash() |
| 251 | + hash2 := b2.computeHash() |
| 252 | + |
| 253 | + assert.Len(t, hash1, 64, "empty bundle hash should be valid 64-char hex") |
| 254 | + _, err := hex.DecodeString(hash1) |
| 255 | + require.NoError(t, err) |
| 256 | + |
| 257 | + assert.NotEqual(t, hash1, hash2, |
| 258 | + "empty bundles with different versions should produce different hashes") |
| 259 | +} |
| 260 | + |
| 261 | +func TestBundle_IsValid_MatchingHash(t *testing.T) { |
| 262 | + t.Parallel() |
| 263 | + |
| 264 | + b := NewBundle("v1", []File{ |
| 265 | + {Name: "x.txt", Content: []byte("data"), Mode: 0o644}, |
| 266 | + }) |
| 267 | + hash := b.computeHash() |
| 268 | + |
| 269 | + dir := t.TempDir() |
| 270 | + require.NoError(t, os.WriteFile(filepath.Join(dir, ".version"), []byte(hash), 0o644)) |
| 271 | + |
| 272 | + assert.True(t, b.isValid(dir, hash)) |
| 273 | +} |
| 274 | + |
| 275 | +func TestBundle_IsValid_WrongHash(t *testing.T) { |
| 276 | + t.Parallel() |
| 277 | + |
| 278 | + b := NewBundle("v1", []File{ |
| 279 | + {Name: "x.txt", Content: []byte("data"), Mode: 0o644}, |
| 280 | + }) |
| 281 | + hash := b.computeHash() |
| 282 | + |
| 283 | + dir := t.TempDir() |
| 284 | + require.NoError(t, os.WriteFile(filepath.Join(dir, ".version"), []byte("wronghash"), 0o644)) |
| 285 | + |
| 286 | + assert.False(t, b.isValid(dir, hash)) |
| 287 | +} |
| 288 | + |
| 289 | +func TestBundle_IsValid_MissingVersionFile(t *testing.T) { |
| 290 | + t.Parallel() |
| 291 | + |
| 292 | + b := NewBundle("v1", nil) |
| 293 | + hash := b.computeHash() |
| 294 | + |
| 295 | + dir := t.TempDir() |
| 296 | + // dir exists but has no .version file. |
| 297 | + assert.False(t, b.isValid(dir, hash)) |
| 298 | +} |
| 299 | + |
| 300 | +func TestBundle_IsValid_NonexistentDir(t *testing.T) { |
| 301 | + t.Parallel() |
| 302 | + |
| 303 | + b := NewBundle("v1", nil) |
| 304 | + hash := b.computeHash() |
| 305 | + |
| 306 | + assert.False(t, b.isValid("/nonexistent/path/that/does/not/exist", hash)) |
| 307 | +} |
0 commit comments