Files
agentic-coding-workflow/internal/jobs/runners/attachment_cleanup_test.go
T

74 lines
2.2 KiB
Go

package runners_test
import (
"context"
"errors"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/yan1h/agent-coding-workflow/internal/jobs/runners"
)
type fakeAttachmentRepo struct {
softDel []runners.AttachmentRow
orphan []runners.AttachmentRow
deletedIDs []uuid.UUID
}
func (f *fakeAttachmentRepo) ListAttachmentsForSoftDeletedConversations(_ context.Context, _ time.Duration) ([]runners.AttachmentRow, error) {
return f.softDel, nil
}
func (f *fakeAttachmentRepo) ListExpiredOrphans(_ context.Context, _ time.Duration) ([]runners.AttachmentRow, error) {
return f.orphan, nil
}
func (f *fakeAttachmentRepo) DeleteAttachments(_ context.Context, ids []uuid.UUID) error {
f.deletedIDs = append(f.deletedIDs, ids...)
return nil
}
type fakeStorage struct {
deleted []string
err error
}
func (s *fakeStorage) Delete(_ context.Context, path string) error {
if s.err != nil {
return s.err
}
s.deleted = append(s.deleted, path)
return nil
}
func TestAttachmentCleanup_DeletesBothCategories(t *testing.T) {
t.Parallel()
a, b, c := uuid.New(), uuid.New(), uuid.New()
repo := &fakeAttachmentRepo{
softDel: []runners.AttachmentRow{{ID: a, StoragePath: "a"}, {ID: b, StoragePath: "b"}},
orphan: []runners.AttachmentRow{{ID: c, StoragePath: "c"}},
}
st := &fakeStorage{}
r := runners.NewAttachmentCleanup(repo, st, runners.AttachmentCleanupConfig{Retention: 30 * 24 * time.Hour}, discardLogger())
r.Run(context.Background())
assert.ElementsMatch(t, []string{"a", "b", "c"}, st.deleted)
assert.ElementsMatch(t, []uuid.UUID{a, b, c}, repo.deletedIDs)
}
func TestAttachmentCleanup_StorageErrorDoesNotDeleteFromDB(t *testing.T) {
t.Parallel()
repo := &fakeAttachmentRepo{
softDel: []runners.AttachmentRow{
{ID: uuid.New(), StoragePath: "fail"},
{ID: uuid.New(), StoragePath: "ok"},
},
}
st := &fakeStorage{err: errors.New("disk full")}
r := runners.NewAttachmentCleanup(repo, st, runners.AttachmentCleanupConfig{Retention: 30 * 24 * time.Hour}, discardLogger())
r.Run(context.Background())
require.Empty(t, st.deleted, "storage error → no path recorded")
require.Empty(t, repo.deletedIDs, "storage error → no DB delete batch")
}