|
| 1 | +import { DEFAULT_WDA_PORT } from '@midscene/shared/constants'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { IOSWebDriverClient } from '../../src/ios-webdriver-client'; |
| 4 | + |
| 5 | +describe('IOSWebDriverClient - WDA 5.x-7.x Compatibility', () => { |
| 6 | + let client: IOSWebDriverClient; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + client = new IOSWebDriverClient({ |
| 10 | + port: DEFAULT_WDA_PORT, |
| 11 | + host: 'localhost', |
| 12 | + }); |
| 13 | + // Mock sessionId to avoid session creation |
| 14 | + (client as any).sessionId = 'test-session-id'; |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + vi.restoreAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('tap() fallback logic', () => { |
| 22 | + it('should use new endpoint when it succeeds', async () => { |
| 23 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 24 | + makeRequestSpy.mockResolvedValueOnce({ status: 0 }); |
| 25 | + |
| 26 | + await client.tap(100, 200); |
| 27 | + |
| 28 | + // Should only call new endpoint once |
| 29 | + expect(makeRequestSpy).toHaveBeenCalledTimes(1); |
| 30 | + expect(makeRequestSpy).toHaveBeenCalledWith( |
| 31 | + 'POST', |
| 32 | + '/session/test-session-id/wda/tap', |
| 33 | + { x: 100, y: 200 }, |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should fallback to legacy endpoint when new endpoint fails', async () => { |
| 38 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 39 | + |
| 40 | + // First call (new endpoint) fails |
| 41 | + makeRequestSpy.mockRejectedValueOnce(new Error('New endpoint not found')); |
| 42 | + // Second call (legacy endpoint) succeeds |
| 43 | + makeRequestSpy.mockResolvedValueOnce({ status: 0 }); |
| 44 | + |
| 45 | + await client.tap(100, 200); |
| 46 | + |
| 47 | + // Should call both endpoints |
| 48 | + expect(makeRequestSpy).toHaveBeenCalledTimes(2); |
| 49 | + expect(makeRequestSpy).toHaveBeenNthCalledWith( |
| 50 | + 1, |
| 51 | + 'POST', |
| 52 | + '/session/test-session-id/wda/tap', |
| 53 | + { x: 100, y: 200 }, |
| 54 | + ); |
| 55 | + expect(makeRequestSpy).toHaveBeenNthCalledWith( |
| 56 | + 2, |
| 57 | + 'POST', |
| 58 | + '/session/test-session-id/wda/tap/0', |
| 59 | + { x: 100, y: 200 }, |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should throw error when both endpoints fail', async () => { |
| 64 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 65 | + |
| 66 | + // Both calls fail |
| 67 | + makeRequestSpy.mockRejectedValueOnce(new Error('New endpoint failed')); |
| 68 | + makeRequestSpy.mockRejectedValueOnce(new Error('Legacy endpoint failed')); |
| 69 | + |
| 70 | + await expect(client.tap(100, 200)).rejects.toThrow( |
| 71 | + 'Failed to tap at coordinates', |
| 72 | + ); |
| 73 | + |
| 74 | + expect(makeRequestSpy).toHaveBeenCalledTimes(2); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle different coordinate types', async () => { |
| 78 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 79 | + makeRequestSpy.mockResolvedValue({ status: 0 }); |
| 80 | + |
| 81 | + await client.tap(0, 0); |
| 82 | + await client.tap(999.5, 888.7); |
| 83 | + |
| 84 | + expect(makeRequestSpy).toHaveBeenCalledTimes(2); |
| 85 | + expect(makeRequestSpy).toHaveBeenNthCalledWith( |
| 86 | + 1, |
| 87 | + 'POST', |
| 88 | + '/session/test-session-id/wda/tap', |
| 89 | + { x: 0, y: 0 }, |
| 90 | + ); |
| 91 | + expect(makeRequestSpy).toHaveBeenNthCalledWith( |
| 92 | + 2, |
| 93 | + 'POST', |
| 94 | + '/session/test-session-id/wda/tap', |
| 95 | + { x: 999.5, y: 888.7 }, |
| 96 | + ); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('getScreenScale() fallback logic', () => { |
| 101 | + it('should return scale when endpoint succeeds with scale value', async () => { |
| 102 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 103 | + makeRequestSpy.mockResolvedValueOnce({ |
| 104 | + status: 0, |
| 105 | + value: { scale: 3 }, |
| 106 | + }); |
| 107 | + |
| 108 | + const scale = await client.getScreenScale(); |
| 109 | + |
| 110 | + expect(scale).toBe(3); |
| 111 | + expect(makeRequestSpy).toHaveBeenCalledTimes(1); |
| 112 | + expect(makeRequestSpy).toHaveBeenCalledWith( |
| 113 | + 'GET', |
| 114 | + '/session/test-session-id/wda/screen', |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should enter fallback logic when endpoint succeeds but has no scale', async () => { |
| 119 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 120 | + const takeScreenshotSpy = vi.spyOn(client, 'takeScreenshot'); |
| 121 | + const getWindowSizeSpy = vi.spyOn(client, 'getWindowSize'); |
| 122 | + |
| 123 | + // First call: endpoint succeeds but no scale |
| 124 | + makeRequestSpy.mockResolvedValueOnce({ |
| 125 | + status: 0, |
| 126 | + value: {}, // No scale field |
| 127 | + }); |
| 128 | + |
| 129 | + // Mock fallback methods to verify they are called |
| 130 | + const mockBase64 = 'data:image/png;base64,mockdata'; |
| 131 | + takeScreenshotSpy.mockResolvedValueOnce(mockBase64); |
| 132 | + getWindowSizeSpy.mockResolvedValueOnce({ |
| 133 | + width: 414, |
| 134 | + height: 896, |
| 135 | + }); |
| 136 | + |
| 137 | + // This will fail at jimpFromBase64, but we verify the fallback is entered |
| 138 | + await client.getScreenScale(); |
| 139 | + |
| 140 | + // Verify fallback logic was entered |
| 141 | + expect(takeScreenshotSpy).toHaveBeenCalledTimes(1); |
| 142 | + expect(getWindowSizeSpy).toHaveBeenCalledTimes(1); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should enter fallback logic when endpoint fails', async () => { |
| 146 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 147 | + const takeScreenshotSpy = vi.spyOn(client, 'takeScreenshot'); |
| 148 | + const getWindowSizeSpy = vi.spyOn(client, 'getWindowSize'); |
| 149 | + |
| 150 | + // First call: endpoint fails |
| 151 | + makeRequestSpy.mockRejectedValueOnce(new Error('Endpoint not found')); |
| 152 | + |
| 153 | + // Mock fallback methods |
| 154 | + const mockBase64 = 'data:image/png;base64,mockdata'; |
| 155 | + takeScreenshotSpy.mockResolvedValueOnce(mockBase64); |
| 156 | + getWindowSizeSpy.mockResolvedValueOnce({ |
| 157 | + width: 375, |
| 158 | + height: 667, |
| 159 | + }); |
| 160 | + |
| 161 | + // This will fail at jimpFromBase64, but we verify the fallback is entered |
| 162 | + await client.getScreenScale(); |
| 163 | + |
| 164 | + // Verify fallback logic was entered |
| 165 | + expect(takeScreenshotSpy).toHaveBeenCalledTimes(1); |
| 166 | + expect(getWindowSizeSpy).toHaveBeenCalledTimes(1); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should return null when both endpoint and calculation fail', async () => { |
| 170 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 171 | + const takeScreenshotSpy = vi.spyOn(client, 'takeScreenshot'); |
| 172 | + |
| 173 | + // First call: endpoint fails |
| 174 | + makeRequestSpy.mockRejectedValueOnce(new Error('Endpoint failed')); |
| 175 | + |
| 176 | + // Fallback: screenshot fails |
| 177 | + takeScreenshotSpy.mockRejectedValueOnce(new Error('Screenshot failed')); |
| 178 | + |
| 179 | + const scale = await client.getScreenScale(); |
| 180 | + |
| 181 | + expect(scale).toBeNull(); |
| 182 | + expect(takeScreenshotSpy).toHaveBeenCalledTimes(1); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should handle response without value field gracefully', async () => { |
| 186 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 187 | + const takeScreenshotSpy = vi.spyOn(client, 'takeScreenshot'); |
| 188 | + const getWindowSizeSpy = vi.spyOn(client, 'getWindowSize'); |
| 189 | + |
| 190 | + // Endpoint returns response without value field |
| 191 | + makeRequestSpy.mockResolvedValueOnce({ |
| 192 | + status: 0, |
| 193 | + // No value field at all |
| 194 | + }); |
| 195 | + |
| 196 | + // Mock fallback |
| 197 | + const mockBase64 = 'data:image/png;base64,mockdata'; |
| 198 | + takeScreenshotSpy.mockResolvedValueOnce(mockBase64); |
| 199 | + getWindowSizeSpy.mockResolvedValueOnce({ |
| 200 | + width: 320, |
| 201 | + height: 568, |
| 202 | + }); |
| 203 | + |
| 204 | + await client.getScreenScale(); |
| 205 | + |
| 206 | + // Verify fallback was triggered |
| 207 | + expect(takeScreenshotSpy).toHaveBeenCalled(); |
| 208 | + expect(getWindowSizeSpy).toHaveBeenCalled(); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should handle scale value of 0 as invalid and trigger fallback', async () => { |
| 212 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 213 | + const takeScreenshotSpy = vi.spyOn(client, 'takeScreenshot'); |
| 214 | + const getWindowSizeSpy = vi.spyOn(client, 'getWindowSize'); |
| 215 | + |
| 216 | + // Endpoint returns scale: 0 (invalid) |
| 217 | + makeRequestSpy.mockResolvedValueOnce({ |
| 218 | + status: 0, |
| 219 | + value: { scale: 0 }, |
| 220 | + }); |
| 221 | + |
| 222 | + const mockBase64 = 'data:image/png;base64,mockdata'; |
| 223 | + takeScreenshotSpy.mockResolvedValueOnce(mockBase64); |
| 224 | + getWindowSizeSpy.mockResolvedValueOnce({ |
| 225 | + width: 320, |
| 226 | + height: 568, |
| 227 | + }); |
| 228 | + |
| 229 | + await client.getScreenScale(); |
| 230 | + |
| 231 | + // scale: 0 should be treated as falsy and trigger fallback |
| 232 | + expect(takeScreenshotSpy).toHaveBeenCalled(); |
| 233 | + }); |
| 234 | + }); |
| 235 | + |
| 236 | + describe('Compatibility scenarios', () => { |
| 237 | + it('should work with WDA 5.x (legacy tap endpoint)', async () => { |
| 238 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 239 | + |
| 240 | + // Simulate WDA 5.x: new endpoint doesn't exist |
| 241 | + makeRequestSpy.mockRejectedValueOnce( |
| 242 | + new Error('404 - Endpoint not found'), |
| 243 | + ); |
| 244 | + // Legacy endpoint works |
| 245 | + makeRequestSpy.mockResolvedValueOnce({ status: 0 }); |
| 246 | + |
| 247 | + await client.tap(50, 50); |
| 248 | + |
| 249 | + expect(makeRequestSpy).toHaveBeenCalledWith( |
| 250 | + 'POST', |
| 251 | + '/session/test-session-id/wda/tap/0', |
| 252 | + { x: 50, y: 50 }, |
| 253 | + ); |
| 254 | + }); |
| 255 | + |
| 256 | + it('should work with WDA 6.x/7.x (new tap endpoint)', async () => { |
| 257 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 258 | + |
| 259 | + // Simulate WDA 6.x/7.x: new endpoint works |
| 260 | + makeRequestSpy.mockResolvedValueOnce({ status: 0 }); |
| 261 | + |
| 262 | + await client.tap(50, 50); |
| 263 | + |
| 264 | + expect(makeRequestSpy).toHaveBeenCalledTimes(1); |
| 265 | + expect(makeRequestSpy).toHaveBeenCalledWith( |
| 266 | + 'POST', |
| 267 | + '/session/test-session-id/wda/tap', |
| 268 | + { x: 50, y: 50 }, |
| 269 | + ); |
| 270 | + }); |
| 271 | + |
| 272 | + it('should handle WDA versions with different screen endpoint responses', async () => { |
| 273 | + const makeRequestSpy = vi.spyOn(client as any, 'makeRequest'); |
| 274 | + |
| 275 | + // Test different scale values |
| 276 | + const testCases = [1, 2, 3, 4]; |
| 277 | + |
| 278 | + for (const expectedScale of testCases) { |
| 279 | + makeRequestSpy.mockResolvedValueOnce({ |
| 280 | + status: 0, |
| 281 | + value: { scale: expectedScale }, |
| 282 | + }); |
| 283 | + |
| 284 | + const scale = await client.getScreenScale(); |
| 285 | + expect(scale).toBe(expectedScale); |
| 286 | + } |
| 287 | + |
| 288 | + expect(makeRequestSpy).toHaveBeenCalledTimes(testCases.length); |
| 289 | + }); |
| 290 | + }); |
| 291 | +}); |
0 commit comments