Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/storage/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package memory

import (
"fmt"
"sync"

"github.com/OCAP2/extension/v5/internal/config"
Expand Down Expand Up @@ -104,6 +105,10 @@ func (b *Backend) EndMission() error {
b.mu.Lock()
defer b.mu.Unlock()

if b.mission == nil {
return fmt.Errorf("no mission to end: mission was never started")
}

return b.exportJSON()
}

Expand Down Expand Up @@ -313,10 +318,15 @@ func (b *Backend) GetExportedFilePath() string {
}

// GetExportMetadata returns metadata about the last export.
// Returns empty metadata if no mission was started.
func (b *Backend) GetExportMetadata() storage.UploadMetadata {
b.mu.RLock()
defer b.mu.RUnlock()

if b.mission == nil || b.world == nil {
return storage.UploadMetadata{}
}

var endFrame uint
for _, record := range b.soldiers {
for _, state := range record.States {
Expand Down
33 changes: 33 additions & 0 deletions internal/storage/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,3 +934,36 @@ func TestStartMissionResetsExportPath(t *testing.T) {
t.Errorf("expected empty path after StartMission, got %s", path)
}
}

func TestEndMissionWithoutStartMission(t *testing.T) {
b := New(config.MemoryConfig{})

// EndMission without StartMission should return an error, not panic
err := b.EndMission()
if err == nil {
t.Error("expected error when ending mission that was never started")
}
if !strings.Contains(err.Error(), "no mission to end") {
t.Errorf("expected error message to contain 'no mission to end', got: %s", err.Error())
}
}

func TestGetExportMetadataWithoutStartMission(t *testing.T) {
b := New(config.MemoryConfig{})

// GetExportMetadata without StartMission should return empty metadata, not panic
meta := b.GetExportMetadata()

if meta.WorldName != "" {
t.Errorf("expected empty WorldName, got %s", meta.WorldName)
}
if meta.MissionName != "" {
t.Errorf("expected empty MissionName, got %s", meta.MissionName)
}
if meta.Tag != "" {
t.Errorf("expected empty Tag, got %s", meta.Tag)
}
if meta.MissionDuration != 0 {
t.Errorf("expected MissionDuration=0, got %f", meta.MissionDuration)
}
}
Loading