wbxcallprov/test/locations.test.js
jmcqueen 261d11f29d
Some checks are pending
CI / verify (push) Waiting to run
Harden /finalizeStore against pending-order and DECT-404 noise
A finalize run against a store whose phone number still had a pending
carrier order surfaced three unrelated-looking 400s (add-number,
caller ID = LOCATION_NUMBER, auto attendant "number already used")
that all traced back to the number not being attached, plus a
spurious "DECT network pre-check failed" warn when the location had
never had a DECT network before.

Fixes:
- addPhoneNumbersToLocation now translates the raw
  NUMBER_HAS_PENDING_ORDERS payload into an actionable operator
  message ("wait for the carrier order, then re-run /finalizeStore").
  Original error preserved as .cause. Extracted as pure
  translateAddNumberError so it can be unit-tested.
- finalizeStore marks the phone-number-add step { critical: true } so
  finalize aborts immediately on that failure instead of cascading
  three downstream errors that bury the root cause.
- findDectNetworkInLocation treats HTTP 404 as "no networks yet"
  (Webex returns 404 for that state, not an empty list), so the
  finalize pre-check stops warning on the normal first-time path.
  The remaining warn now includes status code for anything that does
  reach it.

Tests: 4 new cases in test/locations.test.js locking down the
translator behavior against the exact Webex payload shape.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-09 12:35:00 -04:00

59 lines
2.8 KiB
JavaScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { translateAddNumberError } from '../src/webex/locations.js';
// The exact JSON Webex returns when a freshly-purchased number is
// still being provisioned by the carrier. Kept verbatim from a real
// error so this test breaks loudly if Webex ever changes the payload
// shape (and we can update our detection to match).
const PENDING_ORDERS_BODY =
'[{"errors":[{"errorType":"VERIFICATION","errorCode":"ERR.V.TRM.TMN60027","errorMessage":"Number has pending orders.","errorTitle":"NUMBER_HAS_PENDING_ORDERS"}]}]';
function fakeHttpError(status, body) {
const err = new Error(`HTTP ${status} for POST /whatever\n${body}`);
err.status = status;
err.body = body;
return err;
}
describe('translateAddNumberError', () => {
// Regression: without the translation, the raw Webex error was
// propagated through runStep, cascaded into a Caller-ID failure
// (LOCATION_NUMBER unavailable) and an auto-attendant failure
// (number "used in another location"), and the actionable root
// cause was buried under three stack traces.
it('rewrites NUMBER_HAS_PENDING_ORDERS into an actionable operator message', () => {
const raw = fakeHttpError(400, PENDING_ORDERS_BODY);
const translated = translateAddNumberError(raw, '+14045551212');
assert.notEqual(translated, raw, 'should be a new Error instance');
assert.match(translated.message, /pending order/i);
assert.match(translated.message, /re-run \/finalizeStore/);
assert.match(translated.message, /\+14045551212/);
assert.equal(translated.status, 400);
assert.equal(translated.cause, raw, 'raw error should be preserved as .cause');
});
it('returns unrelated errors unchanged (same instance, no message rewrite)', () => {
const raw = fakeHttpError(400, '{"message":"Nope","errorCode":9999}');
const result = translateAddNumberError(raw, '+14045551212');
assert.equal(result, raw, 'must be same instance so callers can rethrow');
assert.doesNotMatch(result.message, /pending order/i);
});
it('tolerates errors with no body property (e.g. transport failures)', () => {
const raw = new Error('ENOTFOUND webexapis.com');
const result = translateAddNumberError(raw, '+14045551212');
assert.equal(result, raw);
});
it('does not misfire on other 400 payloads that happen to be arrays', () => {
// Same array-of-errors shape but a different error code.
const raw = fakeHttpError(
400,
'[{"errors":[{"errorCode":"ERR.OTHER","errorMessage":"unrelated"}]}]',
);
const result = translateAddNumberError(raw, '+14045551212');
assert.equal(result, raw);
});
});