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.

26.2 KB
Raw Blame
/** @vitest-environment jsdom */
import { createInstance } from 'i18next';
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import en from '../../i18n/en.json';
import { showDockId } from '../../context/dockDoc';
import type { ShowPageAccess } from '../../lib/showPageAccess';
import { ShowPageShareControl } from './ShowPageShareControl';

const permissionsApi = vi.hoisted(() => ({
  getPermissions: vi.fn(),
  getResourceAccess: vi.fn(),
  updateResourceAccess: vi.fn(),
}));

vi.mock('@/features/permissions/api', async (importOriginal) => ({
  ...(await importOriginal<typeof import('@/features/permissions/api')>()),
  getPermissions: permissionsApi.getPermissions,
  getResourceAccess: permissionsApi.getResourceAccess,
  updateResourceAccess: permissionsApi.updateResourceAccess,
}));

const api = {
  getShowPage: vi.fn(),
  ensureShowPage: vi.fn(),
  getShowPageAccess: vi.fn(),
  getShowAccessSettings: vi.fn(),
  applyShowAccess: vi.fn(),
  setShowPageAvailability: vi.fn(),
};

vi.mock('../../context/ApiContext', () => ({
  ApiError: class ApiError extends Error {
    code = null;
  },
  useApi: () => api,
}));

vi.mock('@/context/ApiContext', () => ({
  ApiError: class ApiError extends Error {
    code = null;
  },
  useApi: () => api,
}));

const dock = vi.hoisted(() => ({
  isDocked: vi.fn(() => false),
  isPinned: vi.fn(() => false),
  dock: vi.fn(),
  pin: vi.fn(),
  undock: vi.fn(),
}));

vi.mock('../../context/DockContext', () => ({ useDock: () => dock }));

vi.mock('../useShowPages', () => ({
  useShowPageInventory: () => ({
    pages: [],
    mergePage: vi.fn(),
    removePage: vi.fn(),
    reload: vi.fn(),
  }),
}));

beforeEach(() => {
  permissionsApi.getPermissions.mockReset();
  permissionsApi.getResourceAccess.mockReset();
  permissionsApi.updateResourceAccess.mockReset();
  dock.isDocked.mockReturnValue(false);
  dock.isPinned.mockReturnValue(false);
});

afterEach(() => {
  cleanup();
  vi.clearAllMocks();
});

const i18n = createInstance();
void i18n.use(initReactI18next).init({
  lng: 'en',
  fallbackLng: 'en',
  resources: { en: { translation: en } },
  interpolation: { escapeValue: false },
});

const renderControl = (compact: boolean) =>
  renderToStaticMarkup(
    <I18nextProvider i18n={i18n}>
      <ShowPageShareControl sessionId="ses-1" compact={compact} />
    </I18nextProvider>,
  );

const showPageAccess = (overrides: Partial<ShowPageAccess> = {}): ShowPageAccess => ({
  ok: true,
  mode: 'unmanaged',
  ownership_status: 'unmanaged',
  instance_id: null,
  organization_id: null,
  policy_organization_id: null,
  access_level: 'private',
  group_ids: [],
  policy_revision: null,
  last_applied_control_plane_revision: null,
  can_use: true,
  can_manage: false,
  can_publish_public: false,
  ...overrides,
});

describe('ShowPageShareControl trigger presentation', () => {
  it('renders the header-sized trigger by default', () => {
    const html = renderControl(false);

    expect(html.match(/<button/g)).toHaveLength(1);
    expect(html).toContain('aria-label="Share"');
    expect(html).toContain('size-7');
    expect(html).not.toContain('size-6');
  });

  it('renders the window-chrome trigger when compact', () => {
    const html = renderControl(true);

    expect(html.match(/<button/g)).toHaveLength(1);
    expect(html).toContain('aria-label="Share"');
    // Mirrors the compact annotate control's title-bar styling (§ design q4E5l chrome).
    expect(html).toContain('size-6');
    expect(html).toContain('rounded-md');
    expect(html).toContain('text-muted');
    expect(html).toContain('hover:text-foreground');
    expect(html).not.toContain('size-7');
  });
});

