diff --git a/browser_tests/fixtures/agentConversationFixture.ts b/browser_tests/fixtures/agentConversationFixture.ts index a965196a89a..f6a5934aefe 100644 --- a/browser_tests/fixtures/agentConversationFixture.ts +++ b/browser_tests/fixtures/agentConversationFixture.ts @@ -1,4 +1,4 @@ -import type { Locator, Page, WebSocketRoute } from '@playwright/test' +import type { Locator, Page } from '@playwright/test' import { expect } from '@playwright/test' import { z } from 'zod' @@ -14,12 +14,11 @@ import type { AgentMessages, AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { agentTest, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' -import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' import { TestIds } from '@e2e/fixtures/selectors' import type { @@ -39,7 +38,6 @@ const turnId = (turn: number): string => `0c5b1e77-2d4a-4f9e-8b63-1a2c3d4e5${turn.toString(16).padStart(3, '0')}` const SOCKET_SID = '7d1f2e3a-4b5c-4d6e-8f90-1a2b3c4d5e6f' const PANEL_MOUNT_TIMEOUT = 30_000 -const SUBSCRIBE_TIMEOUT = 15_000 const CANCEL_TIMEOUT = 10_000 const OPEN_AGENT_LABEL = enMessages.agent.askComfyAgent @@ -140,21 +138,16 @@ class AgentConversationHarness { readonly vueNodes: VueNodeHelpers private readonly host: HostDoc + private readonly hostSocket: AgentFollowerHostSocket private readonly streams: Locator private readonly summaries: Locator // Every node id the host has held so far, seed included. private readonly seenIds: Set private readonly expectations: ExpectedTurn[] - private socket: WebSocketRoute | null = null private postedTurns = 0 - private subscribes = 0 private readonly displayNames = new Map() // Resolved when the panel cancels the turn the recording stopped. private readonly cancelWaiters = new Map void>() - private resolveSubscribed: (() => void) | null = null - private readonly subscribed = new Promise((resolve) => { - this.resolveSubscribed = resolve - }) constructor( private readonly page: Page, @@ -164,6 +157,12 @@ class AgentConversationHarness { ) { const { workflow } = conversation this.host = new HostDoc(workflow.id, workflow.seed, workflow.catalog) + this.hostSocket = new AgentFollowerHostSocket( + page, + workflow.id, + this.host, + SOCKET_SID + ) this.seenIds = new Set(workflow.seed.nodes.map((node) => String(node.id))) const expectations = RECORDED_EXPECTATIONS[caseId] const recorded = expectations?.length ?? 0 @@ -180,20 +179,7 @@ class AgentConversationHarness { async boot(agentFlag: boolean): Promise { await this.mockAgentApi() - // The follower re-drives a pending subscribe only on a status frame, which every real connect sends. - await this.page.routeWebSocket(/\/ws/, (socket) => { - this.socket = socket - socket.onMessage((raw) => this.onClientFrame(raw)) - socket.send( - JSON.stringify({ - type: 'status', - data: { - status: { exec_info: { queue_remaining: 0 } }, - sid: SOCKET_SID - } - }) - ) - }) + await this.hostSocket.install() const objectInfo = this.page.waitForResponse((response) => new URL(response.url()).pathname.endsWith('/api/object_info') ) @@ -290,10 +276,11 @@ class AgentConversationHarness { await new Promise((resolve) => setTimeout(resolve, entry.at_ms! - (Date.now() - startedAt)) ) - if (entry.kind === 'event') this.send(this.stampTurn(entry.event, turn)) + if (entry.kind === 'event') + this.hostSocket.send(this.stampTurn(entry.event, turn)) else { - await this.waitForSubscribe() - this.send(this.host.apply(entry.ops)) + await this.hostSocket.waitForSubscribe() + this.hostSocket.send(this.host.apply(entry.ops)) for (const id of Object.keys(this.host.graph().nodes)) this.seenIds.add(id) } @@ -593,51 +580,10 @@ class AgentConversationHarness { return parsed.data } - private send(frame: AgentWsEvent | HostFrame): void { - // Every host frame must satisfy production's own parser, so a host that - // stopped emitting a required field fails here, not silently on the client. - if ( - (frame.type.startsWith('doc_') || frame.type === 'awareness') && - parseServerDocFrame(frame) === null - ) - throw new Error(`host frame ${frame.type} is not a valid doc frame`) - if (!this.socket) throw new Error('the app has not opened /ws yet') - this.socket.send(JSON.stringify(frame)) - } - - private onClientFrame(raw: string | Buffer): void { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return - const { type, data } = frame as { type?: unknown; data?: unknown } - if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) - return - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if ( - workflow_id !== this.conversation.workflow.id || - typeof state_vector_b64 !== 'string' - ) - return - this.send(this.host.subscribed()) - this.send(this.host.catchUp(state_vector_b64)) - this.subscribes += 1 - this.resolveSubscribed?.() - } - // Rises once per follower subscribe; a tab return re-subscribes and the // host answers with the catch-up frame this counter has just sent. subscribeCount(): number { - return this.subscribes - } - - private waitForSubscribe(): Promise { - return withTimeout( - this.subscribed, - SUBSCRIBE_TIMEOUT, - 'the follower never subscribed to the conversation workflow; graph_ops need an agent_active_tab (or a bound tab) first' - ) + return this.hostSocket.subscribeCount() } } diff --git a/browser_tests/fixtures/agentFollowerHostSocket.ts b/browser_tests/fixtures/agentFollowerHostSocket.ts new file mode 100644 index 00000000000..f5024dc5ed9 --- /dev/null +++ b/browser_tests/fixtures/agentFollowerHostSocket.ts @@ -0,0 +1,96 @@ +import type { Page, WebSocketRoute } from '@playwright/test' + +import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' +import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' + +import type { HostDoc, HostFrame } from '@e2e/fixtures/agentConversationHostDoc' + +const SUBSCRIBE_TIMEOUT = 15_000 + +/** Routed `/ws` host shared by black-box Agent follower fixtures. */ +export class AgentFollowerHostSocket { + private socket: WebSocketRoute | null = null + private subscribes = 0 + private resolveSubscribed: (() => void) | null = null + private readonly subscribed = new Promise((resolve) => { + this.resolveSubscribed = resolve + }) + + constructor( + private readonly page: Page, + private readonly workflowId: string, + private readonly host: HostDoc, + private readonly socketSid: string + ) {} + + async install(): Promise { + await this.page.routeWebSocket(/\/ws/, (socket) => { + this.socket = socket + socket.onMessage((raw) => this.onClientFrame(raw)) + socket.send( + JSON.stringify({ + type: 'status', + data: { + status: { exec_info: { queue_remaining: 0 } }, + sid: this.socketSid + } + }) + ) + }) + } + + send(frame: AgentWsEvent | HostFrame): void { + if (frame.type.startsWith('doc_') || frame.type === 'awareness') { + if (parseServerDocFrame(frame) === null) + throw new Error(`host frame ${frame.type} is not a valid doc frame`) + } else if (!parseAgentWsEvent(frame).success) { + throw new Error(`agent event ${frame.type} is not a valid agent event`) + } + if (!this.socket) throw new Error('the app has not opened /ws yet') + this.socket.send(JSON.stringify(frame)) + } + + async waitForSubscribe(): Promise { + let timer: ReturnType | undefined + const timeout = new Promise((_, reject) => { + timer = setTimeout( + () => + reject( + new Error( + `the follower never subscribed to workflow ${this.workflowId}` + ) + ), + SUBSCRIBE_TIMEOUT + ) + }) + try { + await Promise.race([this.subscribed, timeout]) + } finally { + clearTimeout(timer) + } + } + + private onClientFrame(raw: string | Buffer): void { + const frame: unknown = JSON.parse(raw.toString()) + if (typeof frame !== 'object' || frame === null) return + const { type, data } = frame as { type?: unknown; data?: unknown } + if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) + return + const { workflow_id, state_vector_b64 } = data as { + workflow_id?: unknown + state_vector_b64?: unknown + } + if (workflow_id !== this.workflowId || typeof state_vector_b64 !== 'string') + return + this.send(this.host.subscribed()) + this.send(this.host.catchUp(state_vector_b64)) + this.subscribes += 1 + this.resolveSubscribed?.() + } + + /** Rises once per follower subscribe, after the catch-up frame was sent. */ + subscribeCount(): number { + return this.subscribes + } +} diff --git a/browser_tests/fixtures/load3dAgentFixture.ts b/browser_tests/fixtures/load3dAgentFixture.ts new file mode 100644 index 00000000000..63072962197 --- /dev/null +++ b/browser_tests/fixtures/load3dAgentFixture.ts @@ -0,0 +1,272 @@ +import type { Page, Request } from '@playwright/test' +import { expect } from '@playwright/test' + +import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } +import type { ComfyApiWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' +import { + zComfyApiWorkflow, + zComfyWorkflow +} from '@/platform/workflow/validation/schemas/workflowSchema' +import { toNodeId } from '@/types/nodeId' + +import load3dWorkflow from '@e2e/assets/3d/load3d_node.json' with { type: 'json' } +import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' +import { agentTest, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import type { RecordedGraphOperation } from '@e2e/fixtures/data/agent/agentConversation' +import { TestIds } from '@e2e/fixtures/selectors' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' +import { assetPath } from '@e2e/fixtures/utils/paths' +import { Load3DHelper } from '@e2e/tests/load3d/Load3DHelper' + +const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-c33b7bb880f6' +const SOCKET_SID = '5b0e2c9a-6f1d-4a83-9c27-3e4f5a6b7c8d' +const NODE_ID = '1' +const PANEL_MOUNT_TIMEOUT = 30_000 + +// The backend's Load3D definition, in `widgets_values` order; the host applier +// rejects a `set_widget` naming anything outside this list. +const LOAD3D_CATALOG: WidgetCatalog = { + types: { + Load3D: { widget_order: ['model_file', 'image', 'width', 'height'] } + } +} + +// The prompt names a capture as `threed/ [temp]`. +const PROMPT_IMAGE_REF = /^threed\/(.+) \[temp\]$/ + +/** Model files the fixture serves from `browser_tests/assets/3d`. */ +type AgentModelFile = 'cube.obj' | 'workflow.glb' + +const MODEL_ASSETS: Record = { + 'cube.obj': 'cube.obj', + 'workflow.glb': 'animated_triangle.glb' +} + +interface Load3dCapture { + /** The `image` reference the queued prompt carried for the Load3D node. */ + promptImage: string + /** The PNG the viewer uploaded under that reference. */ + imageBytes: Uint8Array +} + +// The seed the host holds: the checked-in Load3D workflow with its widgets +// stored by name, so nothing here depends on positional order. +function seedWorkflow(): WorkflowJSON { + const { extra, ...parsed } = zComfyWorkflow.parse(load3dWorkflow) + const nodes = parsed.nodes.map((node) => ({ + ...node, + widgets_values: { model_file: '', width: 1024, height: 1024 } + })) + // The workflow schema allows `extra: null`; the multi-player seed does not. + return { ...parsed, extra: extra ?? undefined, nodes, links: [] } +} + +function getQueuedPrompt(body: unknown): ComfyApiWorkflow { + if (typeof body !== 'object' || body === null || !('prompt' in body)) + throw new Error('the queued prompt body carries no prompt object') + return zComfyApiWorkflow.parse(body.prompt) +} + +function promptImageRef(body: unknown): string { + const image: unknown = Object.values(getQueuedPrompt(body)).find( + (node) => node.inputs.image !== undefined + )?.inputs.image + const ref = + typeof image === 'object' && image !== null && 'image' in image + ? image.image + : undefined + if (typeof ref !== 'string') + throw new Error('the queued prompt carries no Load3D image reference') + return ref +} + +async function uploadedImageBytes(request: Request): Promise { + const body = request.postDataBuffer() + if (!body) throw new Error('upload request has no body') + const form = await new Response(new Uint8Array(body), { + headers: { 'content-type': request.headers()['content-type'] ?? '' } + }).formData() + const image = form.get('image') + if (!(image instanceof File)) throw new Error('upload has no image file') + return new Uint8Array(await image.arrayBuffer()) +} + +/** + * One Load3D node bound to an agent workflow, driven the way production is: + * the panel opens the agent's tab, the follower subscribes over `/ws`, and the + * fake host applies recorded `set_widget` ops that reach the node through the + * doc frames the real client parses. + */ +class Load3dAgentHarness { + readonly viewer: Load3DHelper + + private readonly host = new HostDoc( + WORKFLOW_ID, + seedWorkflow(), + LOAD3D_CATALOG + ) + private readonly hostSocket: AgentFollowerHostSocket + private uploads = 0 + private readonly uploadsByName = new Map() + + constructor(private readonly page: Page) { + this.hostSocket = new AgentFollowerHostSocket( + page, + WORKFLOW_ID, + this.host, + SOCKET_SID + ) + this.viewer = new Load3DHelper(page.locator(`[data-node-id="${NODE_ID}"]`)) + } + + async boot(agentFlag: boolean): Promise { + await this.mockAgentApi() + await this.mockLoad3dIo() + await this.hostSocket.install() + await bootAgentApp(this.page, agentFlag, { + // Only the Vue node renderer projects follower edits onto the canvas. + settings: { + 'Comfy.VueNodes.Enabled': true, + 'Comfy.Graph.CanvasInfo': false + }, + // The node materializes from the black-box backend's Load3D definition. + objectInfo: 'server' + }) + await this.page + .getByRole('button', { name: enMessages.agent.askComfyAgent }) + .click() + await expect(this.page.locator('#agent-panel-root')).toBeVisible({ + timeout: PANEL_MOUNT_TIMEOUT + }) + // The agent announces its tab; the panel opens it and binds the follower. + this.hostSocket.send({ + type: 'agent_active_tab', + data: { workflow_id: WORKFLOW_ID, name: 'Load3D preview' } + }) + await this.hostSocket.waitForSubscribe() + await expect(this.viewer.node).toBeVisible() + await expect(this.viewer.canvas).toBeVisible() + } + + /** + * The agent sets `model_file`; the value travels as a host doc frame. The + * envelope (`op_id`, actor, version) is minted by the host, as on the wire. + */ + setModelFromAgent(model: AgentModelFile): void { + const widgets = this.host.graph().nodes[NODE_ID]?.widgets + const old = + typeof widgets === 'object' && widgets !== null + ? (widgets as Record)['model_file'] + : undefined + const op: RecordedGraphOperation = { + op: 'set_widget', + node_id: NODE_ID, + widget: 'model_file', + old, + value: model + } + this.hostSocket.send(this.host.apply([op])) + } + + /** Waits until the node's `model_file` widget shows `model`. */ + async expectModel(model: AgentModelFile): Promise { + await expect + .poll(() => + this.page.evaluate( + (nodeId) => + window + .app!.graph.getNodeById(nodeId) + ?.widgets?.find((widget) => widget.name === 'model_file')?.value, + toNodeId(NODE_ID) + ) + ) + .toBe(model) + } + + /** Clicks Queue and resolves once the prompt for it has been posted. */ + private async queuePrompt(): Promise { + const posted = this.nextPrompt() + await this.page.getByTestId(TestIds.topbar.queueButton).click() + return posted + } + + private async nextPrompt(): Promise { + const request = await this.page.waitForRequest( + (candidate) => + candidate.method() === 'POST' && + new URL(candidate.url()).pathname === '/api/prompt' + ) + return request.postDataJSON() + } + + /** Reads the capture the last queued prompt carried and its uploaded bytes. */ + private async captureFor(promptBody: unknown): Promise { + const promptImage = promptImageRef(promptBody) + const name = PROMPT_IMAGE_REF.exec(promptImage)?.[1] + const upload = name === undefined ? undefined : this.uploadsByName.get(name) + if (!upload) + throw new Error( + `the prompt references ${promptImage}, which was never uploaded` + ) + return { promptImage, imageBytes: await uploadedImageBytes(upload) } + } + + /** Queues a prompt and returns the capture it carried. */ + async capture(): Promise { + return this.captureFor(await this.queuePrompt()) + } + + private async mockAgentApi(): Promise { + const { page } = this + await page.route('**/api/agent/threads', (route) => + route.fulfill(jsonRoute({ threads: [] })) + ) + await page.route('**/api/agent/threads/*/messages', (route) => + route.fulfill(jsonRoute([])) + ) + // No cloud workflow carries the agent's id, so the panel opens a new tab for it. + await page.route('**/api/workflows**', (route) => + route.fulfill( + jsonRoute({ + data: [], + pagination: { has_more: false, limit: 100, offset: 0, total: 0 } + }) + ) + ) + } + + private async mockLoad3dIo(): Promise { + const { page } = this + await page.route('**/api/upload/image**', (route) => { + const name = `agent-load3d-capture-${++this.uploads}.png` + this.uploadsByName.set(name, route.request()) + return route.fulfill(jsonRoute({ name, subfolder: 'temp', type: 'temp' })) + }) + for (const [model, asset] of Object.entries(MODEL_ASSETS)) { + await page.route(`**/api/view?*filename=${model}*`, (route) => + route.fulfill({ path: assetPath(asset) }) + ) + } + await page.route(/\/api\/prompt$/, (route) => { + if (route.request().method() !== 'POST') return route.fallback() + return route.fulfill( + jsonRoute({ prompt_id: crypto.randomUUID(), node_errors: {} }) + ) + }) + } +} + +interface Load3dAgentFixtures { + load3dAgent: Load3dAgentHarness +} + +export const load3dAgentTest = agentTest.extend({ + load3dAgent: async ({ page, agentFlagEnabled }, use) => { + const harness = new Load3dAgentHarness(page) + await harness.boot(agentFlagEnabled) + await use(harness) + } +}) diff --git a/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts new file mode 100644 index 00000000000..263f67a57e3 --- /dev/null +++ b/browser_tests/tests/load3d/load3dAgentUpdate.spec.ts @@ -0,0 +1,40 @@ +import { expect } from '@playwright/test' + +import { load3dAgentTest as test } from '@e2e/fixtures/load3dAgentFixture' + +test.describe('Load3D agent updates', { tag: '@cloud' }, () => { + test.describe.configure({ timeout: 60_000 }) + + test('an agent model_file update refreshes the viewer and capture cache', async ({ + load3dAgent + }) => { + const { viewer } = load3dAgent + + const before = + await test.step('capture the initial agent model', async () => { + load3dAgent.setModelFromAgent('cube.obj') + await load3dAgent.expectModel('cube.obj') + await viewer.waitForModelLoaded() + return load3dAgent.capture() + }) + + const after = + await test.step('queue after the agent replacement finishes loading', async () => { + load3dAgent.setModelFromAgent('workflow.glb') + await load3dAgent.expectModel('workflow.glb') + await load3dAgent.viewer.waitForModelLoaded() + return load3dAgent.capture() + }) + + await test.step('the prompt carries the replacement model', async () => { + expect(after.promptImage).not.toBe(before.promptImage) + expect(after.imageBytes.byteLength).toBeGreaterThan(0) + expect(after.imageBytes).not.toEqual(before.imageBytes) + await expect(viewer.canvas).toBeVisible() + await test.info().attach('agent-updated-load3d.png', { + body: await viewer.node.screenshot(), + contentType: 'image/png' + }) + }) + }) +}) diff --git a/src/composables/useLoad3d.test.ts b/src/composables/useLoad3d.test.ts index 26be0aa54bd..566e03fd7db 100644 --- a/src/composables/useLoad3d.test.ts +++ b/src/composables/useLoad3d.test.ts @@ -5,6 +5,7 @@ import { useSettingStore } from '@/platform/settings/settingStore' import { getLoad3dOutputCache, + getLoad3dSceneRevision, isLoad3dSceneDirty, markLoad3dSceneDirty, nodeToLoad3dMap, @@ -142,7 +143,7 @@ describe('useLoad3d', () => { setFOV: vi.fn(), setLightIntensity: vi.fn(), setCameraState: vi.fn(), - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue(true), refreshViewport: vi.fn(), updateStatusMouseOnNode: vi.fn(), updateStatusMouseOnScene: vi.fn(), @@ -1879,6 +1880,26 @@ describe('useLoad3d', () => { expect(isLoad3dSceneDirty(b)).toBe(true) }) + it('setLoad3dOutputCache rejects a stale scene revision and keeps the node dirty', () => { + const fresh = createMockLGraphNode({ properties: {} }) + const capturedRevision = getLoad3dSceneRevision(fresh) + + markLoad3dSceneDirty(fresh) + expect(getLoad3dSceneRevision(fresh)).toBe(capturedRevision + 1) + + expect(setLoad3dOutputCache(fresh, fakeCache, capturedRevision)).toBe( + false + ) + expect(getLoad3dOutputCache(fresh)).toBeUndefined() + expect(isLoad3dSceneDirty(fresh)).toBe(true) + + expect( + setLoad3dOutputCache(fresh, fakeCache, getLoad3dSceneRevision(fresh)) + ).toBe(true) + expect(getLoad3dOutputCache(fresh)).toBe(fakeCache) + expect(isLoad3dSceneDirty(fresh)).toBe(false) + }) + it('markLoad3dSceneDirty on null is a no-op', () => { expect(() => markLoad3dSceneDirty(null)).not.toThrow() }) diff --git a/src/composables/useLoad3d.ts b/src/composables/useLoad3d.ts index 89eba48efdc..b46cd24e021 100644 --- a/src/composables/useLoad3d.ts +++ b/src/composables/useLoad3d.ts @@ -52,13 +52,18 @@ export type Load3dCachedOutput = { } const load3dSceneDirty = new WeakMap() +const load3dSceneRevisions = new WeakMap() const load3dOutputCache = new WeakMap() export const markLoad3dSceneDirty = (node: LGraphNode | null): void => { if (!node) return + load3dSceneRevisions.set(node, (load3dSceneRevisions.get(node) ?? 0) + 1) load3dSceneDirty.set(node, true) } +export const getLoad3dSceneRevision = (node: LGraphNode): number => + load3dSceneRevisions.get(node) ?? 0 + export const isLoad3dSceneDirty = (node: LGraphNode): boolean => load3dSceneDirty.get(node) !== false @@ -68,10 +73,13 @@ export const getLoad3dOutputCache = ( export const setLoad3dOutputCache = ( node: LGraphNode, - output: Load3dCachedOutput -): void => { + output: Load3dCachedOutput, + sceneRevision: number = getLoad3dSceneRevision(node) +): boolean => { + if (getLoad3dSceneRevision(node) !== sceneRevision) return false load3dOutputCache.set(node, output) load3dSceneDirty.set(node, false) + return true } const pendingCallbacks = new Map() const persistentReadyCallbacks = new Map() @@ -843,7 +851,8 @@ export const useLoad3d = (nodeOrRef: MaybeRef) => { ) loadingMessage.value = t('load3d.loadingModel') - await load3d.loadModel(modelUrl) + const accepted = await load3d.loadModel(modelUrl) + if (!accepted) return const modelWidget = node.widgets?.find((w) => w.name === 'model_file') diff --git a/src/composables/useLoad3dViewer.test.ts b/src/composables/useLoad3dViewer.test.ts index 83c025c5b31..e0fcf80b953 100644 --- a/src/composables/useLoad3dViewer.test.ts +++ b/src/composables/useLoad3dViewer.test.ts @@ -125,7 +125,7 @@ describe('useLoad3dViewer', () => { forceRender: vi.fn(), remove: vi.fn(), setTargetSize: vi.fn(), - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue(true), captureThumbnail: vi.fn().mockResolvedValue('data:image/png;base64,x'), setCameraState: vi.fn(), addEventListener: vi.fn(), @@ -626,6 +626,22 @@ describe('useLoad3dViewer', () => { }) describe('handleModelDrop', () => { + it('does not publish dropped-model state when the load was superseded', async () => { + vi.mocked(Load3dUtils.uploadFile).mockResolvedValueOnce( + '3d/superseded.glb' + ) + vi.mocked(mockLoad3d.loadModel!).mockResolvedValueOnce(false) + const viewer = useLoad3dViewer(mockNode) + const containerRef = document.createElement('div') + await viewer.initializeViewer(containerRef, mockSourceLoad3d as Load3d) + vi.mocked(mockLoad3d.getCurrentModelCapabilities!).mockClear() + + await viewer.handleModelDrop(new File([''], 'superseded.glb')) + + expect(mockNode.widgets).toEqual([]) + expect(mockLoad3d.getCurrentModelCapabilities).not.toHaveBeenCalled() + }) + it('refreshes the capability refs after the dropped model loads, so the sidebar reflects the new model', async () => { vi.mocked(Load3dUtils.uploadFile).mockResolvedValueOnce( '3d/dropped.splat' @@ -852,6 +868,39 @@ describe('useLoad3dViewer', () => { await newViewer.initializeStandaloneViewer(containerRef, modelUrl) expect(newViewer.backgroundColor.value).toBe('#0000ff') }) + + it('completes viewer setup when a concurrent call supersedes the first load', async () => { + let settleFirstLoad!: (accepted: boolean) => void + vi.mocked(mockLoad3d.loadModel!) + .mockImplementationOnce( + () => + new Promise((resolve) => { + settleFirstLoad = resolve + }) + ) + .mockResolvedValueOnce(true) + const viewer = useLoad3dViewer() + const containerRef = document.createElement('div') + + const first = viewer.initializeStandaloneViewer(containerRef, 'a.glb') + const replacement = viewer.initializeStandaloneViewer( + containerRef, + 'b.glb' + ) + settleFirstLoad(false) + await Promise.all([first, replacement]) + + expect(createLoad3d).toHaveBeenCalledTimes(1) + expect(viewer.isPreview.value).toBe(true) + expect(mockLoad3d.addEventListener).toHaveBeenCalledWith( + 'animationListChange', + expect.any(Function) + ) + expect(mockLoad3d.addEventListener).toHaveBeenCalledWith( + 'animationProgressChange', + expect.any(Function) + ) + }) }) describe('gizmo controls', () => { diff --git a/src/composables/useLoad3dViewer.ts b/src/composables/useLoad3dViewer.ts index 73bb2000b66..335d722c774 100644 --- a/src/composables/useLoad3dViewer.ts +++ b/src/composables/useLoad3dViewer.ts @@ -495,14 +495,17 @@ export const useLoad3dViewer = (node?: LGraphNode) => { load3d.updateStatusMouseOnViewer(true) } - await load3d.loadModel(modelUrl) + // Viewer-lifetime setup belongs to instance creation, not to this + // request: a concurrent initializeStandaloneViewer() call reuses the + // instance through loadStandaloneModel() and can supersede this load. + isPreview.value = true + setupAnimationEvents() + + const accepted = await load3d.loadModel(modelUrl) + if (!accepted) return currentModelUrl = modelUrl restoreStandaloneConfig(modelUrl) captureAdapterFlags(load3d) - - isPreview.value = true - - setupAnimationEvents() persistStandaloneThumbnail(modelUrl) } catch (error) { console.error('Error initializing standalone 3D viewer:', error) @@ -531,7 +534,8 @@ export const useLoad3dViewer = (node?: LGraphNode) => { try { saveStandaloneConfig() - await load3d.loadModel(modelUrl) + const accepted = await load3d.loadModel(modelUrl) + if (!accepted) return currentModelUrl = modelUrl restoreStandaloneConfig(modelUrl) captureAdapterFlags(load3d) @@ -821,7 +825,8 @@ export const useLoad3dViewer = (node?: LGraphNode) => { ) ) - await load3d.loadModel(modelUrl) + const accepted = await load3d.loadModel(modelUrl) + if (!accepted) return captureAdapterFlags(load3d) diff --git a/src/extensions/core/load3d.test.ts b/src/extensions/core/load3d.test.ts index abadc873b7e..71a93fbb069 100644 --- a/src/extensions/core/load3d.test.ts +++ b/src/extensions/core/load3d.test.ts @@ -57,6 +57,7 @@ vi.mock(import('@/services/load3dService'), () => ({ vi.mock(import('@/composables/useLoad3d'), () => { const sceneDirty = new WeakMap() + const sceneRevisions = new WeakMap() const outputCache = new WeakMap() return { useLoad3d: () => @@ -67,13 +68,21 @@ vi.mock(import('@/composables/useLoad3d'), () => { nodeToLoad3dMap: fromAny(nodeToLoad3dMap), markLoad3dSceneDirty: (node: LGraphNode | null) => { if (!node) return + sceneRevisions.set(node, (sceneRevisions.get(node) ?? 0) + 1) sceneDirty.set(node, true) }, + getLoad3dSceneRevision: (node: LGraphNode) => sceneRevisions.get(node) ?? 0, isLoad3dSceneDirty: (node: LGraphNode) => sceneDirty.get(node) !== false, getLoad3dOutputCache: fromAny((node: LGraphNode) => outputCache.get(node)), - setLoad3dOutputCache: (node: LGraphNode, value: unknown) => { + setLoad3dOutputCache: ( + node: LGraphNode, + value: unknown, + revision: number = sceneRevisions.get(node) ?? 0 + ) => { + if ((sceneRevisions.get(node) ?? 0) !== revision) return false outputCache.set(node, value) sceneDirty.set(node, false) + return true } } }) @@ -151,6 +160,7 @@ const registeredExtensionCount = registerExtensionMock.mock.calls.length interface FakeWidget { name: string + type?: string value: unknown serializeValue?: () => Promise } @@ -475,8 +485,8 @@ describe('Comfy.Load3D.nodeCreated', () => { it('configures with the input folder and width/height widgets', async () => { const widgets: FakeWidget[] = [ { name: 'model_file', value: 'model.glb' }, - { name: 'width', value: 1024 }, - { name: 'height', value: 768 }, + { name: 'width', type: 'number', value: 1024 }, + { name: 'height', type: 'number', value: 768 }, { name: 'image', value: '' } ] const node = makeLoad3DNode({ widgets }) @@ -1109,6 +1119,7 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { function makeFullFakeLoad3d() { return { + whenLoadIdle: vi.fn(async () => {}), getCurrentCameraType: vi.fn(() => 'perspective'), cameraManager: { perspectiveCamera: { fov: 35 } }, getCameraState: vi.fn(() => ({ position: { x: 0, y: 0, z: 0 } })), @@ -1144,16 +1155,39 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { { name: 'image', value: '' } ] const node = makeLoad3DNode({ widgets, properties: {} }) - useLoad3dModule.nodeToLoad3dMap.set(node, makeFullFakeLoad3d() as never) + const load3d = makeFullFakeLoad3d() + useLoad3dModule.nodeToLoad3dMap.set(node, load3d as never) await load3DExt.nodeCreated!(node, app) const serialize = widgets[3].serializeValue! as () => Promise<{ image: string } | null> - return { node, serialize, uploadTempImage, useLoad3dModule } + return { node, load3d, serialize, uploadTempImage, useLoad3dModule } } + it('waits for a pending model load before capturing the scene', async () => { + const { load3d, serialize } = await setup() + let releaseLoad!: () => void + load3d.whenLoadIdle.mockImplementation( + () => + new Promise((resolve) => { + releaseLoad = resolve + }) + ) + + const pending = serialize() + await flush() + expect(load3d.whenLoadIdle).toHaveBeenCalledTimes(1) + expect(load3d.captureScene).not.toHaveBeenCalled() + + releaseLoad() + const result = await pending + + expect(load3d.captureScene).toHaveBeenCalledTimes(1) + expect(result?.image).toBe('threed/scene-1.png [temp]') + }) + it('reuses the cached output when the scene has not been dirtied', async () => { const { node, serialize, uploadTempImage, useLoad3dModule } = await setup() @@ -1181,6 +1215,45 @@ describe('Comfy.Load3D scene widget serializeValue caching', () => { expect(refreshed?.image).toBe('threed/scene-4.png [temp]') }) + it('re-captures when the scene changes during an upload', async () => { + const { node, load3d, serialize, uploadTempImage, useLoad3dModule } = + await setup() + let releaseUploads!: () => void + const uploadsBlocked = new Promise((resolve) => { + releaseUploads = resolve + }) + uploadTempImage.mockImplementationOnce(async () => { + await uploadsBlocked + return { name: 'stale-scene.png' } + }) + + const pending = serialize() + await flush() + + useLoad3dModule.markLoad3dSceneDirty(node) + releaseUploads() + + const refreshed = await pending + + expect(load3d.captureScene).toHaveBeenCalledTimes(2) + expect(uploadTempImage).toHaveBeenCalledTimes(6) + expect(refreshed?.image).toBe('threed/scene-3.png [temp]') + }) + + it('returns no scene when capture never stabilizes', async () => { + const { node, load3d, serialize, useLoad3dModule } = await setup() + load3d.captureScene.mockImplementation(async () => { + useLoad3dModule.markLoad3dSceneDirty(node) + return { scene: 'scene-data', mask: 'mask-data', normal: 'normal-data' } + }) + + expect(await serialize()).toBeNull() + + expect(load3d.captureScene).toHaveBeenCalledTimes(3) + expect(useLoad3dModule.isLoad3dSceneDirty(node)).toBe(true) + expect(useLoad3dModule.getLoad3dOutputCache(node)).toBeUndefined() + }) + it('returns null when no load3d instance is registered for the node', async () => { const widgets: FakeWidget[] = [ { name: 'model_file', value: 'm.glb' }, diff --git a/src/extensions/core/load3d.ts b/src/extensions/core/load3d.ts index 655e696ceba..e3474b27c43 100644 --- a/src/extensions/core/load3d.ts +++ b/src/extensions/core/load3d.ts @@ -4,6 +4,7 @@ import Load3D from '@/components/load3d/Load3D.vue' import Load3DViewerContent from '@/components/load3d/Load3dViewerContent.vue' import { getLoad3dOutputCache, + getLoad3dSceneRevision, isLoad3dSceneDirty, markLoad3dSceneDirty, nodeToLoad3dMap, @@ -29,17 +30,25 @@ import Load3dUtils from '@/extensions/core/load3d/Load3dUtils' import { t } from '@/i18n' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { IContextMenuValue } from '@/lib/litegraph/src/interfaces' -import type { IStringWidget } from '@/lib/litegraph/src/types/widgets' +import type { + INumericWidget, + IStringWidget +} from '@/lib/litegraph/src/types/widgets' import { useToastStore } from '@/platform/updates/common/toastStore' import type { NodeExecutionOutput, NodeOutputWith } from '@/platform/remote/comfyui/execution/types' +import { reportError } from '@/platform/telemetry/reportError' import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' import type { NodeLocatorId } from '@/types/nodeIdentification' import { getNodeByLocatorId } from '@/utils/graphTraversalUtil' type Matrix = number[][] + +/** Extra captures allowed when the scene changes mid-capture. */ +const MAX_STALE_CAPTURE_RETRIES = 2 + type Load3dPreviewOutput = NodeOutputWith<{ result?: [string?, CameraState?, string?, Matrix?, Matrix?] }> @@ -386,8 +395,12 @@ useExtensionService().registerExtension({ useLoad3d(node).onLoad3dReady((load3d) => { const modelWidget = node.widgets?.find((w) => w.name === 'model_file') - const width = node.widgets?.find((w) => w.name === 'width') - const height = node.widgets?.find((w) => w.name === 'height') + const width = node.widgets?.find( + (w): w is INumericWidget => w.name === 'width' && w.type === 'number' + ) + const height = node.widgets?.find( + (w): w is INumericWidget => w.name === 'height' && w.type === 'number' + ) if (!modelWidget || !width || !height) return const cameraConfig = node.properties['Camera Config'] as @@ -420,54 +433,70 @@ useExtensionService().registerExtension({ return null } - if (!isLoad3dSceneDirty(node)) { - const cached = getLoad3dOutputCache(node) - if (cached) return cached - } + for (let attempt = 0; ; attempt++) { + if (!isLoad3dSceneDirty(node)) { + const cached = getLoad3dOutputCache(node) + if (cached) return cached + } - const { camera_info, model_3d_info } = snapshotLoad3dState( - node, - currentLoad3d - ) + const sceneRevision = getLoad3dSceneRevision(node) - const { - scene: imageData, - mask: maskData, - normal: normalData - } = await currentLoad3d.captureScene( - width.value as number, - height.value as number - ) + // A model swap (user or agent) may still be loading. Capture the + // scene the queue will actually run, not the one being replaced. + await currentLoad3d.whenLoadIdle() - const [data, dataMask, dataNormal] = await Promise.all([ - Load3dUtils.uploadTempImage(imageData, 'scene'), - Load3dUtils.uploadTempImage(maskData, 'scene_mask'), - Load3dUtils.uploadTempImage(normalData, 'scene_normal') - ]) + const { camera_info, model_3d_info } = snapshotLoad3dState( + node, + currentLoad3d + ) - currentLoad3d.handleResize() + const { + scene: imageData, + mask: maskData, + normal: normalData + } = await currentLoad3d.captureScene( + width.value as number, + height.value as number + ) - const returnVal: Load3dCachedOutput = { - image: `threed/${data.name} [temp]`, - mask: `threed/${dataMask.name} [temp]`, - normal: `threed/${dataNormal.name} [temp]`, - camera_info, - recording: '', - model_3d_info - } + const [data, dataMask, dataNormal] = await Promise.all([ + Load3dUtils.uploadTempImage(imageData, 'scene'), + Load3dUtils.uploadTempImage(maskData, 'scene_mask'), + Load3dUtils.uploadTempImage(normalData, 'scene_normal') + ]) - const recordingData = currentLoad3d.getRecordingData() + currentLoad3d.handleResize() - if (recordingData) { - const [recording] = await Promise.all([ - Load3dUtils.uploadTempImage(recordingData, 'recording', 'mp4') - ]) - returnVal.recording = `threed/${recording.name} [temp]` - } + const returnVal: Load3dCachedOutput = { + image: `threed/${data.name} [temp]`, + mask: `threed/${dataMask.name} [temp]`, + normal: `threed/${dataNormal.name} [temp]`, + camera_info, + recording: '', + model_3d_info + } - setLoad3dOutputCache(node, returnVal) + const recordingData = currentLoad3d.getRecordingData() - return returnVal + if (recordingData) { + const [recording] = await Promise.all([ + Load3dUtils.uploadTempImage(recordingData, 'recording', 'mp4') + ]) + returnVal.recording = `threed/${recording.name} [temp]` + } + + if (setLoad3dOutputCache(node, returnVal, sceneRevision)) { + return returnVal + } + + if (attempt >= MAX_STALE_CAPTURE_RETRIES) { + reportError( + new Error('Load3D scene did not stabilize during capture'), + { errorType: 'error_capturing_load3d_scene_unstable' } + ) + return null + } + } } } }) diff --git a/src/extensions/core/load3d/Load3DConfiguration.test.ts b/src/extensions/core/load3d/Load3DConfiguration.test.ts index 8a000517b02..b34258dafbf 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.test.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.test.ts @@ -1,5 +1,6 @@ import { fromAny, fromPartial } from '@total-typescript/shoehorn' import { beforeEach, describe, expect, it, vi } from 'vitest' +import { nextTick, reactive } from 'vue' import type Load3d from '@/extensions/core/load3d/Load3d' import Load3DConfiguration, { @@ -16,10 +17,15 @@ import type { SceneConfig, StoredModelConfig } from '@/extensions/core/load3d/interfaces' -import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' +import type { + IBaseWidget, + INumericWidget +} from '@/lib/litegraph/src/types/widgets' import type { Dictionary } from '@/lib/litegraph/src/interfaces' import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' +import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { useSettingStore } from '@/platform/settings/settingStore' +import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { Settings } from '@/platform/settings/types' vi.mock(import('@/scripts/api'), () => ({ @@ -64,6 +70,14 @@ function stubSettings(values: Partial) { vi.mocked(useSettingStore().get).mockImplementation((key) => values[key]) } +function reactiveWidget(value: IBaseWidget['value']): IBaseWidget { + return reactive(fromPartial({ value })) +} + +function reactiveNumericWidget(value: number): INumericWidget { + return reactive(fromPartial({ type: 'number', value })) +} + const defaultGizmo: GizmoConfig = { enabled: false, mode: 'translate', @@ -185,12 +199,13 @@ describe('Load3DConfiguration.loadModelConfig', () => { }) describe('Load3DConfiguration.silentOnNotFound propagation', () => { - let loadModelSpy: ReturnType + let loadModelSpy: ReturnType> function makeLoad3dMock(): Load3d { - loadModelSpy = vi.fn().mockResolvedValue(undefined) + loadModelSpy = vi.fn().mockResolvedValue(true) return { loadModel: loadModelSpy, + clearModel: vi.fn(), setUpDirection: vi.fn(), setMaterialMode: vi.fn(), setTargetSize: vi.fn(), @@ -205,12 +220,14 @@ describe('Load3DConfiguration.silentOnNotFound propagation', () => { setHDRIIntensity: vi.fn(), setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn() + emitModelReady: vi.fn(), + setConfigurationCleanup: vi.fn() } as unknown as Load3d } async function flush() { - await new Promise((resolve) => setTimeout(resolve, 0)) + await nextTick() + await Promise.resolve() } beforeEach(() => { @@ -310,6 +327,60 @@ describe('Load3DConfiguration.silentOnNotFound propagation', () => { await flush() expect(vi.mocked(load3d.emitModelReady)).toHaveBeenCalledTimes(1) }) + + it('does not publish effects for a load superseded by clear', async () => { + let resolveLoad!: (accepted: boolean) => void + const load3d = makeLoad3dMock() + vi.mocked(load3d.loadModel).mockImplementation( + () => + new Promise((resolve) => { + resolveLoad = resolve + }) + ) + const modelWidget = reactiveWidget('a.glb') + const config = new Load3DConfiguration(load3d) + + config.configure({ modelWidget, loadFolder: 'output' }) + modelWidget.value = '' + resolveLoad(false) + await flush() + + expect(load3d.setUpDirection).not.toHaveBeenCalled() + expect(load3d.setMaterialMode).not.toHaveBeenCalled() + expect(load3d.emitModelReady).not.toHaveBeenCalled() + }) + + it('publishes effects only for the replacement after clear', async () => { + let resolveFirst!: (accepted: boolean) => void + let resolveSecond!: (accepted: boolean) => void + const load3d = makeLoad3dMock() + vi.mocked(load3d.loadModel) + .mockImplementationOnce( + () => + new Promise((resolve) => { + resolveFirst = resolve + }) + ) + .mockImplementationOnce( + () => + new Promise((resolve) => { + resolveSecond = resolve + }) + ) + const modelWidget = reactiveWidget('a.glb') + const config = new Load3DConfiguration(load3d) + + config.configure({ modelWidget, loadFolder: 'output' }) + modelWidget.value = '' + modelWidget.value = 'b.glb' + resolveFirst(false) + resolveSecond(true) + await flush() + + expect(load3d.setUpDirection).toHaveBeenCalledTimes(1) + expect(load3d.setMaterialMode).toHaveBeenCalledTimes(1) + expect(load3d.emitModelReady).toHaveBeenCalledTimes(1) + }) }) describe('parseAnnotatedFilename', () => { @@ -474,8 +545,8 @@ describe('Load3DConfiguration.configure forwards persisted + settings to load3d' let load3d: Load3d function makeLoad3dMock(): Load3d { - return { - loadModel: vi.fn().mockResolvedValue(undefined), + return fromPartial({ + loadModel: vi.fn().mockResolvedValue(true), setUpDirection: vi.fn(), setMaterialMode: vi.fn(), setTargetSize: vi.fn(), @@ -490,12 +561,14 @@ describe('Load3DConfiguration.configure forwards persisted + settings to load3d' setHDRIIntensity: vi.fn(), setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn() - } as unknown as Load3d + emitModelReady: vi.fn(), + setConfigurationCleanup: vi.fn() + }) } async function flush() { - await new Promise((resolve) => setTimeout(resolve, 0)) + await nextTick() + await Promise.resolve() } beforeEach(() => { @@ -560,13 +633,14 @@ describe('Load3DConfiguration.configure forwards persisted + settings to load3d' describe('Load3DConfiguration "none" model handling', () => { let load3d: Load3d - let loadModelSpy: ReturnType + let loadModelSpy: ReturnType> let clearModelSpy: ReturnType function makeLoad3dMock(): Load3d { - loadModelSpy = vi.fn().mockResolvedValue(undefined) + let cleanup: (() => void) | undefined + loadModelSpy = vi.fn().mockResolvedValue(true) clearModelSpy = vi.fn() - return { + return fromPartial({ loadModel: loadModelSpy, clearModel: clearModelSpy, setUpDirection: vi.fn(), @@ -583,12 +657,17 @@ describe('Load3DConfiguration "none" model handling', () => { setHDRIIntensity: vi.fn(), setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn() - } as unknown as Load3d + emitModelReady: vi.fn(), + setConfigurationCleanup: vi.fn((nextCleanup: () => void) => { + cleanup?.() + cleanup = nextCleanup + }) + }) } async function flush() { - await new Promise((resolve) => setTimeout(resolve, 0)) + await nextTick() + await Promise.resolve() } beforeEach(() => { @@ -611,7 +690,7 @@ describe('Load3DConfiguration "none" model handling', () => { it('clears the model (and skips loadModel) when the widget value changes to "none"', async () => { const config = new Load3DConfiguration(load3d) - const widget = { value: 'model.glb' } as unknown as IBaseWidget + const widget = reactiveWidget('model.glb') config.configure({ modelWidget: widget, loadFolder: 'input' }) await flush() @@ -627,7 +706,7 @@ describe('Load3DConfiguration "none" model handling', () => { it('loads a model when the widget value transitions from "none" to a real path', async () => { const config = new Load3DConfiguration(load3d) - const widget = { value: 'none' } as unknown as IBaseWidget + const widget = reactiveWidget('none') config.configure({ modelWidget: widget, loadFolder: 'input' }) await flush() @@ -640,12 +719,98 @@ describe('Load3DConfiguration "none" model handling', () => { silentOnNotFound: false }) }) + + it('stops reacting as soon as the viewer lifecycle is removed', async () => { + const graph = new LGraph() + const node = new LGraphNode('Load3D') + graph.add(node) + const modelWidget = node.addWidget( + 'string', + 'model_file', + 'none', + () => undefined + ) + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget, loadFolder: 'input' }) + await flush() + const cleanup = vi + .mocked(load3d.setConfigurationCleanup) + .mock.calls.at(-1)?.[0] + if (!cleanup) throw new Error('Expected reactive configuration cleanup') + graph.remove(node) + expect(modelWidget.widgetId).toBeUndefined() + cleanup() + loadModelSpy.mockClear() + + modelWidget.value = 'ignored.glb' + await flush() + + expect(loadModelSpy).not.toHaveBeenCalled() + }) + + it('reloads the model when the CRDT follower writes model_file through widgetValueStore', async () => { + const onSceneInvalidated = vi.fn() + const graph = new LGraph() + const node = new LGraphNode('Load3D') + graph.add(node) + const modelWidget = node.addWidget( + 'string', + 'model_file', + 'none', + () => undefined + ) + const widgetId = modelWidget.widgetId + if (!widgetId) throw new Error('Expected a store-backed widget id') + + new Load3DConfiguration(load3d).configure({ + modelWidget, + loadFolder: 'input', + onSceneInvalidated + }) + await flush() + loadModelSpy.mockClear() + onSceneInvalidated.mockClear() + + const applied = useWidgetValueStore().setValue(widgetId, 'agent.glb', { + source: 'agent-remote', + actor: 'agent:e2e', + opId: 'op-1' + }) + + expect(applied).toBe(true) + expect(onSceneInvalidated).toHaveBeenCalledTimes(1) + expect(loadModelSpy).toHaveBeenCalledWith(expect.any(String), 'agent.glb', { + silentOnNotFound: false + }) + expect(modelWidget.value).toBe('agent.glb') + }) + + it('stops reacting to the previous widget once configure runs with a replacement', async () => { + const oldWidget = reactiveWidget('none') + const replacementWidget = reactiveWidget('none') + const config = new Load3DConfiguration(load3d) + config.configure({ modelWidget: oldWidget, loadFolder: 'input' }) + config.configure({ modelWidget: replacementWidget, loadFolder: 'input' }) + await flush() + loadModelSpy.mockClear() + + oldWidget.value = 'stale.glb' + replacementWidget.value = 'restored.glb' + await flush() + + expect(loadModelSpy).toHaveBeenCalledTimes(1) + expect(loadModelSpy).toHaveBeenCalledWith( + expect.any(String), + 'restored.glb', + { silentOnNotFound: false } + ) + }) }) describe('Load3DConfiguration.onSceneInvalidated', () => { function makeLoad3dMock(): Load3d { return { - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue(true), clearModel: vi.fn(), setUpDirection: vi.fn(), setMaterialMode: vi.fn(), @@ -661,12 +826,14 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { setHDRIIntensity: vi.fn(), setHDRIAsBackground: vi.fn(), setHDRIEnabled: vi.fn(), - emitModelReady: vi.fn() + emitModelReady: vi.fn(), + setConfigurationCleanup: vi.fn() } as unknown as Load3d } async function flush() { - await new Promise((resolve) => setTimeout(resolve, 0)) + await nextTick() + await Promise.resolve() } beforeEach(() => { @@ -676,8 +843,8 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { it('width.callback invokes onSceneInvalidated', async () => { const onSceneInvalidated = vi.fn() - const width = { value: 1024 } as unknown as IBaseWidget - const height = { value: 1024 } as unknown as IBaseWidget + const width = reactiveNumericWidget(1024) + const height = reactiveNumericWidget(1024) const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -689,15 +856,15 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) await flush() - width.callback!(2048) + width.value = 2048 expect(onSceneInvalidated).toHaveBeenCalledTimes(1) }) it('height.callback invokes onSceneInvalidated', async () => { const onSceneInvalidated = vi.fn() - const width = { value: 1024 } as unknown as IBaseWidget - const height = { value: 1024 } as unknown as IBaseWidget + const width = reactiveNumericWidget(1024) + const height = reactiveNumericWidget(1024) const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -709,14 +876,14 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) await flush() - height.callback!(2048) + height.value = 2048 expect(onSceneInvalidated).toHaveBeenCalledTimes(1) }) it('model_file widget callback invokes onSceneInvalidated after the model loads', async () => { const onSceneInvalidated = vi.fn() - const modelWidget = { value: 'none' } as unknown as IBaseWidget + const modelWidget = reactiveWidget('none') const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -735,10 +902,11 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { it('preserves any pre-existing model widget callback alongside the invalidation hook', async () => { const onSceneInvalidated = vi.fn() const original = vi.fn() - const modelWidget = { + const modelWidget = reactiveWidget('none') + Object.assign(modelWidget, { value: 'none', callback: original - } as unknown as IBaseWidget + }) const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -749,6 +917,7 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { await flush() modelWidget.value = 'model.glb' + modelWidget.callback?.('model.glb') await flush() expect(original).toHaveBeenCalledWith('model.glb') @@ -756,9 +925,9 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) it('callbacks remain safe when onSceneInvalidated is omitted', async () => { - const width = { value: 1024 } as unknown as IBaseWidget - const height = { value: 1024 } as unknown as IBaseWidget - const modelWidget = { value: 'none' } as unknown as IBaseWidget + const width = reactiveNumericWidget(1024) + const height = reactiveNumericWidget(1024) + const modelWidget = reactiveWidget('none') const config = new Load3DConfiguration(makeLoad3dMock()) config.configure({ @@ -769,8 +938,12 @@ describe('Load3DConfiguration.onSceneInvalidated', () => { }) await flush() - expect(() => width.callback!(2048)).not.toThrow() - expect(() => height.callback!(2048)).not.toThrow() + expect(() => { + width.value = 2048 + }).not.toThrow() + expect(() => { + height.value = 2048 + }).not.toThrow() expect(() => { modelWidget.value = 'model.glb' }).not.toThrow() diff --git a/src/extensions/core/load3d/Load3DConfiguration.ts b/src/extensions/core/load3d/Load3DConfiguration.ts index 926eb0eb1f9..fa8cd51719c 100644 --- a/src/extensions/core/load3d/Load3DConfiguration.ts +++ b/src/extensions/core/load3d/Load3DConfiguration.ts @@ -1,3 +1,5 @@ +import { effectScope, watch } from 'vue' + import { LOAD3D_NONE_MODEL } from '@/extensions/core/load3d/constants' import type Load3d from '@/extensions/core/load3d/Load3d' import Load3dUtils from '@/extensions/core/load3d/Load3dUtils' @@ -13,7 +15,10 @@ import type { } from '@/extensions/core/load3d/interfaces' import type { Dictionary } from '@/lib/litegraph/src/interfaces' import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' -import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' +import type { + IBaseWidget, + INumericWidget +} from '@/lib/litegraph/src/types/widgets' import { useSettingStore } from '@/platform/settings/settingStore' import { api } from '@/scripts/api' @@ -21,13 +26,13 @@ type Load3DConfigurationSettings = { loadFolder: string modelWidget: IBaseWidget cameraState?: CameraState - width?: IBaseWidget - height?: IBaseWidget + width?: INumericWidget + height?: INumericWidget bgImagePath?: string silentOnNotFound?: boolean /** - * Called when a user-driven change to one of the wired widgets - * (model_file, width, height) makes the previously captured scene stale. + * Called when any change to one of the wired widgets (model_file, width, + * height), local or remote, makes the previously captured scene stale. * Backend caching covers these inputs by themselves; this hook lets the * caller invalidate any frontend-side capture cache so the next serialize * re-renders at the new state. @@ -77,38 +82,50 @@ class Load3DConfiguration { } configure(setting: Load3DConfigurationSettings) { - this.setupModelHandling( + const onModelWidgetUpdate = this.setupModelHandling( setting.modelWidget, setting.loadFolder, setting.cameraState, - setting.silentOnNotFound ?? false, - setting.onSceneInvalidated - ) - this.setupTargetSize( - setting.width, - setting.height, - setting.onSceneInvalidated + setting.silentOnNotFound ?? false ) + this.setupTargetSize(setting.width, setting.height) + this.setupReactiveHandling(setting, onModelWidgetUpdate) this.setupDefaultProperties(setting.bgImagePath) } - private setupTargetSize( - width?: IBaseWidget, - height?: IBaseWidget, - onSceneInvalidated?: () => void - ) { - if (width && height) { - this.load3d.setTargetSize(width.value as number, height.value as number) + private setupReactiveHandling( + setting: Load3DConfigurationSettings, + onModelWidgetUpdate: (value: IBaseWidget['value']) => Promise + ): void { + const scope = effectScope() + scope.run(() => { + watch( + () => setting.modelWidget.value, + (value) => { + void onModelWidgetUpdate(value) + setting.onSceneInvalidated?.() + }, + { flush: 'sync' } + ) - width.callback = (value: number) => { - this.load3d.setTargetSize(value, height.value as number) - onSceneInvalidated?.() + const { width, height } = setting + if (width && height) { + watch( + [() => width.value, () => height.value], + ([nextWidth, nextHeight]) => { + this.load3d.setTargetSize(nextWidth, nextHeight) + setting.onSceneInvalidated?.() + }, + { flush: 'sync' } + ) } + }) + this.load3d.setConfigurationCleanup(() => scope.stop()) + } - height.callback = (value: number) => { - this.load3d.setTargetSize(width.value as number, value) - onSceneInvalidated?.() - } + private setupTargetSize(width?: INumericWidget, height?: INumericWidget) { + if (width && height) { + this.load3d.setTargetSize(width.value, height.value) } } @@ -132,8 +149,7 @@ class Load3DConfiguration { modelWidget: IBaseWidget, loadFolder: string, cameraState?: CameraState, - silentOnNotFound: boolean = false, - onSceneInvalidated?: () => void + silentOnNotFound: boolean = false ) { const onModelWidgetUpdate = this.createModelUpdateHandler( loadFolder, @@ -144,32 +160,7 @@ class Load3DConfiguration { void onModelWidgetUpdate(modelWidget.value) } - const originalCallback = modelWidget.callback - - let currentValue = modelWidget.value - Object.defineProperty(modelWidget, 'value', { - get() { - return currentValue - }, - set(newValue) { - currentValue = newValue - if (modelWidget.callback && newValue !== undefined && newValue !== '') { - modelWidget.callback(newValue) - } - }, - enumerable: true, - configurable: true - }) - - modelWidget.callback = (value: string | number | boolean | object) => { - void onModelWidgetUpdate(value) - - if (originalCallback) { - originalCallback(value) - } - - onSceneInvalidated?.() - } + return onModelWidgetUpdate } private setupDefaultProperties(bgImagePath?: string) { @@ -296,7 +287,7 @@ class Load3DConfiguration { silentOnNotFound: boolean = false ) { let isFirstLoad = true - return async (value: string | number | boolean | object) => { + return async (value: IBaseWidget['value']) => { if (!value || value === LOAD3D_NONE_MODEL) { this.load3d.clearModel() return @@ -316,7 +307,10 @@ class Load3DConfiguration { ) ) - await this.load3d.loadModel(modelUrl, filename, { silentOnNotFound }) + const accepted = await this.load3d.loadModel(modelUrl, filename, { + silentOnNotFound + }) + if (!accepted) return const modelConfig = this.loadModelConfig() this.applyModelConfig(modelConfig) diff --git a/src/extensions/core/load3d/Load3d.test.ts b/src/extensions/core/load3d/Load3d.test.ts index 3fd825cede4..1f6a279bf57 100644 --- a/src/extensions/core/load3d/Load3d.test.ts +++ b/src/extensions/core/load3d/Load3d.test.ts @@ -136,6 +136,8 @@ function makeInstance() { animationManager, eventManager, adapterRef: { current: null }, + _loadGeneration: 0, + loadingPromise: null, forceRender: vi.fn(), handleResize: vi.fn(), preRenderCallbacks: [], @@ -829,8 +831,8 @@ describe('Load3d', () => { }) it('waits for the current loadingPromise to settle', async () => { - let resolveLoad!: () => void - const p = new Promise((resolve) => { + let resolveLoad!: (accepted: boolean) => void + const p = new Promise((resolve) => { resolveLoad = resolve }) Object.assign(ctx.load3d, { loadingPromise: p }) @@ -844,7 +846,7 @@ describe('Load3d', () => { await Promise.resolve() expect(settled).toBe(false) - resolveLoad() + resolveLoad(true) Object.assign(ctx.load3d, { loadingPromise: null }) await idle @@ -852,12 +854,12 @@ describe('Load3d', () => { }) it('drains a chained sequence of loads before resolving', async () => { - let resolveFirst!: () => void - const first = new Promise((resolve) => { + let resolveFirst!: (accepted: boolean) => void + const first = new Promise((resolve) => { resolveFirst = resolve }) - let resolveSecond!: () => void - const second = new Promise((resolve) => { + let resolveSecond!: (accepted: boolean) => void + const second = new Promise((resolve) => { resolveSecond = resolve }) @@ -872,11 +874,11 @@ describe('Load3d', () => { settled = true }) - resolveFirst() + resolveFirst(true) await new Promise((r) => setTimeout(r, 0)) expect(settled).toBe(false) - resolveSecond() + resolveSecond(true) Object.assign(ctx.load3d, { loadingPromise: null }) await idle expect(settled).toBe(true) @@ -892,6 +894,94 @@ describe('Load3d', () => { await expect(idle).resolves.toBeUndefined() }) + + it('waits for a load accepted while the current load is still pending', async () => { + let resolveFirst!: () => void + let resolveSecond!: () => void + const first = new Promise((resolve) => { + resolveFirst = resolve + }) + const second = new Promise((resolve) => { + resolveSecond = resolve + }) + const internal = vi + .fn() + .mockImplementationOnce(() => first) + .mockImplementationOnce(() => second) + Object.assign(ctx.load3d, { + loadingPromise: null, + _loadModelInternal: internal + }) + + const loadA = ctx.load3d.loadModel('api/view?filename=a.glb') + const idle = ctx.load3d.whenLoadIdle() + const loadB = ctx.load3d.loadModel('api/view?filename=b.glb') + let settled = false + void idle.then(() => { + settled = true + }) + + resolveFirst() + await loadA + await Promise.resolve() + expect(internal).toHaveBeenCalledTimes(2) + expect(settled).toBe(false) + + resolveSecond() + await Promise.all([idle, loadB]) + expect(settled).toBe(true) + }) + + it('keeps the viewer empty when clear is accepted during a pending load', async () => { + let resolveLoad!: () => void + const pendingLoad = new Promise((resolve) => { + resolveLoad = resolve + }) + const loadedModel = new THREE.Group() + const modelManager: typeof ctx.modelManager & { + currentModel: THREE.Object3D | null + originalModel: THREE.Object3D | null + } = { + ...ctx.modelManager, + currentModel: null, + originalModel: null, + clearModel: vi.fn(() => { + modelManager.currentModel = null + }) + } + Object.assign(ctx.load3d, { + _loadGeneration: 0, + loadingPromise: null, + cameraManager: { + ...ctx.cameraManager, + getCameraState: vi.fn(), + getCurrentCameraType: vi.fn(() => 'perspective'), + setCameraState: vi.fn() + }, + controlsManager: { ...ctx.controlsManager, reset: vi.fn() }, + loaderManager: { + loadModel: vi.fn(async () => { + await pendingLoad + modelManager.currentModel = loadedModel + }) + }, + modelManager, + animationManager: { + ...ctx.animationManager, + setupModelAnimations: vi.fn() + }, + hasLoadedModel: false + }) + + const load = ctx.load3d.loadModel('api/view?filename=a.glb') + ctx.load3d.clearModel() + const idle = ctx.load3d.whenLoadIdle() + resolveLoad() + const [accepted] = await Promise.all([load, idle]) + + expect(accepted).toBe(false) + expect(ctx.load3d.getCurrentModel()).toBeNull() + }) }) describe('currentLoadGeneration', () => { @@ -1468,8 +1558,25 @@ describe('Load3d', () => { expect(source(12, 34)).toBe(ndc) expect(clientPointToNdc).toHaveBeenCalledWith(12, 34) + }) + + it('runs the replaced configuration cleanup immediately and the current one once on remove()', () => { + const { container, deps } = makeConstructorDeps() + const load3d = new Load3d(container, deps) + const first = vi.fn() + const second = vi.fn() + + load3d.setConfigurationCleanup(first) + expect(first).not.toHaveBeenCalled() + load3d.setConfigurationCleanup(second) + expect(first).toHaveBeenCalledOnce() + expect(second).not.toHaveBeenCalled() + + load3d.remove() load3d.remove() + expect(first).toHaveBeenCalledOnce() + expect(second).toHaveBeenCalledOnce() }) }) }) diff --git a/src/extensions/core/load3d/Load3d.ts b/src/extensions/core/load3d/Load3d.ts index 2f4d425d60e..f600b36617c 100644 --- a/src/extensions/core/load3d/Load3d.ts +++ b/src/extensions/core/load3d/Load3d.ts @@ -62,8 +62,9 @@ class Load3d extends Viewport3d { animationManager: AnimationManager gizmoManager: GizmoManager adapterRef: AdapterRef + private configurationCleanup?: () => void - private loadingPromise: Promise | null = null + private loadingPromise: Promise | null = null private _loadGeneration: number = 0 private hasLoadedModel: boolean = false @@ -332,27 +333,39 @@ class Load3d extends Viewport3d { url: string, originalFileName?: string, options?: LoadModelOptions - ): Promise { + ): Promise { this._loadGeneration += 1 + const loadGeneration = this._loadGeneration - if (this.loadingPromise) { + const previousLoad = this.loadingPromise + const acceptedLoad = (async () => { try { - await this.loadingPromise + await previousLoad } catch { // Serialization only: the rejection already reached the loadModel caller. } - } - this.loadingPromise = this._loadModelInternal( - url, - originalFileName, - options - ) - return this.loadingPromise + try { + await this._loadModelInternal(url, originalFileName, options) + } finally { + if (loadGeneration !== this._loadGeneration) this.clearModelState() + } + + return loadGeneration === this._loadGeneration + })() + + // Publish the tail before waiting so every accepted load is visible to + // whenLoadIdle(), including loads queued behind the current one. + this.loadingPromise = acceptedLoad + try { + return await acceptedLoad + } finally { + if (this.loadingPromise === acceptedLoad) this.loadingPromise = null + } } async whenLoadIdle(): Promise { - let last: Promise | null = null + let last: Promise | null = null while (this.loadingPromise && this.loadingPromise !== last) { last = this.loadingPromise try { @@ -402,8 +415,6 @@ class Load3d extends Viewport3d { } this.handleResize() - - this.loadingPromise = null } isSplatModel(): boolean { @@ -419,6 +430,11 @@ class Load3d extends Viewport3d { } clearModel(): void { + this._loadGeneration += 1 + this.clearModelState() + } + + private clearModelState(): void { this.animationManager.dispose() this.gizmoManager.detach() this.modelManager.clearModel() @@ -662,7 +678,18 @@ class Load3d extends Viewport3d { this.forceRender() } + setConfigurationCleanup(cleanup: () => void): void { + this.clearConfigurationCleanup() + this.configurationCleanup = cleanup + } + + private clearConfigurationCleanup(): void { + this.configurationCleanup?.() + this.configurationCleanup = undefined + } + protected override disposeManagers(): void { + this.clearConfigurationCleanup() super.disposeManagers() this.hdriManager.dispose() this.loaderManager.dispose() diff --git a/src/extensions/core/load3d/load3dSerialize.test.ts b/src/extensions/core/load3d/load3dSerialize.test.ts index ae99489da39..6a4745cc00a 100644 --- a/src/extensions/core/load3d/load3dSerialize.test.ts +++ b/src/extensions/core/load3d/load3dSerialize.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it, vi } from 'vitest' +import { nextTick, reactive, toRaw, watch } from 'vue' import type Load3d from '@/extensions/core/load3d/Load3d' import { snapshotLoad3dState } from '@/extensions/core/load3d/load3dSerialize' @@ -84,4 +85,53 @@ describe('snapshotLoad3dState', () => { ) expect(result.model_3d_info).toEqual([]) }) + + describe('with reactive node.properties (node data store proxy)', () => { + it('records the camera state without notifying Camera Config watchers', async () => { + const props = reactive>({ + 'Camera Config': { cameraType: 'perspective', fov: 75, state: null } + }) + const node = { properties: props } as unknown as LGraphNode + let triggers = 0 + watch( + () => props['Camera Config'], + () => { + triggers++ + }, + { deep: true } + ) + + const result = snapshotLoad3dState(node, makeLoad3d()) + await nextTick() + + expect(triggers).toBe(0) + expect(result.camera_info).toBe(baseCameraState) + expect( + (toRaw(props)['Camera Config'] as Record).state + ).toBe(baseCameraState) + }) + + it('creates Camera Config without notifying watchers when it is absent', async () => { + const props = reactive>({}) + const node = { properties: props } as unknown as LGraphNode + let triggers = 0 + watch( + () => props['Camera Config'], + () => { + triggers++ + }, + { deep: true } + ) + + snapshotLoad3dState(node, makeLoad3d({ fov: 42 })) + await nextTick() + + expect(triggers).toBe(0) + expect(toRaw(props)['Camera Config']).toMatchObject({ + cameraType: 'perspective', + fov: 42, + state: baseCameraState + }) + }) + }) }) diff --git a/src/extensions/core/load3d/load3dSerialize.ts b/src/extensions/core/load3d/load3dSerialize.ts index ee0541e47fc..20e5bbd58b1 100644 --- a/src/extensions/core/load3d/load3dSerialize.ts +++ b/src/extensions/core/load3d/load3dSerialize.ts @@ -1,3 +1,5 @@ +import { toRaw } from 'vue' + import type Load3d from '@/extensions/core/load3d/Load3d' import type { CameraConfig, @@ -15,14 +17,20 @@ export function snapshotLoad3dState( node: LGraphNode, load3d: Load3d ): Load3dSerializedBase { - const cameraConfig: CameraConfig = (node.properties['Camera Config'] as + // `node.properties` is the node data store's reactive proxy, and useLoad3d + // deep-watches `Camera Config` to mark the scene dirty. Recording the live + // camera into the snapshot is a read of the scene, not a change to it, so + // write through the raw object; otherwise every capture re-dirties the + // scene and the capture-until-stable loop in load3d.ts never settles. + const properties = toRaw(node.properties) + const cameraConfig: CameraConfig = (properties['Camera Config'] as | CameraConfig | undefined) || { cameraType: load3d.getCurrentCameraType(), fov: load3d.cameraManager.perspectiveCamera.fov } cameraConfig.state = load3d.getCameraState() - node.properties['Camera Config'] = cameraConfig + properties['Camera Config'] = cameraConfig load3d.stopRecording() diff --git a/src/extensions/core/load3dAdvanced.ts b/src/extensions/core/load3dAdvanced.ts index 9da4912408f..963887ca334 100644 --- a/src/extensions/core/load3dAdvanced.ts +++ b/src/extensions/core/load3dAdvanced.ts @@ -8,6 +8,7 @@ import Load3DConfiguration from '@/extensions/core/load3d/Load3DConfiguration' import { snapshotLoad3dState } from '@/extensions/core/load3d/load3dSerialize' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { IContextMenuValue } from '@/lib/litegraph/src/interfaces' +import type { INumericWidget } from '@/lib/litegraph/src/types/widgets' import type { CustomInputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2' import { ComponentWidgetImpl, addWidget } from '@/scripts/domWidget' import { useExtensionService } from '@/services/extensionService' @@ -67,8 +68,12 @@ useExtensionService().registerExtension({ useLoad3d(node).onLoad3dReady((load3d) => { const modelWidget = node.widgets?.find((w) => w.name === 'model_file') - const width = node.widgets?.find((w) => w.name === 'width') - const height = node.widgets?.find((w) => w.name === 'height') + const width = node.widgets?.find( + (w): w is INumericWidget => w.name === 'width' && w.type === 'number' + ) + const height = node.widgets?.find( + (w): w is INumericWidget => w.name === 'height' && w.type === 'number' + ) if (!modelWidget || !width || !height) return const cameraConfig = node.properties['Camera Config'] as diff --git a/src/services/load3dService.test.ts b/src/services/load3dService.test.ts index 26858d8658d..ea2691a2c1d 100644 --- a/src/services/load3dService.test.ts +++ b/src/services/load3dService.test.ts @@ -442,7 +442,7 @@ describe('load3dService', () => { remove: sceneRemove } as unknown as THREE.Scene }), - loadModel: vi.fn().mockResolvedValue(undefined), + loadModel: vi.fn().mockResolvedValue(true), setMaterialMode: vi.fn(), setUpDirection: vi.fn(), applyGizmoTransform: vi.fn(), @@ -503,6 +503,26 @@ describe('load3dService', () => { expect(skeletonCloneMock).not.toHaveBeenCalled() }) + it('does not apply stale scene state after a rejected splat load', async () => { + const source = makeSource({ + currentModel: makeModel(), + isSplat: true, + originalURL: 'http://example.com/scan.splat' + }) + const { target } = makeTarget() + vi.mocked(target.loadModel).mockResolvedValue(false) + + await useLoad3dService().copyLoad3dState(source, target) + + expect(target.toggleCamera).not.toHaveBeenCalled() + expect(target.setCameraState).not.toHaveBeenCalled() + expect(target.setBackgroundColor).not.toHaveBeenCalled() + expect(target.toggleGrid).not.toHaveBeenCalled() + expect(target.setBackgroundImage).not.toHaveBeenCalled() + expect(target.setLightIntensity).not.toHaveBeenCalled() + expect(target.setFOV).not.toHaveBeenCalled() + }) + it('skips loadModel for splat models when originalURL is null', async () => { const source = makeSource({ currentModel: makeModel(), diff --git a/src/services/load3dService.ts b/src/services/load3dService.ts index af4e7d7f942..cc1550c16ec 100644 --- a/src/services/load3dService.ts +++ b/src/services/load3dService.ts @@ -232,8 +232,8 @@ class Load3dService { if (source.isSplatModel()) { const originalURL = source.modelManager.originalURL - if (originalURL) { - await target.loadModel(originalURL) + if (originalURL && !(await target.loadModel(originalURL))) { + return } } else { // Use SkeletonUtils.clone for proper skeletal animation support