From 352ae05cf4d17eb726961dec30970f35370df01d Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 2 Oct 2026 15:50:12 -0700 Subject: [PATCH 1/2] fix: backport App Mode camera guard to cloud 1.55 --- .../assets/linear-basic-app-template.json | 223 ++++++++++++++++++ .../fixtures/helpers/CanvasHelper.ts | 5 + .../fixtures/helpers/WorkflowHelper.ts | 17 ++ .../fixtures/utils/appModeTemplate.ts | 24 ++ .../graphCameraSurvivesTemplateLoad.spec.ts | 78 ++++++ .../src/DragAndScale.fitToBounds.test.ts | 68 ++++++ src/lib/litegraph/src/DragAndScale.ts | 4 +- 7 files changed, 418 insertions(+), 1 deletion(-) create mode 100644 browser_tests/assets/linear-basic-app-template.json create mode 100644 browser_tests/fixtures/utils/appModeTemplate.ts create mode 100644 browser_tests/tests/appMode/graphCameraSurvivesTemplateLoad.spec.ts create mode 100644 src/lib/litegraph/src/DragAndScale.fitToBounds.test.ts diff --git a/browser_tests/assets/linear-basic-app-template.json b/browser_tests/assets/linear-basic-app-template.json new file mode 100644 index 00000000000..897186080bd --- /dev/null +++ b/browser_tests/assets/linear-basic-app-template.json @@ -0,0 +1,223 @@ +{ + "last_node_id": 9, + "last_link_id": 9, + "nodes": [ + { + "id": 7, + "type": "CLIPTextEncode", + "pos": [413, 389], + "size": [425.27801513671875, 180.6060791015625], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": 5 + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [6], + "slot_index": 0 + } + ], + "properties": {}, + "widgets_values": ["text, watermark"] + }, + { + "id": 6, + "type": "CLIPTextEncode", + "pos": [415, 186], + "size": [422.84503173828125, 164.31304931640625], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": 3 + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [4], + "slot_index": 0 + } + ], + "properties": {}, + "widgets_values": [ + "beautiful scenery nature glass bottle landscape, , purple galaxy bottle," + ] + }, + { + "id": 5, + "type": "EmptyLatentImage", + "pos": [473, 609], + "size": [315, 106], + "flags": {}, + "order": 1, + "mode": 0, + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [2], + "slot_index": 0 + } + ], + "properties": {}, + "widgets_values": [512, 512, 1] + }, + { + "id": 3, + "type": "KSampler", + "pos": [863, 186], + "size": [315, 262], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": 1 + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": 4 + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": 6 + }, + { + "name": "latent_image", + "type": "LATENT", + "link": 2 + } + ], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [7], + "slot_index": 0 + } + ], + "properties": {}, + "widgets_values": [156680208700286, true, 20, 8, "euler", "normal", 1] + }, + { + "id": 8, + "type": "VAEDecode", + "pos": [1209, 188], + "size": [210, 46], + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [ + { + "name": "samples", + "type": "LATENT", + "link": 7 + }, + { + "name": "vae", + "type": "VAE", + "link": 8 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [9], + "slot_index": 0 + } + ], + "properties": {} + }, + { + "id": 9, + "type": "SaveImage", + "pos": [1451, 189], + "size": [210, 26], + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 9 + } + ], + "properties": {} + }, + { + "id": 4, + "type": "CheckpointLoaderSimple", + "pos": [26, 474], + "size": [315, 98], + "flags": {}, + "order": 0, + "mode": 0, + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [1], + "slot_index": 0 + }, + { + "name": "CLIP", + "type": "CLIP", + "links": [3, 5], + "slot_index": 1 + }, + { + "name": "VAE", + "type": "VAE", + "links": [8], + "slot_index": 2 + } + ], + "properties": {}, + "widgets_values": ["v1-5-pruned-emaonly-fp16.safetensors"] + } + ], + "links": [ + [1, 4, 0, 3, 0, "MODEL"], + [2, 5, 0, 3, 3, "LATENT"], + [3, 4, 1, 6, 0, "CLIP"], + [4, 6, 0, 3, 1, "CONDITIONING"], + [5, 4, 1, 7, 0, "CLIP"], + [6, 7, 0, 3, 2, "CONDITIONING"], + [7, 3, 0, 8, 0, "LATENT"], + [8, 4, 2, 8, 1, "VAE"], + [9, 8, 0, 9, 0, "IMAGE"] + ], + "groups": [], + "config": {}, + "extra": { + "linearData": { + "inputs": [ + ["3", "seed"], + ["3", "steps"], + ["3", "cfg"] + ], + "outputs": ["9"] + }, + "linearMode": true + }, + "version": 0.4 +} diff --git a/browser_tests/fixtures/helpers/CanvasHelper.ts b/browser_tests/fixtures/helpers/CanvasHelper.ts index 871e8b430dd..10bc5c1058a 100644 --- a/browser_tests/fixtures/helpers/CanvasHelper.ts +++ b/browser_tests/fixtures/helpers/CanvasHelper.ts @@ -202,6 +202,11 @@ export class CanvasHelper { ) } + /** Backing-store width of the canvas element; 0 while App Mode hides it. */ + async getElementWidth(): Promise { + return this.page.evaluate(() => window.app!.canvasEl.width) + } + async waitForViewToSettle(): Promise { await this.page.waitForFunction( () => diff --git a/browser_tests/fixtures/helpers/WorkflowHelper.ts b/browser_tests/fixtures/helpers/WorkflowHelper.ts index 4b6793f5a3c..bceba9d792f 100644 --- a/browser_tests/fixtures/helpers/WorkflowHelper.ts +++ b/browser_tests/fixtures/helpers/WorkflowHelper.ts @@ -4,6 +4,7 @@ import { expect } from '@playwright/test' import { networkIsolationFixture as test } from '@e2e/fixtures/networkIsolationFixture' import type { AppMode } from '@/utils/appMode' +import { getWorkflowMode } from '@/utils/appMode' import type { ComfyApiWorkflow, ComfyWorkflowJSON @@ -202,6 +203,22 @@ export class WorkflowHelper { }) } + /** The active workflow's mode, resolved by the production utility. */ + async getActiveWorkflowResolvedMode(): Promise { + // Read both fields in one evaluate so they cannot come from different + // workflow objects, then resolve precedence with the production utility. + const modes = await this.comfyPage.page.evaluate(() => { + const { activeWorkflow } = ( + window.app!.extensionManager as WorkspaceStore + ).workflow + return { + activeMode: activeWorkflow?.activeMode ?? null, + initialMode: activeWorkflow?.initialMode + } + }) + return getWorkflowMode(modes) + } + async getLinearModeFromGraph(): Promise { return this.comfyPage.page.evaluate(() => { return window.app!.rootGraph.extra.linearMode as boolean | undefined diff --git a/browser_tests/fixtures/utils/appModeTemplate.ts b/browser_tests/fixtures/utils/appModeTemplate.ts new file mode 100644 index 00000000000..d76e169c7f6 --- /dev/null +++ b/browser_tests/fixtures/utils/appModeTemplate.ts @@ -0,0 +1,24 @@ +import { makeTemplate } from '@e2e/fixtures/data/templateFixtures' +import { withTemplates } from '@e2e/fixtures/helpers/TemplateHelper' +import type { TemplateHelper } from '@e2e/fixtures/helpers/TemplateHelper' + +export const APP_MODE_TEMPLATE = 'pm-1733-app-template' + +/** + * Registers a template whose workflow carries `extra.linearMode`, so loading + * it keeps App Mode active and the graph canvas hidden. + */ +export async function mockAppModeTemplate( + templates: TemplateHelper +): Promise { + templates.configure( + withTemplates([ + makeTemplate({ name: APP_MODE_TEMPLATE, title: 'App Template' }) + ]) + ) + await templates.mock() + await templates.mockWorkflow( + APP_MODE_TEMPLATE, + 'browser_tests/assets/linear-basic-app-template.json' + ) +} diff --git a/browser_tests/tests/appMode/graphCameraSurvivesTemplateLoad.spec.ts b/browser_tests/tests/appMode/graphCameraSurvivesTemplateLoad.spec.ts new file mode 100644 index 00000000000..e4398bc3a71 --- /dev/null +++ b/browser_tests/tests/appMode/graphCameraSurvivesTemplateLoad.spec.ts @@ -0,0 +1,78 @@ +import { mergeTests } from '@playwright/test' + +import { + comfyExpect as expect, + comfyPageFixture +} from '@e2e/fixtures/ComfyPage' +import { templateApiFixture } from '@e2e/fixtures/templateApiFixture' +import { + APP_MODE_TEMPLATE, + mockAppModeTemplate +} from '@e2e/fixtures/utils/appModeTemplate' + +const test = mergeTests(comfyPageFixture, templateApiFixture) + +/** + * PM-1732 / PM-1733 — App Mode hides the graph canvas, so its element measures + * 0x0 and fitting the view against it produced scale 0 with NaN offsets. + * + * Scope: this checks the camera is still valid after returning to the graph, + * not that the graph is automatically re-framed. The hidden-canvas fit is a + * no-op, so the camera keeps the viewport it had on entry. + */ +test.describe('App mode template load', { tag: ['@canvas'] }, () => { + test.describe.configure({ timeout: 60_000 }) + + test.beforeEach(async ({ templateApi }) => { + await mockAppModeTemplate(templateApi) + }) + + test.afterEach(async ({ comfyPage }) => { + await comfyPage.canvasOps.resetView() + }) + + test('preserves the graph camera when returning from app mode', async ({ + comfyPage, + templateApi + }) => { + const { appMode, canvasOps, workflow } = comfyPage + + const cameraOnEntry = + await test.step('app mode hides the canvas', async () => { + await appMode.enterAppModeWithInputs([['3', 'seed']]) + await expect(appMode.centerPanel).toBeVisible() + await expect.poll(() => canvasOps.getElementWidth()).toBe(0) + return { + scale: await canvasOps.getScale(), + offset: await canvasOps.getOffset() + } + }) + + await test.step('the template loads while the canvas is hidden', async () => { + await templateApi.load(APP_MODE_TEMPLATE) + await expect + .poll(() => workflow.getActiveWorkflowPath()) + .toContain(APP_MODE_TEMPLATE) + await workflow.waitForWorkflowIdle() + await expect + .poll(() => workflow.getActiveWorkflowResolvedMode()) + .toBe('app') + await expect.poll(() => canvasOps.getElementWidth()).toBe(0) + }) + + await test.step('returning to the graph keeps that camera', async () => { + await appMode.toggleAppMode() + await expect + .poll(() => workflow.getActiveWorkflowResolvedMode()) + .toBe('graph') + await expect.poll(() => canvasOps.getElementWidth()).toBeGreaterThan(0) + + expect(await canvasOps.getScale(), 'camera scale is unchanged').toBe( + cameraOnEntry.scale + ) + expect(await canvasOps.getOffset(), 'camera offset is unchanged').toEqual( + cameraOnEntry.offset + ) + }) + }) +}) diff --git a/src/lib/litegraph/src/DragAndScale.fitToBounds.test.ts b/src/lib/litegraph/src/DragAndScale.fitToBounds.test.ts new file mode 100644 index 00000000000..ca502cc22cd --- /dev/null +++ b/src/lib/litegraph/src/DragAndScale.fitToBounds.test.ts @@ -0,0 +1,68 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { DragAndScale } from '@/lib/litegraph/src/DragAndScale' +import type { ReadOnlyRect } from '@/lib/litegraph/src/interfaces' + +const BOUNDS: ReadOnlyRect = [26, 156, 1635, 559] + +function createDragAndScale(width: number, height: number) { + const element = document.createElement('canvas') + element.width = width + element.height = height + return new DragAndScale(element) +} + +function screenCenterX(ds: DragAndScale, bounds: ReadOnlyRect) { + return (bounds[0] + bounds[2] * 0.5 + ds.offset[0]) * ds.scale +} + +function screenCenterY(ds: DragAndScale, bounds: ReadOnlyRect) { + return (bounds[1] + bounds[3] * 0.5 + ds.offset[1]) * ds.scale +} + +describe('DragAndScale.fitToBounds', () => { + beforeEach(() => { + vi.stubGlobal('devicePixelRatio', 1) + }) + + it('centers and fits the bounds on a visible canvas', () => { + const ds = createDragAndScale(1600, 900) + + ds.fitToBounds(BOUNDS) + + expect(ds.scale).toBeCloseTo(0.7339) + expect(screenCenterX(ds, BOUNDS)).toBeCloseTo(800) + expect(screenCenterY(ds, BOUNDS)).toBeCloseTo(450) + }) + + it('substitutes a default size for an uninitialised 300x150 element', () => { + const ds = createDragAndScale(300, 150) + + ds.fitToBounds(BOUNDS) + + expect(ds.scale).toBeCloseTo(0.8807) + expect(screenCenterX(ds, BOUNDS)).toBeCloseTo(960) + expect(screenCenterY(ds, BOUNDS)).toBeCloseTo(540) + }) + + it.for([ + ['both axes collapsed', 0, 0], + ['width collapsed', 0, 900], + ['height collapsed', 1600, 0] + ] as const)( + 'leaves the view untouched when the canvas is hidden — %s', + ([, width, height]) => { + const ds = createDragAndScale(width, height) + ds.offset[0] = 416 + ds.offset[1] = 110 + ds.scale = 0.9 + const offsetBefore = [...ds.offset] + const scaleBefore = ds.scale + + ds.fitToBounds(BOUNDS) + + expect([...ds.offset]).toEqual(offsetBefore) + expect(ds.scale).toBe(scaleBefore) + } + ) +}) diff --git a/src/lib/litegraph/src/DragAndScale.ts b/src/lib/litegraph/src/DragAndScale.ts index 3e9aa6a591d..13ddab5aca6 100644 --- a/src/lib/litegraph/src/DragAndScale.ts +++ b/src/lib/litegraph/src/DragAndScale.ts @@ -181,7 +181,8 @@ export class DragAndScale { } /** - * Fits the view to the specified bounds. + * Fits the view to the specified bounds. No-ops when the canvas backing + * store has zero width or height. * @param bounds The bounds to fit the view to, defined by a rectangle. */ fitToBounds( @@ -196,6 +197,7 @@ export class DragAndScale { : [this.element.width, this.element.height] const cw = width / window.devicePixelRatio const ch = height / window.devicePixelRatio + if (!(cw > 0) || !(ch > 0)) return let targetScale = this.scale if (zoom > 0) { From 000bca91a9c6d56355ed68df3001c29bc88a3df0 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 2 Oct 2026 16:21:22 -0700 Subject: [PATCH 2/2] test(canvas): harden app mode camera regression --- .../fixtures/helpers/CanvasHelper.ts | 17 ++++++++++++ .../fixtures/helpers/WorkflowHelper.ts | 14 +++++----- .../graphCameraSurvivesTemplateLoad.spec.ts | 26 ++++++++++++++----- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/browser_tests/fixtures/helpers/CanvasHelper.ts b/browser_tests/fixtures/helpers/CanvasHelper.ts index 10bc5c1058a..2819f9e9dd8 100644 --- a/browser_tests/fixtures/helpers/CanvasHelper.ts +++ b/browser_tests/fixtures/helpers/CanvasHelper.ts @@ -202,11 +202,28 @@ export class CanvasHelper { ) } + async getCamera(): Promise<{ + scale: number + offset: [number, number] + }> { + return this.page.evaluate(() => ({ + scale: window.app!.canvas.ds.scale, + offset: [...window.app!.canvas.ds.offset] as [number, number] + })) + } + /** Backing-store width of the canvas element; 0 while App Mode hides it. */ async getElementWidth(): Promise { return this.page.evaluate(() => window.app!.canvasEl.width) } + async getElementSize(): Promise<{ width: number; height: number }> { + return this.page.evaluate(() => ({ + width: window.app!.canvasEl.width, + height: window.app!.canvasEl.height + })) + } + async waitForViewToSettle(): Promise { await this.page.waitForFunction( () => diff --git a/browser_tests/fixtures/helpers/WorkflowHelper.ts b/browser_tests/fixtures/helpers/WorkflowHelper.ts index bceba9d792f..5cc5d62f3e3 100644 --- a/browser_tests/fixtures/helpers/WorkflowHelper.ts +++ b/browser_tests/fixtures/helpers/WorkflowHelper.ts @@ -204,19 +204,21 @@ export class WorkflowHelper { } /** The active workflow's mode, resolved by the production utility. */ - async getActiveWorkflowResolvedMode(): Promise { + async getActiveWorkflowResolvedMode(): Promise { // Read both fields in one evaluate so they cannot come from different // workflow objects, then resolve precedence with the production utility. const modes = await this.comfyPage.page.evaluate(() => { const { activeWorkflow } = ( window.app!.extensionManager as WorkspaceStore ).workflow - return { - activeMode: activeWorkflow?.activeMode ?? null, - initialMode: activeWorkflow?.initialMode - } + return activeWorkflow + ? { + activeMode: activeWorkflow.activeMode ?? null, + initialMode: activeWorkflow.initialMode + } + : null }) - return getWorkflowMode(modes) + return modes ? getWorkflowMode(modes) : null } async getLinearModeFromGraph(): Promise { diff --git a/browser_tests/tests/appMode/graphCameraSurvivesTemplateLoad.spec.ts b/browser_tests/tests/appMode/graphCameraSurvivesTemplateLoad.spec.ts index e4398bc3a71..b0c9ca7825e 100644 --- a/browser_tests/tests/appMode/graphCameraSurvivesTemplateLoad.spec.ts +++ b/browser_tests/tests/appMode/graphCameraSurvivesTemplateLoad.spec.ts @@ -42,10 +42,10 @@ test.describe('App mode template load', { tag: ['@canvas'] }, () => { await appMode.enterAppModeWithInputs([['3', 'seed']]) await expect(appMode.centerPanel).toBeVisible() await expect.poll(() => canvasOps.getElementWidth()).toBe(0) - return { - scale: await canvasOps.getScale(), - offset: await canvasOps.getOffset() - } + const camera = await canvasOps.getCamera() + expect(Number.isFinite(camera.scale)).toBe(true) + expect(camera.offset.every(Number.isFinite)).toBe(true) + return camera }) await test.step('the template loads while the canvas is hidden', async () => { @@ -65,12 +65,24 @@ test.describe('App mode template load', { tag: ['@canvas'] }, () => { await expect .poll(() => workflow.getActiveWorkflowResolvedMode()) .toBe('graph') - await expect.poll(() => canvasOps.getElementWidth()).toBeGreaterThan(0) + await expect + .poll(async () => { + const { width, height } = await canvasOps.getElementSize() + return width > 0 && height > 0 + }) + .toBe(true) + await canvasOps.waitForViewToSettle() - expect(await canvasOps.getScale(), 'camera scale is unchanged').toBe( + const cameraOnExit = await canvasOps.getCamera() + expect( + Number.isFinite(cameraOnExit.scale), + 'camera scale is finite' + ).toBe(true) + expect(cameraOnExit.offset.every(Number.isFinite)).toBe(true) + expect(cameraOnExit.scale, 'camera scale is unchanged').toBe( cameraOnEntry.scale ) - expect(await canvasOps.getOffset(), 'camera offset is unchanged').toEqual( + expect(cameraOnExit.offset, 'camera offset is unchanged').toEqual( cameraOnEntry.offset ) })