describe('ShowPageShareControl payload sequencing without prior access', () => {
  it('loads the payload after access resolves for a granted non-manager', async () => {
    // The app-window call site: no initialAccess, no instance authority. The
    // payload read is gated on access, so it must start only once access
    // resolves — the first open still shows the link.
    api.getShowPageAccess.mockResolvedValue(showPageAccess({
      can_use: true,
      can_manage: false,
      can_publish_public: false,
    }));
    api.getShowPage.mockResolvedValue({
      session_id: 'ses-1',
      visibility: 'private',
      active_url: '/show/ses-1/',
      share_id: null,
      url_available: true,
      offline: false,
      title: null,
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl sessionId="ses-1" />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    await waitFor(() => {
      expect(api.getShowPage).toHaveBeenCalledWith('ses-1');
    });
    await waitFor(() => {
      expect((screen.getByDisplayValue(/\/show\/ses-1\//) as HTMLInputElement).value).toContain('/show/ses-1/');
    });
  });

  it('gives an Organization page one sharing axis and no Resource policy block', async () => {
    const organizationAccess = showPageAccess({
      mode: 'organization',
      ownership_status: 'unchanged',
      instance_id: 'inst-1',
      organization_id: 'org-1',
      policy_organization_id: 'org-1',
      can_manage: true,
      can_publish_public: true,
    });
    const pending = new Promise(() => undefined);
    permissionsApi.getPermissions.mockReturnValue(pending);
    permissionsApi.getResourceAccess.mockReturnValue(pending);
    api.getShowPageAccess.mockResolvedValue(organizationAccess);
    api.getShowPage.mockResolvedValue({
      session_id: 'ses-1',
      visibility: 'private',
      active_url: '/show/ses-1/',
      share_id: null,
      url_available: true,
      offline: false,
      title: null,
    });
    api.getShowAccessSettings.mockResolvedValue({
      show_access: {
        page_id: 'ses-1',
        access_mode: 'private',
        share_id: null,
        revision: 3,
        normalized_emails: [],
      },
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl
          sessionId="ses-1"
          initialAccess={organizationAccess}
          canManageInstance
        />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    expect(await screen.findByRole('button', { name: 'Access: Private' })).toBeTruthy();
    expect(api.getShowAccessSettings).toHaveBeenCalledWith('ses-1');
    // The Organization axis and its Resource sync/ACK status are gone.
    expect(screen.queryByText('Organization access')).toBeNull();
    expect(screen.queryByText('Loading Organization access…')).toBeNull();
    expect(screen.queryByText(/has not acknowledged the latest policy/)).toBeNull();
    expect(screen.queryByRole('button', { name: 'Apply' })).toBeNull();
    expect(permissionsApi.getResourceAccess).not.toHaveBeenCalled();
    expect(permissionsApi.updateResourceAccess).not.toHaveBeenCalled();
    // Private needs no audience, so not even the directory is read.
    expect(permissionsApi.getPermissions).not.toHaveBeenCalled();
    const popover = document.querySelector('.overflow-y-auto');
    expect(popover?.classList.contains('max-h-[var(--radix-popover-content-available-height)]')).toBe(true);
  });

  it('never loads the payload when access resolves without page use', async () => {
    api.getShowPageAccess.mockResolvedValue(showPageAccess({
      can_use: false,
      can_manage: true,
      can_publish_public: false,
    }));

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl sessionId="ses-1" />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    await waitFor(() => {
      expect(api.getShowPageAccess).toHaveBeenCalledWith('ses-1');
    });
    // Metadata-only manager: no payload read is ever issued.
    await vi.waitFor(
      () => {
        expect(api.getShowPage).not.toHaveBeenCalled();
      },
      { timeout: 250 },
    );
  });

  it('issues exactly one payload read on the authorized parallel path', async () => {
    // A caller holding access (the chat header passes initialAccess) reads the
    // payload and access in parallel — the post-access hook must NOT fire a
    // second ensure request.
    api.getShowPageAccess.mockResolvedValue(showPageAccess({
      can_use: true,
      can_manage: true,
      can_publish_public: true,
    }));
    api.getShowPage.mockResolvedValue({
      session_id: 'ses-1',
      visibility: 'private',
      active_url: '/show/ses-1/',
      share_id: null,
      url_available: true,
      offline: false,
      title: null,
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl
          sessionId="ses-1"
          initialAccess={showPageAccess({
            can_use: true,
            can_manage: true,
            can_publish_public: true,
          })}
        />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    await waitFor(() => {
      expect(api.getShowPageAccess).toHaveBeenCalled();
    });
    await vi.waitFor(() => {
      expect(api.getShowPage).toHaveBeenCalledTimes(1);
    });
  });

  it('presents the Limited guest-admission link', async () => {
    api.getShowPageAccess.mockResolvedValue(showPageAccess({
      can_use: true,
      can_manage: false,
      can_publish_public: false,
    }));
    api.getShowPage.mockResolvedValue({
      session_id: 'ses-1',
      visibility: 'limited',
      active_url: null,
      public_url: 'https://alice.avibe.bot/p/stable-link/',
      share_id: 'stable-link',
      url_available: true,
      offline: false,
      title: null,
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl sessionId="ses-1" />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    expect(await screen.findByDisplayValue('https://alice.avibe.bot/p/stable-link/')).toBeTruthy();
    const openLink = screen.getByRole('link', { name: 'Open' });
    expect(openLink.getAttribute('href')).toBe('https://alice.avibe.bot/p/stable-link/');
    expect(openLink.getAttribute('target')).toBe('_blank');
    expect(screen.getByRole('button', { name: 'Copy link' })).toBeTruthy();
  });

  it('keeps the Limited share action disabled without Cloud identity', async () => {
    api.getShowPageAccess.mockResolvedValue(showPageAccess({
      can_use: true,
      can_manage: false,
      can_publish_public: false,
    }));
    api.getShowPage.mockResolvedValue({
      session_id: 'ses-1',
      visibility: 'limited',
      active_url: null,
      public_url: null,
      share_id: 'stable-link',
      url_available: false,
      offline: false,
      title: null,
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl sessionId="ses-1" />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    await waitFor(() => expect(api.getShowPage).toHaveBeenCalledWith('ses-1'));
    expect((screen.getByRole('button', { name: 'Open' }) as HTMLButtonElement).disabled).toBe(true);
    expect((screen.getByRole('button', { name: 'Copy link' }) as HTMLButtonElement).disabled).toBe(
      true,
    );
    expect(screen.queryByDisplayValue(/\/p\/stable-link\/$/)).toBeNull();
  });

  it('shares an Organization page through the same Limited audience as any other', async () => {
    const organizationAccess = showPageAccess({
      mode: 'organization',
      ownership_status: 'unchanged',
      instance_id: 'inst-1',
      organization_id: 'org-1',
      policy_organization_id: 'org-1',
      can_use: true,
      can_manage: true,
      can_publish_public: true,
    });
    permissionsApi.getPermissions.mockResolvedValue({
      ok: true,
      source: 'live',
      offline: false,
      cached_at: null,
      projection: {
        schema_version: 1,
        instance: {
          id: 'inst-1',
          organization: { id: 'org-1', name: 'Acme' },
          access_mode: 'allowlist',
          permission_authority: 'cloud',
          local_mutation_allowed: false,
          authorization_revision: 2,
        },
        capabilities: [],
        access: { owner: { email: null, role: 'owner' }, entries: [] },
        directory: {
          members: [],
          groups: [{ id: 'grp-eng', name: 'Engineering', archived_at: null }],
        },
        projects: [],
        policy_sync: {
          status: 'in_sync',
          projects: { active: 0, error: 0, offline: 0, applying: 0, in_sync: 0 },
          resources: { active: 0, error: 0, offline: 0, applying: 0, in_sync: 0 },
        },
      },
    });
    api.getShowPageAccess.mockResolvedValue(organizationAccess);
    api.getShowPage.mockResolvedValue({
      session_id: 'ses-1',
      visibility: 'limited',
      active_url: '/p/stable-link/',
      share_id: 'stable-link',
      url_available: true,
      offline: false,
      title: null,
    });
    api.getShowAccessSettings.mockResolvedValue({
      show_access: {
        page_id: 'ses-1',
        access_mode: 'limited',
        share_id: 'stable-link',
        revision: 0,
        normalized_emails: ['[email protected]'],
        access_entries: [
          { kind: 'group', value: 'grp-eng' },
          { kind: 'email', value: '[email protected]' },
        ],
      },
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl
          sessionId="ses-1"
          initialAccess={organizationAccess}
          canManageInstance
        />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    expect(await screen.findByRole('button', { name: 'Access: Limited' })).toBeTruthy();
    // Custom-link editing is no longer withheld from Organization pages.
    expect((screen.getByRole('textbox', { name: 'Custom link' }) as HTMLInputElement).value).toBe(
      'stable-link',
    );
    // Group, email, and the Organization switch are peers in one audience list.
    expect(await screen.findByRole('switch', { name: 'This Organization' })).toBeTruthy();
    expect(screen.getByText('Engineering')).toBeTruthy();
    expect(screen.getByText('[email protected]')).toBeTruthy();
    expect(screen.queryByText('Organization access')).toBeNull();
    expect(permissionsApi.getResourceAccess).not.toHaveBeenCalled();
    expect(permissionsApi.updateResourceAccess).not.toHaveBeenCalled();
  });

  it('does not mount Organization access for a normal Personal Avibe', async () => {
    const personalAccess = showPageAccess({
      mode: 'personal',
      ownership_status: 'unchanged',
      instance_id: null,
      organization_id: null,
      policy_organization_id: null,
      can_use: true,
      can_manage: true,
      can_publish_public: true,
    });
    api.getShowPageAccess.mockResolvedValue(personalAccess);
    api.getShowPage.mockResolvedValue({
      session_id: 'ses-1',
      visibility: 'private',
      active_url: '/show/ses-1/',
      share_id: null,
      url_available: true,
      offline: false,
      title: null,
    });
    api.getShowAccessSettings.mockResolvedValue({
      show_access: {
        page_id: 'ses-1',
        access_mode: 'private',
        share_id: null,
        revision: 0,
        normalized_emails: [],
      },
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl
          sessionId="ses-1"
          initialAccess={personalAccess}
          canManageInstance
        />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    expect(await screen.findByRole('button', { name: 'Access: Private' })).toBeTruthy();
    expect(screen.queryByText('Organization access')).toBeNull();
    expect(permissionsApi.getPermissions).not.toHaveBeenCalled();
    expect(permissionsApi.getResourceAccess).not.toHaveBeenCalled();
  });

  it('does not resurrect an ownership-domain block on a Personal conflict', async () => {
    // Ownership conflict used to open the Resource-policy control. Sharing no
    // longer depends on ownership, so the conflict adds no second block at all.
    const conflict = showPageAccess({
      mode: 'personal',
      ownership_status: 'conflict',
      instance_id: 'inst-1',
      organization_id: null,
      policy_organization_id: 'org-other',
      can_use: true,
      can_manage: true,
      can_publish_public: false,
    });
    api.getShowPageAccess.mockResolvedValue(conflict);
    api.getShowPage.mockResolvedValue({
      session_id: 'ses-1',
      visibility: 'private',
      active_url: '/show/ses-1/',
      share_id: null,
      url_available: true,
      offline: false,
      title: null,
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl sessionId="ses-1" initialAccess={conflict} canManageInstance />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    await waitFor(() => expect(api.getShowPage).toHaveBeenCalledWith('ses-1'));
    expect(screen.queryByText(/bound to a different ownership domain/)).toBeNull();
    expect(screen.queryByText('Organization access')).toBeNull();
    expect(permissionsApi.getPermissions).not.toHaveBeenCalled();
    expect(permissionsApi.getResourceAccess).not.toHaveBeenCalled();
    expect(permissionsApi.updateResourceAccess).not.toHaveBeenCalled();
  });

  it('keeps explicit custom-link saving while online and Organization controls stay out', async () => {
    api.getShowPageAccess.mockResolvedValue(showPageAccess({
      can_use: true,
      can_manage: true,
      can_publish_public: true,
    }));
    api.getShowPage.mockResolvedValue({
      session_id: 'ses-1',
      visibility: 'public',
      active_url: '/p/stable-link/',
      share_id: 'stable-link',
      url_available: true,
      offline: false,
      title: null,
    });
    api.getShowAccessSettings.mockResolvedValue({
      show_access: {
        page_id: 'ses-1',
        access_mode: 'public',
        share_id: 'stable-link',
        revision: 0,
        normalized_emails: [],
      },
    });
    api.applyShowAccess.mockResolvedValue({
      status: 'applied',
      show_access: {
        page_id: 'ses-1',
        access_mode: 'public',
        share_id: 'new-link',
        revision: 1,
        normalized_emails: [],
      },
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl sessionId="ses-1" canManageInstance />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    expect(await screen.findByRole('button', { name: 'Access: Fully public' })).toBeTruthy();
    const customLink = screen.getByRole('textbox', { name: 'Custom link' });
    expect((customLink as HTMLInputElement).value).toBe('stable-link');
    expect(screen.getByText('Custom link')).toBeTruthy();
    expect(screen.queryByRole('button', { name: 'Save' })).toBeNull();
    fireEvent.change(customLink, { target: { value: 'new-link' } });
    fireEvent.blur(customLink);

    expect(api.applyShowAccess).not.toHaveBeenCalled();
    fireEvent.click(screen.getByRole('button', { name: 'Save' }));

    await waitFor(() => {
      expect(api.applyShowAccess).toHaveBeenCalledWith('ses-1', {
        expected_revision: 0,
        target_access_mode: 'public',
        target_share_id: 'new-link',
        target_entries: [],
      });
    });
    expect(screen.queryByText('Organization access')).toBeNull();
    expect(screen.queryByRole('button', { name: 'Apply' })).toBeNull();
    expect(api.setShowPageAvailability).not.toHaveBeenCalled();
  });

  it('shows the loading state while a first access read is pending', async () => {
    // The app-window caller has no access yet: while the access read is in
    // flight the popover must show the loading row, not the load-error text.
    let release: (() => void) | null = null;
    api.getShowPageAccess.mockImplementation(
      () => new Promise((resolve) => {
        release = () => resolve(showPageAccess({
          can_use: false,
          can_manage: true,
          can_publish_public: false,
        }));
      }),
    );

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl sessionId="ses-1" />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    await waitFor(() => {
      expect(screen.getByText('Loading...')).toBeTruthy();
      expect(screen.queryByText("Couldn't load this Show Page.")).toBeNull();
    });

    (release ?? (() => undefined))();
    await waitFor(() => {
      expect(api.getShowPageAccess).toHaveBeenCalled();
    });
  });
});

describe('ShowPageShareControl request surface', () => {
  // The property: opening this panel READS a Show Page, it never creates one.
  // `POST .../ensure` reports the first-creation edge exactly once, and only the
  // caller that sends the "visualize this session" prompt may consume it — so
  // this panel must stay off that endpoint no matter which branch it opens
  // through. Asserted as a subset check against the declared read-only surface
  // instead of a list of forbidden calls: an API this component starts calling
  // later fails here until someone declares it read-only, and one that is not
  // even on the mocked surface fails louder still.
  const READ_ONLY_ON_OPEN = new Set([
    'getShowPage',
    'getShowPageAccess',
    'getShowAccessSettings',
  ]);

  const mutatingCallsSoFar = () => Object.entries(api)
    .filter(([name, fn]) => !READ_ONLY_ON_OPEN.has(name) && fn.mock.calls.length > 0)
    .map(([name]) => name);

  const page = {
    session_id: 'ses-1',
    visibility: 'private',
    active_url: '/show/ses-1/',
    share_id: null,
    url_available: true,
    offline: false,
    title: null,
  };

  it.each([
    // Both mount shapes: ChatPage passes access it already has, AppWindow does
    // not and makes the panel resolve access first. They open through different
    // branches, so the property is asserted on each.
    ['with access already resolved', true],
    ['resolving access on open', false],
  ])('reaches only read-only APIs when opening %s', async (_label, seeded) => {
    const access = showPageAccess({ can_use: true, can_manage: true, can_publish_public: true });
    api.getShowPageAccess.mockResolvedValue(access);
    api.getShowPage.mockResolvedValue(page);
    api.getShowAccessSettings.mockResolvedValue({
      show_access: {
        page_id: 'ses-1',
        access_mode: 'private',
        share_id: null,
        revision: 1,
        normalized_emails: [],
      },
    });

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl sessionId="ses-1" initialAccess={seeded ? access : null} />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    await waitFor(() => expect(api.getShowPage).toHaveBeenCalledWith('ses-1'));
    await waitFor(() => {
      expect((screen.getByDisplayValue(/\/show\/ses-1\//) as HTMLInputElement).value).toContain('/show/ses-1/');
    });
    expect(mutatingCallsSoFar()).toEqual([]);
  });

  it('reaches only read-only APIs when the page it wants does not exist', async () => {
    // The case that used to CREATE one: a framed session whose page row is gone.
    // The read fails and the panel stays empty; it must not fall back to ensure.
    api.getShowPageAccess.mockResolvedValue(showPageAccess({ can_use: true, can_manage: true }));
    api.getShowPage.mockRejectedValue(new Error('show_page_not_found'));

    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl sessionId="ses-1" />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));

    await waitFor(() => expect(api.getShowPage).toHaveBeenCalledWith('ses-1'));
    expect(mutatingCallsSoFar()).toEqual([]);
  });
});

// PERMISSIONS-021. `/api/dock/pins` is an editor-tier route, so the Dock row
// follows page use, not instance management. PM verified both verbs over signed
// HTTP: an Editor pins and unpins with a persisted readback, a Viewer is denied.
describe('PERMISSIONS-021 ShowPageShareControl Dock pin authority', () => {
  const page = {
    session_id: 'ses-1',
    visibility: 'private',
    active_url: '/show/ses-1/',
    share_id: null,
    url_available: true,
    offline: false,
    title: 'Release notes',
  };

  const renderShare = (props: { canPinToDock?: boolean; canUse?: boolean } = {}) => {
    const access = showPageAccess({ can_use: props.canUse !== false, can_manage: false });
    api.getShowPageAccess.mockResolvedValue(access);
    api.getShowPage.mockResolvedValue(page);
    render(
      <I18nextProvider i18n={i18n}>
        <ShowPageShareControl
          sessionId="ses-1"
          initialAccess={access}
          canPinToDock={props.canPinToDock ?? true}
        />
      </I18nextProvider>,
    );
    fireEvent.click(screen.getByRole('button', { name: 'Share' }));
  };

  it('pins a page for a user who may use it but may not manage the instance', async () => {
    renderShare();

    const toggle = await screen.findByRole('switch', { name: 'Pin to Dock' });
    fireEvent.click(toggle);

    expect(dock.pin).toHaveBeenCalledWith('ses-1');
    expect(dock.undock).not.toHaveBeenCalled();
  });

  it('unpins the same page and reports it as docked', async () => {
    dock.isDocked.mockReturnValue(true);
    dock.isPinned.mockReturnValue(true);
    renderShare();

    expect(await screen.findByText(/Pinned — “Release notes” is in your Dock/)).toBeTruthy();
    fireEvent.click(screen.getByRole('switch', { name: 'Pin to Dock' }));

    expect(dock.undock).toHaveBeenCalledWith(showDockId('ses-1'));
    expect(dock.pin).not.toHaveBeenCalled();
  });

  it('offers no Dock control to a role that cannot reach the pins route', async () => {
    renderShare({ canPinToDock: false });

    // The share link still loads, so this is the Dock row being withheld, not an
    // unreadable page.
    await waitFor(() => expect(screen.getByDisplayValue(/\/show\/ses-1\//)).toBeTruthy());
    expect(screen.queryByRole('switch', { name: 'Pin to Dock' })).toBeNull();
    expect(dock.pin).not.toHaveBeenCalled();
  });

  it('withholds the Dock control when page use itself is revoked', async () => {
    renderShare({ canUse: false });

    await waitFor(() => expect(api.getShowPageAccess).toHaveBeenCalled());
    expect(screen.queryByRole('switch', { name: 'Pin to Dock' })).toBeNull();
    expect(api.getShowPage).not.toHaveBeenCalled();
  });
});