// @rstest-environment jsdom import type { DeviceAction } from '@midscene/core'; import { afterEach, describe, expect, it, rs } from '@rstest/core'; import { act, createElement, useEffect, useRef, useState } from 'react'; import { createRoot } from 'react-dom/client'; import { usePlaygroundExecution } from '../src/hooks/usePlaygroundExecution'; import type { InfoListItem, PlaygroundSDKLike } from '../src/types'; import type { ReplayScriptsInfo } from '../src/utils/replay-scripts'; const { allScriptsFromDumpMock, envConfigMock } = rs.hoisted(() => ({ allScriptsFromDumpMock: rs.fn<(dump: unknown) => ReplayScriptsInfo | null>( () => null, ), envConfigMock: { alwaysRefreshScreenInfo: false, autoDismissKeyboard: false, deepLocate: undefined, deepThink: 'unset' as const, domIncluded: false, imeStrategy: undefined, screenshotStrategy: undefined as 'auto' | 'always-yadb' | undefined, keyboardDismissStrategy: undefined, screenshotIncluded: false, }, })); rs.mock('@midscene/core/agent', () => ({ paramStr: () => '', typeStr: () => 'Plan', })); rs.mock('../src/store/store', () => ({ useEnvConfig: () => envConfigMock, })); rs.mock('../src/utils/replay-scripts', () => ({ allScriptsFromDump: allScriptsFromDumpMock, })); ( globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean } ).IS_REACT_ACT_ENVIRONMENT = true; interface HarnessSnapshot { cancelCurrentExecution: ReturnType< typeof usePlaygroundExecution >['cancelCurrentExecution']; handleRun: ReturnType['handleRun']; handleStop: ReturnType['handleStop']; infoList: InfoListItem[]; loading: boolean; } function Harness({ onSnapshot, playgroundSDK, }: { onSnapshot: (snapshot: HarnessSnapshot) => void; playgroundSDK: PlaygroundSDKLike; }) { const [loading, setLoading] = useState(false); const [infoList, setInfoList] = useState([]); const [replayCounter, setReplayCounter] = useState(0); const currentRunningIdRef = useRef(null); const interruptedFlagRef = useRef>({}); const execution = usePlaygroundExecution({ actionSpace: [] as DeviceAction[], currentRunningIdRef, deviceType: 'web', interruptedFlagRef, loading, playgroundSDK, replayCounter, setInfoList, setLoading, setReplayCounter, storage: null, verticalMode: false, }); useEffect(() => { onSnapshot({ cancelCurrentExecution: execution.cancelCurrentExecution, handleRun: execution.handleRun, handleStop: execution.handleStop, infoList, loading, }); }, [ execution.handleRun, execution.handleStop, infoList, loading, onSnapshot, ]); return null; } function replayDump() { return { sdkVersion: 'test', groupName: 'Playground run', modelBriefs: [], executions: [ { id: 'playground-execution', logTime: 1, name: 'Playground execution', tasks: [ { type: 'Planning', param: { userInstruction: { prompt: 'Compare with the reference image', images: [ { name: 'reference', url: { type: 'midscene_image_url_ref', id: 'reference-1', mimeType: 'image/webp', storage: 'file', path: './screenshots/reference-1.webp', }, }, ], }, }, uiContext: { screenshot: { type: 'midscene_screenshot_ref', id: 'shot-1', capturedAt: 1, mimeType: 'image/png', storage: 'inline', }, }, }, ], }, ], }; } function reportWithReplay() { return ` `; } describe('usePlaygroundExecution stop handling', () => { afterEach(() => { document.body.replaceChildren(); rs.restoreAllMocks(); rs.unstubAllGlobals(); allScriptsFromDumpMock.mockReset(); allScriptsFromDumpMock.mockReturnValue(null); envConfigMock.screenshotStrategy = undefined; }); it('forwards an updated screenshot strategy to the next execution', async () => { let snapshot: HarnessSnapshot | null = null; const getSnapshot = () => { if (!snapshot) throw new Error('Harness snapshot is not ready'); return snapshot; }; const onSnapshot = (nextSnapshot: HarnessSnapshot) => { snapshot = nextSnapshot; }; const playgroundSDK = { executeAction: rs.fn().mockResolvedValue(null), onDumpUpdate: rs.fn(), onProgressUpdate: rs.fn(), } as unknown as PlaygroundSDKLike; const container = document.createElement('div'); document.body.appendChild(container); const root = createRoot(container); await act(async () => { root.render(createElement(Harness, { onSnapshot, playgroundSDK })); }); envConfigMock.screenshotStrategy = 'always-yadb'; await act(async () => { root.render(createElement(Harness, { onSnapshot, playgroundSDK })); }); await act(async () => { await getSnapshot().handleRun({ prompt: 'Capture', type: 'aiAct' }); }); expect(playgroundSDK.executeAction).toHaveBeenCalledWith( 'aiAct', { prompt: 'Capture', type: 'aiAct' }, expect.objectContaining({ deviceOptions: expect.objectContaining({ screenshotStrategy: 'always-yadb', }), }), ); await act(async () => root.unmount()); }); it('restores replay scripts from a report-only stop result', async () => { allScriptsFromDumpMock.mockReturnValue({ scripts: [{ type: 'img', duration: 0, img: 'data:image/png;base64,abc' }], width: 100, height: 200, modelBriefs: [], }); const reportHTML = reportWithReplay(); let snapshot: HarnessSnapshot | null = null; const getSnapshot = () => { if (!snapshot) throw new Error('Harness snapshot is not ready'); return snapshot; }; const playgroundSDK = { cancelExecution: rs.fn(async () => ({ dump: null, reportHTML, })), executeAction: rs.fn(() => new Promise(() => undefined)), onDumpUpdate: rs.fn(), onProgressUpdate: rs.fn(), } as unknown as PlaygroundSDKLike; const container = document.createElement('div'); document.body.appendChild(container); const root = createRoot(container); await act(async () => { root.render( createElement(Harness, { onSnapshot: (nextSnapshot) => { snapshot = nextSnapshot; }, playgroundSDK, }), ); }); await act(async () => { void getSnapshot().handleRun({ prompt: 'Replay', type: 'aiAct' }); }); await act(async () => { await getSnapshot().handleStop(); }); const stoppedResult = getSnapshot().infoList.find((item) => item.id.startsWith('stop-result-'), ); expect(stoppedResult?.result?.reportHTML).toBe(reportHTML); expect(stoppedResult?.replayScriptsInfo?.scripts).toHaveLength(1); const restoredDump = allScriptsFromDumpMock.mock.calls[0]?.[0] as any; expect( restoredDump.executions[0].tasks[0].uiContext.screenshot.base64, ).toBe('data:image/png;base64,abc'); await act(async () => root.unmount()); }); it('restores stopped replay from a compact report reference', async () => { allScriptsFromDumpMock.mockReturnValue({ scripts: [{ type: 'img', duration: 0, img: 'report-image-url' }], width: 100, height: 200, modelBriefs: [], }); const fetchMock = rs.fn(async () => ({ ok: true, json: async () => replayDump(), })); rs.stubGlobal('fetch', fetchMock); let snapshot: HarnessSnapshot | null = null; const getSnapshot = () => { if (!snapshot) throw new Error('Harness snapshot is not ready'); return snapshot; }; const report = { id: 'stopped-report', url: 'http://localhost/reports/stopped-report/', replayUrl: 'http://localhost/reports/stopped-report/replay', bytes: 1024, format: 'single-html' as const, }; const playgroundSDK = { cancelExecution: rs.fn(async () => ({ dump: null, reportHTML: null, report, })), executeAction: rs.fn(() => new Promise(() => undefined)), onDumpUpdate: rs.fn(), onProgressUpdate: rs.fn(), } as unknown as PlaygroundSDKLike; const container = document.createElement('div'); document.body.appendChild(container); const root = createRoot(container); await act(async () => { root.render( createElement(Harness, { onSnapshot: (nextSnapshot) => { snapshot = nextSnapshot; }, playgroundSDK, }), ); }); await act(async () => { void getSnapshot().handleRun({ prompt: 'Replay', type: 'aiAct' }); }); await act(async () => { await getSnapshot().handleStop(); }); const stoppedResult = getSnapshot().infoList.find((item) => item.id.startsWith('stop-result-'), ); expect(stoppedResult?.result?.report).toEqual(report); expect(stoppedResult?.replayScriptsInfo?.scripts).toHaveLength(1); expect(fetchMock).toHaveBeenCalledWith(report.replayUrl); const restoredDump = allScriptsFromDumpMock.mock.calls[0]?.[0] as any; expect( restoredDump.executions[0].tasks[0].uiContext.screenshot.base64, ).toBe('http://localhost/reports/stopped-report/screenshots/shot-1.png'); expect( restoredDump.executions[0].tasks[0].param.userInstruction.images[0].url, ).toBe( 'http://localhost/reports/stopped-report/screenshots/reference-1.webp', ); await act(async () => root.unmount()); }); it('restores replay scripts from a report-only completed result', async () => { allScriptsFromDumpMock.mockReturnValue({ scripts: [{ type: 'img', duration: 0, img: 'data:image/png;base64,abc' }], width: 100, height: 200, modelBriefs: [], }); const reportHTML = reportWithReplay(); const fetchMock = rs.fn(async () => Promise.resolve({ ok: true, json: async () => replayDump(), }), ); rs.stubGlobal('fetch', fetchMock); let snapshot: HarnessSnapshot | null = null; const getSnapshot = () => { if (!snapshot) throw new Error('Harness snapshot is not ready'); return snapshot; }; const playgroundSDK = { cancelExecution: rs.fn(), executeAction: rs.fn(async () => ({ dump: null, reportHTML: null, report: { id: 'report-1', url: 'http://localhost/reports/report-1/', replayUrl: 'http://localhost/reports/report-1/replay', bytes: reportHTML.length, format: 'single-html', }, result: 'done', })), onDumpUpdate: rs.fn(), onProgressUpdate: rs.fn(), } as unknown as PlaygroundSDKLike; const container = document.createElement('div'); document.body.appendChild(container); const root = createRoot(container); await act(async () => { root.render( createElement(Harness, { onSnapshot: (nextSnapshot) => { snapshot = nextSnapshot; }, playgroundSDK, }), ); }); await act(async () => { await getSnapshot().handleRun({ prompt: 'Replay', type: 'aiAct' }); }); const completedResult = getSnapshot().infoList.find( (item) => item.type === 'result', ); expect(completedResult?.result?.reportHTML).toBeNull(); expect(completedResult?.replayScriptsInfo?.scripts).toHaveLength(1); expect(fetchMock).toHaveBeenCalledWith( 'http://localhost/reports/report-1/replay', ); const restoredDump = allScriptsFromDumpMock.mock.calls[0]?.[0] as any; expect( restoredDump.executions[0].tasks[0].uiContext.screenshot.base64, ).toBe('http://localhost/reports/report-1/screenshots/shot-1.png'); expect( restoredDump.executions[0].tasks[0].param.userInstruction.images[0].url, ).toBe('http://localhost/reports/report-1/screenshots/reference-1.webp'); await act(async () => root.unmount()); }); it('does not render abort errors after the user stops execution', async () => { let rejectRun: (error: Error) => void = () => undefined; let resolveCancel: (value: null) => void = () => undefined; let snapshot: HarnessSnapshot | null = null; const getSnapshot = () => { if (!snapshot) { throw new Error('Harness snapshot is not ready'); } return snapshot; }; const playgroundSDK = { cancelExecution: rs.fn( () => new Promise((resolve) => { resolveCancel = resolve; }), ), executeAction: rs.fn( () => new Promise((_resolve, reject) => { rejectRun = reject; }), ), onDumpUpdate: rs.fn(), onProgressUpdate: rs.fn(), } as unknown as PlaygroundSDKLike; const container = document.createElement('div'); document.body.appendChild(container); const root = createRoot(container); await act(async () => { root.render( createElement(Harness, { onSnapshot: (nextSnapshot) => { snapshot = nextSnapshot; }, playgroundSDK, }), ); }); await act(async () => { void getSnapshot().handleRun({ prompt: 'Replay', type: 'aiAct' }); }); expect(getSnapshot().loading).toBe(true); let stopPromise: Promise | undefined; await act(async () => { stopPromise = getSnapshot().handleStop(); }); expect(getSnapshot().loading).toBe(false); await act(async () => { rejectRun(new Error('Request was aborted')); await Promise.resolve(); }); await act(async () => { resolveCancel(null); await stopPromise; }); const renderedText = getSnapshot() .infoList.map((item) => `${item.content} ${item.result?.error ?? ''}`) .join('\n'); expect(renderedText).toContain('Operation stopped'); expect(renderedText).not.toContain('Request was aborted'); expect( getSnapshot().infoList.some( (item) => item.type === 'result' && item.result?.error, ), ).toBe(false); await act(async () => { root.unmount(); }); }); it('silently cancels execution for clear or target changes', async () => { let rejectRun: (error: Error) => void = () => undefined; let resolveCancel: (value: null) => void = () => undefined; let snapshot: HarnessSnapshot | null = null; const getSnapshot = () => { if (!snapshot) { throw new Error('Harness snapshot is not ready'); } return snapshot; }; const playgroundSDK = { cancelExecution: rs.fn( () => new Promise((resolve) => { resolveCancel = resolve; }), ), executeAction: rs.fn( () => new Promise((_resolve, reject) => { rejectRun = reject; }), ), onDumpUpdate: rs.fn(), onProgressUpdate: rs.fn(), } as unknown as PlaygroundSDKLike; const container = document.createElement('div'); document.body.appendChild(container); const root = createRoot(container); await act(async () => { root.render( createElement(Harness, { onSnapshot: (nextSnapshot) => { snapshot = nextSnapshot; }, playgroundSDK, }), ); }); await act(async () => { void getSnapshot().handleRun({ prompt: 'Replay', type: 'aiAct' }); }); let cancelPromise: Promise | undefined; await act(async () => { cancelPromise = getSnapshot().cancelCurrentExecution(); }); await act(async () => { rejectRun(new Error('Request was aborted')); resolveCancel(null); await cancelPromise; }); const renderedText = getSnapshot() .infoList.map((item) => `${item.content} ${item.result?.error ?? ''}`) .join('\n'); expect(renderedText).not.toContain('Request was aborted'); expect(renderedText).not.toContain('Operation stopped'); expect( getSnapshot().infoList.some( (item) => item.type === 'result' && item.result?.error, ), ).toBe(false); await act(async () => { root.unmount(); }); }); it('does not clear shared progress subscriptions while cancelling', async () => { let resolveRun: (value: null) => void = () => undefined; let resolveCancel: (value: null) => void = () => undefined; let snapshot: HarnessSnapshot | null = null; const getSnapshot = () => { if (!snapshot) { throw new Error('Harness snapshot is not ready'); } return snapshot; }; const playgroundSDK = { cancelExecution: rs.fn( () => new Promise((resolve) => { resolveCancel = resolve; }), ), executeAction: rs.fn( () => new Promise((resolve) => { resolveRun = resolve; }), ), onDumpUpdate: rs.fn(), onProgressUpdate: rs.fn(), } as unknown as PlaygroundSDKLike; const container = document.createElement('div'); document.body.appendChild(container); const root = createRoot(container); await act(async () => { root.render( createElement(Harness, { onSnapshot: (nextSnapshot) => { snapshot = nextSnapshot; }, playgroundSDK, }), ); }); await act(async () => { void getSnapshot().handleRun({ prompt: 'Replay', type: 'aiAct' }); }); expect(playgroundSDK.onDumpUpdate).toHaveBeenCalledTimes(1); expect(playgroundSDK.onProgressUpdate).not.toHaveBeenCalled(); let stopPromise: Promise | undefined; await act(async () => { stopPromise = getSnapshot().handleStop(); }); expect(playgroundSDK.onDumpUpdate).toHaveBeenCalledTimes(1); expect(playgroundSDK.onProgressUpdate).not.toHaveBeenCalled(); await act(async () => { resolveCancel(null); resolveRun(null); await stopPromise; }); await act(async () => { root.unmount(); }); container.remove(); }); });