← back to Dw Signup Fulfillment

verification/tk11285/stubs.js

39 lines

'use strict';
// Repro harness stub loader. Intercepts lib/email + lib/shopify so the REAL
// production code paths run with ZERO external calls (no George send, no Shopify).
// lib/trade.js and scripts/recover-stuck-apps.js are NOT stubbed - they are the
// code under test and run verbatim.
const Module = require('module');
const path = require('path');
const origLoad = Module._load;
const SEND_MS = parseInt(process.env.REPRO_SEND_MS || '250', 10);
const sleep = ms => new Promise(r => setTimeout(r, ms));

const emailStub = {
  // Simulates a REAL (non-dryRun) George send that takes time - this is the
  // read->write window that the recover script holds its stale snapshot across.
  async sendEmail() { await sleep(SEND_MS); return { ok: true, status: 200, dryRun: false }; },
  designerAccountReadyEmail: () => ({ subject: 's', html: 'h' }),
  tradeApplicationEmail: () => ({ subject: 's', html: 'h' }),
  repNotifyEmail: () => ({ subject: 's', html: 'h' }),
  tradeApprovedEmail: () => ({ subject: 's', html: 'h' }),
  repAssignmentEmail: () => ({ subject: 's', html: 'h' }),
  tradeRejectedEmail: () => ({ subject: 's', html: 'h' }),
};
const shopifyStub = {
  async findCustomerByEmail() { return '5551111'; },
  async findOrCreateCustomer() { return { ok: true, id: '5551111', via: 'existing', created: false }; },
  async addTags(id) { return { ok: true, status: 200, json: { customer: { id, tags: 'trade,trade_approved' } } }; },
  async setCustomerMetafield(id, mf) { return { ok: true, status: 200, json: { metafield: { id: 'gid://shopify/Metafield/1', namespace: 'custom', key: 'assigned_rep', value: mf.value, type: mf.type, owner_id: id } } }; },
  async getCustomer(id) { return { ok: true, status: 200, json: { customer: { id, tags: 'trade,trade_approved', metafields: [] } } }; },
  async graphql() { return { ok: true, status: 200, json: { data: {} } }; },
};
Module._load = function (request, parent, isMain) {
  const resolved = (() => { try { return Module._resolveFilename(request, parent, isMain); } catch { return ''; } })();
  const base = path.basename(resolved);
  const dir = path.basename(path.dirname(resolved));
  if (dir === 'lib' && base === 'email.js') return emailStub;
  if (dir === 'lib' && base === 'shopify.js') return shopifyStub;
  return origLoad.apply(this, arguments);
};