Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

5.0 KB
Raw Blame
const assert = require('node:assert/strict');
const { readFileSync } = require('node:fs');
const { join } = require('node:path');
const { test } = require('node:test');
const { runInNewContext } = require('node:vm');

test('pixel-addressed controls stay within the first three grid rows', () => {
  const html = readFileSync(join(__dirname, 'index.html'), 'utf8');
  assert.match(html, /grid-template-columns:\s*repeat\(3,/);

  const gridStart = html.indexOf('<main class="harness-grid">');
  assert.notEqual(gridStart, -1, 'shared fixture grid exists');
  for (const id of [
    'txt-input',
    'keyboard-input',
    'drag-source',
    'drop-target',
    'scroll-tall',
    'click-target',
    'btn-open-child-window',
  ]) {
    const control = html.indexOf(`id="${id}"`, gridStart);
    assert.notEqual(control, -1, `${id} exists in the shared fixture grid`);
    const fieldsetCount = (html.slice(gridStart, control).match(/<fieldset>/g) || []).length;
    assert.ok(fieldsetCount <= 9, `${id} moved below the third grid row`);
  }
});

test('journal samples live scroll geometry without an event or DOM mutation', () => {
  const html = readFileSync(join(__dirname, 'index.html'), 'utf8');
  const source = html.match(/\(function startFixtureJournal\(\) \{[\s\S]*?\n  \}\)\(\);/);
  assert.ok(source, 'shared fixture journal exists');
  const scroll = { dataset: { cuaId: 'scroll-tall' }, textContent: 'contents', scrollTop: 208, clientHeight: 128 };
  const mirror = { dataset: { cuaId: 'lbl-scroll-offset' }, textContent: 'scroll_offset=104', scrollTop: 0, clientHeight: 16 };
  const field = { dataset: { cuaId: 'input' }, textContent: '', value: '42', checked: false, scrollTop: 0, clientHeight: 20 };
  const readonly = value => new Proxy(value, {
    get(target, key) {
      const result = target[key];
      return result && typeof result === 'object' ? readonly(result) : result;
    },
    set() { assert.fail('journal must not mutate DOM state'); },
    deleteProperty() { assert.fail('journal must not delete DOM state'); },
    defineProperty() { assert.fail('journal must not define DOM state'); },
  });
  const publications = [];
  const timers = [];
  let periodic;
  runInNewContext(source[0], {
    document: {
      body: {},
      querySelectorAll(selector) {
        assert.equal(selector, '[data-cua-id]');
        return [scroll, mirror, field].map(readonly);
      },
      addEventListener() {},
    },
    window: {
      cuaE2E: {
        journalUrl: 'http://fixture.invalid',
        publishFixtureState(state) { publications.push(JSON.parse(JSON.stringify(state))); },
      },
      addEventListener(name, callback) { assert.equal(name, 'load'); callback(); },
    },
    MutationObserver: class { observe() {} },
    setTimeout(callback, delay) { assert.equal(delay, 0); timers.push(callback); },
    setInterval(callback, delay) { assert.equal(delay, 250); periodic = callback; },
  });
  timers.shift()();
  assert.deepEqual(publications[0]['scroll-tall'], { text: 'contents', scrollTop: 208, clientHeight: 128 });
  assert.deepEqual(publications[0]['lbl-scroll-offset'], { text: 'scroll_offset=104' });
  assert.deepEqual(publications[0].input, { text: '', value: '42', checked: false });
  scroll.scrollTop = 320;
  periodic();
  timers.shift()();
  assert.equal(publications[1]['scroll-tall'].scrollTop, 320);
  assert.deepEqual(publications[1]['lbl-scroll-offset'], { text: 'scroll_offset=104' });
});

test('a collapsed scroller publishes the layout the renderer reports', () => {
  const html = readFileSync(join(__dirname, 'index.html'), 'utf8');
  const source = html.match(/\(function startFixtureJournal\(\) \{[\s\S]*?\n  \}\)\(\);/);
  const scroll = { dataset: { cuaId: 'scroll-tall' }, textContent: 'contents', scrollTop: 0, clientHeight: 0 };
  const publications = [];
  const timers = [];
  runInNewContext(source[0], {
    document: {
      body: {},
      readyState: 'complete',
      documentElement: { getBoundingClientRect: () => ({ x: 0, y: 0, width: 0, height: 0 }) },
      querySelectorAll: () => [scroll],
      addEventListener() {},
    },
    window: {
      innerWidth: 0,
      innerHeight: 0,
      devicePixelRatio: 1,
      cuaE2E: {
        journalUrl: 'http://fixture.invalid',
        publishFixtureState(state) { publications.push(JSON.parse(JSON.stringify(state))); },
      },
      addEventListener(name, callback) { callback(); },
    },
    getComputedStyle: () => ({
      display: 'block', visibility: 'visible', position: 'static',
      width: '260px', height: '120px', overflowY: 'auto',
    }),
    MutationObserver: class { observe() {} },
    setTimeout(callback) { timers.push(callback); },
    setInterval() {},
  });
  timers.shift()();
  assert.deepEqual(publications[0]['scroll-tall'].layout, {
    innerWidth: 0,
    innerHeight: 0,
    devicePixelRatio: 1,
    readyState: 'complete',
    rootRect: { x: 0, y: 0, width: 0, height: 0 },
    scrollStyle: {
      display: 'block', visibility: 'visible', position: 'static',
      width: '260px', height: '120px', overflowY: 'auto',
    },
  });
});