71 lines
2.5 KiB
JavaScript
71 lines
2.5 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
readNavigationFocus,
|
|
rememberNavigationFocus,
|
|
} from '../../resources/js/lib/navigation-focus.ts';
|
|
|
|
test('tab focus survives reload but is isolated by actor and rechecked against authorised dossiers', () => {
|
|
const old = globalThis.sessionStorage,
|
|
values = new Map();
|
|
globalThis.sessionStorage = {
|
|
getItem: (k) => values.get(k),
|
|
setItem: (k, v) => values.set(k, v),
|
|
removeItem: (k) => values.delete(k),
|
|
};
|
|
try {
|
|
const focus = { case_id: 'synthetic-A', step_code: 'S08', version: 3 };
|
|
rememberNavigationFocus(1, focus);
|
|
assert.deepEqual(readNavigationFocus(1, ['synthetic-A']), focus);
|
|
assert.equal(readNavigationFocus(2, ['synthetic-A']), null);
|
|
assert.equal(readNavigationFocus(1, ['synthetic-B']), null);
|
|
const firstTab = globalThis.sessionStorage;
|
|
globalThis.sessionStorage = {
|
|
getItem: () => null,
|
|
setItem: (k, v) => values.set(k, v),
|
|
removeItem: (k) => values.delete(k),
|
|
};
|
|
assert.equal(readNavigationFocus(1, ['synthetic-A']), null);
|
|
globalThis.sessionStorage = firstTab;
|
|
assert.deepEqual(readNavigationFocus(1, ['synthetic-A']), focus);
|
|
rememberNavigationFocus(1, null);
|
|
assert.equal(readNavigationFocus(1, ['synthetic-A']), null);
|
|
} finally {
|
|
globalThis.sessionStorage = old;
|
|
}
|
|
});
|
|
test('corrupt, invalid and unavailable browser persistence cannot become authorisation or prevent use', () => {
|
|
const old = globalThis.sessionStorage;
|
|
try {
|
|
for (const value of [
|
|
'broken',
|
|
'{"case_id":"A","step_code":"S99","version":1}',
|
|
'{"case_id":"A","step_code":null,"version":-1}',
|
|
]) {
|
|
globalThis.sessionStorage = { getItem: () => value };
|
|
assert.equal(readNavigationFocus(1, ['A']), null);
|
|
}
|
|
globalThis.sessionStorage = {
|
|
getItem() {
|
|
throw Error('blocked');
|
|
},
|
|
setItem() {
|
|
throw Error('blocked');
|
|
},
|
|
removeItem() {
|
|
throw Error('blocked');
|
|
},
|
|
};
|
|
assert.equal(readNavigationFocus(1, ['A']), null);
|
|
assert.doesNotThrow(() =>
|
|
rememberNavigationFocus(1, {
|
|
case_id: 'A',
|
|
step_code: null,
|
|
version: 1,
|
|
}),
|
|
);
|
|
} finally {
|
|
globalThis.sessionStorage = old;
|
|
}
|
|
});
|