Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

8.9 KB
Raw Blame
// @vitest-environment node
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { createServer } from 'vite';
import react from '@vitejs/plugin-react';
import { chromium } from 'playwright-core';
import { BROWSER_FIXTURE_STARTUP_MS, startBrowserFixture } from '../test/browserFixture.js';
import { resolveTestChrome } from '../../../scripts/lib/testChrome.js';

const auditCdp = process.env.PORTOS_AUDIT_CDP;
const chrome = resolveTestChrome();

// Native :disabled includes inherited fieldset state and the first-legend
// exception. Happy DOM does not model it, so exercise actual browser tab order.
describe.skipIf(!chrome && !auditCdp)('modal native keyboard boundaries', () => {
  let fixture;
  let browser;
  let page;
  let origin;
  beforeAll(async () => {
    // Each startup phase is bounded and cleaned up inside the hook (#10543).
    // Live audits attach to the existing browser instead of launching one.
    fixture = await startBrowserFixture({ name: 'drawer-focus', createServer,
      chromium: auditCdp ? { launch: () => chromium.connectOverCDP(auditCdp) } : chromium,
      launchOptions: { executablePath: chrome }, viteConfig: temp => ({
      configFile: false,
      root: fileURLToPath(new URL('../..', import.meta.url)),
      cacheDir: join(temp, 'vite-cache'),
      // Pre-bundle only what this fixture renders, not the whole app's graph.
      optimizeDeps: { entries: ['src/components/Drawer.jsx', 'src/components/RapidReader.jsx'], include: ['react', 'react-dom/client'] },
      plugins: [react(), {
        name: 'drawer-focus-fixture',
        resolveId(id) { if (id === '/drawer-fixture.jsx') return '\0drawer-fixture.jsx'; },
        load(id) {
          if (id !== '\0drawer-fixture.jsx') return;
          return `
            import React, { useState } from 'react';
            import { createRoot } from 'react-dom/client';
            import Drawer from '/src/components/Drawer.jsx';
            import { RapidReaderModal } from '/src/components/RapidReader.jsx';
            const h = React.createElement;
            function Editor() {
              const [open, setOpen] = useState(false);
              const [saving, setSaving] = useState(true);
              const [readerOpen, setReaderOpen] = useState(false);
              const disclosures = new URLSearchParams(location.search).has('disclosures');
              return h(React.Fragment, null,
                h('button', { id: 'opener', onClick: () => setOpen(true) }, 'Open editor'),
                h('button', { id: 'outside' }, 'Outside'),
                h('button', { id: 'reader-opener', onClick: () => setReaderOpen(true) }, 'Open reader'),
                h(RapidReaderModal, { open: readerOpen, text: 'Synthetic accessible reading passage.',
                  autoPlay: false, onClose: () => setReaderOpen(false) }),
                h(Drawer, { open, title: 'Edit draft', onClose: () => setOpen(false) },
                  disclosures ? h('details', null,
                    h('summary', null, 'Advanced'),
                    h('button', { id: 'detail-action' }, 'Detail action')) : h('fieldset', { disabled: saving },
                    h('legend', null, 'Draft ', h('button', { id: 'cancel', onClick: () => setSaving(false) }, 'Cancel save')),
                    h('input', { 'aria-label': 'Title' }),
                    h('button', { id: 'save', tabIndex: 0 }, 'Save draft'))));
            }
            createRoot(document.getElementById('root')).render(h(Editor));
          `;
        },
        configureServer(vite) {
          vite.middlewares.use('/drawer-test', async (_req, res) => {
            res.setHeader('Content-Type', 'text/html');
            res.end(await vite.transformIndexHtml('/drawer-test',
              '<div id="root"></div><script type="module" src="/drawer-fixture.jsx"></script>'));
          });
        },
      }],
      server: { host: '127.0.0.1', port: 0 },
    }) });
    ({ browser, origin } = fixture);
    if (auditCdp) {
      // Live audits reuse only the dedicated background target. No new page,
      // context, activation, or browser shutdown is permitted in this mode.
      for (const candidate of browser.contexts().flatMap(context => context.pages())) {
        if (await candidate.evaluate(() => window.name === 'portos-ui-quality-audit')) {
          page = candidate;
          break;
        }
      }
      if (!page) throw new Error('Dedicated PortOS audit target is missing');
      const session = await page.context().newCDPSession(page);
      await session.send('Emulation.setFocusEmulationEnabled', { enabled: true });
    } else {
      page = await browser.newPage();
    }
  }, BROWSER_FIXTURE_STARTUP_MS);
  afterAll(async () => {
    if (auditCdp && page) {
      await page.evaluate(() => {
        window.name = 'portos-ui-quality-audit';
        document.title = 'PortOS UI Audit';
      });
    }
    // close() disconnects an attached CDP client; only a launched browser is
    // owned by this suite and reaped by the fixture.
    await fixture?.close();
  });

  it('wraps around a saving form, preserves its legend control, and restores enabled tab stops', async () => {
    await page.goto(`${origin}drawer-test`);
    const opener = page.getByRole('button', { name: 'Open editor' });
    await opener.focus();
    await page.keyboard.press('Enter');
    const close = page.getByRole('button', { name: 'Close settings' });
    const cancel = page.getByRole('button', { name: 'Cancel save' });
    const save = page.getByRole('button', { name: 'Save draft' });
    await close.waitFor();
    expect(await save.isDisabled()).toBe(true);
    await page.keyboard.press('Shift+Tab');
    expect(await cancel.evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Tab');
    expect(await close.evaluate(el => el === document.activeElement)).toBe(true);

    await page.keyboard.press('Tab');
    await page.keyboard.press('Enter');
    expect(await save.isDisabled()).toBe(false);
    await page.keyboard.press('Tab');
    expect(await page.getByRole('textbox', { name: 'Title' }).evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Tab');
    expect(await save.evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Tab');
    expect(await close.evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Escape');
    expect(await opener.evaluate(el => el === document.activeElement)).toBe(true);
  }, 30000);

  it('includes native summaries and excludes content of closed disclosures from wrap boundaries', async () => {
    await page.goto(`${origin}drawer-test?disclosures`);
    const opener = page.getByRole('button', { name: 'Open editor' });
    await opener.focus();
    await page.keyboard.press('Enter');
    const close = page.getByRole('button', { name: 'Close settings' });
    const summary = page.locator('summary');
    const action = page.getByRole('button', { name: 'Detail action' });
    await close.waitFor();
    await page.keyboard.press('Shift+Tab');
    expect(await summary.evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Tab');
    expect(await close.evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Tab');
    expect(await summary.evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Enter');
    await page.keyboard.press('Tab');
    expect(await action.evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Tab');
    expect(await close.evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Escape');
    expect(await opener.evaluate(el => el === document.activeElement)).toBe(true);
  }, 30000);

  it('reaches the Rapid Reader transcript and opens it with Space without starting playback', async () => {
    await page.goto(`${origin}drawer-test`);
    const opener = page.getByRole('button', { name: 'Open reader' });
    await opener.focus();
    await page.keyboard.press('Enter');
    await page.getByRole('button', { name: 'Show two words at a time' }).focus();
    await page.keyboard.press('Tab');
    const summary = page.locator('summary');
    expect(await summary.evaluate(el => el === document.activeElement)).toBe(true);
    await page.keyboard.press('Space');
    expect(await page.locator('details').evaluate(el => el.open)).toBe(true);
    // Native toggle delivery and React's transcript commit follow details.open.
    await page.getByText('Synthetic accessible reading passage.', { exact: true }).waitFor({ state: 'visible' });
    expect(await page.getByText('Synthetic accessible reading passage.', { exact: true }).isVisible()).toBe(true);
    expect(await page.getByRole('button', { name: 'Play', exact: true }).count()).toBe(1);
    await page.keyboard.press('Escape');
    expect(await opener.evaluate(el => el === document.activeElement)).toBe(true);
  }, 30000);
});