|
| 1 | +import assert from "node:assert"; |
| 2 | +import { describe, test } from "node:test"; |
| 3 | + |
| 4 | +import type { SwinsianLibraryPlist, SwinsianTrackDefinition } from "../index.js"; |
| 5 | +import convertSwinsianToItunesXmlLibrary from "./index.js"; |
| 6 | + |
| 7 | +void describe("convertSwinsianToItunesXmlLibrary", () => { |
| 8 | + const mockLibrary: SwinsianLibraryPlist = { |
| 9 | + "Application Version": "1.0", |
| 10 | + Date: new Date(), |
| 11 | + Features: 5, |
| 12 | + "Library Persistent ID": "test-lib-id", |
| 13 | + "Major Version": 1, |
| 14 | + "Minor Version": 0, |
| 15 | + "Music Folder": "/path/to/music", |
| 16 | + "Show Content Ratings": true, |
| 17 | + Tracks: { |
| 18 | + 1: { |
| 19 | + "Track ID": 1, |
| 20 | + "Persistent ID": "test-persistent-id", |
| 21 | + Name: "Test Track", |
| 22 | + Artist: "Test Artist", |
| 23 | + "Total Time": 180000, |
| 24 | + "Play Count": 5, |
| 25 | + "Date Added": new Date("2024-01-01"), |
| 26 | + Location: "file:///path/to/track.mp3", |
| 27 | + } as SwinsianTrackDefinition, |
| 28 | + }, |
| 29 | + Playlists: [ |
| 30 | + { |
| 31 | + "Playlist ID": "1", |
| 32 | + "Playlist Persistent ID": "playlist-id-1", |
| 33 | + Name: "Playlist 1", |
| 34 | + "Playlist Items": [{ "Track ID": 1 }], |
| 35 | + }, |
| 36 | + { |
| 37 | + "Playlist ID": "2", |
| 38 | + "Playlist Persistent ID": "playlist-id-2", |
| 39 | + Name: "Playlist 2", |
| 40 | + "Playlist Items": [{ "Track ID": 1 }], |
| 41 | + }, |
| 42 | + ], |
| 43 | + }; |
| 44 | + |
| 45 | + void test("should convert library without filtering when no selectedPlaylistIds provided", () => { |
| 46 | + const result = convertSwinsianToItunesXmlLibrary(mockLibrary); |
| 47 | + |
| 48 | + assert.strictEqual(result.Playlists.length, 2); |
| 49 | + assert.strictEqual(result.Playlists[0].Name, "Playlist 1"); |
| 50 | + assert.strictEqual(result.Playlists[1].Name, "Playlist 2"); |
| 51 | + }); |
| 52 | + |
| 53 | + void test("should filter playlists when selectedPlaylistIds provided", () => { |
| 54 | + const result = convertSwinsianToItunesXmlLibrary(mockLibrary, ["playlist-id-1"]); |
| 55 | + |
| 56 | + assert.strictEqual(result.Playlists.length, 1); |
| 57 | + assert.strictEqual(result.Playlists[0].Name, "Playlist 1"); |
| 58 | + assert.strictEqual(result.Playlists[0]["Playlist Persistent ID"], "playlist-id-1"); |
| 59 | + }); |
| 60 | + |
| 61 | + void test("should handle empty selectedPlaylistIds array", () => { |
| 62 | + const result = convertSwinsianToItunesXmlLibrary(mockLibrary, []); |
| 63 | + |
| 64 | + // Should not filter when empty array is provided |
| 65 | + assert.strictEqual(result.Playlists.length, 2); |
| 66 | + }); |
| 67 | + |
| 68 | + void test("should handle non-matching selectedPlaylistIds", () => { |
| 69 | + const result = convertSwinsianToItunesXmlLibrary(mockLibrary, ["non-existent-id"]); |
| 70 | + |
| 71 | + assert.strictEqual(result.Playlists.length, 0); |
| 72 | + }); |
| 73 | + |
| 74 | + void test("should handle library with no Playlists property", () => { |
| 75 | + const libraryWithoutPlaylists = { ...mockLibrary }; |
| 76 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access |
| 77 | + delete (libraryWithoutPlaylists as any).Playlists; |
| 78 | + |
| 79 | + const result = convertSwinsianToItunesXmlLibrary(libraryWithoutPlaylists, ["playlist-id-1"]); |
| 80 | + |
| 81 | + assert.strictEqual(Array.isArray(result.Playlists), true); |
| 82 | + assert.strictEqual(result.Playlists.length, 0); |
| 83 | + }); |
| 84 | + |
| 85 | + void test("should handle library with null Playlists property", () => { |
| 86 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment |
| 87 | + const libraryWithNullPlaylists = { ...mockLibrary, Playlists: null as any }; |
| 88 | + |
| 89 | + const result = convertSwinsianToItunesXmlLibrary(libraryWithNullPlaylists, ["playlist-id-1"]); |
| 90 | + |
| 91 | + assert.strictEqual(Array.isArray(result.Playlists), true); |
| 92 | + assert.strictEqual(result.Playlists.length, 0); |
| 93 | + }); |
| 94 | + |
| 95 | + void test("should handle library with non-array Playlists property", () => { |
| 96 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment |
| 97 | + const libraryWithInvalidPlaylists = { ...mockLibrary, Playlists: "invalid" as any }; |
| 98 | + |
| 99 | + const result = convertSwinsianToItunesXmlLibrary(libraryWithInvalidPlaylists, [ |
| 100 | + "playlist-id-1", |
| 101 | + ]); |
| 102 | + |
| 103 | + assert.strictEqual(Array.isArray(result.Playlists), true); |
| 104 | + assert.strictEqual(result.Playlists.length, 0); |
| 105 | + }); |
| 106 | + |
| 107 | + void test("should convert tracks properly", () => { |
| 108 | + const result = convertSwinsianToItunesXmlLibrary(mockLibrary); |
| 109 | + |
| 110 | + assert.strictEqual(typeof result.Tracks[1], "object"); |
| 111 | + assert.strictEqual(result.Tracks[1]["Track ID"], 1); |
| 112 | + // Persistent ID gets converted from string to hex format |
| 113 | + assert.strictEqual(typeof result.Tracks[1]["Persistent ID"], "string"); |
| 114 | + assert.strictEqual(result.Tracks[1]["Persistent ID"].length, 16); |
| 115 | + }); |
| 116 | +}); |
0 commit comments