package datastore import ( "os" "path/filepath" "testing" ) func TestGetConfiguredSources_DeduceIDs(t *testing.T) { tempDir, err := os.MkdirTemp("", "datastore-test-*") if err != nil { t.Fatalf("Failed to create temp dir: %v", err) } defer os.RemoveAll(tempDir) ds := NewDataStore(tempDir) account := "test-account" device := "test-device" // Create recents with specific source IDs for provider IDs // Let's create a manual Recents.xml and Presets.xml in the temp directory to simulate the state. deviceDir := ds.AccountDeviceDir(account, device) if err := os.MkdirAll(deviceDir, 0755); err != nil { t.Fatalf("Failed to create device dir: %v", err) } // Provider 11 = LOCAL_INTERNET_RADIO (active, present in initial sources) recentsXML := ` 2017-02-07T11:22:00.000+00:00 2017-05-17T13:18:57.000+00:00 52349 Lounge FM Digital 2019-01-24T08:18:37.000+00:00 9330201 11 2019-02-03T18:35:45.000+00:00 9330201 2017-05-17T17:18:58.000+00:00 Lounge FM Digital ` if err := os.WriteFile(filepath.Join(deviceDir, "Recents.xml"), []byte(recentsXML), 0644); err != nil { t.Fatalf("Failed to write Recents.xml: %v", err) } // Now call GetConfiguredSources and expect it to have "9330201" for provider ID "11" sources, err := ds.GetConfiguredSources(account, device) if err != nil { t.Fatalf("GetConfiguredSources failed: %v", err) } foundDeducted := false for _, s := range sources { if s.SourceProviderID == "11" { if s.ID == "9330201" { foundDeducted = true } else { t.Errorf("Expected source ID 9330201 for provider 11, got %s", s.ID) } } } if !foundDeducted { t.Errorf("Did not find source with provider ID 11 and deducted ID 9330201") } } func TestGetConfiguredSources_DeduceIDs_AllProviders(t *testing.T) { tempDir, err := os.MkdirTemp("", "datastore-test-all-*") if err != nil { t.Fatalf("Failed to create temp dir: %v", err) } defer os.RemoveAll(tempDir) ds := NewDataStore(tempDir) account := "test-account" device := "test-device" deviceDir := ds.AccountDeviceDir(account, device) if err := os.MkdirAll(deviceDir, 0755); err != nil { t.Fatalf("Failed to create device dir: %v", err) } // 9: AUX // 11: LOCAL_INTERNET_RADIO // 25: TUNEIN // INTERNET_RADIO (provider 2) is intentionally excluded from initial sources // and therefore not expected in the deduction result. presetsXML := ` http://example.com/art9.png ID9 http://example.com/art11.png ID11 http://example.com/art25.png ID25 ` if err := os.WriteFile(filepath.Join(deviceDir, "Presets.xml"), []byte(presetsXML), 0644); err != nil { t.Fatalf("Failed to write Presets.xml: %v", err) } sources, err := ds.GetConfiguredSources(account, device) if err != nil { t.Fatalf("GetConfiguredSources failed: %v", err) } expected := map[string]string{ "9": "ID9", "11": "ID11", "25": "ID25", } found := make(map[string]bool) for _, s := range sources { if expID, ok := expected[s.SourceProviderID]; ok { if s.ID != expID { t.Errorf("Expected source ID %s for provider %s, got %s", expID, s.SourceProviderID, s.ID) } found[s.SourceProviderID] = true } else if s.SourceKeyType == "AUX" && s.SourceProviderID == "" { // Special case for AUX if it doesn't have provider ID 9 by default if expID, ok := expected["9"]; ok { if s.ID != expID { t.Errorf("Expected source ID %s for AUX, got %s", expID, s.ID) } found["9"] = true } } } for pid := range expected { if !found[pid] { t.Errorf("Did not find source with provider ID %s", pid) } } }