Skip to content

Commit

Permalink
test(caa upload): update Amazon tests
Browse files Browse the repository at this point in the history
The example release has 5 images, but only 4 thumbnails are shown on
amazon.com by default. Using the embedded JavaScript we can extract
larger images whose filenames contain different identifiers.
Additionally we can also detect the type of the front and the back.
Still testing the thumbnail grabbing (which is now only a fallback) by
mocking the failed attempt of extracting images from the embedded JS.
  • Loading branch information
kellnerd committed Oct 18, 2021
1 parent f04f673 commit 98699b6
Show file tree
Hide file tree
Showing 8 changed files with 610 additions and 299 deletions.
4 changes: 2 additions & 2 deletions src/mb_enhanced_cover_art_uploads/providers/amazon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class AmazonProvider extends CoverArtProvider {

// For physical products we have to extract the embedded JS from the
// page source to get all images in their highest available resolution.
let covers = this.#extractFromEmbeddedJS(pageContent);
let covers = this.extractFromEmbeddedJS(pageContent);
if (!covers) {
// Use the (smaller) image thumbnails in the sidebar as a fallback,
// although it might not contain all of them. IMU will maximise,
Expand Down Expand Up @@ -82,7 +82,7 @@ export class AmazonProvider extends CoverArtProvider {
}];
}

#extractFromEmbeddedJS(pageContent: string): CoverArt[] | undefined {
extractFromEmbeddedJS(pageContent: string): CoverArt[] | undefined {
const embeddedImages = pageContent.match(/^'colorImages': { 'initial': (.+)},$/m)?.[1];
if (embeddedImages) {
try {
Expand Down
29 changes: 24 additions & 5 deletions tests/mb_enhanced_cover_art_uploads/providers/amazon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,38 @@ describe('amazon provider', () => {
.toBe(expectedId);
});

it.each`
url | desc
${'https://www.amazon.com/dp/B07QWNQT8X'} | ${'dp URLs'}
${'https://www.amazon.com/gp/product/B07QWNQT8X'} | ${'gp URLs'}
`('grabs all images for physical products on $desc', async ({ url }: { url: string }) => {
const extractionCases = [
['dp URLs', 'https://www.amazon.com/dp/B07QWNQT8X'],
['gp URLs', 'https://www.amazon.com/gp/product/B07QWNQT8X'],
];

it.each(extractionCases)('grabs all images for physical products from the embedded JS on %s', async (_1, url) => {
const covers = await provider.findImages(new URL(url));

expect(covers).toBeArrayOfSize(5);
expect(covers[0].url.pathname).toContain('81bqssuW6LL');
expect(covers[0].types).toStrictEqual([ArtworkTypeIDs.Front]);
expect(covers[1].url.pathname).toContain('61jZYB6BJYL');
expect(covers[1].types).toStrictEqual([ArtworkTypeIDs.Back]);
expect(covers[2].url.pathname).toContain('71TLgC33KgL');
expect(covers[3].url.pathname).toContain('81JCfIAZ71L');
expect(covers[4].url.pathname).toContain('816dglIIJHL');
});

it.each(extractionCases)('falls back to thumbnail grabbing for physical products on %s', async (_1, url) => {
// mock the failed attempt of extracting images from embedded JS to trigger the thumbnail fallback
const spy = jest.spyOn(provider, 'extractFromEmbeddedJS').mockImplementationOnce(() => undefined);
const covers = await provider.findImages(new URL(url));

expect(covers).toBeArrayOfSize(4);
expect(covers[0].url.pathname).toContain('51nM1ikLWPL');
expect(covers[0].types).toStrictEqual([ArtworkTypeIDs.Front]);
expect(covers[1].url.pathname).toContain('41RQivjYeeL');
expect(covers[1].types).toStrictEqual([ArtworkTypeIDs.Back]);
expect(covers[2].url.pathname).toContain('31-n8wloCcL');
expect(covers[3].url.pathname).toMatch(/41MN(?:%2B|\+)eLL(?:%2B|\+)JL/);

spy.mockRestore();
});

it('returns no covers for product without images', async () => {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

0 comments on commit 98699b6

Please sign in to comment.