|
| 1 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 2 | +import { handleSnapshotCommands } from '../snapshot.ts'; |
| 3 | +import type { RawSnapshotNode, SnapshotState } from '../../../utils/snapshot.ts'; |
| 4 | +import { dispatchCommand } from '../../../core/dispatch.ts'; |
| 5 | +import { makeAndroidSession } from '../../../__tests__/test-utils/index.ts'; |
| 6 | +import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts'; |
| 7 | + |
| 8 | +vi.mock('../../../core/dispatch.ts', async (importOriginal) => { |
| 9 | + const actual = await importOriginal<typeof import('../../../core/dispatch.ts')>(); |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + dispatchCommand: vi.fn(async () => ({})), |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +const mockDispatch = vi.mocked(dispatchCommand); |
| 17 | +const ANDROID_SCRIPT_ERROR = 'Unable to load script. Make sure you are running Metro.'; |
| 18 | + |
| 19 | +beforeEach(() => { |
| 20 | + mockDispatch.mockReset(); |
| 21 | + mockDispatch.mockResolvedValue({}); |
| 22 | +}); |
| 23 | + |
| 24 | +test('snapshot resolves @ref scope with the stored source after scoped output replaces refs', async () => { |
| 25 | + const sessionStore = makeSessionStore('agent-device-snapshot-scoped-refs-'); |
| 26 | + const sessionName = 'android-ref-scope-repeat'; |
| 27 | + const session = makeAndroidSession(sessionName, { snapshot: androidRefScopeSourceSnapshot() }); |
| 28 | + sessionStore.set(sessionName, session); |
| 29 | + |
| 30 | + mockDispatch.mockResolvedValue({ |
| 31 | + nodes: scopedScriptErrorNodes(), |
| 32 | + truncated: false, |
| 33 | + backend: 'android', |
| 34 | + }); |
| 35 | + |
| 36 | + for (let attempt = 0; attempt < 2; attempt += 1) { |
| 37 | + const response = await requestScopedSnapshot(sessionName, sessionStore, '@e3'); |
| 38 | + |
| 39 | + expect(response?.ok).toBe(true); |
| 40 | + if (response?.ok) expect(response.data?.nodes).toHaveLength(2); |
| 41 | + } |
| 42 | + |
| 43 | + expect(mockDispatch).toHaveBeenCalledTimes(2); |
| 44 | + expect(mockDispatch.mock.calls.map((call) => call[4])).toEqual([ |
| 45 | + expect.objectContaining({ snapshotScope: ANDROID_SCRIPT_ERROR }), |
| 46 | + expect.objectContaining({ snapshotScope: ANDROID_SCRIPT_ERROR }), |
| 47 | + ]); |
| 48 | + expect(sessionStore.get(sessionName)?.snapshot?.nodes).toHaveLength(2); |
| 49 | + expect(sessionStore.get(sessionName)?.snapshotScopeSource?.nodes[2]?.ref).toBe('e3'); |
| 50 | +}); |
| 51 | + |
| 52 | +test('empty @ref-scoped snapshot output does not replace the stored session snapshot', async () => { |
| 53 | + const sessionStore = makeSessionStore('agent-device-snapshot-scoped-refs-'); |
| 54 | + const sessionName = 'android-empty-scope-preserve'; |
| 55 | + const session = makeAndroidSession(sessionName, { snapshot: currentScreenSnapshot() }); |
| 56 | + sessionStore.set(sessionName, session); |
| 57 | + |
| 58 | + mockDispatch.mockResolvedValue({ |
| 59 | + nodes: [], |
| 60 | + truncated: false, |
| 61 | + backend: 'android', |
| 62 | + }); |
| 63 | + |
| 64 | + const response = await requestScopedSnapshot(sessionName, sessionStore, '@e1'); |
| 65 | + |
| 66 | + expect(response?.ok).toBe(true); |
| 67 | + if (response?.ok) expect(response.data?.nodes).toEqual([]); |
| 68 | + expect(sessionStore.get(sessionName)?.snapshot?.nodes[0]?.label).toBe('Current screen'); |
| 69 | +}); |
| 70 | + |
| 71 | +function requestScopedSnapshot( |
| 72 | + sessionName: string, |
| 73 | + sessionStore: ReturnType<typeof makeSessionStore>, |
| 74 | + snapshotScope: string, |
| 75 | +) { |
| 76 | + return handleSnapshotCommands({ |
| 77 | + req: { |
| 78 | + token: 't', |
| 79 | + session: sessionName, |
| 80 | + command: 'snapshot', |
| 81 | + positionals: [], |
| 82 | + flags: { snapshotScope }, |
| 83 | + }, |
| 84 | + sessionName, |
| 85 | + logPath: '/tmp/daemon.log', |
| 86 | + sessionStore, |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +function currentScreenSnapshot(): SnapshotState { |
| 91 | + return { |
| 92 | + nodes: [ |
| 93 | + { ref: 'e1', index: 0, depth: 0, type: 'android.widget.TextView', label: 'Current screen' }, |
| 94 | + ], |
| 95 | + createdAt: Date.now(), |
| 96 | + backend: 'android', |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +function androidRefScopeSourceSnapshot(): SnapshotState { |
| 101 | + return { |
| 102 | + nodes: [ |
| 103 | + { |
| 104 | + ref: 'e1', |
| 105 | + index: 0, |
| 106 | + depth: 0, |
| 107 | + type: 'android.widget.FrameLayout', |
| 108 | + rect: { x: 0, y: 0, width: 390, height: 844 }, |
| 109 | + }, |
| 110 | + { |
| 111 | + ref: 'e2', |
| 112 | + index: 1, |
| 113 | + depth: 1, |
| 114 | + parentIndex: 0, |
| 115 | + type: 'androidx.recyclerview.widget.RecyclerView', |
| 116 | + rect: { x: 0, y: 80, width: 390, height: 600 }, |
| 117 | + }, |
| 118 | + { |
| 119 | + ref: 'e3', |
| 120 | + index: 2, |
| 121 | + depth: 2, |
| 122 | + parentIndex: 1, |
| 123 | + type: 'android.widget.TextView', |
| 124 | + label: ANDROID_SCRIPT_ERROR, |
| 125 | + value: ANDROID_SCRIPT_ERROR, |
| 126 | + rect: { x: 16, y: 120, width: 358, height: 200 }, |
| 127 | + }, |
| 128 | + { |
| 129 | + ref: 'e4', |
| 130 | + index: 3, |
| 131 | + depth: 3, |
| 132 | + parentIndex: 2, |
| 133 | + type: 'android.widget.TextView', |
| 134 | + label: 'loadJSBundleFromAssets', |
| 135 | + rect: { x: 16, y: 140, width: 358, height: 40 }, |
| 136 | + }, |
| 137 | + ], |
| 138 | + createdAt: Date.now(), |
| 139 | + backend: 'android', |
| 140 | + }; |
| 141 | +} |
| 142 | + |
| 143 | +function scopedScriptErrorNodes(): RawSnapshotNode[] { |
| 144 | + return [ |
| 145 | + { |
| 146 | + index: 0, |
| 147 | + depth: 0, |
| 148 | + type: 'android.widget.TextView', |
| 149 | + label: ANDROID_SCRIPT_ERROR, |
| 150 | + value: ANDROID_SCRIPT_ERROR, |
| 151 | + rect: { x: 16, y: 120, width: 358, height: 200 }, |
| 152 | + }, |
| 153 | + { |
| 154 | + index: 1, |
| 155 | + depth: 1, |
| 156 | + parentIndex: 0, |
| 157 | + type: 'android.widget.TextView', |
| 158 | + label: 'loadJSBundleFromAssets', |
| 159 | + rect: { x: 16, y: 140, width: 358, height: 40 }, |
| 160 | + }, |
| 161 | + ]; |
| 162 | +} |
0 commit comments