« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/data/things.js27
-rwxr-xr-xsrc/upd8.js10
-rw-r--r--test/things.js71
3 files changed, 103 insertions, 5 deletions
diff --git a/src/data/things.js b/src/data/things.js
index b5d6f109..e061d581 100644
--- a/src/data/things.js
+++ b/src/data/things.js
@@ -252,6 +252,9 @@ Thing.common = {
     })
 };
 
+// Get a reference to a thing (e.g. track:showtime-piano-refrain), using its
+// constructor's [Thing.referenceType] as the prefix. This will throw an error
+// if the thing's directory isn't yet provided/computable.
 Thing.getReference = function(thing) {
     if (!thing.constructor[Thing.referenceType])
         throw TypeError(`Passed Thing is ${thing.constructor.name}, which provides no [Thing.referenceType]`);
@@ -398,7 +401,6 @@ Track.propertyDescriptors = {
 
     urls: Thing.common.urls(),
     dateFirstReleased: Thing.common.simpleDate(),
-    coverArtDate: Thing.common.simpleDate(),
 
     hasCoverArt: Thing.common.flag(true),
     hasURLs: Thing.common.flag(true),
@@ -451,9 +453,32 @@ Track.propertyDescriptors = {
         }
     },
 
+    coverArtDate: {
+        flags: {update: true, expose: true},
+
+        update: {validate: isDate},
+
+        expose: {
+            dependencies: ['albumData', 'dateFirstReleased'],
+            transform: (coverArtDate, { albumData, dateFirstReleased, [Track.instance]: track }) => (
+                coverArtDate ??
+                dateFirstReleased ??
+                albumData?.find(album => album.tracks.includes(track))?.trackArtDate ??
+                albumData?.find(album => album.tracks.includes(track))?.date ??
+                null
+            )
+        }
+    },
+
     // Previously known as: (track).artists
     artistContribs: Thing.common.dynamicContribs('artistContribsByRef'),
 
+    // Previously known as: (track).contributors
+    contributorContribs: Thing.common.dynamicContribs('contributorContribsByRef'),
+
+    // Previously known as: (track).coverArtists
+    coverArtistContribs: Thing.common.dynamicContribs('coverArtistContribsByRef'),
+
     artTags: {
         flags: {expose: true},
 
diff --git a/src/upd8.js b/src/upd8.js
index de79a0fb..d2f1b08d 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -2780,10 +2780,12 @@ async function main() {
     console.log(WD.trackData[0].name, WD.trackData[0].album.name);
     console.log(WD.albumData[0].name, WD.albumData[0].tracks[0].name);
     console.log(WD.trackData[0].artistContribs[0].who.name);
-    console.log(
-        (WD.albumData
-            .find(album => album.name === 'Alternia')
-            .artistContribs[0].who.name));
+    const demoAlbum1 = WD.albumData.find(album => album.name === 'Alternia');
+    const demoAlbum2 = WD.albumData.find(album => album.name === 'Homestuck Vol. 5');
+    console.log(demoAlbum1.artistContribs[0]?.who.name);
+    console.log(demoAlbum2.tracks[0].name,
+        demoAlbum2.tracks[0].date,
+        demoAlbum2.tracks[0].coverArtDate);
 
     return;
 
diff --git a/test/things.js b/test/things.js
new file mode 100644
index 00000000..7274515c
--- /dev/null
+++ b/test/things.js
@@ -0,0 +1,71 @@
+import test from 'tape';
+
+import {
+    Album,
+    Thing,
+    Track,
+    TrackGroup,
+} from '../src/data/things.js';
+
+function stubAlbum(tracks) {
+    const album = new Album();
+    const trackGroup = new TrackGroup();
+    trackGroup.tracksByRef = tracks.map(t => Thing.getReference(t));
+    album.trackGroups = [trackGroup];
+    album.trackData = tracks;
+    return album;
+}
+
+test(`Track.coverArtDate`, t => {
+    t.plan(5);
+
+    // Priority order is as follows, with the last (trackCoverArtDate) being
+    // greatest priority.
+    const albumDate = new Date('2010-10-10');
+    const albumTrackArtDate = new Date('2012-12-12');
+    const trackDateFirstReleased = new Date('2008-08-08');
+    const trackCoverArtDate = new Date('2009-09-09');
+
+    const track = new Track();
+    track.directory = 'foo';
+
+    const album = stubAlbum([track]);
+
+    track.albumData = [album];
+
+    // 1. coverArtDate defaults to null
+
+    t.is(track.coverArtDate, null);
+
+    // 2. coverArtDate inherits album release date
+
+    album.date = albumDate;
+
+    // XXX clear cache so change in album's property is reflected
+    track.albumData = [];
+    track.albumData = [album];
+
+    t.is(track.coverArtDate, albumDate);
+
+    // 3. coverArtDate inherits album trackArtDate
+
+    album.trackArtDate = albumTrackArtDate;
+
+    // XXX clear cache again
+    track.albumData = [];
+    track.albumData = [album];
+
+    t.is(track.coverArtDate, albumTrackArtDate);
+
+    // 4. coverArtDate is overridden dateFirstReleased
+
+    track.dateFirstReleased = trackDateFirstReleased;
+
+    t.is(track.coverArtDate, trackDateFirstReleased);
+
+    // 5. coverArtDate is overridden coverArtDate
+
+    track.coverArtDate = trackCoverArtDate;
+
+    t.is(track.coverArtDate, trackCoverArtDate);
+});