← back to Rc Fm Callnotes

test-run.mjs

84 lines

// End-to-end test with TEST DATA ONLY (all records marked "TEST — DELETE ME").
// Exercises: client match -> note append, no-match -> client create,
// invoice reference -> Internal notes stamp, RC caller-ID contact create/verify/delete.
import { createRecord, findRecords, getRecord, deleteRecordSafe } from './lib/fm-test-helpers.js';
import { processCall } from './sync-calls.mjs';
import { listContacts, deleteContact } from './lib/rc.js';
import { normPhone } from './lib/fm.js';

const results = [];
const say = (s) => { console.log(s); results.push(s); };

// --- setup: TEST client + TEST invoice ---
say('1) Creating TEST client (Clients file)…');
const testClient = await createRecord('Clients', 'Main Menu Copy', {
  Company: 'TEST — DELETE ME (Claude call-notes)',
  Name: 'Test Client',
  Phone: '(818) 555-0142',
  Notes: '',
}, { dryRun: false });
say(`   created recordId ${testClient.recordId}`);

say('2) Creating TEST invoice 999998 (invoice file)…');
const testInvoice = await createRecord('invoice', 'PACKING LIST !!', {
  Invoice: '999998',
  'SOLD NAME': 'TEST — DELETE ME (Claude call-notes)',
  'Internal notes': '',
}, { dryRun: false });
say(`   created recordId ${testInvoice.recordId}`);

// --- test A: known caller -> note appended to existing client ---
say('3) Simulating inbound call from the TEST client number (818-555-0142)…');
const callA = {
  id: 'TEST-CALL-A', direction: 'Inbound', result: 'Accepted', duration: 245,
  startTime: new Date().toISOString(),
  from: { phoneNumber: '+18185550142', name: 'TEST CALLER' },
  to: { phoneNumber: '+18883734564' },
};
say('   ' + JSON.stringify(await processCall(callA)));

// --- test B: unknown caller -> new client created ---
say('4) Simulating inbound call from an UNKNOWN number (818-555-0177)…');
const callB = {
  id: 'TEST-CALL-B', direction: 'Inbound', result: 'Missed', duration: 14,
  startTime: new Date().toISOString(),
  from: { phoneNumber: '+18185550177', name: 'TEST UNKNOWN DELETE ME' },
  to: { phoneNumber: '+18883734564' },
};
say('   ' + JSON.stringify(await processCall(callB)));

// --- test C: call note mentioning the TEST invoice -> Internal notes stamped ---
say('5) Simulating call with a note that references invoice 999998…');
const callC = {
  id: 'TEST-CALL-C', direction: 'Inbound', result: 'Accepted', duration: 180,
  startTime: new Date().toISOString(),
  from: { phoneNumber: '+18185550142', name: 'TEST CALLER' },
  to: { phoneNumber: '+18883734564' },
  _noteText: 'Client called about invoice 999998 — asked for ship date and tracking.',
};
say('   ' + JSON.stringify(await processCall(callC)));

// --- verify what landed ---
say('6) Reading back what landed…');
const clientBack = await getRecord('Clients', 'Main Menu Copy', testClient.recordId);
say('   TEST client Notes now:\n' + clientBack.fieldData.Notes.split('\n').map((l) => '   | ' + l).join('\n'));
const invBack = await getRecord('invoice', 'PACKING LIST !!', testInvoice.recordId);
say('   TEST invoice Internal notes now:\n' + String(invBack.fieldData['Internal notes']).split('\n').map((l) => '   | ' + l).join('\n'));

// --- clean up RC test contacts (leave FM records for Steve to eyeball) ---
say('7) Cleaning TEST contacts out of the RingCentral address book…');
let cleaned = 0;
for (let page = 1; page <= 40; page++) {
  const res = await listContacts({ page, perPage: 250 });
  for (const c of res.records || []) {
    const nums = [c.businessPhone, c.mobilePhone, c.homePhone].map(normPhone);
    if (nums.includes('8185550142') || nums.includes('8185550177')) {
      await deleteContact(c.id); cleaned++;
      say(`   deleted RC test contact #${c.id} (${c.firstName || ''} ${c.lastName || ''})`);
    }
  }
  if (!res.navigation?.nextPage) break;
}
say(`   ${cleaned} RC test contact(s) removed — live address book left untouched.`);
say('DONE — FileMaker TEST records left in place for review (all marked TEST — DELETE ME).');