mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 05:37:05 +02:00
145 lines
5.3 KiB
Go
145 lines
5.3 KiB
Go
/*
|
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
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 (
|
|
"bufio"
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uploadedlobster.com/mbtypes"
|
|
"go.uploadedlobster.com/scotty/pkg/scrobblerlog"
|
|
)
|
|
|
|
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 := scrobblerlog.ScrobblerLog{}
|
|
err := result.Parse(data, false)
|
|
require.NoError(t, err)
|
|
assert.Equal(scrobblerlog.TimezoneUnknown, result.TZ)
|
|
assert.Equal("Rockbox sansaclipplus $Revision$", result.Client)
|
|
assert.Len(result.Records, 5)
|
|
record1 := result.Records[0]
|
|
assert.Equal("Özcan Deniz", record1.ArtistName)
|
|
assert.Equal("Ses ve Ayrilik", record1.AlbumName)
|
|
assert.Equal("Sevdanin rengi (sipacik) byMrTurkey", record1.TrackName)
|
|
assert.Equal(5, record1.TrackNumber)
|
|
assert.Equal(time.Duration(306*time.Second), record1.Duration)
|
|
assert.Equal(scrobblerlog.RatingListened, record1.Rating)
|
|
assert.Equal(time.Unix(1260342084, 0), record1.Timestamp)
|
|
assert.Equal(mbtypes.MBID(""), record1.MusicBrainzRecordingID)
|
|
record4 := result.Records[3]
|
|
assert.Equal(scrobblerlog.RatingSkipped, record4.Rating)
|
|
assert.Equal(mbtypes.MBID("385ba9e9-626d-4750-a607-58e541dca78e"),
|
|
record4.MusicBrainzRecordingID)
|
|
}
|
|
|
|
func TestParserIgnoreSkipped(t *testing.T) {
|
|
assert := assert.New(t)
|
|
data := bytes.NewBufferString(testScrobblerLog)
|
|
result := scrobblerlog.ScrobblerLog{}
|
|
err := result.Parse(data, true)
|
|
require.NoError(t, err)
|
|
assert.Len(result.Records, 4)
|
|
record4 := result.Records[3]
|
|
assert.Equal(scrobblerlog.RatingListened, record4.Rating)
|
|
assert.Equal(mbtypes.MBID("1262beaf-19f8-4534-b9ed-7eef9ca8e83f"),
|
|
record4.MusicBrainzRecordingID)
|
|
}
|
|
|
|
func TestParserFallbackTimezone(t *testing.T) {
|
|
assert := assert.New(t)
|
|
data := bytes.NewBufferString(testScrobblerLog)
|
|
result := scrobblerlog.ScrobblerLog{
|
|
FallbackTimezone: time.FixedZone("UTC+2", 7200),
|
|
}
|
|
err := result.Parse(data, false)
|
|
require.NoError(t, err)
|
|
record1 := result.Records[0]
|
|
assert.Equal(
|
|
time.Unix(1260342084, 0).Add(2*time.Hour),
|
|
record1.Timestamp,
|
|
)
|
|
}
|
|
|
|
func TestAppend(t *testing.T) {
|
|
assert := assert.New(t)
|
|
data := make([]byte, 0, 10)
|
|
buffer := bytes.NewBuffer(data)
|
|
log := scrobblerlog.ScrobblerLog{
|
|
TZ: scrobblerlog.TimezoneUnknown,
|
|
Client: "Rockbox foo $Revision$",
|
|
}
|
|
records := []scrobblerlog.Record{
|
|
{
|
|
ArtistName: "Prinzhorn Dance School",
|
|
AlbumName: "Home Economics",
|
|
TrackName: "Reign",
|
|
TrackNumber: 1,
|
|
Duration: 271 * time.Second,
|
|
Rating: scrobblerlog.RatingListened,
|
|
Timestamp: time.Unix(1699572072, 0),
|
|
MusicBrainzRecordingID: mbtypes.MBID("b59cf4e7-caee-4019-a844-79d2c58d4dff"),
|
|
},
|
|
}
|
|
err := log.WriteHeader(buffer)
|
|
require.NoError(t, err)
|
|
lastTimestamp, err := log.Append(buffer, records)
|
|
require.NoError(t, err)
|
|
result := buffer.String()
|
|
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)
|
|
}
|
|
|
|
func TestReadHeader(t *testing.T) {
|
|
data := bytes.NewBufferString(testScrobblerLog)
|
|
reader := bufio.NewReader(data)
|
|
log := scrobblerlog.ScrobblerLog{}
|
|
err := log.ReadHeader(reader)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, log.TZ, scrobblerlog.TimezoneUnknown)
|
|
assert.Equal(t, log.Client, "Rockbox sansaclipplus $Revision$")
|
|
assert.Empty(t, log.Records)
|
|
}
|