Back to Faultline
SOURCE / PINNED RELEASE

Made of little things.

Faultline

Release
da1ffaa5624c…
Author-recorded commit
60f5f03189db…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-6932b12c78c

Archive hash verified: 5a2e0492417ec4e7…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

tests/mouse.mjs
// Run against the URL printed by soyli dev. The same built artifact is also exercised
// on a standalone, pointer-lock-capable page to cover both browser permission paths.
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import { readFile } from 'node:fs/promises';

const require = createRequire(import.meta.url);
const { chromium } = createRequire(require.resolve('@napplet/conformance-cli/package.json'))(
  'playwright',
);
// Native pointer confinement needs a real focused desktop window on macOS.
const browser = await chromium.launch({ channel: 'chromium', headless: false });
try {
  for (const mode of ['shell', 'portable', 'native']) {
    const page = await browser.newPage({ viewport: { width: 1280, height: 800 } });
    const errors = [];
    page.on('pageerror', (e) => errors.push(e.message));
    let frame = page.mainFrame(),
      captureAllowed = true;
    if (mode === 'shell') {
      await page.goto(process.argv[2]);
      const iframe = page.locator('iframe').first();
      await iframe.waitFor();
      const tokens = await iframe.getAttribute('sandbox');
      captureAllowed = tokens === null || tokens.split(/\s+/).includes('allow-pointer-lock');
      frame = await (await iframe.elementHandle()).contentFrame();
    } else if (mode === 'portable') {
      const html = await readFile(new URL('../dist/index.html', import.meta.url), 'utf8');
      await page.setContent(
        '<iframe sandbox="allow-scripts" style="width:95vw;height:90vh"></iframe>',
      );
      await page.locator('iframe').evaluate((el, html) => {
        el.srcdoc = html;
      }, html);
      frame = await (await page.locator('iframe').elementHandle()).contentFrame();
      captureAllowed = false;
    } else {
      await page.route('http://localhost/arena', (route) =>
        route.fulfill({
          path: new URL('../dist/index.html', import.meta.url).pathname,
          contentType: 'text/html',
        }),
      );
      await page.goto('http://localhost/arena');
    }
    await page.bringToFront();
    await frame.getByRole('button', { name: 'Solo practice' }).click();
    await frame.waitForFunction(
      (expected) =>
        Boolean(document.pointerLockElement) === expected &&
        document.querySelector('#scene').dataset.position,
      captureAllowed,
      { timeout: 5000 },
    );
    const canvas = frame.locator('#scene');
    assert.equal(await canvas.evaluate((e) => getComputedStyle(e).cursor), 'none');
    const capture = page.getByRole('button', { name: 'Capture mouse', exact: true });
    if (mode === 'shell') {
      await capture.waitFor({ timeout: 5000 });
      assert.equal(await page.evaluate(() => document.pointerLockElement), null);
      await capture.click();
      await frame.waitForFunction(
        () => document.querySelector('#scene').dataset.mouseLook === 'locked',
      );
      assert.equal(
        await page.evaluate(() => document.pointerLockElement?.className),
        'soy-mouse-capture',
      );
      assert.equal(await frame.evaluate(() => document.pointerLockElement), null);
    }
    assert.equal(
      await canvas.getAttribute('data-mouse-look'),
      mode === 'portable' ? 'free' : 'locked',
    );
    const rect = await canvas.boundingBox();
    await page.mouse.move(rect.x + rect.width * 0.5, rect.y + rect.height * 0.5);
    await page.mouse.move(rect.x + rect.width - 4, rect.y + rect.height * 0.55, { steps: 3 });
    await page.waitForTimeout(150);
    const still = await canvas.getAttribute('data-aim');
    await page.waitForTimeout(400);
    assert.equal(
      await canvas.getAttribute('data-aim'),
      still,
      'a stationary cursor at the edge must not rotate the camera',
    );
    await page.mouse.move(rect.x + rect.width * 0.5, rect.y + rect.height * 0.5);
    await page.mouse.down({ button: 'right' });
    await frame.waitForFunction(() => document.querySelector('#cross').hidden);
    const baseline = Number(await canvas.getAttribute('data-shots'));
    await page.mouse.down({ button: 'left' });
    await frame.waitForFunction(
      (n) => Number(document.querySelector('#scene').dataset.shots) > n,
      baseline,
      { timeout: 2500 },
    );
    await page.mouse.up({ button: 'left' });
    assert.equal(await frame.locator('#cross').isHidden(), true);
    await page.mouse.up({ button: 'right' });
    await frame.waitForFunction(() => !document.querySelector('#cross').hidden);
    if (mode === 'shell') {
      // Exercise the browser unlock notification independently of Faultline's Esc shortcut.
      await page.evaluate(() => document.exitPointerLock());
    } else await page.keyboard.press('Escape');
    await frame.getByRole('heading', { name: 'Practice paused' }).waitFor({ timeout: 3000 });
    assert.equal(await frame.evaluate(() => document.pointerLockElement === null), true);
    await frame.getByRole('button', { name: 'Change loadout' }).click();
    await frame.getByRole('combobox', { name: /Secondary/ }).selectOption({ label: 'Hammer H6' });
    await frame.getByRole('button', { name: 'Equip and resume' }).click();
    if (mode === 'shell') {
      await capture.waitFor();
      await capture.click();
      await frame.waitForFunction(
        () => document.querySelector('#scene').dataset.mouseLook === 'locked',
      );
    }
    await page.keyboard.press('2');
    await frame.waitForFunction(() =>
      document.querySelector('#gun-label').textContent.includes('Hammer H6'),
    );
    await frame.waitForFunction(
      (expected) => Boolean(document.pointerLockElement) === expected,
      captureAllowed,
      { timeout: 3000 },
    );
    if (mode === 'portable' || mode === 'shell') {
      for (const direction of [1, -1]) {
        // Start at spawn each time: getting eliminated resets the camera by design.
        await page.keyboard.press('p');
        await frame.getByRole('button', { name: 'Restart practice' }).click();
        await frame.waitForFunction(
          () =>
            !document.getElementById('dialog').open &&
            Number(document.querySelector('#scene').dataset.ack) < 100,
        );
        await frame.evaluate(
          () => new Promise((done) => requestAnimationFrame(() => requestAnimationFrame(done))),
        );
        const x = rect.x + rect.width * 0.5,
          y = rect.y + rect.height * 0.5;
        if (mode === 'shell') {
          await capture.click();
          await frame.waitForFunction(
            () => document.querySelector('#scene').dataset.mouseLook === 'locked',
          );
        }
        await page.mouse.move(x, y);
        let last = Number(await canvas.getAttribute('data-aim')),
          total = 0;
        if (mode === 'portable') {
          await page.mouse.down({ button: 'middle' });
          await page.mouse.move(x + direction * 75, y, { steps: 3 });
        }
        for (let i = 1; i <= 120 && total < Math.PI * 2 + 0.05; i++) {
          if (mode === 'shell') await page.mouse.move(x + direction * 120 * i, y);
          await page.waitForTimeout(28);
          const yaw = Number(await canvas.getAttribute('data-aim'));
          total += direction * Math.atan2(Math.sin(yaw - last), Math.cos(yaw - last));
          last = yaw;
        }
        assert.ok(total > Math.PI * 2, mode + ' continuous turn ' + direction + ': ' + total);
        if (mode === 'portable') {
          await page.mouse.up({ button: 'middle' });
          await page.waitForTimeout(80);
          const stopped = await canvas.getAttribute('data-aim');
          await page.waitForTimeout(120);
          assert.equal(await canvas.getAttribute('data-aim'), stopped);
        }
      }
    }
    assert.deepEqual(errors, []);
    console.log(mode + ' · ADS plus mouse fire · Escape · loadout');
    await page.close();
  }
} finally {
  await browser.close();
}