191 lines
4.3 KiB
Go
191 lines
4.3 KiB
Go
package jobs
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"git.nakama.town/fmartingr/hako/internal/model"
|
|
)
|
|
|
|
// MockArchiveService is a mock implementation of ArchiveService for testing
|
|
type MockArchiveService struct {
|
|
mu sync.Mutex
|
|
ProcessedCount int
|
|
ShouldFail bool
|
|
ProcessedItems []string // Track which archives were processed
|
|
ProcessCallFunc func(ctx context.Context, archive *model.Archive, link *model.Link, archiverKeys []string) error
|
|
}
|
|
|
|
func NewMockArchiveService() *MockArchiveService {
|
|
return &MockArchiveService{
|
|
ProcessedItems: make([]string, 0),
|
|
}
|
|
}
|
|
|
|
func (m *MockArchiveService) ProcessArchive(ctx context.Context, archive *model.Archive, link *model.Link, archiverKeys []string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
m.ProcessedCount++
|
|
m.ProcessedItems = append(m.ProcessedItems, archive.ID)
|
|
|
|
if m.ProcessCallFunc != nil {
|
|
return m.ProcessCallFunc(ctx, archive, link, archiverKeys)
|
|
}
|
|
|
|
if m.ShouldFail {
|
|
return fmt.Errorf("mock archive service error")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *MockArchiveService) GetProcessedCount() int {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.ProcessedCount
|
|
}
|
|
|
|
func (m *MockArchiveService) GetProcessedItems() []string {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return append([]string{}, m.ProcessedItems...)
|
|
}
|
|
|
|
// MockLinkStore is a mock implementation of LinkStore for testing
|
|
type MockLinkStore struct {
|
|
mu sync.Mutex
|
|
links map[string]*model.Link
|
|
ShouldFail bool
|
|
}
|
|
|
|
func NewMockLinkStore() *MockLinkStore {
|
|
return &MockLinkStore{
|
|
links: make(map[string]*model.Link),
|
|
}
|
|
}
|
|
|
|
func (m *MockLinkStore) GetByID(ctx context.Context, id string) (*model.Link, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
if m.ShouldFail {
|
|
return nil, fmt.Errorf("mock link store error")
|
|
}
|
|
|
|
link, ok := m.links[id]
|
|
if !ok {
|
|
return nil, fmt.Errorf("link not found: %s", id)
|
|
}
|
|
return link, nil
|
|
}
|
|
|
|
func (m *MockLinkStore) AddLink(link *model.Link) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.links[link.ID] = link
|
|
}
|
|
|
|
// MockArchiveStore is a mock implementation of ArchiveStore for testing
|
|
type MockArchiveStore struct {
|
|
mu sync.Mutex
|
|
archives map[string]*model.Archive
|
|
}
|
|
|
|
func NewMockArchiveStore() *MockArchiveStore {
|
|
return &MockArchiveStore{
|
|
archives: make(map[string]*model.Archive),
|
|
}
|
|
}
|
|
|
|
func (m *MockArchiveStore) GetByID(ctx context.Context, id string) (*model.Archive, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
archive, ok := m.archives[id]
|
|
if !ok {
|
|
return nil, fmt.Errorf("archive not found: %s", id)
|
|
}
|
|
return archive, nil
|
|
}
|
|
|
|
func (m *MockArchiveStore) UpdateStatus(ctx context.Context, id string, status model.ArchiveStatus, errorMessage string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
archive, ok := m.archives[id]
|
|
if !ok {
|
|
return fmt.Errorf("archive not found: %s", id)
|
|
}
|
|
|
|
archive.Status = status
|
|
archive.ErrorMessage = errorMessage
|
|
return nil
|
|
}
|
|
|
|
func (m *MockArchiveStore) AddArchive(archive *model.Archive) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.archives[archive.ID] = archive
|
|
}
|
|
|
|
// MockArchiveFileStore is a mock implementation of ArchiveFileStore for testing
|
|
type MockArchiveFileStore struct {
|
|
mu sync.Mutex
|
|
files map[string]*model.ArchiveFile
|
|
ShouldFail bool
|
|
}
|
|
|
|
func NewMockArchiveFileStore() *MockArchiveFileStore {
|
|
return &MockArchiveFileStore{
|
|
files: make(map[string]*model.ArchiveFile),
|
|
}
|
|
}
|
|
|
|
func (m *MockArchiveFileStore) GetByID(ctx context.Context, id string) (*model.ArchiveFile, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
if m.ShouldFail {
|
|
return nil, fmt.Errorf("mock archive file store error")
|
|
}
|
|
|
|
file, ok := m.files[id]
|
|
if !ok {
|
|
return nil, fmt.Errorf("archive file not found: %s", id)
|
|
}
|
|
return file, nil
|
|
}
|
|
|
|
func (m *MockArchiveFileStore) UpdateContent(ctx context.Context, fileID string, content string, contentMimeType string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
file, ok := m.files[fileID]
|
|
if !ok {
|
|
return fmt.Errorf("archive file not found: %s", fileID)
|
|
}
|
|
|
|
file.Content = content
|
|
file.ContentMimeType = contentMimeType
|
|
return nil
|
|
}
|
|
|
|
func (m *MockArchiveFileStore) Create(ctx context.Context, file *model.ArchiveFile) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
if m.ShouldFail {
|
|
return fmt.Errorf("mock archive file store error")
|
|
}
|
|
|
|
m.files[file.ID] = file
|
|
return nil
|
|
}
|
|
|
|
func (m *MockArchiveFileStore) AddFile(file *model.ArchiveFile) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.files[file.ID] = file
|
|
}
|