Skip to content

Modular logistics and supply chain operating system (LSOS)

fix(console): repair two broken static query helpers on the report model

Ronald A. Richardsonc5c884f7722e
diff --git a/console/app/models/report.js b/console/app/models/report.js
index 4e007d6e..0637551a 100644
--- a/console/app/models/report.js
+++ b/console/app/models/report.js
@@ -373,7 +373,8 @@ export default class ReportModel extends Model {
const owner = getOwner(this);
const fetch = owner.lookup('service:fetch');
- const response = await fetch('reports/export-query', {
+ // fetch here is the injected service, not a callable — every sibling posts through it.
+ const response = await fetch.post('reports/export-query', {
query_config: queryConfig,
format,
options,
@@ -399,6 +400,11 @@ export default class ReportModel extends Model {
}
static async getTables() {
+ // Was reaching for a bare `fetch`, which resolved to the global window.fetch and has
+ // no .get() — resolve the service the same way every sibling does.
+ const owner = getOwner(this);
+ const fetch = owner.lookup('service:fetch');
+
const { tables } = await fetch.get('reports/tables');
return tables;
}
diff --git a/console/tests/unit/models/dashboard-test.js b/console/tests/unit/models/dashboard-test.js
index fc0f0494..233cf996 100644
--- a/console/tests/unit/models/dashboard-test.js
+++ b/console/tests/unit/models/dashboard-test.js
@@ -16,3 +16,115 @@ module('Unit | Model | dashboard', function (hooks) {
assert.deepEqual(dashboard.getRegistry(), { dashboardId: 'dash_1' });
});
});
+
+module('Unit | Model | dashboard | widgets', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.store = this.owner.lookup('service:store');
+ this.saved = [];
+ const context = this;
+
+ // addWidget creates the record itself, so intercept creation to control save()
+ // rather than reaching for a network adapter.
+ const createRecord = this.store.createRecord.bind(this.store);
+ this.store.createRecord = (type, props) => {
+ const record = createRecord(type, props);
+ record.save = () => {
+ context.saved.push(record);
+ return context.saveRejectsWith ? Promise.reject(context.saveRejectsWith) : Promise.resolve(record);
+ };
+ return record;
+ };
+
+ this.unloaded = [];
+ const unloadRecord = this.store.unloadRecord.bind(this.store);
+ this.store.unloadRecord = (record) => {
+ this.unloaded.push(record);
+ return unloadRecord(record);
+ };
+
+ this.dashboard = () => this.store.push({ data: { id: 'dash_1', type: 'dashboard' } });
+ });
+
+ test('addWidget moves the registry slug into options and keeps the record id free', async function (assert) {
+ const dashboard = this.dashboard();
+
+ const widget = await dashboard.addWidget({
+ id: 'fleet-ops-kpi-earnings-widget',
+ default: true,
+ name: 'Earnings',
+ component: 'kpi-tile',
+ options: { span: 2 },
+ });
+
+ assert.strictEqual(widget.name, 'Earnings');
+ assert.strictEqual(widget.component, 'kpi-tile');
+ assert.strictEqual(widget.options.widget_key, 'fleet-ops-kpi-earnings-widget', 'the slug is stashed so it can be resolved on reload');
+ assert.strictEqual(widget.options.span, 2, 'the rest of the options survive');
+ assert.notStrictEqual(widget.id, 'fleet-ops-kpi-earnings-widget', 'the slug is kept out of the record id');
+ assert.strictEqual(dashboard.widgets.length, 1, 'the widget joins the dashboard');
+ assert.strictEqual(this.saved.length, 1, 'and was persisted');
+ });
+
+ test('addWidget copes with a widget that carries no options', async function (assert) {
+ const widget = await this.dashboard().addWidget({ id: 'slug-only', name: 'Bare' });
+
+ assert.deepEqual(widget.options, { widget_key: 'slug-only' });
+ });
+
+ test('a widget that fails to save is unloaded and the failure surfaces', async function (assert) {
+ const failure = new Error('save rejected');
+ this.saveRejectsWith = failure;
+ const dashboard = this.dashboard();
+
+ await assert.rejects(dashboard.addWidget({ id: 'slug', name: 'Doomed' }), /save rejected/);
+
+ assert.strictEqual(this.unloaded.length, 1, 'the half-created record is unloaded');
+ assert.strictEqual(dashboard.widgets.length, 0, 'and never joins the dashboard');
+ });
+
+ test('removeWidget destroys the record and detaches it from the dashboard', async function (assert) {
+ // Attach a persisted widget directly: removeWidget looks its argument up with
+ // peekRecord, so the record needs the server id a real save would have assigned.
+ const dashboard = this.dashboard();
+ const widget = this.store.push({ data: { id: 'w_1', type: 'dashboard-widget' } });
+ dashboard.widgets.pushObject(widget);
+ widget.destroyRecord = () => Promise.resolve();
+
+ await dashboard.removeWidget('w_1');
+
+ assert.strictEqual(dashboard.widgets.length, 0);
+ });
+
+ test('a widget that fails to destroy leaves the dashboard untouched', async function (assert) {
+ const dashboard = this.dashboard();
+ const widget = this.store.push({ data: { id: 'w_1', type: 'dashboard-widget' } });
+ dashboard.widgets.pushObject(widget);
+ widget.destroyRecord = () => Promise.reject(new Error('destroy rejected'));
+
+ await assert.rejects(dashboard.removeWidget('w_1'), /destroy rejected/);
+
+ assert.strictEqual(dashboard.widgets.length, 1, 'the widget stays put');
+ });
+
+ test('removeWidget is a no-op for a widget the store does not know', async function (assert) {
+ const dashboard = this.dashboard();
+
+ assert.strictEqual(await dashboard.removeWidget('not-in-the-store'), undefined);
+ });
+
+ test('the timestamp getters format both dates in each style', function (assert) {
+ const dashboard = this.store.createRecord('dashboard', {
+ created_at: new Date('2026-01-15T10:30:00Z'),
+ updated_at: new Date('2026-02-20T08:05:00Z'),
+ });
+
+ assert.ok(/^2026-01-15 /.test(dashboard.createdAt), `createdAt is date and time (got ${dashboard.createdAt})`);
+ assert.ok(/^2026-02-20 /.test(dashboard.updatedAt), `updatedAt is date and time (got ${dashboard.updatedAt})`);
+ assert.strictEqual(dashboard.createdAtShort, 'Jan 15, 2026');
+ assert.strictEqual(dashboard.updatedAtShort, 'Feb 20, 2026');
+ assert.ok(dashboard.createdAgo.length > 0, 'createdAgo is a relative description');
+ assert.ok(dashboard.updatedAgo.length > 0, 'updatedAgo is a relative description');
+ });
+});
diff --git a/console/tests/unit/models/extension-test.js b/console/tests/unit/models/extension-test.js
index bbc05a85..81791420 100644
--- a/console/tests/unit/models/extension-test.js
+++ b/console/tests/unit/models/extension-test.js
@@ -1,5 +1,6 @@
import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
+import Service from '@ember/service';
module('Unit | Model | extension', function (hooks) {
setupTest(hooks);
@@ -12,3 +13,85 @@ module('Unit | Model | extension', function (hooks) {
assert.strictEqual(json.display_name, 'Fleet Ops');
});
});
+
+module('Unit | Model | extension | install', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.posted = [];
+ this.postRejectsWith = null;
+ const context = this;
+
+ class FetchStub extends Service {
+ post(path, payload) {
+ context.posted.push({ path, payload });
+ return context.postRejectsWith ? Promise.reject(context.postRejectsWith) : Promise.resolve({ status: 'ok' });
+ }
+ }
+ this.owner.register('service:fetch', FetchStub);
+
+ this.store = this.owner.lookup('service:store');
+ this.extension = () => this.store.push({ data: { id: 'ext_1', type: 'extension' } });
+ });
+
+ test('install posts the extension id and flags it as installed', async function (assert) {
+ const extension = this.extension();
+
+ const response = await extension.install();
+
+ assert.deepEqual(this.posted, [{ path: 'installer/install', payload: { extension: 'ext_1' } }]);
+ assert.deepEqual(response, { status: 'ok' }, 'the server response is handed back');
+ assert.true(extension.is_installed);
+ });
+
+ test('a failed install surfaces and leaves the flag alone', async function (assert) {
+ this.postRejectsWith = new Error('installer unavailable');
+ const extension = this.extension();
+
+ await assert.rejects(extension.install(), /installer unavailable/);
+
+ assert.notOk(extension.is_installed, 'the extension is not marked installed');
+ });
+
+ test('uninstall posts the extension id and clears the flag', async function (assert) {
+ const extension = this.extension();
+ extension.is_installed = true;
+
+ await extension.uninstall();
+
+ assert.deepEqual(this.posted, [{ path: 'installer/uninstall', payload: { extension: 'ext_1' } }]);
+ assert.false(extension.is_installed);
+ });
+
+ test('a failed uninstall surfaces and leaves the extension installed', async function (assert) {
+ this.postRejectsWith = new Error('uninstall failed');
+ const extension = this.extension();
+ extension.is_installed = true;
+
+ await assert.rejects(extension.uninstall(), /uninstall failed/);
+
+ assert.true(extension.is_installed);
+ });
+
+ test('hasIcon reflects whether an icon is attached', function (assert) {
+ const extension = this.store.createRecord('extension');
+ assert.false(extension.hasIcon);
+
+ extension.icon_uuid = 'icon_1';
+ assert.true(extension.hasIcon);
+ });
+
+ test('the timestamp getters format both dates in each style', function (assert) {
+ const extension = this.store.createRecord('extension', {
+ created_at: new Date('2026-01-15T10:30:00Z'),
+ updated_at: new Date('2026-02-20T08:05:00Z'),
+ });
+
+ assert.ok(/^2026-01-15 /.test(extension.createdAt), `createdAt is date and time (got ${extension.createdAt})`);
+ assert.ok(/^2026-02-20 /.test(extension.updatedAt), `updatedAt is date and time (got ${extension.updatedAt})`);
+ assert.strictEqual(extension.createdAtShort, 'Jan 15, 2026');
+ assert.strictEqual(extension.updatedAtShort, 'Feb 20, 2026');
+ assert.ok(extension.createdAgo.length > 0);
+ assert.ok(extension.updatedAgo.length > 0);
+ });
+});
diff --git a/console/tests/unit/models/report-test.js b/console/tests/unit/models/report-test.js
index 216d261e..9300ba86 100644
--- a/console/tests/unit/models/report-test.js
+++ b/console/tests/unit/models/report-test.js
@@ -1,6 +1,8 @@
import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
import Service from '@ember/service';
+import { getOwner, setOwner } from '@ember/application';
+import ReportModel from '@fleetbase/console/models/report';
// A query_config shaped the way the model expects, with knobs for each feature.
function queryConfig(overrides = {}) {
@@ -305,3 +307,140 @@ module('Unit | Model | report', function (hooks) {
assert.deepEqual(this.posted.at(-1).payload, { format: 'xlsx', options: { sheet: 'one' } });
});
});
+
+/**
+ * The static query helpers resolve their fetch service through getOwner(this), where `this`
+ * is the class itself. Nothing in the app ever gives the class an owner, so as written they
+ * cannot work when called as ReportModel.getTables() — flagged on the PR rather than
+ * redesigned here. Supplying an owner lets the logic inside them be exercised.
+ */
+module('Unit | Model | report | static query API', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.posted = [];
+ this.gets = [];
+ this.getResponse = {
+ 'reports/tables': { tables: [{ name: 'orders' }] },
+ 'reports/tables/orders/schema': { schema: { columns: ['id'] } },
+ 'reports/export-formats': { formats: ['csv', 'pdf'] },
+ };
+ const self = this;
+
+ class FetchStub extends Service {
+ post(path, payload) {
+ self.posted.push({ path, payload });
+ return Promise.resolve({ ok: true });
+ }
+ get(path) {
+ self.gets.push(path);
+ return Promise.resolve(self.getResponse[path]);
+ }
+ }
+ this.owner.register('service:fetch', FetchStub);
+
+ this.previousOwner = getOwner(ReportModel);
+ setOwner(ReportModel, this.owner);
+ });
+
+ hooks.afterEach(function () {
+ setOwner(ReportModel, this.previousOwner);
+ });
+
+ test('executeQuery posts the config to the ad-hoc endpoint', async function (assert) {
+ const config = { table: { name: 'orders' } };
+
+ const response = await ReportModel.executeQuery(config);
+
+ assert.deepEqual(this.posted.at(-1), { path: 'reports/execute-query', payload: { query_config: config } });
+ assert.deepEqual(response, { ok: true }, 'the server response is handed back');
+ });
+
+ test('exportQuery posts the config with its format and options', async function (assert) {
+ const config = { table: { name: 'orders' } };
+
+ await ReportModel.exportQuery(config, 'pdf', { landscape: true });
+
+ assert.deepEqual(this.posted.at(-1), {
+ path: 'reports/export-query',
+ payload: { query_config: config, format: 'pdf', options: { landscape: true } },
+ });
+ });
+
+ test('exportQuery defaults to csv with no options', async function (assert) {
+ await ReportModel.exportQuery({ table: { name: 'orders' } });
+
+ assert.strictEqual(this.posted.at(-1).payload.format, 'csv');
+ assert.deepEqual(this.posted.at(-1).payload.options, {});
+ });
+
+ test('validateQuery and analyzeQuery post to their own endpoints', async function (assert) {
+ const config = { table: { name: 'orders' } };
+
+ await ReportModel.validateQuery(config);
+ assert.deepEqual(this.posted.at(-1), { path: 'reports/validate-query', payload: { query_config: config } });
+
+ await ReportModel.analyzeQuery(config);
+ assert.deepEqual(this.posted.at(-1), { path: 'reports/analyze-query', payload: { query_config: config } });
+ });
+
+ test('getTables unwraps the table list from the response', async function (assert) {
+ const tables = await ReportModel.getTables();
+
+ assert.deepEqual(this.gets, ['reports/tables']);
+ assert.deepEqual(tables, [{ name: 'orders' }], 'the tables are unwrapped, not the envelope');
+ });
+
+ test('getTableSchema requests the schema for the named table', async function (assert) {
+ const schema = await ReportModel.getTableSchema('orders');
+
+ assert.deepEqual(this.gets, ['reports/tables/orders/schema']);
+ assert.deepEqual(schema, { columns: ['id'] });
+ });
+
+ test('getExportFormats unwraps the available formats', async function (assert) {
+ const formats = await ReportModel.getExportFormats();
+
+ assert.deepEqual(this.gets, ['reports/export-formats']);
+ assert.deepEqual(formats, ['csv', 'pdf']);
+ });
+});
+
+module('Unit | Model | report | execution display', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.store = this.owner.lookup('service:store');
+ });
+
+ // These two read properties the model never declares as attributes, so assigning them
+ // does not invalidate the @computed that already cached a value. Build a fresh record
+ // per case rather than reassigning on one.
+ test('averageExecutionTimeDisplay switches from milliseconds to seconds at a second', function (assert) {
+ assert.strictEqual(this.store.createRecord('report').averageExecutionTimeDisplay, 'N/A', 'nothing recorded yet');
+
+ const quick = this.store.createRecord('report');
+ quick.average_execution_time = 512.4;
+ assert.strictEqual(quick.averageExecutionTimeDisplay, '512ms', 'sub-second timings are rounded to ms');
+
+ const slow = this.store.createRecord('report');
+ slow.average_execution_time = 1500;
+ assert.strictEqual(slow.averageExecutionTimeDisplay, '1.50s', 'longer runs are shown in seconds');
+ });
+
+ test('lastResultCountDisplay formats a count and reports when there is none', function (assert) {
+ assert.strictEqual(this.store.createRecord('report').lastResultCountDisplay, 'N/A', 'undefined reads as N/A');
+
+ const nulled = this.store.createRecord('report');
+ nulled.last_result_count = null;
+ assert.strictEqual(nulled.lastResultCountDisplay, 'N/A', 'so does null');
+
+ const counted = this.store.createRecord('report');
+ counted.last_result_count = 1234567;
+ assert.strictEqual(counted.lastResultCountDisplay, (1234567).toLocaleString(), 'a real count is thousands-separated');
+
+ const zero = this.store.createRecord('report');
+ zero.last_result_count = 0;
+ assert.strictEqual(zero.lastResultCountDisplay, '0', 'zero is a real count, not "no result"');
+ });
+});