Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

3.0 KB
Raw Blame
// The host-permission banner (src/shared/permissions.ts) in the real, loaded
// extension. On Chrome the host permissions are granted at install, so the
// banner must stay hidden -- that is the no-regression case for the Chrome
// build. A withheld host (Chrome's "Site access" restriction, or Firefox's
// opt-in host permissions) must make the banner name what is missing.
import { test, expect } from './fixtures.js';

const PAGES = ['popup.html', 'options.html'];

test('popup and options warn only about withheld hosts, and clear once they are granted', async ({
  context,
  extensionId,
}) => {
  // A default Chrome install: everything granted, no warning.
  for (const pageName of PAGES) {
    const page = await context.newPage();
    await page.goto(`chrome-extension://${extensionId}/${pageName}`);
    // The markup starts hidden, so wait for a completed check (data-state)
    // before asserting hidden; otherwise this would pass without running.
    const banner = page.locator('[data-testid="perm-missing"]');
    await expect(banner).toHaveAttribute('data-state', 'granted');
    await expect(banner).toBeHidden();
    await page.close();
  }

  // Chrome refuses to revoke a required host permission from script ("You
  // cannot remove required permissions"), and a revocation through Site access
  // needs the browser UI. So simulate the withheld state the way the page sees
  // it: a permissions API that reports two hosts missing until they are
  // requested. Everything else -- markup, CSS, the click wiring -- is real. The
  // init script applies only to pages opened after this point.
  const withheld = ['https://chatgpt.com/*', 'http://127.0.0.1/*'];
  await context.addInitScript((missing: string[]) => {
    if (location.protocol !== 'chrome-extension:') return;
    const perms = chrome.permissions;
    const denied = new Set(missing);
    const requested: string[][] = [];
    (globalThis as any).__beaconRequested = requested;
    perms.contains = (async (p: chrome.permissions.Permissions) =>
      !(p.origins ?? []).some((o) => denied.has(o))) as typeof perms.contains;
    perms.request = (async (p: chrome.permissions.Permissions) => {
      requested.push([...(p.origins ?? [])]);
      (p.origins ?? []).forEach((o) => denied.delete(o));
      return true;
    }) as typeof perms.request;
  }, withheld);

  for (const pageName of PAGES) {
    const page = await context.newPage();
    await page.goto(`chrome-extension://${extensionId}/${pageName}`);
    const banner = page.locator('[data-testid="perm-missing"]');
    await expect(banner).toBeVisible();
    await expect(banner).toHaveAttribute('data-state', 'missing');
    await expect(page.locator('[data-testid="perm-list"]')).toHaveText('chatgpt.com, 127.0.0.1');

    await page.locator('[data-testid="perm-grant"]').click();
    await expect(banner).toHaveAttribute('data-state', 'granted');
    await expect(banner).toBeHidden();
    expect(await page.evaluate(() => (globalThis as any).__beaconRequested)).toEqual([withheld]);
    await page.close();
  }
});