/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { test as it, expect } from './config/contextTest';
import { InMemorySnapshotter } from '../lib/server/snapshot/inMemorySnapshotter';
import { HttpServer } from '../lib/utils/httpServer';
import { SnapshotServer } from '../lib/server/snapshot/snapshotServer';
it.describe('snapshots', () => {
  let snapshotter: any;
  let httpServer: any;
  let snapshotPort: number;
  it.beforeEach(async ({ mode, toImpl, context }, testInfo) => {
    it.skip(mode !== 'default');
    snapshotter = new InMemorySnapshotter(toImpl(context));
    await snapshotter.initialize();
    httpServer = new HttpServer();
    new SnapshotServer(httpServer, snapshotter);
    snapshotPort = 11000 + testInfo.workerIndex;
    httpServer.start(snapshotPort);
  });
  it.afterEach(async () => {
    await snapshotter.dispose();
    httpServer.stop();
  });
  it('should collect snapshot', async ({ page, toImpl }) => {
    await page.setContent('');
    const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot');
    expect(distillSnapshot(snapshot)).toBe('');
  });
  it('should capture resources', async ({ page, toImpl, server }) => {
    await page.goto(server.EMPTY_PAGE);
    await page.route('**/style.css', route => {
      route.fulfill({ body: 'button { color: red; }', }).catch(() => {});
    });
    await page.setContent('');
    const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot');
    const { resources } = snapshot.render();
    const cssHref = `http://localhost:${server.PORT}/style.css`;
    expect(resources[cssHref]).toBeTruthy();
  });
  it('should collect multiple', async ({ page, toImpl }) => {
    await page.setContent('');
    const snapshots = [];
    snapshotter.on('snapshot', snapshot => snapshots.push(snapshot));
    await snapshotter.captureSnapshot(toImpl(page), 'snapshot1');
    await snapshotter.captureSnapshot(toImpl(page), 'snapshot2');
    expect(snapshots.length).toBe(2);
  });
  it('should only collect on change', async ({ page }) => {
    await page.setContent('');
    const snapshots = [];
    snapshotter.on('snapshot', snapshot => snapshots.push(snapshot));
    await Promise.all([
      new Promise(f => snapshotter.once('snapshot', f)),
      snapshotter.setAutoSnapshotIntervalForTest(25),
    ]);
    await Promise.all([
      new Promise(f => snapshotter.once('snapshot', f)),
      page.setContent('')
    ]);
    expect(snapshots.length).toBe(2);
  });
  it('should respect inline CSSOM change', async ({ page }) => {
    await page.setContent('');
    const snapshots = [];
    snapshotter.on('snapshot', snapshot => snapshots.push(snapshot));
    await Promise.all([
      new Promise(f => snapshotter.once('snapshot', f)),
      snapshotter.setAutoSnapshotIntervalForTest(25),
    ]);
    expect(distillSnapshot(snapshots[0])).toBe('');
    await Promise.all([
      new Promise(f => snapshotter.once('snapshot', f)),
      page.evaluate(() => {
        (document.styleSheets[0].cssRules[0] as any).style.color = 'blue';
      })
    ]);
    expect(distillSnapshot(snapshots[1])).toBe('');
  });
  it('should respect subresource CSSOM change', async ({ page, server }) => {
    await page.goto(server.EMPTY_PAGE);
    await page.route('**/style.css', route => {
      route.fulfill({ body: 'button { color: red; }', }).catch(() => {});
    });
    await page.setContent('');
    const snapshots = [];
    snapshotter.on('snapshot', snapshot => snapshots.push(snapshot));
    await Promise.all([
      new Promise(f => snapshotter.once('snapshot', f)),
      snapshotter.setAutoSnapshotIntervalForTest(25),
    ]);
    expect(distillSnapshot(snapshots[0])).toBe('');
    await Promise.all([
      new Promise(f => snapshotter.once('snapshot', f)),
      page.evaluate(() => {
        (document.styleSheets[0].cssRules[0] as any).style.color = 'blue';
      })
    ]);
    const { resources } = snapshots[1].render();
    const cssHref = `http://localhost:${server.PORT}/style.css`;
    const { sha1 } = resources[cssHref];
    expect(snapshotter.resourceContent(sha1).toString()).toBe('button { color: blue; }');
  });
  it('should capture iframe', async ({ page, contextFactory, server, toImpl, browserName }) => {
    it.skip(browserName === 'firefox');
    await page.route('**/empty.html', route => {
      route.fulfill({
        body: '',
        contentType: 'text/html'
      }).catch(() => {});
    });
    await page.route('**/iframe.html', route => {
      route.fulfill({
        body: '',
        contentType: 'text/html'
      }).catch(() => {});
    });
    await page.goto(server.EMPTY_PAGE);
    // Marking iframe hierarchy is racy, do not expect snapshot, wait for it.
    let counter = 0;
    let snapshot: any;
    for (; ; ++counter) {
      snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot' + counter);
      const text = distillSnapshot(snapshot).replace(/frame@[^"]+["]/, '"');
      if (text === '')
        break;
      await page.waitForTimeout(250);
    }
    // Render snapshot, check expectations.
    const previewContext = await contextFactory();
    const previewPage = await previewContext.newPage();
    await previewPage.goto(`http://localhost:${snapshotPort}/snapshot/`);
    await previewPage.evaluate(snapshotId => {
      (window as any).showSnapshot(snapshotId);
    }, `${snapshot.snapshot().pageId}?name=snapshot${counter}`);
    while (previewPage.frames().length < 4)
      await new Promise(f => previewPage.once('frameattached', f));
    const button = await previewPage.frames()[3].waitForSelector('button');
    expect(await button.textContent()).toBe('Hello iframe');
  });
  it('should capture snapshot target', async ({ page, toImpl }) => {
    await page.setContent('');
    {
      const handle = await page.$('text=Hello');
      const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot', toImpl(handle));
      expect(distillSnapshot(snapshot)).toBe('');
    }
    {
      const handle = await page.$('text=World');
      const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot2', toImpl(handle));
      expect(distillSnapshot(snapshot)).toBe('');
    }
  });
  it('should collect on attribute change', async ({ page, toImpl }) => {
    await page.setContent('');
    {
      const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot');
      expect(distillSnapshot(snapshot)).toBe('');
    }
    const handle = await page.$('text=Hello')!;
    await handle.evaluate(element => element.setAttribute('data', 'one'));
    {
      const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot2');
      expect(distillSnapshot(snapshot)).toBe('');
    }
    await handle.evaluate(element => element.setAttribute('data', 'two'));
    {
      const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot2');
      expect(distillSnapshot(snapshot)).toBe('');
    }
  });
});
function distillSnapshot(snapshot) {
  const { html } = snapshot.render();
  return html
      .replace(/