All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 50s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
2.4 KiB
TypeScript
93 lines
2.4 KiB
TypeScript
import "../setup.ts";
|
|
import { assert, assertEquals } from "@std/assert";
|
|
import { parseTralbum } from "../../remote/bandcamp-tralbum.ts";
|
|
|
|
function escapeAttr(s: string): string {
|
|
return s.replaceAll("&", "&").replaceAll('"', """);
|
|
}
|
|
|
|
function tralbumPage(data: unknown): string {
|
|
return `<div id="player" data-tralbum="${
|
|
escapeAttr(JSON.stringify(data))
|
|
}"></div>`;
|
|
}
|
|
|
|
const STREAM = "https://t4.bcbits.com/stream/abc/mp3-128/1?p=0&ts=1&t=x";
|
|
|
|
const ALBUM = {
|
|
item_type: "album",
|
|
artist: "Artist",
|
|
art_id: 12345,
|
|
current: { title: 'The "Album" & more' },
|
|
trackinfo: [
|
|
{
|
|
track_num: 1,
|
|
title: "One",
|
|
duration: 180.5,
|
|
streaming: 1,
|
|
file: { "mp3-128": STREAM },
|
|
},
|
|
{
|
|
track_num: 2,
|
|
title: "Preorder",
|
|
streaming: 1,
|
|
unreleased_track: true,
|
|
file: null,
|
|
},
|
|
{
|
|
track_num: 3,
|
|
title: "",
|
|
streaming: 1,
|
|
file: { "mp3-128": "https://evil.test/x.mp3" },
|
|
},
|
|
{
|
|
track_num: 4,
|
|
title: "Capped",
|
|
streaming: 1,
|
|
is_capped: true,
|
|
file: { "mp3-128": STREAM },
|
|
},
|
|
],
|
|
};
|
|
|
|
Deno.test("parseTralbum: tracks, streamability and art", () => {
|
|
const album = parseTralbum(
|
|
tralbumPage(ALBUM),
|
|
"https://a.bandcamp.com/album/x",
|
|
);
|
|
assert(album);
|
|
assertEquals(album.itemType, "album");
|
|
assertEquals(album.artist, "Artist");
|
|
// A title containing a literal & survives the single-pass decode.
|
|
assertEquals(album.title, 'The "Album" & more');
|
|
assertEquals(album.artworkUrl, "https://f4.bcbits.com/img/a12345_16.jpg");
|
|
assertEquals(album.tracks.map((t) => t.streamable), [
|
|
true,
|
|
false,
|
|
false,
|
|
true,
|
|
]);
|
|
assertEquals(album.tracks[0].streamUrl, STREAM);
|
|
assertEquals(album.tracks[0].duration, 180.5);
|
|
// A non-bcbits stream host is never relayed.
|
|
assertEquals(album.tracks[2].streamUrl, null);
|
|
assertEquals(album.tracks[2].title, "Track 3");
|
|
assert(album.tracks[3].capped);
|
|
});
|
|
|
|
Deno.test("parseTralbum: pages it can't use", () => {
|
|
assertEquals(parseTralbum("<html></html>", "u"), null);
|
|
assertEquals(parseTralbum(`<div data-tralbum="{oops"></div>`, "u"), null);
|
|
assertEquals(
|
|
parseTralbum(tralbumPage({ ...ALBUM, hasAudio: false }), "u"),
|
|
null,
|
|
);
|
|
assertEquals(
|
|
parseTralbum(
|
|
tralbumPage({ ...ALBUM, trackinfo: [ALBUM.trackinfo[1]] }),
|
|
"u",
|
|
),
|
|
null,
|
|
);
|
|
});
|