From 57a4cb9036d2f762b6f6c5c1ebb30e1465216735 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Fri, 4 Oct 2024 16:28:08 -0400 Subject: [PATCH] Add default toast error handling for command execution (#1106) * Error handling execute command * Cleanup * Add playwright test * Mock i18n in jest test * Reduce test func timeout --- browser_tests/ComfyPage.ts | 30 ++++++++++++ browser_tests/commands.spec.ts | 49 +++++++++++++++++++ src/components/actionbar/ComfyActionbar.vue | 13 ++--- src/components/graph/GraphCanvasMenu.vue | 13 ++--- .../sidebar/tabs/QueueSidebarTab.vue | 4 +- .../sidebar/tabs/WorkflowsSidebarTab.vue | 10 ++-- src/extensions/core/keybinds.ts | 2 +- src/hooks/errorHooks.ts | 2 +- src/stores/commandStore.ts | 16 +++--- src/stores/menuItemStore.ts | 14 +++--- src/stores/workspaceStateStore.ts | 6 +++ src/types/extensionTypes.ts | 5 ++ tests-ui/globalSetup.ts | 6 +++ 13 files changed, 126 insertions(+), 44 deletions(-) create mode 100644 browser_tests/commands.spec.ts diff --git a/browser_tests/ComfyPage.ts b/browser_tests/ComfyPage.ts index eedd5885df..5b08c456e5 100644 --- a/browser_tests/ComfyPage.ts +++ b/browser_tests/ComfyPage.ts @@ -485,6 +485,36 @@ export class ComfyPage { return `./browser_tests/assets/${fileName}` } + async executeCommand(commandId: string) { + await this.page.evaluate((id: string) => { + return window['app'].extensionManager.command.execute(id) + }, commandId) + } + + async registerCommand( + commandId: string, + command: (() => void) | (() => Promise) + ) { + await this.page.evaluate( + ({ commandId, commandStr }) => { + const app = window['app'] + const randomSuffix = Math.random().toString(36).substring(2, 8) + const extensionName = `TestExtension_${randomSuffix}` + + app.registerExtension({ + name: extensionName, + commands: [ + { + id: commandId, + function: eval(commandStr) + } + ] + }) + }, + { commandId, commandStr: command.toString() } + ) + } + async registerKeybinding(keyCombo: KeyCombo, command: () => void) { await this.page.evaluate( ({ keyCombo, commandStr }) => { diff --git a/browser_tests/commands.spec.ts b/browser_tests/commands.spec.ts new file mode 100644 index 0000000000..7ee3744c99 --- /dev/null +++ b/browser_tests/commands.spec.ts @@ -0,0 +1,49 @@ +import { expect } from '@playwright/test' +import { comfyPageFixture as test } from './ComfyPage' + +test.describe('Keybindings', () => { + test('Should execute command', async ({ comfyPage }) => { + await comfyPage.registerCommand('TestCommand', () => { + window['foo'] = true + }) + + await comfyPage.executeCommand('TestCommand') + expect(await comfyPage.page.evaluate(() => window['foo'])).toBe(true) + }) + + test('Should execute async command', async ({ comfyPage }) => { + await comfyPage.registerCommand('TestCommand', async () => { + await new Promise((resolve) => + setTimeout(() => { + window['foo'] = true + resolve() + }, 5) + ) + }) + + await comfyPage.executeCommand('TestCommand') + expect(await comfyPage.page.evaluate(() => window['foo'])).toBe(true) + }) + + test('Should handle command errors', async ({ comfyPage }) => { + await comfyPage.registerCommand('TestCommand', () => { + throw new Error('Test error') + }) + + await comfyPage.executeCommand('TestCommand') + await expect(comfyPage.page.locator('.p-toast')).toBeVisible() + }) + + test('Should handle async command errors', async ({ comfyPage }) => { + await comfyPage.registerCommand('TestCommand', async () => { + await new Promise((resolve, reject) => + setTimeout(() => { + reject(new Error('Test error')) + }, 5) + ) + }) + + await comfyPage.executeCommand('TestCommand') + await expect(comfyPage.page.locator('.p-toast')).toBeVisible() + }) +}) diff --git a/src/components/actionbar/ComfyActionbar.vue b/src/components/actionbar/ComfyActionbar.vue index 6f4ad95783..fb3f8fab8f 100644 --- a/src/components/actionbar/ComfyActionbar.vue +++ b/src/components/actionbar/ComfyActionbar.vue @@ -37,7 +37,7 @@ :severity="executingPrompt ? 'danger' : 'secondary'" :disabled="!executingPrompt" text - @click="() => commandStore.getCommandFunction('Comfy.Interrupt')()" + @click="() => commandStore.execute('Comfy.Interrupt')" >