|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +const mockState = vi.hoisted(() => ({ |
| 4 | + ctorOpts: [] as any[], |
| 5 | + instances: [] as any[], |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock('@/playwright/index', () => { |
| 9 | + class MockPlaywrightAgent { |
| 10 | + reportFile?: string; |
| 11 | + |
| 12 | + constructor(_page: any, opts: any) { |
| 13 | + mockState.ctorOpts.push(opts); |
| 14 | + mockState.instances.push(this); |
| 15 | + this.reportFile = opts?.generateReport ? 'mock-report.html' : undefined; |
| 16 | + } |
| 17 | + |
| 18 | + async destroy() {} |
| 19 | + } |
| 20 | + |
| 21 | + return { |
| 22 | + PlaywrightAgent: MockPlaywrightAgent, |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +import { PlaywrightAiFixture } from '@/playwright/ai-fixture'; |
| 27 | + |
| 28 | +describe('PlaywrightAiFixture option forwarding', () => { |
| 29 | + beforeEach(() => { |
| 30 | + mockState.ctorOpts.length = 0; |
| 31 | + mockState.instances.length = 0; |
| 32 | + }); |
| 33 | + |
| 34 | + const createPage = () => |
| 35 | + ({ |
| 36 | + on: vi.fn(), |
| 37 | + }) as any; |
| 38 | + |
| 39 | + const createTestInfo = () => |
| 40 | + ({ |
| 41 | + testId: 'test-id', |
| 42 | + titlePath: ['fixture.spec.ts', 'forwards options'], |
| 43 | + annotations: [], |
| 44 | + retry: 0, |
| 45 | + }) as any; |
| 46 | + |
| 47 | + it('should forward fixture-level AgentOpt and WebPageOpt to the first agent creation', async () => { |
| 48 | + const fixture = PlaywrightAiFixture({ |
| 49 | + autoPrintReportMsg: false, |
| 50 | + outputFormat: 'html-and-external-assets', |
| 51 | + waitAfterAction: 120, |
| 52 | + enableTouchEventsInActionSpace: true, |
| 53 | + forceChromeSelectRendering: true, |
| 54 | + }); |
| 55 | + |
| 56 | + await fixture.ai({ page: createPage() }, async () => {}, createTestInfo()); |
| 57 | + |
| 58 | + expect(mockState.ctorOpts).toHaveLength(1); |
| 59 | + expect(mockState.ctorOpts[0]).toMatchObject({ |
| 60 | + autoPrintReportMsg: false, |
| 61 | + outputFormat: 'html-and-external-assets', |
| 62 | + waitAfterAction: 120, |
| 63 | + enableTouchEventsInActionSpace: true, |
| 64 | + forceChromeSelectRendering: true, |
| 65 | + generateReport: true, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should allow the first agentForPage call to override fixture defaults', async () => { |
| 70 | + const fixture = PlaywrightAiFixture({ |
| 71 | + autoPrintReportMsg: true, |
| 72 | + waitAfterAction: 300, |
| 73 | + enableTouchEventsInActionSpace: true, |
| 74 | + }); |
| 75 | + |
| 76 | + let getAgentForPage: any; |
| 77 | + await fixture.agentForPage( |
| 78 | + { page: createPage() }, |
| 79 | + async (agentForPage: any) => { |
| 80 | + getAgentForPage = agentForPage; |
| 81 | + }, |
| 82 | + createTestInfo(), |
| 83 | + ); |
| 84 | + |
| 85 | + await getAgentForPage(createPage(), { |
| 86 | + autoPrintReportMsg: false, |
| 87 | + waitAfterAction: 50, |
| 88 | + enableTouchEventsInActionSpace: false, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(mockState.ctorOpts).toHaveLength(1); |
| 92 | + expect(mockState.ctorOpts[0]).toMatchObject({ |
| 93 | + autoPrintReportMsg: false, |
| 94 | + waitAfterAction: 50, |
| 95 | + enableTouchEventsInActionSpace: false, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should reuse the existing agent instead of recreating it with later overrides', async () => { |
| 100 | + const page = createPage(); |
| 101 | + const fixture = PlaywrightAiFixture({ |
| 102 | + autoPrintReportMsg: true, |
| 103 | + }); |
| 104 | + |
| 105 | + let getAgentForPage: any; |
| 106 | + await fixture.agentForPage( |
| 107 | + { page }, |
| 108 | + async (agentForPage: any) => { |
| 109 | + getAgentForPage = agentForPage; |
| 110 | + }, |
| 111 | + createTestInfo(), |
| 112 | + ); |
| 113 | + |
| 114 | + const firstAgent = await getAgentForPage(page, { |
| 115 | + autoPrintReportMsg: false, |
| 116 | + }); |
| 117 | + const secondAgent = await getAgentForPage(page, { |
| 118 | + autoPrintReportMsg: true, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(firstAgent).toBe(secondAgent); |
| 122 | + expect(mockState.ctorOpts).toHaveLength(1); |
| 123 | + expect(mockState.ctorOpts[0]).toMatchObject({ |
| 124 | + autoPrintReportMsg: false, |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments