skyview/internal/merger/merger_test.go

92 lines
2.2 KiB
Go
Raw Permalink Normal View History

package merger
import (
"encoding/json"
"testing"
"time"
"skyview/internal/modes"
)
func TestAircraftState_MarshalJSON_AgeCalculation(t *testing.T) {
// Create a test aircraft state with a known LastUpdate time
pastTime := time.Now().Add(-30 * time.Second) // 30 seconds ago
state := &AircraftState{
Aircraft: &modes.Aircraft{
ICAO24: 0x123456,
Callsign: "TEST123",
},
LastUpdate: pastTime,
}
// Marshal to JSON
jsonData, err := json.Marshal(state)
if err != nil {
t.Fatalf("Failed to marshal AircraftState: %v", err)
}
// Parse the JSON to check the age field
var result map[string]interface{}
if err := json.Unmarshal(jsonData, &result); err != nil {
t.Fatalf("Failed to unmarshal JSON: %v", err)
}
// Check that age is not 0 and is approximately 30 seconds
age, ok := result["age"].(float64)
if !ok {
t.Fatal("Age field not found or not a number")
}
if age < 25 || age > 35 {
t.Errorf("Expected age to be around 30 seconds, got %f", age)
}
if age == 0 {
t.Error("Age should not be 0 - this indicates the bug is still present")
}
t.Logf("Aircraft age calculated correctly: %f seconds", age)
}
func TestAircraftState_MarshalJSON_ZeroAge(t *testing.T) {
// Test with very recent time to ensure age is very small but not exactly 0
recentTime := time.Now()
state := &AircraftState{
Aircraft: &modes.Aircraft{
ICAO24: 0x123456,
Callsign: "TEST123",
},
LastUpdate: recentTime,
}
// Marshal to JSON
jsonData, err := json.Marshal(state)
if err != nil {
t.Fatalf("Failed to marshal AircraftState: %v", err)
}
// Parse the JSON to check the age field
var result map[string]interface{}
if err := json.Unmarshal(jsonData, &result); err != nil {
t.Fatalf("Failed to unmarshal JSON: %v", err)
}
// Check that age is very small but should not be exactly 0 (unless extremely fast)
age, ok := result["age"].(float64)
if !ok {
t.Fatal("Age field not found or not a number")
}
if age < 0 {
t.Errorf("Age should not be negative, got %f", age)
}
// Age should be very small (less than 1 second) but might not be exactly 0
if age > 1.0 {
t.Errorf("Age should be very small for recent update, got %f", age)
}
t.Logf("Recent aircraft age calculated correctly: %f seconds", age)
}