mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-23 04:46:08 -08:00
feat(controller): route status messages through notifications
- Send controller connected and disabled messages over a new IPC channel so they follow ankiConnect.behavior.notificationType - Remove the separate controller status toast from the overlay - Only announce connected controllers when controller.enabled is on - Announce a controller again after it reconnects, and update usage docs
This commit is contained in:
15 files changed
+84
-167
No files matched your search
@@ -3,99 +3,40 @@ import test from 'node:test';
|
||||
|
||||
import { createControllerStatusIndicator } from './controller-status-indicator.js';
|
||||
|
||||
function createClassList(initialTokens: string[] = []) {
|
||||
const tokens = new Set(initialTokens);
|
||||
return {
|
||||
add: (...entries: string[]) => {
|
||||
for (const entry of entries) tokens.add(entry);
|
||||
},
|
||||
remove: (...entries: string[]) => {
|
||||
for (const entry of entries) tokens.delete(entry);
|
||||
},
|
||||
contains: (entry: string) => tokens.has(entry),
|
||||
};
|
||||
}
|
||||
const pad = (id: string, index = 0) => ({ id, index, mapping: 'standard', connected: true });
|
||||
|
||||
test('controller status indicator shows once when a controller is first detected and auto-hides', () => {
|
||||
let nextTimerId = 1;
|
||||
const scheduled = new Map<number, () => void>();
|
||||
const classList = createClassList(['hidden']);
|
||||
const toast = {
|
||||
textContent: '',
|
||||
classList,
|
||||
};
|
||||
test('controller status indicator notifies once when a controller is first detected', () => {
|
||||
const messages: string[] = [];
|
||||
const indicator = createControllerStatusIndicator((message) => messages.push(message));
|
||||
|
||||
const indicator = createControllerStatusIndicator({ controllerStatusToast: toast } as never, {
|
||||
durationMs: 1500,
|
||||
setTimeout: (callback: () => void) => {
|
||||
const id = nextTimerId++;
|
||||
scheduled.set(id, callback);
|
||||
return id as never;
|
||||
},
|
||||
clearTimeout: (id) => {
|
||||
scheduled.delete(id as never as number);
|
||||
},
|
||||
});
|
||||
indicator.update({ connectedGamepads: [], activeGamepadId: null });
|
||||
assert.deepEqual(messages, []);
|
||||
|
||||
indicator.update({
|
||||
connectedGamepads: [],
|
||||
activeGamepadId: null,
|
||||
});
|
||||
|
||||
assert.equal(classList.contains('hidden'), true);
|
||||
assert.equal(toast.textContent, '');
|
||||
|
||||
indicator.update({
|
||||
connectedGamepads: [{ id: 'pad-1', index: 0, mapping: 'standard', connected: true }],
|
||||
activeGamepadId: 'pad-1',
|
||||
});
|
||||
|
||||
assert.equal(classList.contains('hidden'), false);
|
||||
assert.match(toast.textContent, /controller detected/i);
|
||||
assert.match(toast.textContent, /pad-1/i);
|
||||
assert.equal(scheduled.size, 1);
|
||||
|
||||
indicator.update({
|
||||
connectedGamepads: [{ id: 'pad-1', index: 0, mapping: 'standard', connected: true }],
|
||||
activeGamepadId: 'pad-1',
|
||||
});
|
||||
|
||||
assert.equal(scheduled.size, 1);
|
||||
|
||||
const [hide] = scheduled.values();
|
||||
hide?.();
|
||||
|
||||
assert.equal(classList.contains('hidden'), true);
|
||||
assert.equal(toast.textContent, '');
|
||||
indicator.update({ connectedGamepads: [pad('pad-1')], activeGamepadId: 'pad-1' });
|
||||
indicator.update({ connectedGamepads: [pad('pad-1')], activeGamepadId: 'pad-1' });
|
||||
assert.deepEqual(messages, ['Controller connected: pad-1']);
|
||||
});
|
||||
|
||||
test('controller status indicator announces newly detected controllers after startup', () => {
|
||||
const toast = {
|
||||
textContent: '',
|
||||
classList: createClassList(['hidden']),
|
||||
};
|
||||
|
||||
const indicator = createControllerStatusIndicator({ controllerStatusToast: toast } as never, {
|
||||
setTimeout: () => 1 as never,
|
||||
clearTimeout: () => {},
|
||||
});
|
||||
const messages: string[] = [];
|
||||
const indicator = createControllerStatusIndicator((message) => messages.push(message));
|
||||
|
||||
indicator.update({ connectedGamepads: [pad('pad-1')], activeGamepadId: 'pad-1' });
|
||||
indicator.update({
|
||||
connectedGamepads: [{ id: 'pad-1', index: 0, mapping: 'standard', connected: true }],
|
||||
connectedGamepads: [pad('pad-1'), pad('pad-2', 1)],
|
||||
activeGamepadId: 'pad-1',
|
||||
});
|
||||
|
||||
toast.classList.add('hidden');
|
||||
toast.textContent = '';
|
||||
assert.deepEqual(messages, ['Controller connected: pad-1', 'Controller connected: pad-2']);
|
||||
});
|
||||
|
||||
indicator.update({
|
||||
connectedGamepads: [
|
||||
{ id: 'pad-1', index: 0, mapping: 'standard', connected: true },
|
||||
{ id: 'pad-2', index: 1, mapping: 'standard', connected: true },
|
||||
],
|
||||
activeGamepadId: 'pad-1',
|
||||
});
|
||||
test('controller status indicator announces a controller again after it reconnects', () => {
|
||||
const messages: string[] = [];
|
||||
const indicator = createControllerStatusIndicator((message) => messages.push(message));
|
||||
|
||||
assert.equal(toast.classList.contains('hidden'), false);
|
||||
assert.match(toast.textContent, /pad-2/i);
|
||||
indicator.update({ connectedGamepads: [pad('pad-1')], activeGamepadId: 'pad-1' });
|
||||
indicator.update({ connectedGamepads: [], activeGamepadId: null });
|
||||
indicator.update({ connectedGamepads: [pad('pad-1')], activeGamepadId: 'pad-1' });
|
||||
|
||||
assert.equal(messages.length, 2);
|
||||
});
|
||||
Reference in new issue
Block a user