Skip to content

Modular logistics and supply chain operating system (LSOS)

fix(console): stop a rejected onboarding guard recursing until the stack overflows

Ronald A. Richardson0c5299cafe60
diff --git a/console/app/services/onboarding-orchestrator.js b/console/app/services/onboarding-orchestrator.js
index b0a2f5f9..caf8997b 100644
--- a/console/app/services/onboarding-orchestrator.js
+++ b/console/app/services/onboarding-orchestrator.js
@@ -59,7 +59,7 @@ export default class OnboardingOrchestratorService extends Service {
// Guard function - skip step if guard returns false
if (typeof step.guard === 'function' && !step.guard(this.onboardingContext)) {
- return this.next();
+ return this._skip(step);
}
// beforeEnter lifecycle hook
@@ -100,27 +100,57 @@ export default class OnboardingOrchestratorService extends Service {
// If no next step, flow is complete
if (!nextId) {
- // Execute onFlowWillEnd hook if defined
- if (typeof this.flow.onFlowWillEnd === 'function') {
- await this.flow.onFlowWillEnd(leaving, this);
- }
-
- this.current = null; // finished
+ return this._completeFlow(leaving);
+ }
- // Execute onFlowDidEnd hook if defined
- if (typeof this.flow.onFlowDidEnd === 'function') {
- await this.flow.onFlowDidEnd(leaving, this);
- }
+ return this.goto(nextId);
+ }
- // Clear history from localStorage when flow completes
- this._clearHistory();
+ /**
+ * Advance past a step whose guard rejected it.
+ *
+ * Resolves the skipped step's own `next` rather than delegating to next(). next() reads
+ * `this.current`, which is still the step *before* this one — a guarded step is never
+ * entered — so it would resolve the same rejected step again and recurse until the call
+ * stack overflowed. A skipped step was never entered, so it also gets no afterLeave hook
+ * and is kept out of the history.
+ *
+ * @param {Object} step The step whose guard returned false
+ * @private
+ */
+ async _skip(step) {
+ const nextId = typeof step.next === 'function' ? step.next(this.onboardingContext) : step.next;
- return;
+ if (!nextId) {
+ return this._completeFlow(step);
}
return this.goto(nextId);
}
+ /**
+ * Run the flow's end hooks, clear the current step and drop persisted history.
+ *
+ * @param {Object} leaving The last step of the flow
+ * @private
+ */
+ async _completeFlow(leaving) {
+ // Execute onFlowWillEnd hook if defined
+ if (typeof this.flow.onFlowWillEnd === 'function') {
+ await this.flow.onFlowWillEnd(leaving, this);
+ }
+
+ this.current = null; // finished
+
+ // Execute onFlowDidEnd hook if defined
+ if (typeof this.flow.onFlowDidEnd === 'function') {
+ await this.flow.onFlowDidEnd(leaving, this);
+ }
+
+ // Clear history from localStorage when flow completes
+ this._clearHistory();
+ }
+
async back() {
if (!this.flow || this.history.length === 0) return;
const prev = this.history[this.history.length - 1];
diff --git a/console/tests/unit/services/onboarding-context-test.js b/console/tests/unit/services/onboarding-context-test.js
index 7a57af4f..53c6f85b 100644
--- a/console/tests/unit/services/onboarding-context-test.js
+++ b/console/tests/unit/services/onboarding-context-test.js
@@ -2,6 +2,9 @@ import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
import Service from '@ember/service';
+const CONTEXT_PREFIX = 'onboarding:context:';
+const KEYS_INDEX = `${CONTEXT_PREFIX}__keys__`;
+
class AppCacheStub extends Service {
store = new Map();
set(key, value) {
@@ -25,6 +28,44 @@ class QuotaCacheStub extends Service {
}
}
+/**
+ * Reports a quota failure the Firefox way — a legacy name that carries no numeric code —
+ * so _isQuotaError() has to fall all the way through its name checks.
+ */
+class LegacyQuotaCacheStub extends Service {
+ set() {
+ throw new DOMException('storage is full', 'NS_ERROR_DOM_QUOTA_REACHED');
+ }
+ get() {
+ return undefined;
+ }
+}
+
+/**
+ * Fails for a reason that is not about storage limits, which must not be swallowed.
+ */
+class BrokenCacheStub extends Service {
+ set() {
+ throw new Error('cache backend unavailable');
+ }
+ get() {
+ return undefined;
+ }
+}
+
+/**
+ * Writes succeed but reads blow up, standing in for a corrupted cache bucket.
+ */
+class UnreadableCacheStub extends Service {
+ store = new Map();
+ set(key, value) {
+ this.store.set(key, value);
+ }
+ get() {
+ throw new Error('cache read failed');
+ }
+}
+
class NotificationsStub extends Service {
warnings = [];
warning(message, options) {
@@ -85,4 +126,131 @@ module('Unit | Service | onboarding-context', function (hooks) {
assert.true(status.usingMemoryFallback, 'using memory fallback');
assert.strictEqual(service.get('big'), 'x', 'value still readable from memory');
});
+
+ test('it reports clean storage status before anything is written', function (assert) {
+ const service = this.owner.lookup('service:onboarding-context');
+
+ // Reads the tracked defaults before any write flips them.
+ assert.deepEqual(service.getStorageStatus(), {
+ quotaExceeded: false,
+ usingMemoryFallback: false,
+ memoryItemCount: 0,
+ });
+ });
+
+ test('it warns the user once when it starts falling back to memory', function (assert) {
+ this.owner.register('service:app-cache', QuotaCacheStub);
+ const service = this.owner.lookup('service:onboarding-context');
+ const notifications = this.owner.lookup('service:notifications');
+
+ service.set('a', 1, { persist: true });
+ service.set('b', 2, { persist: true });
+
+ assert.strictEqual(notifications.warnings.length, 1, 'the storage warning is not repeated per key');
+
+ const [message, options] = notifications.warnings[0];
+ assert.ok(message.includes('browser storage is full'));
+ assert.deepEqual(options, { timeout: 10000, clearDuration: 300 });
+ assert.strictEqual(service.getStorageStatus().memoryItemCount, 3, 'both values plus the key index live in memory');
+ });
+
+ test('it recognises a quota error reported only by its legacy name', function (assert) {
+ this.owner.register('service:app-cache', LegacyQuotaCacheStub);
+ const service = this.owner.lookup('service:onboarding-context');
+
+ service.set('a', 1, { persist: true });
+
+ assert.true(service.getStorageStatus().quotaExceeded, 'NS_ERROR_DOM_QUOTA_REACHED counts as a quota error');
+ assert.strictEqual(service.getFromCache('a'), 1, 'and the value is served from the memory fallback');
+ });
+
+ test('it falls back to memory even when there is no notifications service to warn through', function (assert) {
+ this.owner.register('service:app-cache', QuotaCacheStub);
+ const service = this.owner.lookup('service:onboarding-context');
+
+ Object.defineProperty(service, 'notifications', { configurable: true, value: null });
+
+ service.set('a', 1, { persist: true });
+
+ assert.true(service.getStorageStatus().quotaExceeded, 'the fallback still engages');
+ assert.strictEqual(service.getFromCache('a'), 1, 'and the value survives in memory');
+ });
+
+ test('a cache failure that is not about quota is not swallowed', function (assert) {
+ this.owner.register('service:app-cache', BrokenCacheStub);
+ const service = this.owner.lookup('service:onboarding-context');
+
+ assert.throws(() => service.set('a', 1, { persist: true }), /cache backend unavailable/, 'a genuine backend failure propagates');
+ assert.false(service.getStorageStatus().quotaExceeded, 'and is not mistaken for a quota problem');
+ });
+
+ test('an unreadable cache degrades to undefined rather than throwing', function (assert) {
+ this.owner.register('service:app-cache', UnreadableCacheStub);
+ const service = this.owner.lookup('service:onboarding-context');
+
+ service.set('a', 1, { persist: true });
+
+ assert.strictEqual(service.getFromCache('a'), undefined, 'the read error is contained');
+ assert.strictEqual(service.get('a'), 1, 'the in-memory value is still served');
+ });
+
+ test('restore returns an empty object when nothing was ever persisted', function (assert) {
+ const service = this.owner.lookup('service:onboarding-context');
+
+ assert.deepEqual(service.restore(), {}, 'a missing key index is treated as no keys');
+ });
+
+ test('restore reads every persisted key back and skips ones that went missing', function (assert) {
+ const service = this.owner.lookup('service:onboarding-context');
+ service.merge({ email: '[email protected]', org: 'Fleetbase' }, { persist: true });
+
+ // Add a key to the index whose value was never written, as an evicted entry would look.
+ const cache = this.owner.lookup('service:app-cache');
+ cache.set(KEYS_INDEX, [...cache.get(KEYS_INDEX), 'ghost']);
+
+ assert.deepEqual(service.restore(), { email: '[email protected]', org: 'Fleetbase' }, 'the ghost key is skipped rather than stored as undefined');
+ });
+
+ test('merge persists every non-sensitive key it merged', function (assert) {
+ const service = this.owner.lookup('service:onboarding-context');
+
+ service.merge({ email: '[email protected]', org: 'Fleetbase', password: 'secret' }, { persist: true });
+
+ assert.strictEqual(service.getFromCache('email'), '[email protected]');
+ assert.strictEqual(service.getFromCache('org'), 'Fleetbase');
+ assert.strictEqual(service.getFromCache('password'), undefined, 'sensitive fields are never persisted');
+ assert.deepEqual(this.owner.lookup('service:app-cache').get(KEYS_INDEX).sort(), ['email', 'org'], 'the key index tracks exactly what was written');
+ });
+
+ test('merge ignores anything that is not an object', function (assert) {
+ const service = this.owner.lookup('service:onboarding-context');
+ service.set('keep', 'me');
+
+ service.merge();
+ service.merge(null);
+ service.merge('not-an-object');
+
+ assert.deepEqual(service.data, { keep: 'me' }, 'existing context is left untouched');
+ });
+
+ test('del copes with a missing key index', function (assert) {
+ const service = this.owner.lookup('service:onboarding-context');
+ service.set('a', 1);
+
+ service.del('a');
+
+ assert.strictEqual(service.get('a'), undefined, 'del works without a persisted key index');
+ });
+
+ test('reset copes with a missing key index', function (assert) {
+ // Deliberately a separate test from the del() case: del() writes an empty key index
+ // on its way out, which would stop reset() from ever taking its own missing-index path.
+ const service = this.owner.lookup('service:onboarding-context');
+ service.set('a', 1);
+
+ service.reset();
+
+ assert.deepEqual(service.data, {}, 'reset works without a persisted key index');
+ assert.strictEqual(service.get('a'), undefined);
+ });
});
diff --git a/console/tests/unit/services/onboarding-orchestrator-test.js b/console/tests/unit/services/onboarding-orchestrator-test.js
index 22c59dc0..d52c219d 100644
--- a/console/tests/unit/services/onboarding-orchestrator-test.js
+++ b/console/tests/unit/services/onboarding-orchestrator-test.js
@@ -9,6 +9,22 @@ function registerFlow(owner, flow) {
owner.lookup('service:onboarding-registry').registerFlow(flow, { default: true });
}
+/**
+ * Replaces a localStorage method with one that throws, the way a private-browsing or
+ * quota-exhausted browser behaves. Storage methods live on Storage.prototype, so an own
+ * property on the instance shadows them until it is deleted.
+ */
+function breakStorage(method) {
+ Object.defineProperty(window.localStorage, method, {
+ configurable: true,
+ value() {
+ throw new Error('QuotaExceededError');
+ },
+ });
+
+ return () => delete window.localStorage[method];
+}
+
module('Unit | Service | onboarding-orchestrator', function (hooks) {
setupTest(hooks);
@@ -67,4 +83,450 @@ module('Unit | Service | onboarding-orchestrator', function (hooks) {
assert.strictEqual(service.current.id, 'a');
});
+
+ test('it starts out idle with no flow and a default history key', function (assert) {
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ // Reads every tracked default before a flow assigns over it.
+ assert.strictEqual(service.flow, null);
+ assert.strictEqual(service.current, null);
+ assert.strictEqual(service.sessionId, null);
+ assert.deepEqual(service.history, []);
+ assert.strictEqual(service.historyStorageKey, 'onboarding:history:default', 'the key falls back to "default" with no flow loaded');
+ });
+
+ test('start with no arguments uses the registry default flow', async function (assert) {
+ registerFlow(this.owner, { id: 'registry-default', entry: 'a', steps: [{ id: 'a' }] });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start();
+
+ assert.strictEqual(service.flow.id, 'registry-default');
+ assert.strictEqual(service.current.id, 'a');
+ assert.strictEqual(service.sessionId, null, 'no session id was supplied');
+ assert.strictEqual(service.historyStorageKey, 'onboarding:history:registry-default');
+ });
+
+ test('start records the session id and the flow wrapper', async function (assert) {
+ registerFlow(this.owner, { id: 'w', entry: 'a', wrapper: 'onboarding/shell', steps: [{ id: 'a' }] });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('w', { sessionId: 'sess-1' });
+
+ assert.strictEqual(service.wrapper, 'onboarding/shell');
+ assert.strictEqual(service.sessionId, 'sess-1');
+ });
+
+ test('start runs the flow-level start hooks around the entry step', async function (assert) {
+ const calls = [];
+ registerFlow(this.owner, {
+ id: 'hooks',
+ entry: 'a',
+ steps: [{ id: 'a' }],
+ onFlowWillStart(flow, orchestrator) {
+ calls.push(['willStart', flow.id, orchestrator.current]);
+ },
+ onFlowDidStart(flow, orchestrator) {
+ calls.push(['didStart', flow.id, orchestrator.current?.id]);
+ },
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('hooks');
+
+ assert.deepEqual(calls, [
+ ['willStart', 'hooks', null],
+ ['didStart', 'hooks', 'a'],
+ ]);
+ });
+
+ test('goto runs the step-change hooks with the outgoing and incoming steps', async function (assert) {
+ const calls = [];
+ registerFlow(this.owner, {
+ id: 'sc',
+ entry: 'a',
+ steps: [{ id: 'a', next: 'b' }, { id: 'b' }],
+ onStepWillChange(step, previous) {
+ calls.push(['will', step.id, previous?.id ?? null]);
+ },
+ onStepDidChange(step, previous) {
+ calls.push(['did', step.id, previous?.id ?? null]);
+ },
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('sc');
+ await service.next();
+
+ assert.deepEqual(calls, [
+ ['will', 'a', null],
+ ['did', 'a', null],
+ ['will', 'b', 'a'],
+ ['did', 'b', 'a'],
+ ]);
+ });
+
+ test('goto skips a step whose guard rejects the current context', async function (assert) {
+ registerFlow(this.owner, {
+ id: 'guarded',
+ entry: 'a',
+ steps: [{ id: 'a', next: 'b' }, { id: 'b', next: 'c', guard: () => false }, { id: 'c' }],
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('guarded');
+ await service.next();
+
+ assert.strictEqual(service.current.id, 'c', 'the guarded step is skipped straight through to its own next');
+ assert.deepEqual(
+ service.history.map((step) => step.id),
+ ['a'],
+ 'a step that was never entered is not recorded in history'
+ );
+ });
+
+ test('a skipped step resolves a function next() to find where to go', async function (assert) {
+ registerFlow(this.owner, {
+ id: 'guarded-fn',
+ entry: 'a',
+ steps: [{ id: 'a', next: 'b' }, { id: 'b', guard: () => false, next: () => 'c' }, { id: 'c' }],
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('guarded-fn');
+ await service.next();
+
+ assert.strictEqual(service.current.id, 'c');
+ });
+
+ test('skipping the last step completes the flow', async function (assert) {
+ const ended = [];
+ registerFlow(this.owner, {
+ id: 'guarded-last',
+ entry: 'a',
+ steps: [
+ { id: 'a', next: 'b' },
+ { id: 'b', guard: () => false },
+ ],
+ onFlowWillEnd(step) {
+ ended.push(step.id);
+ },
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('guarded-last');
+ await service.next();
+
+ assert.strictEqual(service.current, null, 'a guarded final step ends the flow rather than looping');
+ assert.deepEqual(ended, ['b'], 'the skipped step is reported as the one the flow ended on');
+ });
+
+ test('consecutive guarded steps are skipped without recursing on the previous step', async function (assert) {
+ registerFlow(this.owner, {
+ id: 'guarded-chain',
+ entry: 'a',
+ steps: [{ id: 'a', next: 'b' }, { id: 'b', next: 'c', guard: () => false }, { id: 'c', next: 'd', guard: () => false }, { id: 'd' }],
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('guarded-chain');
+ await service.next();
+
+ assert.strictEqual(service.current.id, 'd', 'both guarded steps are stepped over in one move');
+ });
+
+ test('a guard receives the onboarding context', async function (assert) {
+ let received;
+ registerFlow(this.owner, {
+ id: 'guard-ctx',
+ entry: 'a',
+ steps: [
+ { id: 'a', next: 'b' },
+ {
+ id: 'b',
+ guard: (context) => {
+ received = context;
+ return true;
+ },
+ },
+ ],
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('guard-ctx');
+ await service.next();
+
+ assert.strictEqual(received, this.owner.lookup('service:onboarding-context'), 'the real context service is handed to the guard');
+ assert.strictEqual(service.current.id, 'b', 'a passing guard does not skip the step');
+ });
+
+ test('goto refuses to run before a flow is started', async function (assert) {
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await assert.rejects(service.goto('a'), /No active onboarding flow/);
+ });
+
+ test('next is a no-op when no flow is active', async function (assert) {
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ assert.strictEqual(await service.next(), undefined, 'returns without touching state');
+ assert.strictEqual(service.current, null);
+ });
+
+ test('next does not record the same step in history twice', async function (assert) {
+ registerFlow(this.owner, {
+ id: 'loop',
+ entry: 'a',
+ steps: [
+ { id: 'a', next: 'b' },
+ { id: 'b', next: 'a' },
+ ],
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('loop');
+ await service.next();
+ await service.next();
+
+ assert.strictEqual(service.current.id, 'a', 'the flow looped back round');
+ assert.deepEqual(
+ service.history.map((step) => step.id),
+ ['a', 'b'],
+ 'history holds each step once'
+ );
+
+ await service.next();
+
+ assert.deepEqual(
+ service.history.map((step) => step.id),
+ ['a', 'b'],
+ 'revisiting a step does not duplicate the entry'
+ );
+ });
+
+ test('completing a flow runs the end hooks and clears persisted history', async function (assert) {
+ const calls = [];
+ registerFlow(this.owner, {
+ id: 'ending',
+ entry: 'a',
+ steps: [{ id: 'a' }],
+ onFlowWillEnd(step, orchestrator) {
+ calls.push(['willEnd', step.id, orchestrator.current?.id]);
+ },
+ onFlowDidEnd(step, orchestrator) {
+ calls.push(['didEnd', step.id, orchestrator.current]);
+ },
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('ending');
+ assert.strictEqual(window.localStorage.getItem('onboarding:history:ending'), null, 'precondition: nothing persisted yet');
+
+ await service.next();
+
+ assert.deepEqual(calls, [
+ ['willEnd', 'a', 'a'],
+ ['didEnd', 'a', null],
+ ]);
+ assert.strictEqual(service.current, null, 'the flow is finished');
+ assert.strictEqual(window.localStorage.getItem('onboarding:history:ending'), null, 'history is cleared on completion');
+ });
+
+ test('back is a no-op with no flow or an empty history', async function (assert) {
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+ assert.strictEqual(await service.back(), undefined, 'no flow at all');
+
+ registerFlow(this.owner, { id: 'nb', entry: 'a', steps: [{ id: 'a', next: 'b' }, { id: 'b' }] });
+ await service.start('nb');
+
+ await service.back();
+
+ assert.strictEqual(service.current.id, 'a', 'nothing to go back to, so we stay put');
+ });
+
+ test('back refuses to leave a step that disallows it', async function (assert) {
+ registerFlow(this.owner, {
+ id: 'noback',
+ entry: 'a',
+ steps: [{ id: 'a', next: 'b', allowBack: false }, { id: 'b' }],
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('noback');
+ await service.next();
+ assert.strictEqual(service.current.id, 'b');
+
+ await service.back();
+
+ assert.strictEqual(service.current.id, 'b', 'the step marked allowBack: false is not returned to');
+ assert.strictEqual(service.history.length, 1, 'and history is left intact');
+ });
+
+ test('getCurrentPath returns null for a flow without paths', async function (assert) {
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+ assert.strictEqual(service.getCurrentPath(), null, 'no flow at all');
+
+ registerFlow(this.owner, { id: 'np', entry: 'a', steps: [{ id: 'a' }] });
+ await service.start('np');
+
+ assert.strictEqual(service.getCurrentPath(), null, 'a flow with no paths defined');
+ });
+
+ test('getCurrentPath finds the path containing the current step', async function (assert) {
+ const driver = { steps: [{ id: 'b' }] };
+ registerFlow(this.owner, {
+ id: 'paths',
+ entry: 'a',
+ steps: [{ id: 'a', next: 'b' }, { id: 'b' }],
+ paths: { empty: {}, driver },
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('paths');
+ assert.strictEqual(service.getCurrentPath(), null, 'step a belongs to no declared path');
+
+ await service.next();
+
+ assert.strictEqual(service.getCurrentPath(), driver, 'step b resolves to the driver path');
+ });
+
+ test('isStepInPath allows everything when the flow declares no paths', async function (assert) {
+ registerFlow(this.owner, { id: 'isp-none', entry: 'a', steps: [{ id: 'a' }] });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+ await service.start('isp-none');
+
+ assert.true(service.isStepInPath('anything'), 'with no path context every step is valid');
+ });
+
+ test('isStepInPath is limited to the steps of the active path', async function (assert) {
+ registerFlow(this.owner, {
+ id: 'isp',
+ entry: 'a',
+ steps: [{ id: 'a' }, { id: 'b' }],
+ paths: { main: { steps: [{ id: 'a' }] } },
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+ await service.start('isp');
+
+ assert.true(service.isStepInPath('a'), 'a is in the active path');
+ assert.false(service.isStepInPath('b'), 'b is not');
+ });
+
+ test('isStepInPath rejects every step when the active path lists none', async function (assert) {
+ registerFlow(this.owner, {
+ id: 'isp-empty',
+ entry: 'a',
+ steps: [{ id: 'a' }],
+ paths: { main: { steps: [{ id: 'a' }] } },
+ });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+ await service.start('isp-empty');
+
+ // getCurrentPath() only ever returns a path that already matched on `pathDef.steps`,
+ // so a truthy path with no steps array cannot arise through the public API. Stub the
+ // lookup directly to pin down what isStepInPath() does if one ever did.
+ Object.defineProperty(service, 'getCurrentPath', { configurable: true, value: () => ({}) });
+
+ assert.false(service.isStepInPath('a'), 'a path with no steps admits nothing');
+ });
+
+ test('history is persisted as step ids and restored on resume', async function (assert) {
+ const flow = { id: 'resume', entry: 'a', steps: [{ id: 'a', next: 'b' }, { id: 'b', next: 'c' }, { id: 'c' }] };
+ registerFlow(this.owner, flow);
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('resume');
+ await service.next();
+
+ assert.deepEqual(JSON.parse(window.localStorage.getItem('onboarding:history:resume')), ['a'], 'only ids are stored');
+
+ await service.start('resume', { resume: true });
+
+ assert.deepEqual(
+ service.history.map((step) => step.id),
+ ['a'],
+ 'the stored ids are rehydrated into step objects'
+ );
+ assert.strictEqual(service.history[0], flow.steps[0], 'and resolve to the real step objects');
+ });
+
+ test('resuming ignores stored ids that are no longer part of the flow', async function (assert) {
+ registerFlow(this.owner, { id: 'drift', entry: 'a', steps: [{ id: 'a', next: 'b' }, { id: 'b' }] });
+ window.localStorage.setItem('onboarding:history:drift', JSON.stringify(['a', 'removed-step']));
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('drift', { resume: true });
+
+ assert.deepEqual(
+ service.history.map((step) => step.id),
+ ['a'],
+ 'ids with no matching step are dropped'
+ );
+ });
+
+ test('resuming with nothing stored leaves history empty', async function (assert) {
+ registerFlow(this.owner, { id: 'fresh', entry: 'a', steps: [{ id: 'a' }] });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('fresh', { resume: true });
+
+ assert.deepEqual(service.history, []);
+ });
+
+ test('resuming from unparseable stored history resets it instead of failing', async function (assert) {
+ registerFlow(this.owner, { id: 'corrupt', entry: 'a', steps: [{ id: 'a' }] });
+ window.localStorage.setItem('onboarding:history:corrupt', 'not-json');
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ await service.start('corrupt', { resume: true });
+
+ assert.deepEqual(service.history, [], 'corrupt history is discarded');
+ assert.strictEqual(service.current.id, 'a', 'and the flow still starts');
+ });
+
+ test('the history helpers are inert until a flow is loaded', function (assert) {
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+
+ service._persistHistory();
+ service._restoreHistory();
+ service._clearHistory();
+
+ assert.deepEqual(service.history, [], 'no flow means nothing to persist, restore or clear');
+ assert.strictEqual(window.localStorage.getItem('onboarding:history:default'), null, 'and nothing is written under the fallback key');
+ });
+
+ test('a storage failure while persisting history does not break navigation', async function (assert) {
+ registerFlow(this.owner, { id: 'nostore', entry: 'a', steps: [{ id: 'a', next: 'b' }, { id: 'b' }] });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+ await service.start('nostore');
+
+ const restore = breakStorage('setItem');
+ try {
+ await service.next();
+ } finally {
+ restore();
+ }
+
+ assert.strictEqual(service.current.id, 'b', 'the step change still happened');
+ assert.deepEqual(
+ service.history.map((step) => step.id),
+ ['a'],
+ 'in-memory history is still correct'
+ );
+ });
+
+ test('a storage failure while clearing history does not break flow completion', async function (assert) {
+ registerFlow(this.owner, { id: 'noclear', entry: 'a', steps: [{ id: 'a' }] });
+ const service = this.owner.lookup('service:onboarding-orchestrator');
+ await service.start('noclear');
+
+ const restore = breakStorage('removeItem');
+ try {
+ await service.next();
+ } finally {
+ restore();
+ }
+
+ assert.strictEqual(service.current, null, 'the flow still completed');
+ });
});
diff --git a/console/tests/unit/services/onboarding-registry-test.js b/console/tests/unit/services/onboarding-registry-test.js
index 5ea6eb53..c4689753 100644
--- a/console/tests/unit/services/onboarding-registry-test.js
+++ b/console/tests/unit/services/onboarding-registry-test.js
@@ -13,6 +13,15 @@ function validFlow(overrides = {}) {
module('Unit | Service | onboarding-registry', function (hooks) {
setupTest(hooks);
+ test('it starts out pointing at the built-in default flow', function (assert) {
+ const service = this.owner.lookup('service:onboarding-registry');
+
+ // Read defaultFlow before anything writes to it: @tracked initializers only run on
+ // first access, so a test that assigns first never evaluates this one.
+ assert.strictEqual(service.defaultFlow, 'default@v1');
+ assert.strictEqual(service.getFlow(service.defaultFlow)?.id, 'default@v1', 'and the app registers a flow under that id at boot');
+ });
+
test('registerFlow stores a valid flow and getFlow returns it', function (assert) {
const service = this.owner.lookup('service:onboarding-registry');
const flow = validFlow();