/* Copyright © 2023 Philipp Wolfer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package scrobblerlog_test import ( "bytes" "strings" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uploadedlobster.com/scotty/backends/scrobblerlog" "go.uploadedlobster.com/scotty/models" ) var testScrobblerLog = `#AUDIOSCROBBLER/1.1 #TZ/UNKNOWN #CLIENT/Rockbox sansaclipplus $Revision$ Özcan Deniz Ses ve Ayrilik Sevdanin rengi (sipacik) byMrTurkey 5 306 L 1260342084 Özcan Deniz Hediye 2@V@7 Bir Dudaktan 1 210 L 1260342633 KOMPROMAT Traum und Existenz Possession 1 220 L 1260357290 d66b1084-b2ae-4661-8382-5d0c1c484b6d Kraftwerk Trans-Europe Express The Hall of Mirrors 2 474 S 1260358000 385ba9e9-626d-4750-a607-58e541dca78e Teeth Agency You Don't Have To Live In Pain Wolfs Jam 2 107 L 1260359404 1262beaf-19f8-4534-b9ed-7eef9ca8e83f ` func TestParser(t *testing.T) { assert := assert.New(t) data := bytes.NewBufferString(testScrobblerLog) result, err := scrobblerlog.Parse(data, true) require.NoError(t, err) assert.Equal("UNKNOWN", result.Timezone) assert.Equal("Rockbox sansaclipplus $Revision$", result.Client) assert.Len(result.Listens, 5) listen1 := result.Listens[0] assert.Equal("Özcan Deniz", listen1.ArtistName()) assert.Equal("Ses ve Ayrilik", listen1.ReleaseName) assert.Equal("Sevdanin rengi (sipacik) byMrTurkey", listen1.TrackName) assert.Equal(5, listen1.TrackNumber) assert.Equal(time.Duration(306*time.Second), listen1.Duration) assert.Equal("L", listen1.AdditionalInfo["rockbox_rating"]) assert.Equal(time.Unix(1260342084, 0), listen1.ListenedAt) assert.Equal(models.MBID(""), listen1.RecordingMbid) listen4 := result.Listens[3] assert.Equal("S", listen4.AdditionalInfo["rockbox_rating"]) assert.Equal(models.MBID("385ba9e9-626d-4750-a607-58e541dca78e"), listen4.RecordingMbid) } func TestParserExcludeSkipped(t *testing.T) { assert := assert.New(t) data := bytes.NewBufferString(testScrobblerLog) result, err := scrobblerlog.Parse(data, false) require.NoError(t, err) assert.Len(result.Listens, 4) listen4 := result.Listens[3] assert.Equal("L", listen4.AdditionalInfo["rockbox_rating"]) assert.Equal(models.MBID("1262beaf-19f8-4534-b9ed-7eef9ca8e83f"), listen4.RecordingMbid) } func TestWrite(t *testing.T) { assert := assert.New(t) data := make([]byte, 0, 10) buffer := bytes.NewBuffer(data) log := scrobblerlog.ScrobblerLog{ Timezone: "Unknown", Client: "Rockbox foo $Revision$", Listens: []models.Listen{ { ListenedAt: time.Unix(1699572072, 0), Track: models.Track{ ArtistNames: []string{"Prinzhorn Dance School"}, ReleaseName: "Home Economics", TrackName: "Reign", TrackNumber: 1, Duration: 271 * time.Second, RecordingMbid: models.MBID("b59cf4e7-caee-4019-a844-79d2c58d4dff"), AdditionalInfo: models.AdditionalInfo{"rockbox_rating": "L"}, }, }, }, } err := scrobblerlog.WriteHeader(buffer, &log) require.NoError(t, err) lastTimestamp, err := scrobblerlog.Write(buffer, log.Listens) require.NoError(t, err) result := string(buffer.Bytes()) lines := strings.Split(result, "\n") assert.Equal(5, len(lines)) assert.Equal("#AUDIOSCROBBLER/1.1", lines[0]) assert.Equal("#TZ/Unknown", lines[1]) assert.Equal("#CLIENT/Rockbox foo $Revision$", lines[2]) assert.Equal( "Prinzhorn Dance School\tHome Economics\tReign\t1\t271\tL\t1699572072\tb59cf4e7-caee-4019-a844-79d2c58d4dff", lines[3]) assert.Equal("", lines[4]) assert.Equal(time.Unix(1699572072, 0), lastTimestamp) }