|
1 | | -import { describe, it, expect } from 'vitest'; |
| 1 | +import { writeFile } from 'node:fs/promises'; |
| 2 | +import { resolve as resolvePath } from 'node:path'; |
| 3 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
2 | 4 |
|
3 | 5 | import { |
| 6 | + AppiumBackend, |
4 | 7 | parseAppiumAndroidHierarchy, |
5 | 8 | parseAppiumIosHierarchy, |
6 | 9 | } from './appium-backend.js'; |
| 10 | +import type { AttachSessionConfig } from './session-file.js'; |
| 11 | + |
| 12 | +const mockGetStatus = vi.fn(); |
| 13 | +const mockExecute = vi.fn(); |
| 14 | + |
| 15 | +vi.mock('./webdriver-client.js', () => ({ |
| 16 | + WebDriverClient: vi.fn().mockImplementation(() => ({ |
| 17 | + getStatus: mockGetStatus, |
| 18 | + execute: mockExecute, |
| 19 | + })), |
| 20 | + createSession: vi.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock('node:fs/promises', () => ({ |
| 24 | + writeFile: vi.fn(), |
| 25 | +})); |
| 26 | + |
| 27 | +const mockWriteFile = vi.mocked(writeFile); |
7 | 28 |
|
8 | 29 | const SAMPLE_ANDROID_XML = `<?xml version="1.0" encoding="UTF-8"?> |
9 | 30 | <hierarchy rotation="0"> |
@@ -103,3 +124,79 @@ describe('parseAppiumIosHierarchy', () => { |
103 | 124 | expect(parseAppiumIosHierarchy('')).toStrictEqual([]); |
104 | 125 | }); |
105 | 126 | }); |
| 127 | + |
| 128 | +describe('AppiumBackend.screenshot', () => { |
| 129 | + const config: AttachSessionConfig = { |
| 130 | + mode: 'attach', |
| 131 | + appiumUrl: 'http://localhost:4723', |
| 132 | + sessionId: 'session-1', |
| 133 | + platform: 'ios', |
| 134 | + }; |
| 135 | + |
| 136 | + let backend: AppiumBackend; |
| 137 | + |
| 138 | + beforeEach(async () => { |
| 139 | + vi.clearAllMocks(); |
| 140 | + mockGetStatus.mockResolvedValue({}); |
| 141 | + backend = new AppiumBackend(config); |
| 142 | + await backend.ensureConnected(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns base64 image data from the WebDriver session by default', async () => { |
| 146 | + mockExecute.mockResolvedValue('YmFzZTY0LWltYWdl'); |
| 147 | + |
| 148 | + const result = await backend.screenshot(); |
| 149 | + |
| 150 | + expect(mockExecute).toHaveBeenCalledWith('mobile: getScreenshot', []); |
| 151 | + expect(mockWriteFile).not.toHaveBeenCalled(); |
| 152 | + expect(result).toStrictEqual({ |
| 153 | + data: 'YmFzZTY0LWltYWdl', |
| 154 | + format: 'png', |
| 155 | + path: undefined, |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + it('writes to a temp file and omits base64 data when encode is false', async () => { |
| 160 | + mockExecute.mockResolvedValue('YmFzZTY0LWltYWdl'); |
| 161 | + |
| 162 | + const result = await backend.screenshot(undefined, { encode: false }); |
| 163 | + |
| 164 | + expect(result.data).toBeUndefined(); |
| 165 | + expect(result.format).toBe('png'); |
| 166 | + expect(result.path).toMatch(/^\/tmp\/device-mcp-screenshot-\d+\.png$/u); |
| 167 | + expect(mockWriteFile).toHaveBeenCalledWith( |
| 168 | + result.path, |
| 169 | + Buffer.from('YmFzZTY0LWltYWdl', 'base64'), |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it('writes the decoded image to outputPath when provided', async () => { |
| 174 | + mockExecute.mockResolvedValue('YmFzZTY0LWltYWdl'); |
| 175 | + |
| 176 | + const result = await backend.screenshot('/tmp/appium-shot.png'); |
| 177 | + |
| 178 | + expect(mockWriteFile).toHaveBeenCalledWith( |
| 179 | + resolvePath('/tmp/appium-shot.png'), |
| 180 | + Buffer.from('YmFzZTY0LWltYWdl', 'base64'), |
| 181 | + ); |
| 182 | + expect(result.path).toBe(resolvePath('/tmp/appium-shot.png')); |
| 183 | + }); |
| 184 | + |
| 185 | + it('writes to outputPath and omits base64 data when encode is false', async () => { |
| 186 | + mockExecute.mockResolvedValue('YmFzZTY0LWltYWdl'); |
| 187 | + |
| 188 | + const result = await backend.screenshot('/tmp/appium-shot.png', { |
| 189 | + encode: false, |
| 190 | + }); |
| 191 | + |
| 192 | + expect(mockWriteFile).toHaveBeenCalledWith( |
| 193 | + resolvePath('/tmp/appium-shot.png'), |
| 194 | + Buffer.from('YmFzZTY0LWltYWdl', 'base64'), |
| 195 | + ); |
| 196 | + expect(result).toStrictEqual({ |
| 197 | + data: undefined, |
| 198 | + format: 'png', |
| 199 | + path: resolvePath('/tmp/appium-shot.png'), |
| 200 | + }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments