'use strict';

const path = require('path');
const { promisify } = require('util');
const readFile = promisify(require('fs').readFile);
const q = require('qunit');
const { client, usePlayer, config, tracks } = require('./test_context');

function getFile(name)
{
    return readFile(path.join(config.musicDir, name), null);
}

function getArtwork(playlist, item)
{
    return client.handler.axios.get(`api/artwork/${playlist}/${item}`, {
        responseType: 'arraybuffer',
        validateStatus: () => true,
    });
}

q.module('artwork 1', usePlayer());

q.test('get from folder', async assert =>
{
    await client.addPlaylistItems(0, [tracks.t1]);
    await client.waitPlaybackMetadata();
    
    const expected = await getFile('cover-black.png');
    const response = await getArtwork(0, 0);

    assert.equal(response.status, 200);
    assert.ok(response.data.equals(expected));
});

q.module('artwork 2', usePlayer());

q.test('get from tag', async assert =>
{
    await client.addPlaylistItems(0, [tracks.t2]);
    await client.waitPlaybackMetadata();

    const expected = await getFile('cover-white.png.hidden');
    const response = await getArtwork(0, 0);

    assert.equal(response.status, 200);
    assert.ok(response.data.equals(expected));
});

q.module('artwork 3', usePlayer());

q.test('missing', async assert =>
{
    await client.addPlaylistItems(0, [tracks.t3]);
    await client.waitPlaybackMetadata();

    const response = await getArtwork(0, 0);
    assert.equal(response.status, 404);
});

@@ -688,20 +688,16 @@
 data();%0A
-    
 %0A    con
@@ -822,32 +822,97 @@
 e.status, 200);%0A
+    assert.equal(response.headers%5B'content-type'%5D, 'image/png');%0A
     assert.ok(re
@@ -1258,32 +1258,97 @@
 e.status, 200);%0A
+    assert.equal(response.headers%5B'content-type'%5D, 'image/png');%0A
     assert.ok(re

validate response content type