|
| 1 | +import { expect } from '@playwright/test' |
| 2 | + |
| 3 | +import { ComfyPage, comfyPageFixture as test } from './fixtures/ComfyPage' |
| 4 | + |
| 5 | +test.describe('Remote COMBO Widget', () => { |
| 6 | + const mockOptions = ['d', 'c', 'b', 'a'] |
| 7 | + |
| 8 | + const addRemoteWidgetNode = async ( |
| 9 | + comfyPage: ComfyPage, |
| 10 | + nodeName: string, |
| 11 | + count: number = 1 |
| 12 | + ) => { |
| 13 | + const tab = comfyPage.menu.nodeLibraryTab |
| 14 | + await tab.open() |
| 15 | + await tab.getFolder('DevTools').click() |
| 16 | + const nodeEntry = tab.getNode(nodeName).first() |
| 17 | + for (let i = 0; i < count; i++) { |
| 18 | + await nodeEntry.click() |
| 19 | + await comfyPage.nextFrame() |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + const getWidgetOptions = async ( |
| 24 | + comfyPage: ComfyPage, |
| 25 | + nodeName: string |
| 26 | + ): Promise<string[] | undefined> => { |
| 27 | + return await comfyPage.page.evaluate((name) => { |
| 28 | + const node = window['app'].graph.nodes.find((node) => node.title === name) |
| 29 | + return node.widgets[0].options.values |
| 30 | + }, nodeName) |
| 31 | + } |
| 32 | + |
| 33 | + const waitForWidgetUpdate = async (comfyPage: ComfyPage) => { |
| 34 | + // Force re-render to trigger first access of widget's options |
| 35 | + await comfyPage.page.mouse.click(100, 100) |
| 36 | + await comfyPage.page.waitForTimeout(256) |
| 37 | + } |
| 38 | + |
| 39 | + test.beforeEach(async ({ comfyPage }) => { |
| 40 | + await comfyPage.setSetting('Comfy.UseNewMenu', 'Top') |
| 41 | + }) |
| 42 | + |
| 43 | + test.describe('Loading options', () => { |
| 44 | + test.beforeEach(async ({ comfyPage }) => { |
| 45 | + await comfyPage.setSetting('Comfy.UseNewMenu', 'Top') |
| 46 | + await comfyPage.page.route( |
| 47 | + '**/api/models/checkpoints**', |
| 48 | + async (route, request) => { |
| 49 | + const params = new URL(request.url()).searchParams |
| 50 | + const sort = params.get('sort') |
| 51 | + await route.fulfill({ |
| 52 | + body: JSON.stringify(sort ? [...mockOptions].sort() : mockOptions), |
| 53 | + status: 200 |
| 54 | + }) |
| 55 | + } |
| 56 | + ) |
| 57 | + }) |
| 58 | + |
| 59 | + test.afterEach(async ({ comfyPage }) => { |
| 60 | + await comfyPage.page.unroute('**/api/models/checkpoints**') |
| 61 | + }) |
| 62 | + |
| 63 | + test('lazy loads options when widget is added from node library', async ({ |
| 64 | + comfyPage |
| 65 | + }) => { |
| 66 | + const nodeName = 'Remote Widget Node' |
| 67 | + await addRemoteWidgetNode(comfyPage, nodeName) |
| 68 | + await waitForWidgetUpdate(comfyPage) |
| 69 | + const widgetOptions = await getWidgetOptions(comfyPage, nodeName) |
| 70 | + expect(widgetOptions).toEqual(mockOptions) |
| 71 | + }) |
| 72 | + |
| 73 | + test('lazy loads options when widget is added via workflow load', async ({ |
| 74 | + comfyPage |
| 75 | + }) => { |
| 76 | + const nodeName = 'Remote Widget Node' |
| 77 | + await comfyPage.loadWorkflow('remote_widget') |
| 78 | + await comfyPage.page.waitForTimeout(512) |
| 79 | + |
| 80 | + const node = await comfyPage.page.evaluate((name) => { |
| 81 | + return window['app'].graph.nodes.find((node) => node.title === name) |
| 82 | + }, nodeName) |
| 83 | + expect(node).toBeDefined() |
| 84 | + |
| 85 | + await waitForWidgetUpdate(comfyPage) |
| 86 | + const widgetOptions = await getWidgetOptions(comfyPage, nodeName) |
| 87 | + expect(widgetOptions).toEqual(mockOptions) |
| 88 | + }) |
| 89 | + |
| 90 | + test('applies query parameters from input spec', async ({ comfyPage }) => { |
| 91 | + const nodeName = 'Remote Widget Node With Sort Query Param' |
| 92 | + await addRemoteWidgetNode(comfyPage, nodeName) |
| 93 | + await waitForWidgetUpdate(comfyPage) |
| 94 | + const widgetOptions = await getWidgetOptions(comfyPage, nodeName) |
| 95 | + expect(widgetOptions).not.toEqual(mockOptions) |
| 96 | + expect(widgetOptions).toEqual([...mockOptions].sort()) |
| 97 | + }) |
| 98 | + |
| 99 | + test('handles empty list of options', async ({ comfyPage }) => { |
| 100 | + await comfyPage.page.route( |
| 101 | + '**/api/models/checkpoints**', |
| 102 | + async (route) => { |
| 103 | + await route.fulfill({ body: JSON.stringify([]), status: 200 }) |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + const nodeName = 'Remote Widget Node' |
| 108 | + await addRemoteWidgetNode(comfyPage, nodeName) |
| 109 | + await waitForWidgetUpdate(comfyPage) |
| 110 | + const widgetOptions = await getWidgetOptions(comfyPage, nodeName) |
| 111 | + expect(widgetOptions).toEqual([]) |
| 112 | + }) |
| 113 | + |
| 114 | + test('falls back to default value when non-200 response', async ({ |
| 115 | + comfyPage |
| 116 | + }) => { |
| 117 | + await comfyPage.page.route( |
| 118 | + '**/api/models/checkpoints**', |
| 119 | + async (route) => { |
| 120 | + await route.fulfill({ status: 500 }) |
| 121 | + } |
| 122 | + ) |
| 123 | + |
| 124 | + const nodeName = 'Remote Widget Node' |
| 125 | + await addRemoteWidgetNode(comfyPage, nodeName) |
| 126 | + await waitForWidgetUpdate(comfyPage) |
| 127 | + const widgetOptions = await getWidgetOptions(comfyPage, nodeName) |
| 128 | + |
| 129 | + const defaultValue = 'Loading...' |
| 130 | + expect(widgetOptions).toEqual(defaultValue) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + test.describe('Lazy Loading Behavior', () => { |
| 135 | + test('does not fetch options before widget is added to graph', async ({ |
| 136 | + comfyPage |
| 137 | + }) => { |
| 138 | + let requestWasMade = false |
| 139 | + |
| 140 | + comfyPage.page.on('request', (request) => { |
| 141 | + if (request.url().includes('/api/models/checkpoints')) { |
| 142 | + requestWasMade = true |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + // Wait a reasonable time to ensure no request is made |
| 147 | + await comfyPage.page.waitForTimeout(512) |
| 148 | + expect(requestWasMade).toBe(false) |
| 149 | + }) |
| 150 | + |
| 151 | + test('fetches options immediately after widget is added to graph', async ({ |
| 152 | + comfyPage |
| 153 | + }) => { |
| 154 | + const requestPromise = comfyPage.page.waitForRequest((request) => |
| 155 | + request.url().includes('/api/models/checkpoints') |
| 156 | + ) |
| 157 | + await addRemoteWidgetNode(comfyPage, 'Remote Widget Node') |
| 158 | + const request = await requestPromise |
| 159 | + expect(request.url()).toContain('/api/models/checkpoints') |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | + test.describe('Refresh Behavior', () => { |
| 164 | + test('refreshes options when TTL expires', async ({ comfyPage }) => { |
| 165 | + // Fulfill each request with a unique timestamp |
| 166 | + await comfyPage.page.route( |
| 167 | + '**/api/models/checkpoints**', |
| 168 | + async (route, request) => { |
| 169 | + await route.fulfill({ |
| 170 | + body: JSON.stringify([Date.now()]), |
| 171 | + status: 200 |
| 172 | + }) |
| 173 | + } |
| 174 | + ) |
| 175 | + |
| 176 | + const nodeName = 'Remote Widget Node With 300ms Refresh' |
| 177 | + await addRemoteWidgetNode(comfyPage, nodeName) |
| 178 | + await waitForWidgetUpdate(comfyPage) |
| 179 | + const initialOptions = await getWidgetOptions(comfyPage, nodeName) |
| 180 | + |
| 181 | + // Wait for the refresh (TTL) to expire |
| 182 | + await comfyPage.page.waitForTimeout(302) |
| 183 | + await comfyPage.page.mouse.click(100, 100) |
| 184 | + |
| 185 | + const refreshedOptions = await getWidgetOptions(comfyPage, nodeName) |
| 186 | + expect(refreshedOptions).not.toEqual(initialOptions) |
| 187 | + }) |
| 188 | + |
| 189 | + test('does not refresh when TTL is not set', async ({ comfyPage }) => { |
| 190 | + let requestCount = 0 |
| 191 | + await comfyPage.page.route( |
| 192 | + '**/api/models/checkpoints**', |
| 193 | + async (route) => { |
| 194 | + requestCount++ |
| 195 | + await route.fulfill({ body: JSON.stringify(['test']), status: 200 }) |
| 196 | + } |
| 197 | + ) |
| 198 | + |
| 199 | + const nodeName = 'Remote Widget Node' |
| 200 | + await addRemoteWidgetNode(comfyPage, nodeName) |
| 201 | + await waitForWidgetUpdate(comfyPage) |
| 202 | + |
| 203 | + // Force multiple re-renders |
| 204 | + for (let i = 0; i < 3; i++) { |
| 205 | + await comfyPage.page.mouse.click(100, 100) |
| 206 | + await comfyPage.nextFrame() |
| 207 | + } |
| 208 | + |
| 209 | + expect(requestCount).toBe(1) // Should only make initial request |
| 210 | + }) |
| 211 | + |
| 212 | + test('retries failed requests with backoff', async ({ comfyPage }) => { |
| 213 | + const timestamps: number[] = [] |
| 214 | + await comfyPage.page.route( |
| 215 | + '**/api/models/checkpoints**', |
| 216 | + async (route) => { |
| 217 | + timestamps.push(Date.now()) |
| 218 | + await route.fulfill({ status: 500 }) |
| 219 | + } |
| 220 | + ) |
| 221 | + |
| 222 | + const nodeName = 'Remote Widget Node' |
| 223 | + await addRemoteWidgetNode(comfyPage, nodeName) |
| 224 | + |
| 225 | + // Wait for a few retries |
| 226 | + await comfyPage.page.waitForTimeout(1024) |
| 227 | + |
| 228 | + // Verify exponential backoff between retries |
| 229 | + const intervals = timestamps.slice(1).map((t, i) => t - timestamps[i]) |
| 230 | + expect(intervals[1]).toBeGreaterThan(intervals[0]) |
| 231 | + }) |
| 232 | + }) |
| 233 | + |
| 234 | + test.describe('Cache Behavior', () => { |
| 235 | + test('reuses cached data between widgets with same params', async ({ |
| 236 | + comfyPage |
| 237 | + }) => { |
| 238 | + let requestCount = 0 |
| 239 | + await comfyPage.page.route( |
| 240 | + '**/api/models/checkpoints**', |
| 241 | + async (route) => { |
| 242 | + requestCount++ |
| 243 | + await route.fulfill({ |
| 244 | + body: JSON.stringify(mockOptions), |
| 245 | + status: 200 |
| 246 | + }) |
| 247 | + } |
| 248 | + ) |
| 249 | + |
| 250 | + // Add two widgets with same config |
| 251 | + const nodeName = 'Remote Widget Node' |
| 252 | + await addRemoteWidgetNode(comfyPage, nodeName, 2) |
| 253 | + await waitForWidgetUpdate(comfyPage) |
| 254 | + |
| 255 | + expect(requestCount).toBe(1) // Should reuse cached data |
| 256 | + }) |
| 257 | + }) |
| 258 | +}) |
0 commit comments