Skip to content

Commit 92df420

Browse files
committed
test(core): cover repository link reuse in the partial image-upload allowance
The floor lives in internal/core because that is the only layer that sees reused plus newly published images for a target. Two cases pin that: a host that publishes fewer images than the floor still succeeds once reuse makes up the difference, and reuse that is still short of the floor fails. Applying the floor in internal/imagehosting, which only receives the missing subset, would fail the first case.
1 parent 5c7fc1d commit 92df420

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

internal/core/media_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,33 @@ func sortedCallHosts(calls []imageHostCall) []string {
363363
return hosts
364364
}
365365

366+
// reusableImageRepository reports images a previous run already published, so
367+
// the reuse branch of uploadImagesToTarget can be exercised. Only the uploaded
368+
// image lookup carries behavior; the rest satisfies the interface.
369+
type reusableImageRepository struct {
370+
mediaRepository
371+
links []api.UploadedImageLink
372+
}
373+
374+
func (r *reusableImageRepository) ListUploadedImagesByPath(context.Context, string) ([]api.UploadedImageLink, error) {
375+
return slices.Clone(r.links), nil
376+
}
377+
378+
// reusedImageLinks builds host records that uploadedImagesByPathForTarget
379+
// matches back to the given images.
380+
func reusedImageLinks(images []api.ScreenshotImage, target trackers.ImageUploadTarget) []api.UploadedImageLink {
381+
links := make([]api.UploadedImageLink, 0, len(images))
382+
for _, image := range images {
383+
links = append(links, api.UploadedImageLink{
384+
ImagePath: image.Path,
385+
Host: target.Host,
386+
UsageScope: target.UsageScope,
387+
RawURL: "https://images.example.invalid/reused",
388+
})
389+
}
390+
return links
391+
}
392+
366393
// partialImageHostingService publishes a fixed number of images and then fails
367394
// the batch, mirroring a host that drops individual uploads under load.
368395
type partialImageHostingService struct {
@@ -413,6 +440,7 @@ Host: "pixhost",
413440
name string
414441
minimum int
415442
published int
443+
reused int
416444
wantLinks int
417445
wantFailure bool
418446
}{
@@ -445,17 +473,39 @@ name: "minimum above requested",
445473
minimum: 8,
446474
published: 5,
447475
wantFailure: true,
476+
},
477+
// Reuse counts toward the floor: the host publishes fewer images than
478+
// the floor on its own, so this only passes when the allowance is
479+
// applied where reused links are visible.
480+
{
481+
name: "reuse completes the minimum",
482+
minimum: 3,
483+
published: 2,
484+
reused: 2,
485+
wantLinks: 4,
486+
},
487+
{
488+
name: "reuse still short of the minimum",
489+
minimum: 5,
490+
published: 2,
491+
reused: 2,
492+
wantFailure: true,
448493
},
449494
} {
450495
t.Run(testCase.name, func(t *testing.T) {
451496
t.Parallel()
452497

498+
var repo mediaRepository
499+
if testCase.reused > 0 {
500+
repo = &reusableImageRepository{links: reusedImageLinks(images[:testCase.reused], target)}
501+
}
453502
module := &mediaModule{
454503
cfg: config.Config{
455504
ImageHosting: config.ImageHostingConfig{Host1: "pixhost"},
456505
ScreenshotHandling: config.ScreenshotHandlingConfig{MinSuccessfulUploads: testCase.minimum},
457506
},
458507
images: &partialImageHostingService{published: testCase.published},
508+
repo: repo,
459509
logger: &recordingMediaLogger{},
460510
registry: mediaImageHostRegistry(t),
461511
}

0 commit comments

Comments
 (0)