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): real unit tests for models with pure logic
Ronald A. Richardson3e1a686de97a
diff --git a/console/tests/unit/models/chat-channel-test.js b/console/tests/unit/models/chat-channel-test.jsnew file mode 100644index 00000000..39edead3--- /dev/null+++ b/console/tests/unit/models/chat-channel-test.js@@ -0,0 +1,26 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';++module('Unit | Model | chat-channel', function (hooks) {+ setupTest(hooks);++ test('toJSON emits company_uuid, name and meta', function (assert) {+ const store = this.owner.lookup('service:store');+ const json = store.createRecord('chat-channel', { name: 'General', company_uuid: 'c1' }).toJSON();++ assert.strictEqual(json.name, 'General');+ assert.strictEqual(json.company_uuid, 'c1');+ assert.deepEqual(Object.keys(json).sort(), ['company_uuid', 'meta', 'name']);+ });++ test('existsInFeed matches on type and record id', function (assert) {+ const store = this.owner.lookup('service:store');+ const channel = store.createRecord('chat-channel', { name: 'General' });+ channel.feed = [{ type: 'message', record: { id: 'm1' } }];++ assert.true(channel.existsInFeed('message', { id: 'm1' }));+ assert.false(channel.existsInFeed('message', { id: 'm2' }), 'different id does not match');+ assert.false(channel.existsInFeed('log', { id: 'm1' }), 'different type does not match');+ assert.true(channel.doesntExistsInFeed('log', { id: 'm1' }));+ });+});diff --git a/console/tests/unit/models/chat-message-test.js b/console/tests/unit/models/chat-message-test.jsnew file mode 100644index 00000000..5775b8aa--- /dev/null+++ b/console/tests/unit/models/chat-message-test.js@@ -0,0 +1,30 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';++module('Unit | Model | chat-message', function (hooks) {+ setupTest(hooks);++ test('hasReadReceipt detects a receipt for the given participant', function (assert) {+ const store = this.owner.lookup('service:store');+ const participant = store.push({ data: { id: 'p1', type: 'chat-participant' } });+ const receipt = store.push({ data: { id: 'r1', type: 'chat-receipt', attributes: { participant_uuid: 'p1' } } });+ const msg = store.createRecord('chat-message', { receipts: [receipt] });++ assert.true(msg.hasReadReceipt(participant));+ assert.false(msg.doesntHaveReadReceipt(participant));++ const other = store.push({ data: { id: 'p2', type: 'chat-participant' } });+ assert.false(msg.hasReadReceipt(other), 'no receipt for a different participant');+ assert.notOk(msg.hasReadReceipt(null), 'guards against a missing participant');+ });++ test('the date getters return null for an invalid date', function (assert) {+ const store = this.owner.lookup('service:store');+ const msg = store.createRecord('chat-message', {});++ assert.strictEqual(msg.updatedAgo, null);+ assert.strictEqual(msg.updatedAt, null);+ assert.strictEqual(msg.createdAgo, null);+ assert.strictEqual(msg.createdAt, null);+ });+});diff --git a/console/tests/unit/models/company-test.js b/console/tests/unit/models/company-test.jsindex 9cf72b02..d23c5891 100644--- a/console/tests/unit/models/company-test.js+++ b/console/tests/unit/models/company-test.js@@ -4,10 +4,17 @@ import { setupTest } from '@fleetbase/console/tests/helpers';module('Unit | Model | company', function (hooks) {setupTest(hooks);- // Replace this with your real tests.- test('it exists', function (assert) {- let store = this.owner.lookup('service:store');- let model = store.createRecord('company', {});- assert.ok(model);+ test('statusLabel defaults to active when status is unset', function (assert) {+ const store = this.owner.lookup('service:store');++ assert.strictEqual(store.createRecord('company', {}).statusLabel, 'active');+ assert.strictEqual(store.createRecord('company', { status: 'suspended' }).statusLabel, 'suspended');+ });++ test('toJSON serializes the record attributes', function (assert) {+ const store = this.owner.lookup('service:store');+ const json = store.createRecord('company', { name: 'Acme' }).toJSON();++ assert.strictEqual(json.name, 'Acme');});});diff --git a/console/tests/unit/models/dashboard-test.js b/console/tests/unit/models/dashboard-test.jsindex 38ee52ca..fc0f0494 100644--- a/console/tests/unit/models/dashboard-test.js+++ b/console/tests/unit/models/dashboard-test.js@@ -1,14 +1,18 @@import { module, test } from 'qunit';-import { setupTest } from '@fleetbase/console/tests/helpers';module('Unit | Model | dashboard', function (hooks) {setupTest(hooks);- // Replace this with your real tests.- test('it exists', function (assert) {- let store = this.owner.lookup('service:store');- let model = store.createRecord('dashboard', {});- assert.ok(model);+ test('getRegistry delegates to the universe service for this dashboard id', function (assert) {+ // service:universe is a pre-resolved singleton and can't be swapped via owner.register,+ // so patch the method on the injected instance getRegistry actually calls.+ const universe = this.owner.lookup('service:universe');+ universe.getDashboardRegistry = (id) => ({ dashboardId: id });++ const store = this.owner.lookup('service:store');+ const dashboard = store.push({ data: { id: 'dash_1', type: 'dashboard' } });++ assert.deepEqual(dashboard.getRegistry(), { dashboardId: 'dash_1' });});});diff --git a/console/tests/unit/models/extension-test.js b/console/tests/unit/models/extension-test.jsindex dcfd6cee..bbc05a85 100644--- a/console/tests/unit/models/extension-test.js+++ b/console/tests/unit/models/extension-test.js@@ -4,10 +4,11 @@ import { setupTest } from '@fleetbase/console/tests/helpers';module('Unit | Model | extension', function (hooks) {setupTest(hooks);- // Replace this with your real tests.- test('it exists', function (assert) {- let store = this.owner.lookup('service:store');- let model = store.createRecord('extension', {});- assert.ok(model);+ test('toJSON emits a plain object of the model attributes', function (assert) {+ const store = this.owner.lookup('service:store');+ const json = store.createRecord('extension', { name: 'fleet-ops', display_name: 'Fleet Ops' }).toJSON();++ assert.strictEqual(json.name, 'fleet-ops');+ assert.strictEqual(json.display_name, 'Fleet Ops');});});diff --git a/console/tests/unit/models/permission-test.js b/console/tests/unit/models/permission-test.jsindex f01e4578..b860cda5 100644--- a/console/tests/unit/models/permission-test.js+++ b/console/tests/unit/models/permission-test.js@@ -4,10 +4,13 @@ import { setupTest } from '@fleetbase/console/tests/helpers';module('Unit | Model | permission', function (hooks) {setupTest(hooks);- // Replace this with your real tests.- test('it exists', function (assert) {- let store = this.owner.lookup('service:store');- let model = store.createRecord('permission', {});- assert.ok(model);+ test('toJSON emits the persisted attributes', function (assert) {+ const store = this.owner.lookup('service:store');+ const json = store.createRecord('permission', { name: 'fleet-ops view driver', guard_name: 'sanctum', service: 'fleet-ops' }).toJSON();++ assert.strictEqual(json.name, 'fleet-ops view driver');+ assert.strictEqual(json.guard_name, 'sanctum');+ assert.strictEqual(json.service, 'fleet-ops');+ assert.deepEqual(Object.keys(json).sort(), ['created_at', 'guard_name', 'name', 'service', 'updated_at']);});});diff --git a/console/tests/unit/models/policy-test.js b/console/tests/unit/models/policy-test.jsindex de6b5797..5a9167e1 100644--- a/console/tests/unit/models/policy-test.js+++ b/console/tests/unit/models/policy-test.js@@ -4,10 +4,10 @@ import { setupTest } from '@fleetbase/console/tests/helpers';module('Unit | Model | policy', function (hooks) {setupTest(hooks);- // Replace this with your real tests.- test('it exists', function (assert) {- let store = this.owner.lookup('service:store');- let model = store.createRecord('policy', {});- assert.ok(model);+ test('toJSON serializes the record via its serializer', function (assert) {+ const store = this.owner.lookup('service:store');+ const json = store.createRecord('policy', { name: 'Dispatcher' }).toJSON();++ assert.strictEqual(json.name, 'Dispatcher');});});diff --git a/console/tests/unit/models/report-test.js b/console/tests/unit/models/report-test.jsindex 29975cee..b063c290 100644--- a/console/tests/unit/models/report-test.js+++ b/console/tests/unit/models/report-test.js@@ -1,14 +1,56 @@import { module, test } from 'qunit';-import { setupTest } from '@fleetbase/console/tests/helpers';module('Unit | Model | report', function (hooks) {setupTest(hooks);- // Replace this with your real tests.- test('it exists', function (assert) {- let store = this.owner.lookup('service:store');- let model = store.createRecord('report', {});- assert.ok(model);+ test('countConditionsRecursively counts leaf conditions across nested groups', function (assert) {+ const store = this.owner.lookup('service:store');+ const report = store.createRecord('report', {});++ const conditions = [{ field: 'a' }, { conditions: [{ field: 'b' }, { field: 'c' }] }];++ assert.strictEqual(report.countConditionsRecursively(conditions), 3);+ assert.strictEqual(report.countConditionsRecursively(null), 0, 'non-array yields 0');+ assert.strictEqual(report.countConditionsRecursively([]), 0);+ });++ test('extractConditionsSummary flattens leaf conditions with labels', function (assert) {+ const store = this.owner.lookup('service:store');+ const report = store.createRecord('report', {});++ const summary = report.extractConditionsSummary([+ { field: { label: 'Name', table: 't1' }, operator: { label: '=' }, value: 'x' },+ { conditions: [{ field: { name: 'Age', table: 't2' }, operator: { value: '>' }, value: 18 }] },+ ]);++ assert.strictEqual(summary.length, 2);+ assert.strictEqual(summary[0].field, 'Name');+ assert.strictEqual(summary[0].operator, '=');+ assert.strictEqual(summary[1].field, 'Age', 'falls back to field name');+ assert.strictEqual(summary[1].operator, '>', 'falls back to operator value');+ });++ test('fillResult copies columns/data/meta and flags the report as generated', function (assert) {+ const store = this.owner.lookup('service:store');+ const report = store.createRecord('report', {});++ report.fillResult({ columns: ['a', 'b'], data: [[1, 2]], meta: { total_rows: 5, execution_time_ms: 12 } });++ assert.deepEqual(report.result_columns, ['a', 'b']);+ assert.strictEqual(report.row_count, 5);+ assert.strictEqual(report.execution_time, 12);+ assert.true(report.is_generated);+ });++ test('fillResult applies safe defaults when passed an empty result', function (assert) {+ const store = this.owner.lookup('service:store');+ const report = store.createRecord('report', {});++ report.fillResult();++ assert.deepEqual(report.result_columns, []);+ assert.strictEqual(report.row_count, 0);+ assert.strictEqual(report.execution_time, -1);});});diff --git a/console/tests/unit/models/role-test.js b/console/tests/unit/models/role-test.jsindex 51c66b4a..49a2dc7e 100644--- a/console/tests/unit/models/role-test.js+++ b/console/tests/unit/models/role-test.js@@ -4,10 +4,10 @@ import { setupTest } from '@fleetbase/console/tests/helpers';module('Unit | Model | role', function (hooks) {setupTest(hooks);- // Replace this with your real tests.- test('it exists', function (assert) {- let store = this.owner.lookup('service:store');- let model = store.createRecord('role', {});- assert.ok(model);+ test('toJSON serializes the record via its serializer', function (assert) {+ const store = this.owner.lookup('service:store');+ const json = store.createRecord('role', { name: 'Administrator' }).toJSON();++ assert.strictEqual(json.name, 'Administrator');});});diff --git a/console/tests/unit/models/schedule-exception-test.js b/console/tests/unit/models/schedule-exception-test.jsnew file mode 100644index 00000000..58778619--- /dev/null+++ b/console/tests/unit/models/schedule-exception-test.js@@ -0,0 +1,29 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';++module('Unit | Model | schedule-exception', function (hooks) {+ setupTest(hooks);++ test('status getters reflect the status attribute', function (assert) {+ const store = this.owner.lookup('service:store');+ const rec = store.createRecord('schedule-exception', { status: 'approved' });++ assert.true(rec.isApproved);+ assert.false(rec.isPending);+ assert.false(rec.isRejected);++ rec.status = 'rejected';+ assert.true(rec.isRejected);+ assert.false(rec.isApproved);+ });++ test('typeLabel maps known types and falls back to the raw value', function (assert) {+ const store = this.owner.lookup('service:store');+ const rec = store.createRecord('schedule-exception', { type: 'sick_leave' });++ assert.strictEqual(rec.typeLabel, 'Sick Leave');++ rec.type = 'unmapped';+ assert.strictEqual(rec.typeLabel, 'unmapped', 'falls back to the raw type');+ });+});diff --git a/console/tests/unit/models/schedule-template-test.js b/console/tests/unit/models/schedule-template-test.jsnew file mode 100644index 00000000..0d9029aa--- /dev/null+++ b/console/tests/unit/models/schedule-template-test.js@@ -0,0 +1,21 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';++module('Unit | Model | schedule-template', function (hooks) {+ setupTest(hooks);++ test('recurrenceSummary summarizes the RRULE with days and times', function (assert) {+ const store = this.owner.lookup('service:store');+ const rec = store.createRecord('schedule-template', { rrule: 'FREQ=WEEKLY;BYDAY=MO,WE,FR', start_time: '08:00', end_time: '16:00' });++ const summary = rec.recurrenceSummary;+ assert.ok(summary.startsWith('Weekly on Mon, Wed, Fri'), summary);+ assert.ok(summary.includes('08:00'));+ assert.ok(summary.includes('16:00'));+ });++ test('recurrenceSummary is null without an rrule', function (assert) {+ const store = this.owner.lookup('service:store');+ assert.strictEqual(store.createRecord('schedule-template', {}).recurrenceSummary, null);+ });+});diff --git a/console/tests/unit/models/user-test.js b/console/tests/unit/models/user-test.jsindex b4dd010e..199a4fbc 100644--- a/console/tests/unit/models/user-test.js+++ b/console/tests/unit/models/user-test.js@@ -4,10 +4,18 @@ import { setupTest } from '@fleetbase/console/tests/helpers';module('Unit | Model | user', function (hooks) {setupTest(hooks);- // Replace this with your real tests.- test('it exists', function (assert) {- let store = this.owner.lookup('service:store');- let model = store.createRecord('user', {});- assert.ok(model);+ test('isAdmin is true only when is_admin is strictly true', function (assert) {+ const store = this.owner.lookup('service:store');++ assert.true(store.createRecord('user', { is_admin: true }).isAdmin);+ assert.false(store.createRecord('user', { is_admin: false }).isAdmin);+ assert.false(store.createRecord('user', {}).isAdmin, 'unset is not admin');+ });++ test('isEmailVerified reflects a valid email_verified_at timestamp', function (assert) {+ const store = this.owner.lookup('service:store');++ assert.notOk(store.createRecord('user', {}).isEmailVerified, 'unverified when unset');+ assert.ok(store.createRecord('user', { email_verified_at: '2024-01-01T00:00:00Z' }).isEmailVerified);});});