Skip to content

Modular logistics and supply chain operating system (LSOS)

test(console): cover the alert, permission, company and chat models

Ronald A. Richardson621b8d94e3fc
diff --git a/console/tests/unit/models/alert-test.js b/console/tests/unit/models/alert-test.js
index 109ca639..f52736ee 100644
--- a/console/tests/unit/models/alert-test.js
+++ b/console/tests/unit/models/alert-test.js
@@ -1,14 +1,183 @@
import { module, test } from 'qunit';
-
import { setupTest } from '@fleetbase/console/tests/helpers';
+import { subMinutes } from 'date-fns';
module('Unit | Model | alert', 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('alert', {});
- assert.ok(model);
+ hooks.beforeEach(function () {
+ this.store = this.owner.lookup('service:store');
+ this.alert = (attrs = {}) => this.store.createRecord('alert', attrs);
+ });
+
+ test('lifecycle flags derive from the acknowledged/resolved timestamps', function (assert) {
+ const pending = this.alert({});
+ assert.false(pending.isAcknowledged);
+ assert.false(pending.isResolved);
+ assert.true(pending.isPending, 'neither acknowledged nor resolved');
+ assert.false(pending.isActive);
+
+ const active = this.alert({ acknowledged_at: new Date() });
+ assert.true(active.isAcknowledged);
+ assert.false(active.isPending);
+ assert.true(active.isActive, 'acknowledged but unresolved');
+
+ const resolved = this.alert({ acknowledged_at: new Date(), resolved_at: new Date() });
+ assert.true(resolved.isResolved);
+ assert.false(resolved.isActive);
+ assert.false(resolved.isPending);
+ });
+
+ test('status text, badge and icon follow resolved > acknowledged > pending', function (assert) {
+ const pending = this.alert({});
+ assert.strictEqual(pending.statusText, 'Pending');
+ assert.strictEqual(pending.statusIcon, 'fas fa-bell');
+ assert.ok(pending.statusBadgeClass.includes('red'), 'pending is red');
+
+ const acknowledged = this.alert({ acknowledged_at: new Date() });
+ assert.strictEqual(acknowledged.statusText, 'Acknowledged');
+ assert.strictEqual(acknowledged.statusIcon, 'fas fa-eye');
+ assert.ok(acknowledged.statusBadgeClass.includes('blue'), 'acknowledged is blue');
+
+ const resolved = this.alert({ resolved_at: new Date() });
+ assert.strictEqual(resolved.statusText, 'Resolved');
+ assert.strictEqual(resolved.statusIcon, 'fas fa-check-circle');
+ assert.ok(resolved.statusBadgeClass.includes('green'), 'resolved is green');
+ });
+
+ test('the timestamp getters format known dates and fall back when unset', function (assert) {
+ const at = new Date('2026-05-06T14:30:00Z');
+ const filled = this.alert({ triggered_at: at, acknowledged_at: at, resolved_at: at, created_at: at, updated_at: at });
+
+ assert.ok(filled.triggeredAt.startsWith('2026-05-06'), filled.triggeredAt);
+ assert.ok(filled.acknowledgedAt.startsWith('2026-05-06'));
+ assert.ok(filled.resolvedAt.startsWith('2026-05-06'));
+ assert.ok(filled.createdAt.startsWith('2026-05-06'));
+ assert.ok(filled.updatedAt.startsWith('2026-05-06'));
+ assert.ok(filled.triggeredAgo.endsWith(' ago'));
+ assert.ok(filled.acknowledgedAgo.endsWith(' ago'));
+ assert.ok(filled.resolvedAgo.endsWith(' ago'));
+
+ const empty = this.alert({});
+ assert.strictEqual(empty.triggeredAgo, 'Unknown');
+ assert.strictEqual(empty.triggeredAt, 'Unknown');
+ assert.strictEqual(empty.acknowledgedAgo, null, 'no acknowledgement yields null, not a label');
+ assert.strictEqual(empty.acknowledgedAt, 'Not acknowledged');
+ assert.strictEqual(empty.resolvedAgo, null);
+ assert.strictEqual(empty.resolvedAt, 'Not resolved');
+ });
+
+ test('duration getters measure from triggered_at and are null when incomplete', function (assert) {
+ const triggered = subMinutes(new Date(), 90);
+ const alert = this.alert({ triggered_at: triggered, acknowledged_at: subMinutes(new Date(), 60), resolved_at: new Date() });
+
+ assert.strictEqual(alert.acknowledgmentDurationMinutes, 30);
+ assert.strictEqual(alert.resolutionDurationMinutes, 90);
+
+ const untriggered = this.alert({ acknowledged_at: new Date() });
+ assert.strictEqual(untriggered.acknowledgmentDurationMinutes, null, 'needs both ends');
+ assert.strictEqual(untriggered.resolutionDurationMinutes, null);
+ });
+
+ test('durations format as minutes, hours or days', function (assert) {
+ const now = new Date();
+ const mins = this.alert({ triggered_at: subMinutes(now, 45), acknowledged_at: now });
+ assert.strictEqual(mins.acknowledgmentDurationFormatted, '45m');
+
+ const hours = this.alert({ triggered_at: subMinutes(now, 150), acknowledged_at: now });
+ assert.strictEqual(hours.acknowledgmentDurationFormatted, '2h 30m');
+
+ const days = this.alert({ triggered_at: subMinutes(now, 1500), resolved_at: now });
+ assert.strictEqual(days.resolutionDurationFormatted, '1d 1h');
+
+ assert.strictEqual(this.alert({}).acknowledgmentDurationFormatted, null);
+ assert.strictEqual(this.alert({}).resolutionDurationFormatted, null);
+ });
+
+ test('ageMinutes and ageFormatted measure elapsed time since trigger', function (assert) {
+ assert.strictEqual(this.alert({}).ageMinutes, 0, 'untriggered alerts have no age');
+ assert.strictEqual(this.alert({}).ageFormatted, '0m');
+
+ const aged = this.alert({ triggered_at: subMinutes(new Date(), 200) });
+ assert.strictEqual(aged.ageMinutes, 200);
+ assert.strictEqual(aged.ageFormatted, '3h 20m');
+ });
+
+ test('severity presentation maps known severities and falls back to info', function (assert) {
+ assert.ok(this.alert({ severity: 'critical' }).severityBadgeClass.includes('red'));
+ assert.strictEqual(this.alert({ severity: 'critical' }).severityIcon, 'fas fa-exclamation-circle');
+ assert.ok(this.alert({ severity: 'critical' }).severityColor.includes('red'));
+
+ assert.strictEqual(this.alert({ severity: 'high' }).severityIcon, 'fas fa-exclamation-triangle');
+ assert.strictEqual(this.alert({ severity: 'medium' }).severityIcon, 'fas fa-exclamation');
+ assert.strictEqual(this.alert({ severity: 'low' }).severityIcon, 'fas fa-info-circle');
+
+ const unknown = this.alert({ severity: 'not-a-severity' });
+ assert.strictEqual(unknown.severityIcon, 'fas fa-info', 'unknown severity falls back to info');
+ assert.ok(unknown.severityBadgeClass.includes('gray'));
+ assert.ok(unknown.severityColor.includes('gray'));
+ });
+
+ test('type presentation maps known types and falls back to a bell', function (assert) {
+ assert.strictEqual(this.alert({ type: 'maintenance' }).typeIcon, 'fas fa-wrench');
+ assert.strictEqual(this.alert({ type: 'fuel' }).typeIcon, 'fas fa-gas-pump');
+ assert.strictEqual(this.alert({ type: 'security' }).typeIcon, 'fas fa-shield-alt');
+ assert.ok(this.alert({ type: 'temperature' }).typeBadgeClass.includes('red'));
+
+ const unknown = this.alert({ type: 'not-a-type' });
+ assert.strictEqual(unknown.typeIcon, 'fas fa-bell');
+ assert.ok(unknown.typeBadgeClass.includes('gray'));
+ });
+
+ test('subjectTypeFormatted maps known subjects and humanizes the rest', function (assert) {
+ assert.strictEqual(this.alert({ subject_type: 'vehicle' }).subjectTypeFormatted, 'Vehicle');
+ assert.strictEqual(this.alert({ subject_type: 'fuel_report' }).subjectTypeFormatted, 'Fuel Report');
+ assert.strictEqual(this.alert({ subject_type: 'custom_thing' }).subjectTypeFormatted, 'Custom Thing', 'unknown types are humanized');
+ assert.strictEqual(this.alert({}).subjectTypeFormatted, 'Unknown');
+ });
+
+ test('urgencyLevel escalates with severity and age', function (assert) {
+ const now = new Date();
+
+ assert.strictEqual(this.alert({ severity: 'critical', triggered_at: now }).urgencyLevel, 'high', 'fresh critical is high');
+ assert.strictEqual(this.alert({ severity: 'critical', triggered_at: subMinutes(now, 120) }).urgencyLevel, 'urgent', 'critical over an hour is urgent');
+ assert.strictEqual(this.alert({ severity: 'medium', triggered_at: now }).urgencyLevel, 'medium');
+ assert.strictEqual(this.alert({ severity: 'medium', triggered_at: subMinutes(now, 300) }).urgencyLevel, 'urgent', 'medium over four hours is urgent');
+ assert.strictEqual(this.alert({ severity: 'info', triggered_at: now }).urgencyLevel, 'low');
+ assert.strictEqual(this.alert({ severity: 'nonsense', triggered_at: now }).urgencyLevel, 'low', 'unknown severity carries no weight');
+
+ assert.ok(this.alert({ severity: 'critical', triggered_at: subMinutes(now, 120) }).urgencyBadgeClass.includes('animate-pulse'), 'urgent pulses');
+ assert.ok(this.alert({ severity: 'info', triggered_at: now }).urgencyBadgeClass.includes('green'));
+ });
+
+ test('hasContext, hasRule and hasLocation report populated payloads', function (assert) {
+ assert.false(this.alert({}).hasContext);
+ assert.false(this.alert({ context: {} }).hasContext, 'an empty object does not count');
+ assert.true(this.alert({ context: { value: 1 } }).hasContext);
+
+ assert.false(this.alert({ rule: {} }).hasRule);
+ assert.true(this.alert({ rule: { threshold: 5 } }).hasRule);
+
+ assert.false(this.alert({ context: {} }).hasLocation);
+ assert.true(this.alert({ context: { location: 'here' } }).hasLocation);
+ });
+
+ test('thresholdExceeded evaluates every supported operator', function (assert) {
+ const check = (operator, value, threshold) => this.alert({ context: { value }, rule: { operator, threshold } }).thresholdExceeded;
+
+ assert.true(check('>', 10, 5));
+ assert.false(check('>', 3, 5));
+ assert.true(check('<', 3, 5));
+ assert.true(check('>=', 5, 5));
+ assert.true(check('<=', 5, 5));
+ assert.true(check('==', 5, 5));
+ assert.true(check('!=', 6, 5));
+ assert.strictEqual(check('~', 6, 5), null, 'an unsupported operator yields null');
+ });
+
+ test('thresholdExceeded defaults to greater-than and is null without both operands', function (assert) {
+ assert.true(this.alert({ context: { value: 10 }, rule: { threshold: 5 } }).thresholdExceeded, 'defaults to >');
+ assert.strictEqual(this.alert({ context: { value: 10 }, rule: {} }).thresholdExceeded, null);
+ assert.strictEqual(this.alert({ context: {}, rule: { threshold: 5 } }).thresholdExceeded, null);
});
});
diff --git a/console/tests/unit/models/chat-attachment-test.js b/console/tests/unit/models/chat-attachment-test.js
new file mode 100644
index 00000000..1abded81
--- /dev/null
+++ b/console/tests/unit/models/chat-attachment-test.js
@@ -0,0 +1,65 @@
+import { module, test } from 'qunit';
+import { setupTest } from '@fleetbase/console/tests/helpers';
+import Service from '@ember/service';
+
+module('Unit | Model | chat-attachment', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.downloads = [];
+ const downloads = this.downloads;
+
+ class FetchStub extends Service {
+ download(path, params, options) {
+ downloads.push({ path, params, options });
+ return Promise.resolve();
+ }
+ }
+ this.owner.register('service:fetch', FetchStub);
+
+ this.store = this.owner.lookup('service:store');
+ });
+
+ test('isImage and isVideo classify the content type', function (assert) {
+ const attachment = (content_type) => this.store.createRecord('chat-attachment', { content_type });
+
+ assert.true(attachment('image/png').isImage);
+ assert.false(attachment('image/png').isVideo);
+ assert.true(attachment('video/mp4').isVideo);
+ assert.false(attachment('video/mp4').isImage);
+ assert.false(attachment('application/pdf').isImage, 'other types are neither');
+ assert.false(attachment('application/pdf').isVideo);
+ });
+
+ test('the date getters format valid timestamps and null otherwise', function (assert) {
+ const at = new Date('2026-05-06T14:30:00Z');
+ const filled = this.store.createRecord('chat-attachment', { created_at: at, updated_at: at });
+
+ assert.strictEqual(typeof filled.createdAt, 'string');
+ assert.strictEqual(typeof filled.updatedAt, 'string');
+ assert.strictEqual(typeof filled.createdAgo, 'string');
+ assert.strictEqual(typeof filled.updatedAgo, 'string');
+
+ const empty = this.store.createRecord('chat-attachment', {});
+ assert.strictEqual(empty.createdAt, null);
+ assert.strictEqual(empty.createdAgo, null);
+ assert.strictEqual(empty.updatedAt, null);
+ assert.strictEqual(empty.updatedAgo, null);
+ });
+
+ test('download requests the file through the fetch service', async function (assert) {
+ const attachment = this.store.createRecord('chat-attachment', {
+ file_uuid: 'file_1',
+ filename: 'notes.pdf',
+ content_type: 'application/pdf',
+ });
+
+ await attachment.download();
+
+ assert.deepEqual(this.downloads.at(-1), {
+ path: 'files/download',
+ params: { file: 'file_1' },
+ options: { fileName: 'notes.pdf', mimeType: 'application/pdf' },
+ });
+ });
+});
diff --git a/console/tests/unit/models/chat-log-test.js b/console/tests/unit/models/chat-log-test.js
index f52b4584..e3f98b0d 100644
--- a/console/tests/unit/models/chat-log-test.js
+++ b/console/tests/unit/models/chat-log-test.js
@@ -1,14 +1,37 @@
import { module, test } from 'qunit';
-
import { setupTest } from '@fleetbase/console/tests/helpers';
-module('Unit | Model | chat log', function (hooks) {
+module('Unit | Model | chat-log', 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('chat-log', {});
- assert.ok(model);
+ test('the date getters format valid timestamps', function (assert) {
+ const at = new Date('2026-05-06T14:30:00Z');
+ const log = this.owner.lookup('service:store').createRecord('chat-log', { created_at: at, updated_at: at });
+
+ assert.strictEqual(typeof log.createdAt, 'string');
+ assert.strictEqual(typeof log.updatedAt, 'string');
+ assert.ok(log.createdAgo.includes('ago'), 'relative times carry a suffix');
+ assert.ok(log.updatedAgo.includes('ago'));
+ });
+
+ test('the date getters return null for missing timestamps', function (assert) {
+ const log = this.owner.lookup('service:store').createRecord('chat-log', {});
+
+ assert.strictEqual(log.createdAt, null);
+ assert.strictEqual(log.createdAgo, null);
+ assert.strictEqual(log.updatedAt, null);
+ assert.strictEqual(log.updatedAgo, null);
+ });
+
+ test('it keeps its event attributes', function (assert) {
+ const log = this.owner.lookup('service:store').createRecord('chat-log', {
+ event_type: 'participant_added',
+ content: 'Ron joined',
+ status: 'ok',
+ });
+
+ assert.strictEqual(log.event_type, 'participant_added');
+ assert.strictEqual(log.content, 'Ron joined');
+ assert.strictEqual(log.status, 'ok');
});
});
diff --git a/console/tests/unit/models/chat-participant-test.js b/console/tests/unit/models/chat-participant-test.js
new file mode 100644
index 00000000..661ca6f0
--- /dev/null
+++ b/console/tests/unit/models/chat-participant-test.js
@@ -0,0 +1,29 @@
+import { module, test } from 'qunit';
+import { setupTest } from '@fleetbase/console/tests/helpers';
+
+module('Unit | Model | chat-participant', function (hooks) {
+ setupTest(hooks);
+
+ test('the date getters format valid timestamps', function (assert) {
+ const at = new Date('2026-05-06T14:30:00Z');
+ const participant = this.owner.lookup('service:store').createRecord('chat-participant', { created_at: at, updated_at: at, last_seen_at: at });
+
+ assert.strictEqual(typeof participant.createdAt, 'string');
+ assert.strictEqual(typeof participant.updatedAt, 'string');
+ assert.strictEqual(typeof participant.lastSeenAt, 'string');
+ assert.strictEqual(typeof participant.createdAgo, 'string');
+ assert.strictEqual(typeof participant.updatedAgo, 'string');
+ assert.strictEqual(typeof participant.lastSeenAgo, 'string');
+ });
+
+ test('the date getters return null for missing timestamps', function (assert) {
+ const participant = this.owner.lookup('service:store').createRecord('chat-participant', {});
+
+ assert.strictEqual(participant.createdAt, null);
+ assert.strictEqual(participant.createdAgo, null);
+ assert.strictEqual(participant.updatedAt, null);
+ assert.strictEqual(participant.updatedAgo, null);
+ assert.strictEqual(participant.lastSeenAt, null, 'a participant who has never been seen has no timestamp');
+ assert.strictEqual(participant.lastSeenAgo, null);
+ });
+});
diff --git a/console/tests/unit/models/chat-receipt-test.js b/console/tests/unit/models/chat-receipt-test.js
new file mode 100644
index 00000000..84b7b08c
--- /dev/null
+++ b/console/tests/unit/models/chat-receipt-test.js
@@ -0,0 +1,29 @@
+import { module, test } from 'qunit';
+import { setupTest } from '@fleetbase/console/tests/helpers';
+
+module('Unit | Model | chat-receipt', function (hooks) {
+ setupTest(hooks);
+
+ test('the date getters format valid timestamps', function (assert) {
+ const at = new Date('2026-05-06T14:30:00Z');
+ const receipt = this.owner.lookup('service:store').createRecord('chat-receipt', { created_at: at, updated_at: at, read_at: at });
+
+ assert.strictEqual(typeof receipt.createdAt, 'string');
+ assert.strictEqual(typeof receipt.updatedAt, 'string');
+ assert.strictEqual(typeof receipt.readAt, 'string');
+ assert.ok(receipt.createdAgo.includes('ago'), 'relative times carry a suffix');
+ assert.ok(receipt.updatedAgo.includes('ago'));
+ assert.ok(receipt.readAgo.includes('ago'));
+ });
+
+ test('the date getters return null for missing timestamps', function (assert) {
+ const receipt = this.owner.lookup('service:store').createRecord('chat-receipt', {});
+
+ assert.strictEqual(receipt.createdAt, null);
+ assert.strictEqual(receipt.createdAgo, null);
+ assert.strictEqual(receipt.updatedAt, null);
+ assert.strictEqual(receipt.updatedAgo, null);
+ assert.strictEqual(receipt.readAt, null, 'an unread receipt has no read timestamp');
+ assert.strictEqual(receipt.readAgo, null);
+ });
+});
diff --git a/console/tests/unit/models/company-test.js b/console/tests/unit/models/company-test.js
index d23c5891..b9f9db47 100644
--- a/console/tests/unit/models/company-test.js
+++ b/console/tests/unit/models/company-test.js
@@ -1,20 +1,100 @@
import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
+import Service from '@ember/service';
module('Unit | Model | company', function (hooks) {
setupTest(hooks);
- test('statusLabel defaults to active when status is unset', function (assert) {
- const store = this.owner.lookup('service:store');
+ hooks.beforeEach(function () {
+ this.posted = [];
+ this.getResponse = () => Promise.resolve(['user-a']);
+ const self = this;
- assert.strictEqual(store.createRecord('company', {}).statusLabel, 'active');
- assert.strictEqual(store.createRecord('company', { status: 'suspended' }).statusLabel, 'suspended');
+ class FetchStub extends Service {
+ post(path, payload) {
+ self.posted.push({ path, payload });
+ return Promise.resolve({ ok: true });
+ }
+ get(path, params, options) {
+ self.posted.push({ path, params, options });
+ return self.getResponse();
+ }
+ }
+ this.owner.register('service:fetch', FetchStub);
+
+ this.store = this.owner.lookup('service:store');
+ this.company = (attrs = {}) => this.store.createRecord('company', attrs);
+ });
+
+ test('statusLabel defaults to active and onboardingStatus reflects the flag', function (assert) {
+ assert.strictEqual(this.company({}).statusLabel, 'active');
+ assert.strictEqual(this.company({ status: 'suspended' }).statusLabel, 'suspended');
+
+ assert.strictEqual(this.company({}).onboardingStatus, 'incomplete', 'defaults to incomplete');
+ assert.strictEqual(this.company({ onboarding_completed: true }).onboardingStatus, 'complete');
+ });
+
+ test('phoneCountryCode falls back to the country', function (assert) {
+ assert.strictEqual(this.company({ country: 'SG' }).phoneCountryCode, 'SG');
+ });
+
+ test('the timestamp getters format created_at and updated_at', function (assert) {
+ const at = new Date('2026-05-06T14:30:00Z');
+ const company = this.company({ created_at: at, updated_at: at });
+
+ assert.ok(company.createdAt.startsWith('2026-05-06'), company.createdAt);
+ assert.ok(company.updatedAt.startsWith('2026-05-06'));
+ assert.strictEqual(typeof company.createdAtShort, 'string');
+ assert.strictEqual(typeof company.updatedAtShort, 'string');
+ assert.strictEqual(typeof company.createdAgo, 'string');
+ assert.strictEqual(typeof company.updatedAgo, 'string');
});
test('toJSON serializes the record attributes', function (assert) {
- const store = this.owner.lookup('service:store');
- const json = store.createRecord('company', { name: 'Acme' }).toJSON();
+ const json = this.company({ name: 'Acme' }).toJSON();
assert.strictEqual(json.name, 'Acme');
});
+
+ test('transferOwnership posts the company and the new owner', async function (assert) {
+ const company = this.store.push({ data: { id: 'co_1', type: 'company' } });
+
+ await company.transferOwnership('user_2', { leave: true });
+
+ assert.deepEqual(this.posted.at(-1), {
+ path: 'companies/transfer-ownership',
+ payload: { company: 'co_1', newOwner: 'user_2', leave: true },
+ });
+ });
+
+ test('leave posts the company and optional user', async function (assert) {
+ const company = this.store.push({ data: { id: 'co_1', type: 'company' } });
+
+ await company.leave();
+ assert.deepEqual(this.posted.at(-1).payload, { company: 'co_1', user: null });
+
+ await company.leave('user_3', { force: true });
+ assert.deepEqual(this.posted.at(-1).payload, { company: 'co_1', user: 'user_3', force: true });
+ });
+
+ test('loadUsers stores the fetched users on the record', async function (assert) {
+ const company = this.store.push({ data: { id: 'co_1', type: 'company' } });
+
+ const users = await company.loadUsers({ exclude: ['user_1'] });
+
+ assert.deepEqual(users, ['user-a']);
+ assert.deepEqual(company.get('users'), ['user-a'], 'the result is cached on the record');
+ assert.strictEqual(this.posted.at(-1).path, 'companies/co_1/users');
+ assert.deepEqual(this.posted.at(-1).params, { exclude: ['user_1'] });
+ });
+
+ test('loadUsers falls back to an empty list when the request fails', async function (assert) {
+ this.getResponse = () => Promise.reject(new Error('nope'));
+ const company = this.store.push({ data: { id: 'co_1', type: 'company' } });
+
+ const users = await company.loadUsers();
+
+ assert.deepEqual(users, [], 'the failure is swallowed');
+ assert.deepEqual(company.get('users'), []);
+ });
});
diff --git a/console/tests/unit/models/permission-test.js b/console/tests/unit/models/permission-test.js
index b860cda5..3db65efa 100644
--- a/console/tests/unit/models/permission-test.js
+++ b/console/tests/unit/models/permission-test.js
@@ -1,16 +1,79 @@
import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
+import { parserPermissionName, getPermissionExtension, getPermissionAction, getPermissionResource } from '@fleetbase/console/models/permission';
module('Unit | Model | permission', function (hooks) {
setupTest(hooks);
+ hooks.beforeEach(function () {
+ this.store = this.owner.lookup('service:store');
+ this.permission = (attrs = {}) => this.store.createRecord('permission', attrs);
+ });
+
+ test('parserPermissionName picks the requested segment and returns null past the end', function (assert) {
+ assert.strictEqual(parserPermissionName('fleet-ops view driver'), 'fleet-ops', 'defaults to the first segment');
+ assert.strictEqual(parserPermissionName('fleet-ops view driver', 1), 'view');
+ assert.strictEqual(parserPermissionName('fleet-ops view driver', 2), 'driver');
+ assert.strictEqual(parserPermissionName('fleet-ops', 1), null, 'missing segments yield null');
+ });
+
+ test('the extension/action/resource helpers read their segments', function (assert) {
+ assert.strictEqual(getPermissionExtension('iam view user'), 'iam');
+ assert.strictEqual(getPermissionAction('iam view user'), 'view');
+ assert.strictEqual(getPermissionResource('iam view user'), 'user');
+ });
+
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();
+ const json = this.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']);
});
+
+ test('serviceName and extensionName both read the leading segment', function (assert) {
+ const permission = this.permission({ name: 'fleet-ops view driver' });
+
+ assert.strictEqual(permission.serviceName, 'fleet-ops');
+ assert.strictEqual(permission.extensionName, 'fleet-ops');
+ });
+
+ test('resourceName reads the third segment', function (assert) {
+ assert.strictEqual(this.permission({ name: 'fleet-ops view driver' }).resourceName, 'driver');
+ assert.strictEqual(this.permission({ name: 'fleet-ops view' }).resourceName, null);
+ });
+
+ test('actionName titleizes the action and special-cases wildcard and see', function (assert) {
+ assert.strictEqual(this.permission({ name: 'fleet-ops * driver' }).actionName, 'do anything');
+ assert.strictEqual(this.permission({ name: 'fleet-ops see driver' }).actionName, 'Visibly See');
+ assert.strictEqual(this.permission({ name: 'fleet-ops view driver' }).actionName, 'View');
+ assert.strictEqual(this.permission({ name: 'fleet-ops create_order driver' }).actionName, 'Create Order', 'underscores are humanized');
+ });
+
+ test('description reads as a sentence and pluralizes the resource', function (assert) {
+ const description = this.permission({ name: 'fleet-ops view driver' }).description;
+
+ assert.ok(description.startsWith('Permission to View'), description);
+ assert.ok(description.includes('Drivers'), 'the resource is pluralized');
+ assert.ok(/on Fleet.?Ops$/i.test(description), `it ends with the extension: ${description}`);
+ });
+
+ test('description uses "with" for wildcard actions and upper-cases IAM', function (assert) {
+ const wildcard = this.permission({ name: 'iam * user' });
+
+ assert.ok(wildcard.description.includes('with'), `wildcard uses the "with" preposition: ${wildcard.description}`);
+ assert.ok(wildcard.description.includes('IAM'), 'the iam extension is upper-cased');
+ assert.ok(wildcard.description.includes('do anything'));
+ });
+
+ test('the timestamp getters format created_at and updated_at', function (assert) {
+ const at = new Date('2026-05-06T14:30:00Z');
+ const permission = this.permission({ created_at: at, updated_at: at });
+
+ assert.ok(permission.createdAt.startsWith('2026-05-06'), permission.createdAt);
+ assert.strictEqual(typeof permission.updatedAt, 'string');
+ assert.strictEqual(typeof permission.createdAgo, 'string');
+ assert.strictEqual(typeof permission.updatedAgo, 'string');
+ });
});