Skip to content

Commit f89bdfa

Browse files
committed
Add playwright test
1 parent 4779f8a commit f89bdfa

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

browser_tests/ComfyPage.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import dotenv from 'dotenv'
66
dotenv.config()
77
import * as fs from 'fs'
88
import { NodeBadgeMode } from '../src/types/nodeSource'
9-
import { NodeId } from '../src/types/comfyWorkflow'
9+
import type { NodeId } from '../src/types/comfyWorkflow'
10+
import type { KeyCombo } from '../src/types/keyBindingTypes'
1011
import { ManageGroupNode } from './helpers/manageGroupNode'
1112
import { ComfyTemplates } from './helpers/templates'
1213

@@ -488,6 +489,34 @@ export class ComfyPage {
488489
return `./browser_tests/assets/${fileName}`
489490
}
490491

492+
async registerKeybinding(keyCombo: KeyCombo, command: () => void) {
493+
await this.page.evaluate(
494+
({ keyCombo, commandStr }) => {
495+
const app = window['app']
496+
const randomSuffix = Math.random().toString(36).substring(2, 8)
497+
const extensionName = `TestExtension_${randomSuffix}`
498+
const commandId = `TestCommand_${randomSuffix}`
499+
500+
app.registerExtension({
501+
name: extensionName,
502+
keybindings: [
503+
{
504+
combo: keyCombo,
505+
commandId: commandId
506+
}
507+
],
508+
commands: [
509+
{
510+
id: commandId,
511+
function: eval(commandStr)
512+
}
513+
]
514+
})
515+
},
516+
{ keyCombo, commandStr: command.toString() }
517+
)
518+
}
519+
491520
async setSetting(settingId: string, settingValue: any) {
492521
return await this.page.evaluate(
493522
async ({ id, value }) => {

browser_tests/keybindings.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect } from '@playwright/test'
2+
import { comfyPageFixture as test } from './ComfyPage'
3+
4+
test.describe('Keybindings', () => {
5+
test('Should not trigger non-modifier keybinding when typing in input fields', async ({
6+
comfyPage
7+
}) => {
8+
await comfyPage.registerKeybinding({ key: 'k' }, () => {
9+
window['TestCommand'] = true
10+
})
11+
12+
const textBox = comfyPage.widgetTextBox
13+
await textBox.click()
14+
await textBox.fill('k')
15+
await expect(textBox).toHaveValue('k')
16+
expect(await comfyPage.page.evaluate(() => window['TestCommand'])).toBe(
17+
undefined
18+
)
19+
})
20+
21+
test('Should not trigger modifier keybinding when typing in input fields', async ({
22+
comfyPage
23+
}) => {
24+
await comfyPage.registerKeybinding({ key: 'k', ctrl: true }, () => {
25+
window['TestCommand'] = true
26+
})
27+
28+
const textBox = comfyPage.widgetTextBox
29+
await textBox.click()
30+
await textBox.fill('q')
31+
await textBox.press('Control+k')
32+
await expect(textBox).toHaveValue('q')
33+
expect(await comfyPage.page.evaluate(() => window['TestCommand'])).toBe(
34+
true
35+
)
36+
})
37+
})

src/extensions/core/keybinds.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ app.registerExtension({
1111
if (!app.vueAppReady) return
1212

1313
const keyCombo = KeyComboImpl.fromEvent(event)
14+
if (keyCombo.isModifier) {
15+
return
16+
}
1417

1518
// Ignore non-modifier keybindings if typing in input fields
1619
const target = event.composedPath()[0] as HTMLElement

src/stores/keybindingStore.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export class KeyComboImpl implements KeyCombo {
8080
get hasModifier(): boolean {
8181
return this.ctrl || this.alt || this.shift
8282
}
83+
84+
get isModifier(): boolean {
85+
return ['Control', 'Meta', 'Alt', 'Shift'].includes(this.key)
86+
}
8387
}
8488

8589
export const useKeybindingStore = defineStore('keybinding', () => {

0 commit comments

Comments
 (0)