fix(desktop): resolve native notification action indexes
Adapted from https://github.com/NousResearch/hermes-agent/pull/51893 (a70b0991b2d72796479d63532ccf0fac15b17ba0). Co-authored-by: brooklyn! <brooklyn.bb.nicholson@gmail.com>
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { test } from 'vitest'
|
||||
|
||||
import { resolveNotificationAction } from './notification-actions'
|
||||
|
||||
const actions = [
|
||||
{ id: 'approve', text: 'Approve' },
|
||||
{ id: 'reject', text: 'Reject' }
|
||||
]
|
||||
|
||||
test('prefers Electron event details and supports the legacy positional index', () => {
|
||||
assert.deepEqual(resolveNotificationAction(actions, { actionIndex: 1 }, undefined), actions[1])
|
||||
assert.deepEqual(resolveNotificationAction(actions, {}, 0), actions[0])
|
||||
assert.deepEqual(resolveNotificationAction(actions, { actionIndex: 1 }, 0), actions[1])
|
||||
})
|
||||
|
||||
test('ignores missing and out-of-range action indexes', () => {
|
||||
assert.equal(resolveNotificationAction(actions, {}, undefined), null)
|
||||
assert.equal(resolveNotificationAction(actions, { actionIndex: -1 }, undefined), null)
|
||||
assert.equal(resolveNotificationAction(actions, { actionIndex: 2 }, undefined), null)
|
||||
})
|
||||
@@ -0,0 +1,29 @@
|
||||
function isActionIndex(value: unknown): value is number {
|
||||
return typeof value === 'number' && Number.isInteger(value)
|
||||
}
|
||||
|
||||
function notificationActionIndex(actionEvent: unknown, legacyIndex: unknown): number | null {
|
||||
if (actionEvent && typeof actionEvent === 'object' && 'actionIndex' in actionEvent) {
|
||||
const { actionIndex } = actionEvent as { actionIndex?: unknown }
|
||||
|
||||
if (isActionIndex(actionIndex)) {
|
||||
return actionIndex
|
||||
}
|
||||
}
|
||||
|
||||
return isActionIndex(legacyIndex) ? legacyIndex : null
|
||||
}
|
||||
|
||||
export function resolveNotificationAction<T>(
|
||||
actions: readonly T[],
|
||||
actionEvent: unknown,
|
||||
legacyIndex: unknown
|
||||
): T | null {
|
||||
const index = notificationActionIndex(actionEvent, legacyIndex)
|
||||
|
||||
if (index === null || index < 0 || index >= actions.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
return actions[index] ?? null
|
||||
}
|
||||
Reference in New Issue
Block a user