← back to Tk 10965 Zero Price Analysis
tk10964-canary-guard/test.mjs
50 lines
#!/usr/bin/env node
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { SEARCHES, exitCodeFor, hydrateVariantPages, inScope, summarize } from './check.mjs';
const products = JSON.parse(fs.readFileSync(new URL('./fixtures/mixed.json', import.meta.url)));
const result = summarize(products, '2026-08-30T00:00:00.000Z');
assert.equal(SEARCHES.length, 3);
assert.ok(SEARCHES.some(search => search.includes("tag:'quotes'") && search.includes("tag:'Needs-Price'")));
assert.ok(SEARCHES.some(search => search.includes("vendor:'Fentucci Naturals'")));
assert.equal(inScope(products.at(-1)), false, 'search-parser over-return must be rejected locally');
assert.equal(result.searched_active_unique, 4, 'duplicate query hits must collapse by product GID');
assert.equal(result.zero_price_orderable, 2);
assert.deepEqual(result.by_vendor, { 'Fentucci Naturals': 1, 'Phillipe Romano': 1 });
assert.deepEqual(result.mechanism, { qty_2026: 2, deny: 2, tracked: 2 });
assert.equal(result.verdict, 'WARN');
assert.equal(exitCodeFor({ verdict: 'PASS' }), 0);
assert.equal(exitCodeFor({ verdict: 'WARN' }), 2);
assert.equal(exitCodeFor({ verdict: 'FAIL' }), 3);
const firstTwenty = Array.from({ length: 20 }, (_, index) => ({ title: `Roll ${index + 1}`, price: '100', availableForSale: true }));
const variant21Product = {
id: 'gid://shopify/Product/21', title: 'Variant 21 boundary', vendor: 'Fentucci Naturals', tags: [],
variants: { nodes: firstTwenty, pageInfo: { hasNextPage: true, endCursor: 'cursor-20' } }
};
let pageCalls = 0;
await hydrateVariantPages([variant21Product], async (_query, variables) => {
pageCalls++;
assert.deepEqual(variables, { id: variant21Product.id, after: 'cursor-20' });
return { product: { variants: { pageInfo: { hasNextPage: false, endCursor: 'cursor-21' }, nodes: [{ title: 'Roll 21', price: '0', availableForSale: true, inventoryPolicy: 'DENY', inventoryQuantity: 2026, inventoryItem: { tracked: true } }] } } };
});
const variant21Result = summarize([variant21Product]);
assert.equal(pageCalls, 1, 'only products with another variant page should incur a follow-up read');
assert.equal(variant21Result.zero_price_orderable, 1, 'bad variant #21 must not be a false negative');
const productionArtifact = fileURLToPath(new URL('./data/latest.json', import.meta.url));
const productionBefore = fs.readFileSync(productionArtifact, 'utf8');
const fixtureRun = spawnSync(process.execPath, [
fileURLToPath(new URL('./check.mjs', import.meta.url)),
'--fixture',
fileURLToPath(new URL('./fixtures/mixed.json', import.meta.url))
], { encoding: 'utf8' });
assert.equal(fixtureRun.status, 2, fixtureRun.stderr);
assert.equal(fs.readFileSync(productionArtifact, 'utf8'), productionBefore, 'fixture CLI must not overwrite production heartbeat');
assert.match(fixtureRun.stdout, /^WARN: 2 zero-price-orderable/m);
console.log('PASS: broad searches, Fentucci blind spot, dedupe, pagination, controls, and fixture heartbeat isolation');