Files
faceclaw/tests/switcher-order.test.cjs
T

155 lines
8.3 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const ts = require('typescript');
function fixture() {
const source = ts.createSourceFile('shell.ts', fs.readFileSync('app/ui/shell/shell.ts', 'utf8'), ts.ScriptTarget.Latest, true);
const cls = source.statements.find(n => ts.isClassDeclaration(n) && n.name.text === 'Shell');
const names = new Set(['moveSwitcherWindow', 'openSwitcherMoveMenu', 'switcherWindows', 'foregroundWindow',
'openEscapeMenu', 'registerWindow', 'removeWindow', 'noteWindowVisible', 'mostRecentWindowIndex']);
const methods = cls.members.filter(n => names.has(n.name?.getText(source)));
assert.equal(methods.length, names.size);
const overlay = source.statements.find(n => ts.isClassDeclaration(n) && n.name.text === 'ShellOverlayMenuLayer');
class MenuLayer {
constructor(title, items) { this.title = title; this.items = items; }
selectItem(index) { this.selectedIndex = index; }
}
const context = { exports: {}, MenuLayer, appViewportRect: () => ({ x: 0, y: 0, width: 576 }),
CONTEXT_MENU_DIM: 0.5, brightnessSetting: { get: () => 'auto' }, makeInputEvent: e => e,
appFullscreenSetting: () => ({ get: () => false }), appKeepAwakeSetting: () => ({ label: 'Keep screen awake' }),
toggleSettingMenuItem: setting => ({ label: setting.label }),
swapMenuGesturesSetting: { get: () => false }, GESTURE_SHORT_THEN_LONG_PRESS: 'tap-hold',
};
vm.runInNewContext(ts.transpileModule(`${overlay.getText(source)}\nexport class Shell { ${methods.map(n => n.getText(source)).join('\n')} }`, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
const s = new context.exports.Shell();
const foregroundCalls = [], closed = [], input = [];
const window = (windowId, appId = windowId, closeable = true, title = windowId) => ({
windowId, appId, closeable, title, requestRender() {},
setForeground: value => foregroundCalls.push([windowId, value]),
close: () => closed.push(windowId), handleInput: event => input.push([windowId, event.type]),
});
const launcher = window('launcher', 'launcher', false, 'Apps');
const first = window('terminal:one', 'terminal', true, 'Terminal');
const second = window('terminal:two', 'terminal', true, 'Terminal');
const third = window('calendar', 'calendar', true, 'Calendar');
const layers = [];
let changed = 0, renders = 0;
Object.assign(s, {
windows: [launcher, first, second, third], selectedIndex: 1, focus: 'sidebar', screenOn: true,
attention: new Map(), mruWindowIds: ['terminal:one', 'calendar', 'terminal:two', 'launcher'],
config: { voiceInputEnabled: false, onWindowsChanged: () => changed++, requestShellRender: () => renders++ },
yieldFocusToSidebar() { this.focus = 'sidebar'; },
stack: { isAtBase: () => !layers.length, push: layer => layers.push(layer),
pop: () => layers.pop()?.onRemoved?.() },
});
return { s, launcher, first, second, third, window, foregroundCalls, closed, input,
ids: () => Array.from(s.windows, w => w.windowId), changed: () => changed, renders: () => renders,
menu: () => layers.at(-1), choose(label) {
const item = layers.at(-1).items.find(item => item.label === label);
assert.ok(item, label); item.onSelect({ stack: s.stack });
} };
}
test('moving open windows in either direction keeps foreground, MRU and pinned launcher stable', () => {
const h = fixture(); h.s.focus = 'window';
const mru = Array.from(h.s.mruWindowIds);
assert.equal(h.s.moveSwitcherWindow(h.first.windowId, h.third.windowId), true);
assert.deepEqual(h.ids(), ['launcher', 'terminal:two', 'calendar', 'terminal:one']);
assert.equal(h.s.foregroundWindow(), h.first);
assert.equal(h.s.selectedIndex, 3);
assert.equal(h.s.focus, 'window');
assert.deepEqual(Array.from(h.s.mruWindowIds), mru);
assert.deepEqual(h.foregroundCalls, []);
assert.deepEqual(h.closed, []);
assert.equal(h.changed(), 1); assert.equal(h.renders(), 1);
assert.equal(h.s.moveSwitcherWindow(h.first.windowId, h.second.windowId), true);
assert.deepEqual(h.ids(), ['launcher', 'terminal:one', 'terminal:two', 'calendar']);
assert.equal(h.s.foregroundWindow(), h.first);
});
test('system menu opens a numbered position picker and moves the captured window, even with duplicate titles', () => {
const h = fixture();
h.s.openEscapeMenu(); h.choose('Move in app switcher');
assert.equal(h.menu().title, 'Move app to position');
assert.deepEqual(Array.from(h.menu().items, item => item.label), ['1. Terminal', '2. Terminal', '3. Calendar']);
assert.equal(h.menu().selectedIndex, 0);
// A delayed focus change must not move a different app than the menu captured.
h.s.selectedIndex = 2;
h.choose('3. Calendar');
assert.deepEqual(h.ids(), ['launcher', 'terminal:two', 'calendar', 'terminal:one']);
assert.equal(h.s.foregroundWindow(), h.second);
assert.equal(h.menu(), undefined);
assert.equal(h.s.focus, 'sidebar');
});
test('cancelling the picker and choosing the current slot leave ordering unchanged', () => {
const h = fixture(); const before = h.ids();
h.s.openSwitcherMoveMenu(h.first.windowId); h.s.stack.pop();
assert.deepEqual(h.ids(), before);
h.s.openSwitcherMoveMenu(h.first.windowId); h.choose('1. Terminal');
assert.deepEqual(h.ids(), before);
assert.equal(h.changed(), 0);
});
test('closing the pending source or destination prevents a stale move from affecting another window', () => {
for (const removed of ['terminal:one', 'calendar']) {
const h = fixture(); h.s.openSwitcherMoveMenu(h.first.windowId);
h.s.removeWindow(removed);
const afterClose = h.ids();
h.choose('3. Calendar');
assert.deepEqual(h.ids(), afterClose);
assert.equal(h.menu(), undefined);
}
});
test('temporary status windows and pinned slots stay fixed, including selected status foreground', () => {
const h = fixture();
const hidden = h.window('notifications:temporary');
const pinned = h.window('another-pinned', 'pinned', false);
h.s.windows.splice(2, 0, hidden, pinned);
h.s.selectedIndex = 2;
h.s.selectedStatus = { transient: true, host: hidden, item: { key: 'notification' } };
assert.equal(h.s.moveSwitcherWindow(hidden.windowId, h.first.windowId), false);
h.s.moveSwitcherWindow(h.first.windowId, h.third.windowId);
assert.deepEqual(h.ids(), ['launcher', 'terminal:two', 'notifications:temporary', 'another-pinned', 'calendar', 'terminal:one']);
assert.equal(h.s.foregroundWindow(), hidden);
assert.equal(h.s.selectedStatus.host, hidden);
h.s.openEscapeMenu();
assert.ok(!h.menu().items.some(item => item.label === 'Move in app switcher'));
h.s.stack.pop();
h.s.openSwitcherMoveMenu(h.first.windowId);
assert.deepEqual(Array.from(h.menu().items, item => item.label), ['1. Terminal', '2. Calendar', '3. Terminal']);
});
test('new windows append, replacements keep their slot, and closing uses unchanged recency after a move', () => {
const h = fixture(); h.s.moveSwitcherWindow(h.first.windowId, h.third.windowId);
h.s.registerWindow(h.window('new'));
assert.equal(h.ids().at(-1), 'new');
const replacement = h.window(h.second.windowId, 'terminal', true, 'Renamed session');
h.s.registerWindow(replacement);
assert.equal(h.s.windows[1], replacement);
assert.equal(h.s.foregroundWindow(), h.first);
h.s.removeWindow(h.first.windowId);
assert.equal(h.s.foregroundWindow(), h.third);
});
test('pinned, missing, single-window, screen-off and overlay cases cannot start a reorder', () => {
const h = fixture(); const before = h.ids();
for (const [from, to] of [['launcher', 'calendar'], ['calendar', 'launcher'], ['missing', 'calendar']]) {
assert.equal(h.s.moveSwitcherWindow(from, to), false);
}
h.s.openSwitcherMoveMenu('missing'); assert.equal(h.menu(), undefined);
h.s.screenOn = false; h.s.openSwitcherMoveMenu(h.first.windowId); assert.equal(h.menu(), undefined);
h.s.screenOn = true; h.s.stack.push({});
h.s.openSwitcherMoveMenu(h.first.windowId); assert.equal(h.menu().title, undefined); h.s.stack.pop();
assert.deepEqual(h.ids(), before);
h.s.selectedIndex = 0; h.s.openEscapeMenu();
assert.ok(!h.menu().items.some(item => item.label === 'Move in app switcher')); h.s.stack.pop();
h.s.windows = [h.launcher, h.first]; h.s.selectedIndex = 1; h.s.openEscapeMenu();
assert.ok(!h.menu().items.some(item => item.label === 'Move in app switcher'));
});