|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const stateMocks = vi.hoisted(() => ({ |
| 4 | + ensurePageState: vi.fn(), |
| 5 | + getPageForTargetId: vi.fn(), |
| 6 | + devices: { |
| 7 | + "iPhone 14": { |
| 8 | + userAgent: "iphone-14-user-agent", |
| 9 | + viewport: { width: 390, height: 664 }, |
| 10 | + screen: { width: 390, height: 844 }, |
| 11 | + deviceScaleFactor: 3, |
| 12 | + isMobile: true, |
| 13 | + hasTouch: true, |
| 14 | + defaultBrowserType: "webkit", |
| 15 | + }, |
| 16 | + "Desktop Chrome": { |
| 17 | + userAgent: "desktop-chrome-user-agent", |
| 18 | + viewport: { width: 1280, height: 720 }, |
| 19 | + screen: { width: 1920, height: 1080 }, |
| 20 | + deviceScaleFactor: 1, |
| 21 | + isMobile: false, |
| 22 | + hasTouch: false, |
| 23 | + defaultBrowserType: "chromium", |
| 24 | + }, |
| 25 | + "iPhone 14 landscape": { |
| 26 | + userAgent: "iphone-14-user-agent", |
| 27 | + viewport: { width: 750, height: 340 }, |
| 28 | + screen: { width: 390, height: 844 }, |
| 29 | + deviceScaleFactor: 3, |
| 30 | + isMobile: true, |
| 31 | + hasTouch: true, |
| 32 | + defaultBrowserType: "webkit", |
| 33 | + }, |
| 34 | + }, |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock("./playwright-core.runtime.js", () => ({ |
| 38 | + playwrightCore: { devices: stateMocks.devices }, |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock("./pw-session.js", () => ({ |
| 42 | + ensurePageState: stateMocks.ensurePageState, |
| 43 | + getPageForTargetId: stateMocks.getPageForTargetId, |
| 44 | +})); |
| 45 | + |
| 46 | +import { |
| 47 | + setDeviceViaPlaywright, |
| 48 | + setLocaleViaPlaywright, |
| 49 | + setTimezoneViaPlaywright, |
| 50 | +} from "./pw-tools-core.state.js"; |
| 51 | + |
| 52 | +function createPage() { |
| 53 | + const send = vi.fn(async (_method: string, _params?: Record<string, unknown>) => ({})); |
| 54 | + const detach = vi.fn(async () => {}); |
| 55 | + const newCDPSession = vi.fn(async () => ({ send, detach })); |
| 56 | + const setViewportSize = vi.fn(async () => {}); |
| 57 | + const page = { |
| 58 | + context: () => ({ newCDPSession }), |
| 59 | + setViewportSize, |
| 60 | + }; |
| 61 | + return { page, send, detach, newCDPSession, setViewportSize }; |
| 62 | +} |
| 63 | + |
| 64 | +describe("setDeviceViaPlaywright", () => { |
| 65 | + beforeEach(() => { |
| 66 | + vi.clearAllMocks(); |
| 67 | + stateMocks.ensurePageState.mockReturnValue({}); |
| 68 | + }); |
| 69 | + |
| 70 | + it("keeps one page-scoped CDP session attached for persistent emulation", async () => { |
| 71 | + const fixture = createPage(); |
| 72 | + stateMocks.getPageForTargetId.mockResolvedValue(fixture.page); |
| 73 | + |
| 74 | + await setTimezoneViaPlaywright({ |
| 75 | + cdpUrl: "http://127.0.0.1:9222", |
| 76 | + targetId: "tab-1", |
| 77 | + timezoneId: "America/New_York", |
| 78 | + }); |
| 79 | + await setLocaleViaPlaywright({ |
| 80 | + cdpUrl: "http://127.0.0.1:9222", |
| 81 | + targetId: "tab-1", |
| 82 | + locale: "en-GB", |
| 83 | + }); |
| 84 | + |
| 85 | + expect(fixture.newCDPSession).toHaveBeenCalledTimes(1); |
| 86 | + expect(fixture.detach).not.toHaveBeenCalled(); |
| 87 | + expect(fixture.send.mock.calls).toEqual([ |
| 88 | + ["Emulation.setTimezoneOverride", { timezoneId: "America/New_York" }], |
| 89 | + ["Emulation.setLocaleOverride", { locale: "en-GB" }], |
| 90 | + ]); |
| 91 | + }); |
| 92 | + |
| 93 | + it("fully replaces iPhone 14 emulation with Desktop Chrome", async () => { |
| 94 | + const fixture = createPage(); |
| 95 | + stateMocks.getPageForTargetId.mockResolvedValue(fixture.page); |
| 96 | + |
| 97 | + await setDeviceViaPlaywright({ |
| 98 | + cdpUrl: "http://127.0.0.1:9222", |
| 99 | + targetId: "tab-1", |
| 100 | + name: "iPhone 14", |
| 101 | + }); |
| 102 | + await setDeviceViaPlaywright({ |
| 103 | + cdpUrl: "http://127.0.0.1:9222", |
| 104 | + targetId: "tab-1", |
| 105 | + name: "Desktop Chrome", |
| 106 | + }); |
| 107 | + |
| 108 | + expect(fixture.setViewportSize.mock.calls).toEqual([ |
| 109 | + [{ width: 390, height: 664 }], |
| 110 | + [{ width: 1280, height: 720 }], |
| 111 | + ]); |
| 112 | + expect(fixture.send.mock.calls).toEqual([ |
| 113 | + ["Emulation.setUserAgentOverride", { userAgent: "iphone-14-user-agent" }], |
| 114 | + [ |
| 115 | + "Emulation.setDeviceMetricsOverride", |
| 116 | + { |
| 117 | + mobile: true, |
| 118 | + width: 390, |
| 119 | + height: 664, |
| 120 | + deviceScaleFactor: 3, |
| 121 | + screenWidth: 390, |
| 122 | + screenHeight: 844, |
| 123 | + screenOrientation: { angle: 0, type: "portraitPrimary" }, |
| 124 | + }, |
| 125 | + ], |
| 126 | + ["Emulation.setTouchEmulationEnabled", { enabled: true }], |
| 127 | + ["Emulation.setUserAgentOverride", { userAgent: "desktop-chrome-user-agent" }], |
| 128 | + [ |
| 129 | + "Emulation.setDeviceMetricsOverride", |
| 130 | + { |
| 131 | + mobile: false, |
| 132 | + width: 1280, |
| 133 | + height: 720, |
| 134 | + deviceScaleFactor: 1, |
| 135 | + screenWidth: 1920, |
| 136 | + screenHeight: 1080, |
| 137 | + screenOrientation: { angle: 0, type: "landscapePrimary" }, |
| 138 | + }, |
| 139 | + ], |
| 140 | + ["Emulation.setTouchEmulationEnabled", { enabled: false }], |
| 141 | + ]); |
| 142 | + expect(fixture.newCDPSession).toHaveBeenCalledTimes(1); |
| 143 | + expect(fixture.detach).not.toHaveBeenCalled(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("derives mobile orientation from the effective screen instead of the viewport", async () => { |
| 147 | + const fixture = createPage(); |
| 148 | + stateMocks.getPageForTargetId.mockResolvedValue(fixture.page); |
| 149 | + |
| 150 | + await setDeviceViaPlaywright({ |
| 151 | + cdpUrl: "http://127.0.0.1:9222", |
| 152 | + targetId: "tab-1", |
| 153 | + name: "iPhone 14 landscape", |
| 154 | + }); |
| 155 | + |
| 156 | + expect(fixture.send).toHaveBeenNthCalledWith(2, "Emulation.setDeviceMetricsOverride", { |
| 157 | + mobile: true, |
| 158 | + width: 750, |
| 159 | + height: 340, |
| 160 | + deviceScaleFactor: 3, |
| 161 | + screenWidth: 390, |
| 162 | + screenHeight: 844, |
| 163 | + screenOrientation: { angle: 0, type: "portraitPrimary" }, |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + it("serializes overlapping descriptor transitions on the same page", async () => { |
| 168 | + let releaseFirstUserAgent!: () => void; |
| 169 | + const firstUserAgentBlocked = new Promise<void>((resolve) => { |
| 170 | + releaseFirstUserAgent = resolve; |
| 171 | + }); |
| 172 | + const fixture = createPage(); |
| 173 | + fixture.send.mockImplementation(async (method, params) => { |
| 174 | + if ( |
| 175 | + method === "Emulation.setUserAgentOverride" && |
| 176 | + params?.userAgent === "iphone-14-user-agent" |
| 177 | + ) { |
| 178 | + await firstUserAgentBlocked; |
| 179 | + } |
| 180 | + return {}; |
| 181 | + }); |
| 182 | + stateMocks.getPageForTargetId.mockResolvedValue(fixture.page); |
| 183 | + |
| 184 | + const phone = setDeviceViaPlaywright({ |
| 185 | + cdpUrl: "http://127.0.0.1:9222", |
| 186 | + targetId: "tab-1", |
| 187 | + name: "iPhone 14", |
| 188 | + }); |
| 189 | + await vi.waitFor(() => expect(fixture.send).toHaveBeenCalledTimes(1)); |
| 190 | + const desktop = setDeviceViaPlaywright({ |
| 191 | + cdpUrl: "http://127.0.0.1:9222", |
| 192 | + targetId: "tab-1", |
| 193 | + name: "Desktop Chrome", |
| 194 | + }); |
| 195 | + |
| 196 | + await Promise.resolve(); |
| 197 | + const viewportCallsWhileFirstDescriptorBlocked = fixture.setViewportSize.mock.calls.length; |
| 198 | + const cdpCallsWhileFirstDescriptorBlocked = fixture.send.mock.calls.length; |
| 199 | + |
| 200 | + releaseFirstUserAgent(); |
| 201 | + await Promise.all([phone, desktop]); |
| 202 | + expect(viewportCallsWhileFirstDescriptorBlocked).toBe(1); |
| 203 | + expect(cdpCallsWhileFirstDescriptorBlocked).toBe(1); |
| 204 | + expect(fixture.send.mock.calls.map(([method]) => method)).toEqual([ |
| 205 | + "Emulation.setUserAgentOverride", |
| 206 | + "Emulation.setDeviceMetricsOverride", |
| 207 | + "Emulation.setTouchEmulationEnabled", |
| 208 | + "Emulation.setUserAgentOverride", |
| 209 | + "Emulation.setDeviceMetricsOverride", |
| 210 | + "Emulation.setTouchEmulationEnabled", |
| 211 | + ]); |
| 212 | + }); |
| 213 | + |
| 214 | + it("skips an aborted descriptor while it is waiting for the page", async () => { |
| 215 | + let releaseFirstUserAgent!: () => void; |
| 216 | + const firstUserAgentBlocked = new Promise<void>((resolve) => { |
| 217 | + releaseFirstUserAgent = resolve; |
| 218 | + }); |
| 219 | + const fixture = createPage(); |
| 220 | + fixture.send.mockImplementation(async (method, params) => { |
| 221 | + if ( |
| 222 | + method === "Emulation.setUserAgentOverride" && |
| 223 | + params?.userAgent === "iphone-14-user-agent" |
| 224 | + ) { |
| 225 | + await firstUserAgentBlocked; |
| 226 | + } |
| 227 | + return {}; |
| 228 | + }); |
| 229 | + stateMocks.getPageForTargetId.mockResolvedValue(fixture.page); |
| 230 | + |
| 231 | + const phone = setDeviceViaPlaywright({ |
| 232 | + cdpUrl: "http://127.0.0.1:9222", |
| 233 | + targetId: "tab-1", |
| 234 | + name: "iPhone 14", |
| 235 | + }); |
| 236 | + await vi.waitFor(() => expect(fixture.send).toHaveBeenCalledTimes(1)); |
| 237 | + const controller = new AbortController(); |
| 238 | + const desktop = setDeviceViaPlaywright({ |
| 239 | + cdpUrl: "http://127.0.0.1:9222", |
| 240 | + targetId: "tab-1", |
| 241 | + name: "Desktop Chrome", |
| 242 | + signal: controller.signal, |
| 243 | + }); |
| 244 | + await vi.waitFor(() => expect(stateMocks.ensurePageState).toHaveBeenCalledTimes(2)); |
| 245 | + controller.abort(new Error("request closed")); |
| 246 | + |
| 247 | + releaseFirstUserAgent(); |
| 248 | + await phone; |
| 249 | + await expect(desktop).rejects.toThrow("request closed"); |
| 250 | + expect(fixture.setViewportSize).toHaveBeenCalledTimes(1); |
| 251 | + expect(fixture.send).toHaveBeenCalledTimes(3); |
| 252 | + }); |
| 253 | +}); |
0 commit comments