Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

8.2 KB
Raw Blame
import { expect, test } from '@playwright/test';
import { copy } from '../support/copy';

// What a browser has to answer that jsdom cannot: do several source badges stay
// on one line, does a mouse hover and a finger tap each reach the preview, is
// the original page reachable from the keyboard, and — the constraint this
// feature was given — does rendering a citation fetch nothing from the cited
// site until the reader explicitly asks for it.
const GUIDE = 'https://developers.openai.com/api/docs/guides/tools-web-search';
const SOURCES = [
  { index: 1, title: 'Web search — OpenAI API', label: 'developers.openai.com' },
  { index: 2, title: '引用探针来源 — Café', label: 'example.com' },
  // A search that returned no title is attributed by its domain instead.
  { index: 3, title: 'reference.invalid', label: 'reference.invalid' },
];

for (const locale of ['en', 'zh'] as const) {
  test.describe(locale, () => {
    let citedSiteRequests = 0;

    test.beforeEach(async ({ page, context }) => {
      citedSiteRequests = 0;
      await page.addInitScript((language) => localStorage.setItem('i18nextLng', language), locale);
      // Every cited destination is served locally. A real request would make this
      // suite depend on the open internet; counting them is also how the
      // "nothing is fetched" property below is asserted rather than assumed.
      await context.route(/developers\.openai\.com|example\.com|reference\.invalid/, (route) => {
        citedSiteRequests += 1;
        return route.fulfill({ contentType: 'text/html', body: '<title>Cited source stub</title>' });
      });
      await page.goto('/e2e/citations/fixture.html');
    });

    // A titled source is announced with the domain the link actually goes to;
    // an untitled one is already named by that domain.
    const badge = (page: import('@playwright/test').Page, source: typeof SOURCES[number]) =>
      page.getByRole('link', {
        name: source.title === source.label
          ? copy('chat.citation.badge', { index: source.index, source: source.title }, locale)
          : copy('chat.citation.badgeWithDomain', { index: source.index, source: source.title, domain: source.label }, locale),
        exact: true,
      }).first();

    test(`badges stay compact and inline (${locale})`, async ({ page }, testInfo) => {
      const reference = (await page.getByText('Reference badge', { exact: true }).boundingBox())!;

      for (const source of SOURCES) {
        const bounds = (await badge(page, source).boundingBox())!;
        expect(bounds.height).toBeLessThanOrEqual(reference.height);
        expect(bounds.height).toBeLessThan(30);
      }

      // The property that makes them "compact": three sources in one sentence
      // leave that line exactly as tall as an uncited line of the same answer.
      const plain = (await page.getByText('Plain answer line without any citation.').boundingBox())!;
      const row = (await page.getByText('Three in a row.').boundingBox())!;
      expect(row.height).toBeCloseTo(plain.height, 0);

      // The grammar quoted in a code example is text, not a badge.
      await expect(page.locator('pre code')).toContainText('cite');
      await expect(page.locator('pre a')).toHaveCount(0);
      // An unresolved ref keeps its label as plain prose — never as a link.
      await expect(page.getByText('(source unavailable)')).toBeVisible();
      await expect(page.getByRole('link', { name: /source unavailable/ })).toHaveCount(0);
      // A link the answer worded itself points at source 1's page and is still
      // not a badge: the attribution belongs to the link the backend wrote, not
      // to every link that happens to go there.
      const prose = page.getByRole('link', { name: 'the web search guide', exact: true });
      await expect(prose).toHaveAttribute('href', GUIDE);
      await expect(prose).not.toHaveAttribute('data-citation-index', /.*/);
      await expect(page.locator('a[data-citation-index]')).toHaveCount(5);

      await page.screenshot({ path: testInfo.outputPath(`compact-${locale}.png`), scale: 'css' });
    });

    test(`the cited page is reachable and nothing is fetched before that (${locale})`, async ({
      page, context, isMobile,
    }, testInfo) => {
      const first = badge(page, SOURCES[0]);
      await expect(first).toBeVisible();

      if (isMobile) {
        // No hover on a phone: the first tap must reveal the preview instead of
        // navigating, or the reader can never see what they are about to open.
        await first.tap();
      } else {
        await first.hover();
      }
      await expect(page.getByText(SOURCES[0].title, { exact: true })).toBeVisible();
      await expect(page.getByText(SOURCES[0].label, { exact: true }).last()).toBeVisible();
      await expect(page).toHaveURL(/fixture\.html$/);
      // `animations: 'disabled'` finishes the panel's fade-in first, so the shot
      // shows the settled preview instead of whatever opacity it was passing through.
      await page.screenshot({ path: testInfo.outputPath(`preview-${locale}.png`), scale: 'css', animations: 'disabled' });

      // Rendering and previewing a citation reads only what the backend already
      // sent: no page, no favicon, no metadata request to the cited site.
      expect(citedSiteRequests).toBe(0);

      const opened = context.waitForEvent('page');
      if (isMobile) {
        // …and the next tap follows the link.
        await first.tap();
      } else {
        await page.getByRole('link', { name: copy('chat.citation.open', undefined, locale), exact: true })
          .click();
      }
      const source = await opened;
      await expect(source).toHaveURL(GUIDE);
      expect(citedSiteRequests).toBe(1);
      await source.close();
    });

    test(`a tap on a badge never reaches its neighbour (${locale})`, async ({ page, isMobile }) => {
      test.skip(!isMobile, 'the touch hit area only exists below the md breakpoint');
      // Three chips separated by one space each. The mobile hit area has to be
      // big enough to tap and small enough that it stays inside the space the
      // chip owns - a box that reaches its neighbour opens the WRONG source,
      // which is worse than a small target.
      const row = page.locator('p', { hasText: 'Three in a row.' }).last();
      const chips = row.getByRole('link');
      await expect(chips).toHaveCount(3);

      const owners = await row.evaluate((node) => {
        const badges = Array.from(node.querySelectorAll('a[data-citation-index]')) as HTMLElement[];
        return badges.flatMap((badge) => {
          const box = badge.getBoundingClientRect();
          const points: Array<[number, number]> = [
            [box.left + 1, box.top + box.height / 2],
            [box.right - 1, box.top + box.height / 2],
            [box.left + box.width / 2, box.top + 1],
            [box.left + box.width / 2, box.bottom - 1],
          ];
          return points.map(([x, y]) => {
            const hit = document.elementFromPoint(x, y) as HTMLElement | null;
            const owner = hit?.closest('a[data-citation-index]') as HTMLElement | null;
            return `${badge.dataset.citationIndex}->${owner?.dataset.citationIndex ?? 'none'}`;
          });
        });
      });
      expect(owners.filter((pair) => pair !== `${pair.split('->')[0]}->${pair.split('->')[0]}`)).toEqual([]);

      // …and the edge a neighbour used to cover still opens its own source.
      const box = (await chips.first().boundingBox())!;
      await chips.first().tap({ position: { x: box.width - 1, y: box.height / 2 } });
      await expect(page.getByText(SOURCES[0].title, { exact: true })).toBeVisible();
      await expect(page.getByText(SOURCES[1].title, { exact: true })).toHaveCount(0);
    });

    test(`a badge opens its source from the keyboard (${locale})`, async ({ page, context }) => {
      const first = badge(page, SOURCES[0]);
      await first.focus();

      // Focus alone reveals the preview, and focus stays on the badge — so the
      // portalled panel never has to be tabbed into to reach the page.
      await expect(page.getByText(SOURCES[0].title, { exact: true })).toBeVisible();
      await expect(first).toBeFocused();

      const opened = context.waitForEvent('page');
      await page.keyboard.press('Enter');
      await expect(await opened).toHaveURL(GUIDE);
    });
  });
}