mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 10:09:28 +02:00
109 lines
3.7 KiB
Go
109 lines
3.7 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 jspf_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uploadedlobster.com/scotty/pkg/jspf"
|
|
)
|
|
|
|
func TestUnmarshalSimple(t *testing.T) {
|
|
data, err := readSampleJson("testdata/simple.jspf")
|
|
require.NoError(t, err)
|
|
|
|
assert := assert.New(t)
|
|
playlist := data.Playlist
|
|
assert.Equal("Two Songs From Thriller", playlist.Title)
|
|
assert.Equal("MJ Fan", playlist.Creator)
|
|
require.Len(t, playlist.Tracks, 2)
|
|
track1 := playlist.Tracks[0]
|
|
require.Len(t, track1.Location, 1)
|
|
assert.Equal("http://example.com/billiejean.mp3", track1.Location[0])
|
|
assert.Equal("Billie Jean", track1.Title)
|
|
assert.Equal("Michael Jackson", track1.Creator)
|
|
assert.Equal("Thriller", track1.Album)
|
|
}
|
|
|
|
func TestUnmarshalComprehensive(t *testing.T) {
|
|
data, err := readSampleJson("testdata/comprehensive.jspf")
|
|
require.NoError(t, err)
|
|
|
|
assert := assert.New(t)
|
|
playlist := data.Playlist
|
|
assert.Equal("http://example.com/", playlist.License)
|
|
require.Len(t, playlist.Attribution, 2)
|
|
assert.Equal("http://example.com/", playlist.Attribution[0]["identifier"])
|
|
assert.Equal("http://example.com/", playlist.Attribution[1]["location"])
|
|
require.Len(t, playlist.Meta, 2)
|
|
assert.Equal("345", playlist.Meta[1]["http://example.com/rel/2/"])
|
|
require.Len(t, playlist.Links, 2)
|
|
assert.Equal("http://example.com/body/1/", playlist.Links[0]["http://example.com/rel/1/"])
|
|
}
|
|
|
|
func TestUnmarshalListenBrainzPlaylist(t *testing.T) {
|
|
data, err := readSampleJson("testdata/lb-playlist.jspf")
|
|
require.NoError(t, err)
|
|
|
|
assert := assert.New(t)
|
|
playlist := data.Playlist
|
|
assert.Equal(
|
|
"https://listenbrainz.org/playlist/96485e27-967a-492a-9d04-c5a819baa2f3",
|
|
playlist.Identifier)
|
|
expectedPlaylistDate, err := time.Parse(time.RFC3339, "2023-07-04T21:03:52.317148+00:00")
|
|
require.NoError(t, err)
|
|
assert.Equal(expectedPlaylistDate, playlist.Date)
|
|
assert.NotNil(playlist.Extension["https://musicbrainz.org/doc/jspf#playlist"])
|
|
|
|
require.Len(t, playlist.Tracks, 2)
|
|
track1 := playlist.Tracks[0]
|
|
assert.Equal(
|
|
"https://musicbrainz.org/recording/3f2bdbbd-063e-478c-a394-6da0cb303302",
|
|
track1.Identifier[0])
|
|
extension := track1.Extension["https://musicbrainz.org/doc/jspf#track"].(map[string]any)
|
|
assert.NotNil(extension)
|
|
assert.Equal("outsidecontext", extension["added_by"])
|
|
}
|
|
|
|
func readSampleJson(path string) (jspf.JSPF, error) {
|
|
var result jspf.JSPF
|
|
jsonFile, err := os.Open(path)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
defer jsonFile.Close()
|
|
|
|
byteValue, err := io.ReadAll(jsonFile)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
err = json.Unmarshal(byteValue, &result)
|
|
return result, err
|
|
}
|