/* What a public Apple Music page says about itself, and which links this API is willing to fetch. * * The parser is best-effort by design — Apple's markup is theirs to change — so these tests are * about the two things that must not wobble: the schema block is read when it is there, the Open * Graph tags take over when it is not, and a page that is not a playlist at all returns nothing * rather than throwing. The allowlist gets the same treatment, because that one is a boundary * rather than a convenience. */ import { test } from 'node:test'; import assert from 'node:assert/strict'; import { parseMusicPage, hostAllowed } from '../music.js'; const PAGE = ` Leg Day`; test('reads the schema block first, and asks Apple\'s CDN for one useful size', () => { const m = parseMusicPage(PAGE); assert.equal(m.title, "Leg Day '24"); assert.equal(m.tracks, 42); assert.equal(m.curator, 'Ana'); assert.equal(m.artwork, 'https://is1-ssl.mzstatic.com/image/thumb/Feature/abc/600x600bb.jpg'); }); test('falls back to Open Graph, and never invents what it cannot read', () => { const noSchema = PAGE.replace(//, ''); const m = parseMusicPage(noSchema); assert.equal(m.title, 'Leg Day — Playlist by Ana'); assert.ok(m.artwork.endsWith('600x600bb.jpg')); assert.equal(m.tracks, null); assert.equal(m.curator, null); // A schema block that is there but unparseable is the same as one that is not: half a JSON object // must not take the title down with it. const broken = parseMusicPage(PAGE.replace('"numTracks":42,', '"numTracks":,')); assert.equal(broken.title, 'Leg Day — Playlist by Ana'); assert.equal(broken.tracks, null); }); test('says nothing rather than throwing when the page is not a playlist', () => { const empty = { title: null, artwork: null, tracks: null, curator: null }; assert.deepEqual(parseMusicPage('Sign in to Apple Music'), empty); assert.deepEqual(parseMusicPage(''), empty); assert.deepEqual(parseMusicPage(null), empty); // The `content` before `property` order is real markup too, and is matched. assert.equal(parseMusicPage('').title, 'X'); }); test('only Apple\'s own music hosts, over https — this is a boundary, not a preference', () => { assert.equal(hostAllowed('https://music.apple.com/us/playlist/x/pl.1'), true); assert.equal(hostAllowed('https://itunes.apple.com/us/album/x/1'), true); assert.equal(hostAllowed('https://geo.music.apple.com/us/playlist/x/pl.1'), true); // The interesting ones: the api container sits on the owner's network, so anything that could // name a machine on it is refused — a lookalike domain, a LAN address, localhost, plain http, and // a scheme that is not http at all. assert.equal(hostAllowed('https://music.apple.com.evil.test/us/playlist/x/pl.1'), false); assert.equal(hostAllowed('http://music.apple.com/us/playlist/x/pl.1'), false); assert.equal(hostAllowed('http://127.0.0.1:3000/api/health'), false); assert.equal(hostAllowed('http://192.168.1.1/'), false); assert.equal(hostAllowed('http://169.254.169.254/latest/meta-data/'), false); assert.equal(hostAllowed('file:///etc/passwd'), false); assert.equal(hostAllowed('gopher://music.apple.com/'), false); assert.equal(hostAllowed('not a url'), false); assert.equal(hostAllowed(''), false); });