'use strict';
const assert = require('assert');
const Parser = require('../server.js');

(function run() {
  const source = Parser.SOURCES.shire3d_com;
  const html = `<!doctype html><html><head>
    <meta property="og:title" content="Dragon Bowl | Shire 3D">
    <script type="application/ld+json">{
      "@context":"https://schema.org","@type":"Product","name":"Dragon Yarn Bowl",
      "description":"A useful &amp; dramatic yarn bowl.","sku":"DRAGON-1",
      "image":"https://shire3d.com/dragon.jpg",
      "offers":{"@type":"Offer","price":"35.00","priceCurrency":"AUD","availability":"https://schema.org/InStock"},
      "url":"https://shire3d.com/product/dragon/DRAGON-1"
    }</script></head><body>
    <a href="/product/skull/SKULL-2" aria-label="Skull Planter AU$ 29.00"><img src="/skull.jpg" alt="Skull Planter"></a>
  </body></html>`;
  const ld = Parser.collectJsonLdProducts(html, 'https://shire3d.com/s/shop');
  assert.equal(ld.length, 1);
  assert.equal(ld[0].name, 'Dragon Yarn Bowl');
  assert.equal(ld[0].price, 35);
  assert.equal(ld[0].externalId, 'DRAGON-1');

  const anchors = Parser.extractAnchorProducts(html, 'https://shire3d.com/s/shop', source);
  assert.equal(anchors.length, 1);
  assert.equal(anchors[0].externalId, 'SKULL-2');
  assert(anchors[0].listingUrl.startsWith('https://shire3d.com/product/'));

  const merged = Parser.mergeProducts([...ld, { ...ld[0], description: '', price: 0 }]);
  assert.equal(merged.length, 1);
  assert.equal(merged[0].price, 35);

  console.log('PASS: website parser tests');
})();
