forked from mtusi2026/mtusi2026lab4
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package media
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto"
|
|
)
|
|
|
|
type pagedFixture struct {
|
|
reconciliationFixture
|
|
metadataPage int
|
|
objectPage int
|
|
}
|
|
|
|
// Scan returns one full fresh page followed by an empty continuation.
|
|
func (f *pagedFixture) Scan(context.Context, uuid.UUID, int) ([]dto.Attachment, error) {
|
|
f.metadataPage++
|
|
if f.metadataPage > 1 {
|
|
return nil, nil
|
|
}
|
|
records := make([]dto.Attachment, 1000)
|
|
for i := range records {
|
|
records[i] = dto.Attachment{ID: uuid.New(), CreatedAt: time.Now()}
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
// List returns one full fresh page followed by an empty continuation.
|
|
func (f *pagedFixture) List(context.Context, string) ([]ObjectInfo, error) {
|
|
f.objectPage++
|
|
if f.objectPage > 1 {
|
|
return nil, nil
|
|
}
|
|
records := make([]ObjectInfo, 1000)
|
|
for i := range records {
|
|
records[i] = ObjectInfo{Key: uuid.NewString(), ModifiedAt: time.Now()}
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
type failingWriter struct {
|
|
successful int
|
|
}
|
|
|
|
// Write injects a broken report sink after a fixed number of complete pages.
|
|
func (f *failingWriter) Write(data []byte) (int, error) {
|
|
if f.successful == 0 {
|
|
return 0, io.ErrClosedPipe
|
|
}
|
|
f.successful--
|
|
return len(data), nil
|
|
}
|
|
|
|
// TestReconciliationPagination proves both inventories advance beyond a full page.
|
|
func TestReconciliationPagination(t *testing.T) {
|
|
f := &pagedFixture{}
|
|
if err := RunReconciliation(t.Context(), f, f, io.Discard, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f.metadataPage != 2 || f.objectPage != 2 {
|
|
t.Fatalf("pages: metadata=%d objects=%d", f.metadataPage, f.objectPage)
|
|
}
|
|
}
|
|
|
|
// TestReconciliationStopsOnFailures prevents misleading successful reports after lost evidence or dependencies.
|
|
func TestReconciliationStopsOnFailures(t *testing.T) {
|
|
fault := errors.New("dependency failure")
|
|
cases := []struct {
|
|
name string
|
|
fixture reconciliationFixture
|
|
writer io.Writer
|
|
}{
|
|
{"metadata", reconciliationFixture{scanErr: fault}, io.Discard},
|
|
{"objects", reconciliationFixture{listErr: fault}, io.Discard},
|
|
{"first report", reconciliationFixture{}, &failingWriter{}},
|
|
{"second report", reconciliationFixture{}, &failingWriter{successful: 1}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
f := tc.fixture
|
|
if err := RunReconciliation(t.Context(), &f, &f, tc.writer, false); err == nil {
|
|
t.Fatal("failed run reported success")
|
|
}
|
|
})
|
|
}
|
|
}
|