"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fixtures_1 = require("./fixtures");
fixtures_1.test.describe('KeyPressEventHandler', () => {
    fixtures_1.test.describe('handleKeyDownEvent - snapshot', () => {
        (0, fixtures_1.test)('should send snapshot message on keydown', async ({ contentJs }) => {
            await contentJs.setContentAndInject(`
        <html>
          <body>
            <input type="text" id="test-input" />
          </body>
        </html>
      `);
            const initialCount = contentJs.receivedMessages.length;
            await contentJs.page.focus('#test-input');
            await contentJs.page.keyboard.press('a');
            const newMessages = contentJs.receivedMessages.slice(initialCount);
            const snapshotMessages = newMessages.filter((msg) => msg.action === 'snapshot' && msg.listener === 'snapshot');
            (0, fixtures_1.expect)(snapshotMessages.length).toBeGreaterThanOrEqual(1);
            (0, fixtures_1.expect)(snapshotMessages[0]).toMatchObject({
                action: 'snapshot',
                listener: 'snapshot',
                xPath: '',
            });
            (0, fixtures_1.expect)(snapshotMessages[0]).toHaveProperty('sourceEventTimeStamp');
            (0, fixtures_1.expect)(snapshotMessages[0]).toHaveProperty('frameInfo');
        });
        (0, fixtures_1.test)('should not send duplicate snapshots for same input within debounce period', async ({ contentJs, }) => {
            await contentJs.setContentAndInject(`
        <html>
          <body>
            <input type="text" id="test-input" />
          </body>
        </html>
      `);
            const initialCount = contentJs.receivedMessages.length;
            await contentJs.page.focus('#test-input');
            await contentJs.page.keyboard.press('a');
            await contentJs.page.keyboard.press('b');
            await contentJs.page.keyboard.press('c');
            const newMessages = contentJs.receivedMessages.slice(initialCount);
            const snapshotMessages = newMessages.filter((msg) => msg.action === 'snapshot' && msg.listener === 'snapshot');
            (0, fixtures_1.expect)(snapshotMessages.length).toBe(1);
        });
        (0, fixtures_1.test)('should send snapshot immediately when switching to different input', async ({ contentJs, }) => {
            await contentJs.setContentAndInject(`
        <html>
          <body>
            <input type="text" id="input-1" />
            <input type="text" id="input-2" />
          </body>
        </html>
      `);
            const initialCount = contentJs.receivedMessages.length;
            await contentJs.page.focus('#input-1');
            await contentJs.page.keyboard.press('a');
            await contentJs.page.focus('#input-2');
            await contentJs.page.keyboard.press('b');
            const newMessages = contentJs.receivedMessages.slice(initialCount);
            const snapshotMessages = newMessages.filter((msg) => msg.action === 'snapshot' && msg.listener === 'snapshot');
            (0, fixtures_1.expect)(snapshotMessages.length).toBe(2);
        });
        (0, fixtures_1.test)('should send snapshot again after debounce period expires', async ({ contentJs, }) => {
            await contentJs.setContentAndInject(`
        <html>
          <body>
            <input type="text" id="test-input" />
          </body>
        </html>
      `);
            const initialCount = contentJs.receivedMessages.length;
            await contentJs.page.focus('#test-input');
            await contentJs.page.keyboard.press('a');
            await contentJs.page.waitForTimeout(2100);
            await contentJs.page.keyboard.press('b');
            const newMessages = contentJs.receivedMessages.slice(initialCount);
            const snapshotMessages = newMessages.filter((msg) => msg.action === 'snapshot' && msg.listener === 'snapshot');
            (0, fixtures_1.expect)(snapshotMessages.length).toBe(2);
        });
    });
    fixtures_1.test.describe('keypress event recording', () => {
        (0, fixtures_1.test)('should record keypress event for special keys', async ({ contentJs, }) => {
            await contentJs.setContentAndInject(`
        <html>
          <body>
            <input type="text" id="test-input" />
          </body>
        </html>
      `);
            const initialCount = contentJs.receivedMessages.length;
            await contentJs.page.focus('#test-input');
            await contentJs.page.keyboard.press('Enter');
            const newMessages = contentJs.receivedMessages.slice(initialCount);
            const keypressMessage = newMessages.find((msg) => msg.action === 'record' && msg.listener === 'keypress');
            (0, fixtures_1.expect)(keypressMessage).toBeDefined();
            (0, fixtures_1.expect)(keypressMessage).toMatchObject({
                action: 'record',
                listener: 'keypress',
            });
        });
        (0, fixtures_1.test)('should record keypress with modifier keys', async ({ contentJs }) => {
            await contentJs.setContentAndInject(`
        <html>
          <body>
            <div id="test-div" tabindex="0">Focus me</div>
          </body>
        </html>
      `);
            const initialCount = contentJs.receivedMessages.length;
            await contentJs.page.focus('#test-div');
            await contentJs.page.keyboard.press('Control+a');
            const newMessages = contentJs.receivedMessages.slice(initialCount);
            const keypressMessage = newMessages.find((msg) => msg.action === 'record' && msg.listener === 'keypress');
            (0, fixtures_1.expect)(keypressMessage).toBeDefined();
            (0, fixtures_1.expect)(keypressMessage).toHaveProperty('keyInfoArray');
        });
    });
});
