securityos/node_modules/music-metadata-browser/lib/index.spec.js

111 lines
5.1 KiB
JavaScript
Raw Normal View History

2024-09-06 15:32:35 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
localStorage.debug = 'music-metadata-browser:*';
const http = require("stream-http");
const mm = require("./index");
const testData = require("../test/test-data");
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
function httpGetByUrl(url) {
// Assume URL
return new Promise(resolve => {
http.get(url, stream => {
resolve(stream);
});
});
}
const urlInBloom = 'https://raw.githubusercontent.com/Borewit/music-metadata/master/test/samples/Nirvana - In Bloom - 2-sec.ogg';
function getAsBlob(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'blob'; // force the HTTP response, response-type header to be blob
xhr.onload = () => {
resolve(xhr.response); // xhr.response is now a blob object
};
xhr.onerror = () => {
reject(new Error(`Failed download url=${url}`));
};
xhr.send();
});
}
const parsers = [
{
methodDescription: 'parseStream()',
parseUrl: async (audioTrackUrl, options) => {
const stream = await httpGetByUrl(audioTrackUrl);
return mm.parseNodeStream(stream, stream.type, options);
}
},
{
methodDescription: 'parseBlob()',
parseUrl: async (audioTrackUrl, options) => {
const blob = await getAsBlob(audioTrackUrl);
return mm.parseBlob(blob, options);
}
},
{
methodDescription: 'parseBlob() without blob.stream being implemented',
parseUrl: async (audioTrackUrl, options) => {
const blob = await getAsBlob(audioTrackUrl);
blob.stream = undefined; // Simulate `stream()` not being implemented by browser (e.g. Safari < 14.1)
return mm.parseBlob(blob, options);
}
},
{
methodDescription: 'fetchFromUrl()',
parseUrl: (audioTrackUrl, options) => {
return mm.fetchFromUrl(audioTrackUrl, options);
}
}
];
xdescribe('music-metadata-browser', () => {
describe('Parse Ogg audio track: Nirvana - In Bloom', () => {
parsers.forEach(parser => {
it(parser.methodDescription, async () => {
const metadata = await parser.parseUrl(urlInBloom);
expect(metadata).toBeDefined();
expect(metadata.format.tagTypes).toEqual(['vorbis'], 'expect Vorbis metadata header');
expect(metadata.format.duration).toEqual(2.0, 'duration should be 2.0 sec');
expect(metadata.format.sampleRate).toEqual(44100, 'sample-rate should be 44.1 kHz');
expect(metadata.format.numberOfChannels).toEqual(2, 'number of channels should be 2 (stereo)');
expect(metadata.format.bitrate).toEqual(64000, 'bitrate should be 64 kbit/sec');
expect(metadata.common.title).toEqual('In Bloom');
expect(metadata.common.artist).toEqual('Nirvana');
expect(metadata.common.albumartist).toEqual('Nirvana', 'common.albumartist');
expect(metadata.common.album).toEqual('Nevermind', 'common.album');
expect(metadata.common.year).toEqual(1991, 'common.year');
expect(metadata.common.track).toEqual({ no: 2, of: 12 }, 'common.track');
expect(metadata.common.disk).toEqual({ no: 1, of: 1 }, 'common.disk');
expect(metadata.common.genre).toEqual(['Grunge', 'Alternative'], 'genre');
expect(metadata.common.picture[0].format).toEqual('image/jpeg', 'picture format');
expect(metadata.common.picture[0].data.length).toEqual(30966, 'picture length');
expect(metadata.common.barcode).toEqual('0720642442524', 'common.barcode (including leading zero)');
expect(metadata.common.asin).toEqual('B000003TA4', 'common.asin');
expect(metadata.common.catalognumber).toEqual(['GED24425'], 'common.asin');
expect(metadata.common.isrc).toEqual(['USGF19942502'], 'common.isrc');
// Make sure the orderTags is working
const vorbisTags = mm.orderTags(metadata.native.vorbis);
expect(vorbisTags.TRACKNUMBER).toEqual(['2'], 'vorbis.TRACKNUMBER');
expect(vorbisTags.TRACKTOTAL).toEqual(['12'], 'vorbis.TRACKTOTAL');
}, 5000);
});
});
it('Should expose the `ratingToStars()`', () => {
expect(mm.ratingToStars(1.0)).toEqual(5);
});
});
describe('Parse Tiuqottigeloot Vol 24 tracks', () => {
parsers.forEach(parser => {
describe(`Parser: ${parser.methodDescription}`, () => {
testData.tracks.forEach(track => {
it(`track ${track.metaData.artist} - ${track.metaData.title}`, async () => {
const url = testData.providers.netlify.getUrl(track);
const metadata = await parser.parseUrl(url);
expect(metadata.common.artist).toEqual(track.metaData.artist);
expect(metadata.common.title).toEqual(track.metaData.title);
});
});
});
});
});