<commit_before>/**
 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
 * @license MIT
 */

import { assert } from 'chai';
import { pollFor, timeout, writeSync, openTerminal, launchBrowser } from './TestUtils';
import { Browser, Page } from 'playwright';
import { fail } from 'assert';

const APP = 'http://127.0.0.1:3001/test';

let browser: Browser;
let page: Page;
const width = 800;
const height = 600;

describe('API Integration Tests', function(): void {
  before(async () => {
    browser = await launchBrowser();
    page = await (await browser.newContext()).newPage();
    await page.setViewportSize({ width, height });
  });

  after(async () => browser.close());
  beforeEach(async () => page.goto(APP));

  it('Default options', async () => {
    await openTerminal(page);
    assert.equal(await page.evaluate(`window.term.cols`), 80);
    assert.equal(await page.evaluate(`window.term.rows`), 24);
  });

  it('Proposed API check', async () => {
    await openTerminal(page, { allowProposedApi: false });
    await page.evaluate(`
      try {
        window.term.buffer;
      } catch (e) {
        window.throwMessage = e.message;
      }
    `);
    await pollFor(page, 'window.throwMessage', 'You must set the allowProposedApi option to true to use proposed API');
  });

  it('write', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.write('foo');
      window.term.write('bar');
      window.term.write('文');
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foobar文');
  });

  it('write with callback', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.write('foo', () => { window.__x = 'a'; });
      window.term.write('bar', () => { window.__x += 'b'; });
      window.term.write('文', () => { window.__x += 'c'; });
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foobar文');
    await pollFor(page, `window.__x`, 'abc');
  });

  it('write - bytes (UTF8)', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.write(new Uint8Array([102, 111, 111])); // foo
      window.term.write(new Uint8Array([98, 97, 114])); // bar
      window.term.write(new Uint8Array([230, 150, 135])); // 文
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foobar文');
  });

  it('write - bytes (UTF8) with callback', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.write(new Uint8Array([102, 111, 111]), () => { window.__x = 'A'; }); // foo
      window.term.write(new Uint8Array([98, 97, 114]), () => { window.__x += 'B'; }); // bar
      window.term.write(new Uint8Array([230, 150, 135]), () => { window.__x += 'C'; }); // 文
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foobar文');
    await pollFor(page, `window.__x`, 'ABC');
  });

  it('writeln', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.writeln('foo');
      window.term.writeln('bar');
      window.term.writeln('文');
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foo');
    await pollFor(page, `window.term.buffer.active.getLine(1).translateToString(true)`, 'bar');
    await pollFor(page, `window.term.buffer.active.getLine(2).translateToString(true)`, '文');
  });

  it('writeln with callback', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.writeln('foo', () => { window.__x = '1'; });
      window.term.writeln('bar', () => { window.__x += '2'; });
      window.term.writeln('文', () => { window.__x += '3'; });
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foo');
    await pollFor(page, `window.term.buffer.active.getLine(1).translateToString(true)`, 'bar');
    await pollFor(page, `window.term.buffer.active.getLine(2).translateToString(true)`, '文');
    await pollFor(page, `window.__x`, '123');
  });

  it('writeln - bytes (UTF8)', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.writeln(new Uint8Array([102, 111, 111]));
      window.term.writeln(new Uint8Array([98, 97, 114]));
      window.term.writeln(new Uint8Array([230, 150, 135]));
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foo');
    await pollFor(page, `window.term.buffer.active.getLine(1).translateToString(true)`, 'bar');
    await pollFor(page, `window.term.buffer.active.getLine(2).translateToString(true)`, '文');
  });

  it('paste', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.calls = [];
      window.term.onData(e => calls.push(e));
      window.term.paste('foo');
      window.term.paste('\\r\\nfoo\\nbar\\r');
      window.term.write('\\x1b[?2004h', () => {
        window.term.paste('foo');
      });
    `);
    await pollFor(page, `window.calls`, ['foo', '\rfoo\rbar\r', '\x1b[200~foo\x1b[201~']);
  });

  it('clear', async () => {
    await openTerminal(page, { rows: 5 });
    await page.evaluate(`
      window.term.write('test0');
      window.parsed = 0;
      for (let i = 1; i < 10; i++) {
        window.term.write('\\n\\rtest' + i, () => window.parsed++);
      }
    `);
    await pollFor(page, `window.parsed`, 9);
    await page.evaluate(`window.term.clear()`);
    await pollFor(page, `window.term.buffer.active.length`, 5);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'test9');
    for (let i = 1; i < 5; i++) {
      await pollFor(page, `window.term.buffer.active.getLine(${i}).translateToString(true)`, '');
    }
  });

  describe('options', () => {
    it('getter', async () => {
      await openTerminal(page);
      assert.equal(await page.evaluate(`window.term.options.rendererType`), 'canvas');
      assert.equal(await page.evaluate(`window.term.options.cols`), 80);
      assert.equal(await page.evaluate(`window.term.options.rows`), 24);
    });
    it('setter', async () => {
      await openTerminal(page);
      try {
        await page.evaluate('window.term.options.cols = 40');
        fail();<commit_msg>Bring back getOption/setOption api test<commit_after>/**
 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
 * @license MIT
 */

import { assert } from 'chai';
import { pollFor, timeout, writeSync, openTerminal, launchBrowser } from './TestUtils';
import { Browser, Page } from 'playwright';
import { fail } from 'assert';

const APP = 'http://127.0.0.1:3001/test';

let browser: Browser;
let page: Page;
const width = 800;
const height = 600;

describe('API Integration Tests', function(): void {
  before(async () => {
    browser = await launchBrowser();
    page = await (await browser.newContext()).newPage();
    await page.setViewportSize({ width, height });
  });

  after(async () => browser.close());
  beforeEach(async () => page.goto(APP));

  it('Default options', async () => {
    await openTerminal(page);
    assert.equal(await page.evaluate(`window.term.cols`), 80);
    assert.equal(await page.evaluate(`window.term.rows`), 24);
  });

  it('Proposed API check', async () => {
    await openTerminal(page, { allowProposedApi: false });
    await page.evaluate(`
      try {
        window.term.buffer;
      } catch (e) {
        window.throwMessage = e.message;
      }
    `);
    await pollFor(page, 'window.throwMessage', 'You must set the allowProposedApi option to true to use proposed API');
  });

  it('write', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.write('foo');
      window.term.write('bar');
      window.term.write('文');
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foobar文');
  });

  it('write with callback', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.write('foo', () => { window.__x = 'a'; });
      window.term.write('bar', () => { window.__x += 'b'; });
      window.term.write('文', () => { window.__x += 'c'; });
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foobar文');
    await pollFor(page, `window.__x`, 'abc');
  });

  it('write - bytes (UTF8)', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.write(new Uint8Array([102, 111, 111])); // foo
      window.term.write(new Uint8Array([98, 97, 114])); // bar
      window.term.write(new Uint8Array([230, 150, 135])); // 文
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foobar文');
  });

  it('write - bytes (UTF8) with callback', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.write(new Uint8Array([102, 111, 111]), () => { window.__x = 'A'; }); // foo
      window.term.write(new Uint8Array([98, 97, 114]), () => { window.__x += 'B'; }); // bar
      window.term.write(new Uint8Array([230, 150, 135]), () => { window.__x += 'C'; }); // 文
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foobar文');
    await pollFor(page, `window.__x`, 'ABC');
  });

  it('writeln', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.writeln('foo');
      window.term.writeln('bar');
      window.term.writeln('文');
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foo');
    await pollFor(page, `window.term.buffer.active.getLine(1).translateToString(true)`, 'bar');
    await pollFor(page, `window.term.buffer.active.getLine(2).translateToString(true)`, '文');
  });

  it('writeln with callback', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.writeln('foo', () => { window.__x = '1'; });
      window.term.writeln('bar', () => { window.__x += '2'; });
      window.term.writeln('文', () => { window.__x += '3'; });
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foo');
    await pollFor(page, `window.term.buffer.active.getLine(1).translateToString(true)`, 'bar');
    await pollFor(page, `window.term.buffer.active.getLine(2).translateToString(true)`, '文');
    await pollFor(page, `window.__x`, '123');
  });

  it('writeln - bytes (UTF8)', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.term.writeln(new Uint8Array([102, 111, 111]));
      window.term.writeln(new Uint8Array([98, 97, 114]));
      window.term.writeln(new Uint8Array([230, 150, 135]));
    `);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'foo');
    await pollFor(page, `window.term.buffer.active.getLine(1).translateToString(true)`, 'bar');
    await pollFor(page, `window.term.buffer.active.getLine(2).translateToString(true)`, '文');
  });

  it('paste', async () => {
    await openTerminal(page);
    await page.evaluate(`
      window.calls = [];
      window.term.onData(e => calls.push(e));
      window.term.paste('foo');
      window.term.paste('\\r\\nfoo\\nbar\\r');
      window.term.write('\\x1b[?2004h', () => {
        window.term.paste('foo');
      });
    `);
    await pollFor(page, `window.calls`, ['foo', '\rfoo\rbar\r', '\x1b[200~foo\x1b[201~']);
  });

  it('clear', async () => {
    await openTerminal(page, { rows: 5 });
    await page.evaluate(`
      window.term.write('test0');
      window.parsed = 0;
      for (let i = 1; i < 10; i++) {
        window.term.write('\\n\\rtest' + i, () => window.parsed++);
      }
    `);
    await pollFor(page, `window.parsed`, 9);
    await page.evaluate(`window.term.clear()`);
    await pollFor(page, `window.term.buffer.active.length`, 5);
    await pollFor(page, `window.term.buffer.active.getLine(0).translateToString(true)`, 'test9');
    for (let i = 1; i < 5; i++) {
      await pollFor(page, `window.term.buffer.active.getLine(${i}).translateToString(true)`, '');
    }
  });

  it('getOption, setOption', async () => {
    await openTerminal(page);
    assert.equal(await page.evaluate(`window.term.getOption('rendererType')`), 'canvas');
    await page.evaluate(`window.term.setOption('rendererType', 'dom')`);
    assert.equal(await page.evaluate(`window.term.getOption('rendererType')`), 'dom');
  });

  describe('options', () => {
    it('getter', async () => {
      await openTerminal(page);
      assert.equal(await page.evaluate(`window.term.options.rendererType`), 'canvas');
      assert.equal(await page.evaluate(`window.term.options.cols`), 80);
      assert.equal(await page.evaluate(`window.term.options.rows`), 24);
    });
    it('setter', async () => {
      await openTerminal(page);
      try {
        await page.evaluate('window.term.options.cols = 40');
        fail();
{'fraction_non_alphanumeric': 0.20283819219497146, 'fraction_numerical': 0.024834181706000307, 'mean_word_length': 3.4407534246575344, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1041233', 'n_tokens_mistral': 4549, 'n_tokens_neox': 4367, 'n_words': 793}