Modular logistics and supply chain operating system (LSOS)
JavaScript driver-appfleet-managementfreight-managementinfrastructureinventory-managementlast-mile-deliverylogisticsondemandopen-sourceorder-managementroute-optimizationsupply-chain-managementtracking-and-monitoringtransport-management-systemtransportationwarehouse-management
test(console): cover the services panel tasks and SMS normalisation
Ronald A. Richardsoncb1637a2e90b
diff --git a/console/config/coverage.js b/console/config/coverage.jsindex 3d87d584..7f0b9bb2 100644--- a/console/config/coverage.js+++ b/console/config/coverage.js@@ -28,7 +28,10 @@ const MODULE_PREFIX = '@fleetbase/console/';module.exports = {coverageEnvVar: 'COVERAGE',- reporters: ['lcov', 'json-summary', 'text-summary'],+ // `json` emits coverage/coverage-final.json, which carries the statement, branch and+ // function maps. That detail is what makes it possible to tell a genuinely untested+ // line from an instrumentation artifact when chasing the last few percent.+ reporters: ['lcov', 'json', 'json-summary', 'text-summary'],// Exclude generated/vendor and test files from the coverage denominator.// app/extensions/** are auto-generated extension manifests (declarative config// written by the extensions generator) — not hand-written, testable logic.diff --git a/console/tests/integration/components/configure/services-test.js b/console/tests/integration/components/configure/services-test.jsindex 3275efd1..e0e6ce25 100644--- a/console/tests/integration/components/configure/services-test.js+++ b/console/tests/integration/components/configure/services-test.js@@ -3,6 +3,8 @@ import { module, test } from 'qunit';import { setupRenderingTest } from '@fleetbase/console/tests/helpers';import { click, fillIn, render, select, waitFor } from '@ember/test-helpers';import { hbs } from 'ember-cli-htmlbars';+import ConfigureServicesComponent from '@fleetbase/console/components/configure/services';+import { captureComponent } from '@fleetbase/console/tests/helpers/capture-component';class FetchStub extends Service {lastPost = null;@@ -218,3 +220,383 @@ module('Integration | Component | configure/services', function (hooks) {});});});++/**+ * The panel's ember-concurrency tasks, error paths and SMS normalisation helpers are not+ * reachable from the rendered UI (there is no button for most of them, and the error paths+ * need a failing endpoint), so this module drives the component instance directly. Glimmer+ * components cannot be constructed by hand, so captureComponent registers a capturing+ * subclass and lets Ember build it.+ */+module('Integration | Component | configure/services | actions', function (hooks) {+ setupRenderingTest(hooks);++ hooks.beforeEach(function () {+ this.posted = [];+ this.getResponse = {};+ this.postResponse = { status: 'success' };+ this.getRejectsWith = null;+ this.postRejectsWith = null;+ const context = this;++ class ConfigurableFetchStub extends Service {+ async get() {+ if (context.getRejectsWith) {+ throw context.getRejectsWith;+ }++ return context.getResponse;+ }+ async post(url, payload) {+ context.posted.push({ url, payload });++ if (context.postRejectsWith) {+ throw context.postRejectsWith;+ }++ return context.postResponse;+ }+ }+ class RecordingNotificationsStub extends Service {+ serverErrors = [];+ serverError(error) {+ this.serverErrors.push(error);+ }+ }++ this.owner.register('service:fetch', ConfigurableFetchStub);+ this.owner.register('service:notifications', RecordingNotificationsStub);++ const captured = captureComponent(this.owner, 'configure/services', ConfigureServicesComponent);+ this.build = async () => {+ await render(hbs`+ <div id="next-view-section-subheader-actions"></div>+ <Configure::Services />+ `);++ return captured.instance;+ };+ this.notifications = () => this.owner.lookup('service:notifications');+ });++ test('a failed config load is reported instead of thrown', async function (assert) {+ const failure = new Error('services config unavailable');+ this.getRejectsWith = failure;++ const component = await this.build();++ assert.deepEqual(this.notifications().serverErrors, [failure]);+ assert.strictEqual(component.awsKey, null, 'nothing is applied from a failed load');+ });++ test('setConfigValues applies known keys and leaves the sms block alone when absent', async function (assert) {+ this.getResponse = { awsKey: 'key', awsRegion: 'eu-west-1', notAFieldOnThisComponent: 'ignored' };++ const component = await this.build();++ assert.strictEqual(component.awsKey, 'key');+ assert.strictEqual(component.awsRegion, 'eu-west-1');+ assert.strictEqual(component.notAFieldOnThisComponent, undefined, 'keys the component does not declare are dropped');+ assert.strictEqual(component.sms, null, 'no sms block means sms stays untouched');+ assert.strictEqual(component.smsSelectedProvider, 'twilio');+ });++ test('an sms block with no default provider falls back to twilio', async function (assert) {+ this.getResponse = { sms: { providers: { vonage: { api_key: 'v' } } } };++ const component = await this.build();++ assert.strictEqual(component.smsSelectedProvider, 'twilio');+ assert.strictEqual(component.sms.defaultProvider, 'twilio');+ assert.deepEqual(component.sms.routingRules, {}, 'missing routing rules normalise to an empty map');+ });++ test('save posts every service section', async function (assert) {+ const component = await this.build();++ component.awsKey = 'aws-key';+ component.awsSecret = 'aws-secret';+ component.awsRegion = 'us-east-1';+ component.ipinfoApiKey = 'ipinfo';+ component.googleMapsApiKey = 'maps-key';+ component.twilioSid = 'sid';+ component.twilioToken = 'token';+ component.twilioFrom = '+15550000000';+ component.sentryDsn = 'https://sentry.example.com/1';++ await component.save.perform();++ const { url, payload } = this.posted.at(-1);+ assert.strictEqual(url, 'settings/services-config');+ assert.deepEqual(payload.aws, { key: 'aws-key', secret: 'aws-secret', region: 'us-east-1' });+ assert.deepEqual(payload.ipinfo, { api_key: 'ipinfo' });+ assert.deepEqual(payload.googleMaps, { api_key: 'maps-key', locale: 'us' });+ assert.deepEqual(payload.twilio, { sid: 'sid', token: 'token', from: '+15550000000' });+ assert.deepEqual(payload.sentry, { dsn: 'https://sentry.example.com/1' });+ assert.strictEqual(payload.sms.defaultProvider, 'twilio');+ assert.deepEqual(payload.sms.providers.twilio, { sid: 'sid', token: 'token', from: '+15550000000' }, 'the twilio credentials are folded into the sms block');+ });++ test('a failed save is reported instead of thrown', async function (assert) {+ const component = await this.build();+ const failure = new Error('save rejected');+ this.postRejectsWith = failure;++ await component.save.perform();++ assert.deepEqual(this.notifications().serverErrors, [failure]);+ });++ test('testTwilio posts the credentials and keeps the response', async function (assert) {+ this.postResponse = { status: 'success', message: 'Twilio reachable' };+ const component = await this.build();++ component.twilioSid = 'sid';+ component.twilioToken = 'token';+ component.twilioFrom = '+15550000000';+ component.twilioTestPhone = '+15551111111';++ const result = await component.testTwilio.perform();++ assert.deepEqual(this.posted.at(-1), {+ url: 'settings/test-twilio-config',+ payload: { sid: 'sid', token: 'token', from: '+15550000000', phone: '+15551111111' },+ });+ assert.deepEqual(component.twilioTestResponse, this.postResponse);+ assert.deepEqual(result, this.postResponse);+ });++ test('a failed twilio test is reported instead of thrown', async function (assert) {+ const component = await this.build();+ const failure = new Error('twilio unreachable');+ this.postRejectsWith = failure;++ await component.testTwilio.perform();++ assert.deepEqual(this.notifications().serverErrors, [failure]);+ assert.strictEqual(component.twilioTestResponse, undefined, 'no response is stored');+ });++ test('testSentry posts the dsn and keeps the response', async function (assert) {+ this.postResponse = { status: 'success', message: 'Sentry reachable' };+ const component = await this.build();++ component.sentryDsn = 'https://sentry.example.com/1';++ const result = await component.testSentry.perform();++ assert.deepEqual(this.posted.at(-1), { url: 'settings/test-sentry-config', payload: { dsn: 'https://sentry.example.com/1' } });+ assert.deepEqual(component.sentryTestResponse, this.postResponse);+ assert.deepEqual(result, this.postResponse);+ });++ test('a failed sentry test is reported instead of thrown', async function (assert) {+ const component = await this.build();+ const failure = new Error('sentry unreachable');+ this.postRejectsWith = failure;++ await component.testSentry.perform();++ assert.deepEqual(this.notifications().serverErrors, [failure]);+ assert.strictEqual(component.sentryTestResponse, undefined);+ });++ test('a failed sms provider test is reported instead of thrown', async function (assert) {+ const component = await this.build();+ const failure = new Error('gateway unreachable');+ this.postRejectsWith = failure;++ await component.testSmsProvider.perform();++ assert.deepEqual(this.notifications().serverErrors, [failure]);+ assert.strictEqual(component.smsTestResponse, undefined);+ });++ test('selectSmsProvider switches the panel being configured', async function (assert) {+ const component = await this.build();++ component.selectSmsProvider({ target: { value: 'vonage' } });++ assert.strictEqual(component.smsSelectedProvider, 'vonage');+ });++ test('updateSmsDefaultProvider records the choice on the sms config', async function (assert) {+ const component = await this.build();++ component.updateSmsDefaultProvider({ target: { value: 'messagebird' } });++ assert.strictEqual(component.smsSelectedProvider, 'messagebird');+ assert.strictEqual(component.sms.defaultProvider, 'messagebird');+ });++ test('updateSmsProviderField mirrors the twilio credentials onto the component', async function (assert) {+ const component = await this.build();++ component.updateSmsProviderField('twilio', 'sid', { target: { value: 'new-sid' } });+ component.updateSmsProviderField('twilio', 'token', { target: { value: 'new-token' } });+ component.updateSmsProviderField('twilio', 'from', { target: { value: '+15559999999' } });++ assert.strictEqual(component.twilioSid, 'new-sid');+ assert.strictEqual(component.twilioToken, 'new-token');+ assert.strictEqual(component.twilioFrom, '+15559999999');+ assert.strictEqual(component.sms.providers.twilio.sid, 'new-sid');+ });++ test('updateSmsProviderField leaves the twilio credentials alone for other providers and fields', async function (assert) {+ const component = await this.build();++ component.updateSmsProviderField('twilio', 'messaging_service_sid', { target: { value: 'MG1' } });+ component.updateSmsProviderField('vonage', 'sid', { target: { value: 'not-twilio' } });++ assert.strictEqual(component.twilioSid, null, 'another provider never writes the twilio sid');+ assert.strictEqual(component.sms.providers.vonage.sid, 'not-twilio');+ assert.strictEqual(component.sms.providers.twilio.messaging_service_sid, 'MG1');+ });++ test('updateSmsProviderChecked stores a boolean toggle', async function (assert) {+ const component = await this.build();++ component.updateSmsProviderChecked('smpp', 'tls', { target: { checked: true } });++ assert.true(component.sms.providers.smpp.tls);+ });++ test('updateSmsProviderNumberField parses integers and falls back to zero', async function (assert) {+ const component = await this.build();++ component.updateSmsProviderNumberField('smpp', 'port', { target: { value: '2775' } });+ assert.strictEqual(component.sms.providers.smpp.port, 2775);++ component.updateSmsProviderNumberField('smpp', 'port', { target: { value: 'not-a-number' } });+ assert.strictEqual(component.sms.providers.smpp.port, 0, 'an unparseable value becomes 0');+ });++ test('toggleSmppAdvancedSettings flips the advanced panel', async function (assert) {+ const component = await this.build();++ assert.false(component.isSmppAdvancedSettingsVisible);+ component.toggleSmppAdvancedSettings();+ assert.true(component.isSmppAdvancedSettingsVisible);+ component.toggleSmppAdvancedSettings();+ assert.false(component.isSmppAdvancedSettingsVisible);+ });++ test('updateSmsProviderJsonField stores the raw text and applies parseable JSON', async function (assert) {+ const component = await this.build();++ component.updateSmsProviderJsonField('custom_http', 'headers', { target: { value: '{"X-Key":"abc"}' } });+ component.updateSmsProviderJsonField('custom_http', 'query_params', { target: { value: '{"debug":"1"}' } });+ component.updateSmsProviderJsonField('custom_http', 'body', { target: { value: '{"text":"{{text}}"}' } });++ assert.strictEqual(component.customHttpHeadersText, '{"X-Key":"abc"}', 'the raw text is kept verbatim for the editor');+ assert.deepEqual(component.sms.providers.custom_http.headers, { 'X-Key': 'abc' });+ assert.deepEqual(component.sms.providers.custom_http.query_params, { debug: '1' });+ assert.deepEqual(component.sms.providers.custom_http.body, { text: '{{text}}' });+ });++ test('updateSmsProviderJsonField keeps the last good value while the JSON is invalid', async function (assert) {+ const component = await this.build();++ component.updateSmsProviderJsonField('custom_http', 'headers', { target: { value: '{"X-Key":"abc"}' } });+ component.updateSmsProviderJsonField('custom_http', 'headers', { target: { value: '{"X-Key": ' } });++ assert.strictEqual(component.customHttpHeadersText, '{"X-Key": ', 'the half-typed text is still shown');+ assert.deepEqual(component.sms.providers.custom_http.headers, { 'X-Key': 'abc' }, 'but the parsed config is not clobbered');+ });++ test('updateSmsProviderJsonField ignores JSON that is not an object', async function (assert) {+ const component = await this.build();++ component.updateSmsProviderJsonField('custom_http', 'headers', { target: { value: '42' } });++ assert.strictEqual(component.customHttpHeadersText, '42', 'the raw text is still shown in the editor');+ assert.strictEqual(component.sms, null, 'a bare number is not a headers map, so nothing is applied');+ assert.deepEqual(component.normalizedSms.providers.custom_http.headers, {}, 'headers stay at their default');+ });++ test('selectedSmsProviderConfig is empty for a provider with no config', async function (assert) {+ const component = await this.build();++ component.smsSelectedProvider = 'not-a-provider';++ assert.deepEqual(component.selectedSmsProviderConfig, {});+ assert.true(component.isCustomHttpPostMethod, 'POST is assumed when no method is set');+ });++ test('isCustomHttpPostMethod follows the configured method case-insensitively', async function (assert) {+ const component = await this.build();++ component.smsSelectedProvider = 'custom_http';+ assert.true(component.isCustomHttpPostMethod, 'custom_http defaults to POST');++ component.updateSmsProviderField('custom_http', 'method', { target: { value: 'get' } });+ assert.false(component.isCustomHttpPostMethod);++ component.updateSmsProviderField('custom_http', 'method', { target: { value: 'post' } });+ assert.true(component.isCustomHttpPostMethod, 'lower case post still counts as POST');+ });++ test('smsProviders lists every supported gateway', async function (assert) {+ const component = await this.build();++ assert.deepEqual(+ component.smsProviders.map((provider) => provider.key),+ ['twilio', 'callpro', 'vonage', 'messagebird', 'aws_sns', 'smpp', 'custom_http']+ );+ });++ test('normalizeSmsConfig fills in the defaults for a missing or empty config', async function (assert) {+ const component = await this.build();++ const fromNothing = component.normalizeSmsConfig();+ const fromNull = component.normalizeSmsConfig(null);++ assert.strictEqual(fromNothing.defaultProvider, 'twilio');+ assert.deepEqual(fromNothing.routingRules, {});+ assert.strictEqual(fromNull.defaultProvider, 'twilio', 'null is treated the same as absent');+ assert.strictEqual(fromNothing.providers.smpp.bind_type, 'transceiver', 'smpp defaults are filled in');+ assert.strictEqual(fromNothing.providers.custom_http.method, 'POST');+ assert.deepEqual(fromNothing.providers.callpro, {});+ });++ test('normalizeSmsConfig keeps stored twilio credentials over the component fields', async function (assert) {+ const component = await this.build();++ component.twilioSid = 'component-sid';+ const normalized = component.normalizeSmsConfig({ providers: { twilio: { sid: 'stored-sid' } } });++ assert.strictEqual(normalized.providers.twilio.sid, 'stored-sid');+ assert.strictEqual(normalized.providers.twilio.token, null, 'unset credentials fall back to the component field');+ });++ test('updateSmsProvider adds a provider that has no config yet', async function (assert) {+ const component = await this.build();++ const withNewProvider = component.updateSmsProvider('brand_new', { api_key: 'k' });+ assert.deepEqual(withNewProvider.providers.brand_new, { api_key: 'k' });++ const unchanged = component.updateSmsProvider('vonage');+ assert.deepEqual(unchanged.providers.vonage, {}, 'no changes leaves the provider as it was');+ });++ test('parseJsonValue only accepts objects', async function (assert) {+ const component = await this.build();++ assert.deepEqual(component.parseJsonValue('{"a":1}'), { a: 1 });+ assert.deepEqual(component.parseJsonValue('[1,2]'), [1, 2], 'arrays are objects too');+ assert.strictEqual(component.parseJsonValue('42'), null, 'a number is rejected');+ assert.strictEqual(component.parseJsonValue('null'), null, 'null is rejected');+ assert.strictEqual(component.parseJsonValue('not json at all'), null, 'unparseable text is rejected');+ });++ test('syncSmsJsonText falls back to the default body when custom_http is bare', async function (assert) {+ const component = await this.build();++ component.sms = { providers: { custom_http: {} } };+ component.syncSmsJsonText();++ assert.strictEqual(component.customHttpHeadersText, '{}');+ assert.strictEqual(component.customHttpQueryParamsText, '{}');+ assert.deepEqual(JSON.parse(component.customHttpBodyText), { to: '{{to}}', text: '{{text}}', from: '{{from}}' }, 'the templated default body is offered');+ });+});