← back to Dw Rotation Activator

test/five-field-extra.test.js

89 lines

'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const { fiveFieldExtra } = require('../lib/five-field-extra.js');

const product = (variants, tags = ['wallpaper', 'modern']) => ({ variants: { nodes: variants }, tags });

test('passes sample plus positive-price sellable variant', () => {
  assert.deepEqual(fiveFieldExtra(product([
    { sku: 'DW-100-SAMPLE', price: '5.00' }, { sku: 'DW-100', price: '125.00' },
  ])), { ok: true, reasons: [] });
});

test('fails closed without a sample', () => {
  assert.deepEqual(fiveFieldExtra(product([{ sku: 'DW-100', price: '125.00' }])), {
    ok: false, reasons: ['no-sample-variant'],
  });
});

test('rejects missing, zero, malformed, and negative sellable prices', () => {
  for (const price of [undefined, '', '0', 'not-a-price', '-1', '125 USD', '1e309']) {
    const result = fiveFieldExtra(product([
      { sku: 'DW-100-SAMPLE', price: '5.00' }, { sku: 'DW-100', price },
    ]));
    assert.equal(result.ok, false, `price ${String(price)} should fail`);
    assert.ok(result.reasons.includes('sellable-price-not-gt-0'));
  }
});

test('blocks the $4.25 sample-leak price on the sellable variant (TK-10456)', () => {
  // a "sellable" roll stuck at the memo-sample price 4.25 is the leak, not a real price
  const leak = fiveFieldExtra(product([
    { sku: 'DW-100-Sample', price: '4.25' }, { sku: 'DW-100-Roll', price: '4.25' },
  ]));
  assert.equal(leak.ok, false);
  assert.ok(leak.reasons.includes('sellable-price-is-sample-leak-4.25'));

  // a real price alongside a 4.25 sample still passes
  const real = fiveFieldExtra(product([
    { sku: 'DW-100-Sample', price: '4.25' }, { sku: 'DW-100-Roll', price: '384.00' },
  ]));
  assert.deepEqual(real, { ok: true, reasons: [] });

  // mixed: a real sellable + a leaked sellable still passes (any real price is enough)
  const mixed = fiveFieldExtra(product([
    { sku: 'DW-100-Sample', price: '4.25' }, { sku: 'DW-100-Roll', price: '4.25' },
    { sku: 'DW-100-DR', price: '768.00' },
  ]));
  assert.equal(mixed.ok, true);
});

test('fails closed on non-array variants and tags with explicit shape reasons', () => {
  const variants = [{ sku: 'DW-100-SAMPLE', price: '5.00' }, { sku: 'DW-100', price: '125.00' }];
  const tagsString = fiveFieldExtra({ variants: { nodes: variants }, tags: 'wallpaper' });
  assert.equal(tagsString.ok, false);
  assert.ok(tagsString.reasons.includes('invalid-tags-shape'));

  const quoteString = fiveFieldExtra({ variants: { nodes: [variants[0]] }, tags: 'quotes' });
  assert.equal(quoteString.ok, false);
  assert.ok(quoteString.reasons.includes('invalid-tags-shape'));
  assert.ok(quoteString.reasons.includes('no-sellable-variant'));
  assert.ok(quoteString.reasons.includes('sellable-price-not-gt-0'));

  const variantsString = fiveFieldExtra({ variants: { nodes: 'not-an-array' }, tags: ['quotes', 'wallpaper'] });
  assert.equal(variantsString.ok, false);
  assert.ok(variantsString.reasons.includes('invalid-variants-shape'));
});

test('normalizes valid tag arrays before applying the quote exemption', () => {
  assert.deepEqual(fiveFieldExtra(product([
    { sku: 'DW-QUOTE-SAMPLE', price: '5.00' },
  ], [' Quotes ', 'Wallpaper'])), { ok: true, reasons: [] });
});

test('requires at least two tags', () => {
  assert.deepEqual(fiveFieldExtra(product([
    { sku: 'DW-100-SAMPLE', price: '5.00' }, { sku: 'DW-100', price: '125.00' },
  ], ['wallpaper'])), { ok: false, reasons: ['fewer-than-2-tags'] });
});

test('quote-only products may be sample-only but still need sample and tags', () => {
  assert.deepEqual(fiveFieldExtra(product([
    { sku: 'DW-QUOTE-SAMPLE', price: '5.00' },
  ], ['quotes', 'wallpaper'])), { ok: true, reasons: [] });
  assert.deepEqual(fiveFieldExtra(product([], ['quotes', 'wallpaper'])), {
    ok: false, reasons: ['no-sample-variant'],
  });
});