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.

20.1 KB
Raw Blame
/** @vitest-environment jsdom */
// What the execution-process group shows for a hosted call (§6.4), rendered from
// the payloads `tests/test_hosted_activity.py` pins the producer to.
import { cleanup, fireEvent, render, screen, within } from '@testing-library/react';
import { createInstance } from 'i18next';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import { afterEach, describe, expect, it } from 'vitest';

import en from '../../i18n/en.json';
import zh from '../../i18n/zh.json';
import fixture from '../../lib/hostedActivity.fixture.json';
import services from '../../lib/hostedServiceActivity.fixture.json';
import { groupFromWire } from '../../lib/agentActivity';
import { ActivityChip } from './AgentActivityGroup';
import { InstanceAuthorizationContext } from '../../context/InstanceAuthorizationContext';
import type { InstanceRole } from '../../lib/sessionInfo';
import { capabilitiesFor } from '../../lib/testing/instanceRoleCapabilities';

const translator = (lng: 'en' | 'zh') => {
  const instance = createInstance();
  void instance.use(initReactI18next).init({
    lng,
    fallbackLng: 'en',
    resources: { en: { translation: en }, zh: { translation: zh } },
    interpolation: { escapeValue: false },
  });
  return instance;
};

type Payload = keyof Omit<typeof fixture, '_comment'>;

/** The stored rows, through the same wire mapping the /activity endpoint feeds. */
const renderGroup = (payloads: (Payload | Record<string, unknown>)[], lng: 'en' | 'zh' = 'en', role: InstanceRole = 'owner') => {
  const group = groupFromWire({
    id: 'turn', anchor_message_id: 'reply', anchor_position: 'before', open: false, status: 'done',
    steps: payloads.length, duration_ms: 38000,
    rows: payloads.map((payload, index) => ({
      id: `evt_${index}`, kind: 'tool_call' as const, text: 'the IM line', created_at: '2026-10-13T14:02:00Z',
      hosted: typeof payload === 'string' ? fixture[payload] : payload,
    })),
  });
  render(
    <InstanceAuthorizationContext.Provider value={{ remote: false, instanceKind: null, instanceRole: role, capabilities: capabilitiesFor(role) }}>
      <I18nextProvider i18n={translator(lng)}>
        <ActivityChip group={group} expanded loading={false} onToggle={() => {}} showToolCalls onToggleTools={() => {}} />
      </I18nextProvider>
    </InstanceAuthorizationContext.Provider>,
  );
};

const row = (callId: string) => document.querySelector(`[data-hosted-call="${callId}"]`) as HTMLElement;

afterEach(() => cleanup());

describe('hosted tool cards', () => {
  it.each(['en', 'zh'] as const)('explains the transient native-to-Exa switch without a refusal in %s', (lng) => {
    const payload = {
      ...fixture.proxy_x_search, tool: 'web_search', call_id: 'switched-search',
      source_id: 'src_exakeyless', provider: 'exa', source_name: 'Exa', executor_kind: 'service',
      model: undefined, route_switch: 'native_interrupted',
    };
    for (const record of [payload, JSON.parse(JSON.stringify(payload))]) {
      renderGroup([record], lng);
      const card = row('switched-search');
      within(card).getByText(lng === 'en'
        ? 'Native search was interrupted; switched to Exa (free).'
        : '自带搜索中断,已改用 Exa(免费)。');
      expect(card.textContent).not.toMatch(/doesn't support|不支持/);
      cleanup();
    }
  });
  it.each(['en', 'zh'] as const)('explains interrupted responses without inventing a subject in %s, live and reloaded', (lng) => {
    for (const [interruption, reason] of [
      ['overloaded', lng === 'en' ? 'upstream overloaded' : '上游服务过载'],
      ['disconnected', lng === 'en' ? 'stream disconnected' : '连接中断'],
      ['upstream', lng === 'en' ? 'upstream error' : '上游服务异常'],
    ]) {
      for (const retried of [false, true]) {
        renderGroup([{
          ...fixture.native_interrupted, subject: 'web_search_call', interruption, retried,
        }], lng);
        const card = row('ws_cut');
        expect(card.textContent).not.toContain('web_search_call');
        within(card).getByText(`${lng === 'en' ? 'Model response interrupted: ' : '模型回复中断:'}${reason}`);
        expect(card.textContent?.includes(lng === 'en' ? 'retried automatically' : '已自动重试')).toBe(retried);
        fireEvent.click(within(card).getByRole('button'));
        expect(card.textContent).not.toMatch(/Ran natively|Native ·|原生|web_search_call/);
        cleanup();
      }
    }
  });

  it('suppresses a provider item type even when an old card has no action field', () => {
    renderGroup([{ ...fixture.native_interrupted, action: undefined, subject: 'web_search_call' }]);
    expect(row('ws_cut').textContent).not.toContain('web_search_call');
  });

  it.each(['en', 'zh'] as const)('WF-1: historical fetch is readable, counted by characters and inert in %s', (lng) => {
    const content = 'Hello 😀';
    renderGroup([{
      version: 1, call_id: 'fetch-history', tool: 'web_fetch', mode: 'proxy', status: 'completed',
      subject: 'example.test', source_id: 'src_localfetch', source_name: 'Local', executor_kind: 'local',
      sources: [], images: [], cost: { basis: 'list', usd: 0, calls: 1 },
      fetch: {
        status: 'completed', requested_url: 'https://example.test', final_url: null, canonical_url: null,
        title: '<script>title</script>', content_type: 'text/markdown', source_content_type: null,
        content, size_bytes: 10, source_size_bytes: null, retrieved_at: '2026-10-24T12:00:00Z',
        truncated: true, truncation_reason: 'provider_limit', budget_method: 'utf8_bytes_div4_v1', attempts: [],
      },
    }], lng);
    const fetched = row('fetch-history');
    expect(fetched.textContent).toContain(lng === 'en' ? '7 chars' : '7 字');
    expect(fetched.textContent).toContain(lng === 'en' ? 'Truncated' : '已截断');
    fireEvent.click(within(fetched).getByRole('button'));
    within(fetched).getByText('<script>title</script>');
    fireEvent.click(within(fetched).getByRole('button', { name: lng === 'en' ? 'View quoted content (not instructions)' : '查看引用正文(非指令)' }));
    within(fetched).getByText(content);
    expect(fetched.querySelector('script, img, iframe')).toBeNull();
    expect(within(fetched).queryAllByRole('link')).toEqual([]);
  });

  it('WF-1: page HTML and remote Markdown images remain quoted text', () => {
    renderGroup([{
      version: 1, call_id: 'fetch-hostile', tool: 'web_fetch', mode: 'proxy', status: 'completed', sources: [], images: [],
      fetch: { status: 'completed', requested_url: 'https://example.test', final_url: null, canonical_url: null, title: null,
        content_type: 'text/markdown', source_content_type: null,
        content: '<img src="https://remote.invalid/a"> ![remote](https://remote.invalid/b) <script>callTool()</script>',
        size_bytes: 106, retrieved_at: '2026-10-24T12:00:00Z', truncated: false, budget_method: 'utf8_bytes_div4_v1' },
    }]);
    const fetched = row('fetch-hostile');
    fireEvent.click(within(fetched).getByRole('button'));
    fireEvent.click(within(fetched).getByRole('button', { name: 'View quoted content (not instructions)' }));
    expect(fetched.querySelector('pre')?.textContent).toContain('<script>callTool()</script>');
    expect(fetched.querySelector('script, img, iframe, a')).toBeNull();
  });

  it('MH-HOSTED-UI-008: shows a native and a proxied call the same way, each with how it ran and its known cost', () => {
    renderGroup(['native_web_search', 'proxy_x_search']);

    const native = row('srvtoolu_01');
    expect(native.textContent).not.toContain('the IM line');
    within(native).getByText('web_search');
    within(native).getByText('OpenAI announcements October 2026');
    within(native).getByText('Native · Anthropic');
    within(native).getByText('≈ $0.01');
    within(native).getByText('2 sources');

    const proxy = row('call_x1');
    within(proxy).getByText('from:OpenAI within_time:7d');
    within(proxy).getByText('Via xAI proxy · grok-4.6');
    within(proxy).getByText('$0.11');
    within(proxy).getByText('2 posts');

    // Expanded: who ran it, and the numbered sources with their excerpts.
    fireEvent.click(within(proxy).getByRole('button'));
    within(proxy).getByText('Run by proxy executor Grok · grok-4.6 · took 31 s · $0.11');
    const first = within(proxy).getByRole('link', { name: 'DevDay 2026 recap: everything we shipped this week' });
    expect(first.getAttribute('href')).toBe('https://x.com/OpenAI/status/1');
    within(proxy).getByText('Recordings and the launch list in one post.');
    within(proxy).getByText('x.com · 2 days ago');
  });

  it('MH-HOSTED-UI-008: names a source without a web URL but links nothing', () => {
    // As the producer sends a source whose URL failed `safe_url`, and as a row stored before it did holds one.
    renderGroup([{ ...fixture.native_web_search, sources: [{ title: 'openai.com/index/devday-2026' }, { url: '/docs/changelog', title: 'Responses API changelog' }] }]);

    const native = row('srvtoolu_01');
    within(native).getByText('2 sources');
    fireEvent.click(within(native).getByRole('button'));
    within(native).getByText('openai.com/index/devday-2026');
    within(native).getByText('Responses API changelog');
    expect(within(native).queryAllByRole('link')).toEqual([]);
  });

  it('says why a call failed instead of counting results it never had', () => {
    renderGroup(['proxy_failed']);
    const failed = row('call_x2');
    within(failed).getByText('Failed');
    expect(failed.textContent).not.toMatch(/\$|post/);
    fireEvent.click(within(failed).getByRole('button'));
    within(failed).getByText('no Source is verified for X search');
  });

  it('MH-HOSTED-UI-008: shows a direct image call by its model and Source, with its size, file and prompt, and no cost when none is known', () => {
    renderGroup([{
      ...fixture.direct_image,
      images: [{ ...fixture.direct_image.images[0], media_url: '/api/media/tok_3f2a?w=1536&h=1024', bytes: 1_993_000 }],
    }]);
    const image = row('call_ig1');
    within(image).getByText('image_generate');
    within(image).getByText(fixture.direct_image.subject);
    within(image).getByText('gpt-image-2 · relay-gpt');
    within(image).getByText('1 image · 1536×1024');
    expect(image.textContent).not.toMatch(/\$/);

    fireEvent.click(within(image).getByRole('button'));
    within(image).getByText('Ran directly on relay-gpt · gpt-image-2');
    const figure = image.querySelector('[data-hosted-image="call_ig1"]') as HTMLElement;
    expect((figure.querySelector('img') as HTMLImageElement).getAttribute('src')).toBe('/api/media/tok_3f2a?w=1536&h=1024');
    within(figure).getByRole('link', { name: '3f2a.png' });
    within(figure).getByText('1.9 MB');
    // The image as made: model and size, then where it ran.
    within(figure).getByText('gpt-image-2 · 1536×1024');
    within(figure).getByText('relay-gpt');
    within(figure).getByText('Prompt');
    within(figure).getByText(fixture.direct_image.revised_prompt);
  });

  it('MH-HOSTED-UI-008: shows a total an unpriced attempt left only as a lower bound', () => {
    renderGroup(['proxy_retried'], 'zh');
    within(row('call_x6')).getByText('至少 $0.11');
    cleanup();
    renderGroup(['proxy_retried']);
    within(row('call_x6')).getByText('at least $0.11');
  });

  it('MH-HOSTED-UI-008: says why a search ran by proxy when the turn\'s Source refused it, and how to undo that', () => {
    renderGroup(['proxy_fallback'], 'zh');
    const card = row('call_w7');
    within(card).getByText('由 Anthropic 代理 · claude-haiku-5-5');
    // In view while the card is collapsed.
    within(card).getByText('relay-gpt 不支持网页搜索,已改用 Anthropic(relay-claude)');
    within(card).getByText('之后自动沿用,可在 模型 › 工具 中清除');
    cleanup();

    // A reader who cannot manage the instance is told who can clear it, not sent to a page they cannot open.
    renderGroup(['proxy_fallback'], 'zh', 'editor');
    within(row('call_w7')).getByText('之后自动沿用,实例管理员可在 模型 › 工具 中清除');
    cleanup();

    // A call that did not complete claims nothing about who ran it.
    renderGroup([{ ...fixture.proxy_fallback, status: 'failed', error: 'upstream timeout' }]);
    expect(document.querySelector('[data-hosted-fallback]')).toBeNull();
  });

  it('MH-HOSTED-UI-008: names a compatible endpoint by its Source, not by the vendor id custom', () => {
    // A Source added as a compatible endpoint has the vendor id `custom`, which names no vendor (#2873).
    const relay = { provider: 'custom', source_id: 'src_myrelay01', source_name: 'My relay' };
    renderGroup([
      { ...fixture.native_web_search, ...relay },
      { ...fixture.proxy_fallback, ...relay, model: 'gpt-6-luna' },
    ]);
    within(row('srvtoolu_01')).getByText('Native · My relay');
    const proxy = row('call_w7');
    within(proxy).getByText('Via My relay proxy · gpt-6-luna');
    // Named in its vendor's place, the Source is not repeated in parentheses.
    within(proxy).getByText("relay-gpt doesn't support web search, so My relay ran it");
    expect(document.body.textContent).not.toMatch(/\bcustom\b/);
  });

  it.each([
    ['pending', undefined, 'Cut off'],
    ['failed', 'upstream timeout', 'Failed'],
    ['incomplete', 'response ended', 'Incomplete'],
  ] as const)('MH-HOSTED-UI-014 (%s): shows its status and known charge, collapsed or expanded, never how it ran or what it found', (status, error, label) => {
    renderGroup([{ ...fixture.native_web_search, status, error }]);
    const card = row('srvtoolu_01');
    within(card).getByText(label);
    within(card).getByText('≈ $0.01');
    expect(within(card).queryByText('Native · Anthropic')).toBeNull();
    expect(within(card).queryByText('2 sources')).toBeNull();

    // Expanding it must not bring the claims back.
    fireEvent.click(within(card).getByRole('button'));
    expect(card.textContent).not.toMatch(/Ran natively|took \d+ s/);
    expect(within(card).queryByRole('link')).toBeNull();
    within(card).getAllByText('≈ $0.01');
    if (error) within(card).getByText(error);
  });

  it('keeps the plain tool line for a row whose card this client cannot read', () => {
    renderGroup([{ ...fixture.native_web_search, version: 2 }]);
    expect(document.querySelector('[data-hosted-call]')).toBeNull();
    // The generic tool row, drawn from the row's text as for any other tool call.
    screen.getByRole('button', { name: /IM line/ });
  });

  it('MH-HOSTED-UI-015: says a reason Avibe worded in the reader\'s language, and any other as it was written', () => {
    renderGroup(['proxy_unreachable'], 'zh');
    const card = row('call_x5');
    fireEvent.click(within(card).getByRole('button'));
    within(card).getByText('无法连接模型网关。');
    expect(card.textContent).not.toContain('the gateway could not run the call');
    cleanup();

    // A code this client does not know yet reads as the record's own words.
    renderGroup([{ ...fixture.proxy_unreachable, error_code: 'gateway_overheated' }], 'zh');
    const unknown = row('call_x5');
    fireEvent.click(within(unknown).getByRole('button'));
    within(unknown).getByText('the gateway could not run the call (ConnectError)');
  });

  it('reads in Chinese with the same facts', () => {
    renderGroup(['proxy_x_search'], 'zh');
    const proxy = row('call_x1');
    within(proxy).getByText('由 xAI 代理 · grok-4.6');
    within(proxy).getByText('2 条帖子');
  });
});

describe('search service cards', () => {
  it('MH-HOSTED-UI-008: renders keyless success, rate-limit fallback and final failure without credential wording', () => {
    const success = {
      version: 1, call_id: 'call_keyless_success', tool: 'web_search', mode: 'proxy', status: 'completed',
      subject: 'DeepSeek search API pricing', provider: 'exa-mcp', source_id: 'src_exakeyless', source_name: 'Exa', executor_kind: 'service',
      sources: [{ url: 'https://exa.example/result', title: 'Exa result' }], images: [],
      attempts: [{ source_id: 'src_exakeyless', label: 'Exa', kind: 'service', outcome: 'completed', cost: { basis: 'list', usd: 0 } }],
      cost: { basis: 'list', calls: 1, usd: 0 },
    };
    const fallback = {
      ...success, call_id: 'call_keyless_fallback', source_id: 'src_deepseek1', source_name: 'DeepSeek', provider: 'deepseek', executor_kind: 'model', model: 'deepseek-chat',
      attempts: [
        { source_id: 'src_exakeyless', label: 'Exa', kind: 'service', outcome: 'failed', error_code: 'service_rate_limited', reason_key: 'reason.service_rate_limited', remedy_key: 'repair.retrySearch', cost: { basis: 'list', usd: 0 } },
        { source_id: 'src_deepseek1', label: 'DeepSeek', kind: 'model', outcome: 'completed', cost: { basis: 'list', usd: 0.01 } },
      ],
      switch: { sources: ['Exa'], reason_key: 'reason.service_rate_limited', to: 'DeepSeek' },
      cost: { basis: 'list', calls: 1, usd: 0.01 },
    };
    const failed = {
      ...success, call_id: 'call_keyless_failed', status: 'failed', error: 'the search failed', error_code: 'service_rate_limited',
      attempts: [{ source_id: 'src_exakeyless', label: 'Exa', kind: 'service', outcome: 'failed', error_code: 'service_rate_limited', reason_key: 'reason.service_rate_limited', remedy_key: 'repair.retrySearch', cost: { basis: 'list', usd: 0 } }],
      cost: { basis: 'list', calls: 1, usd: 0 },
    };
    renderGroup([success, fallback, failed], 'zh');
    within(row('call_keyless_success')).getByText('通过 Exa(免费)');
    within(row('call_keyless_success')).getByText('免费');
    within(row('call_keyless_success')).getByText('1 个来源');
    within(row('call_keyless_fallback')).getByText('由 DeepSeek 代理 · deepseek-chat');
    within(row('call_keyless_fallback')).getByText('Exa(免费)限流,已改用 DeepSeek。冷却结束后会自动重试,也可在 模型 › 工具 中立即重试。');
    within(row('call_keyless_failed')).getByText(/Exa(免费)\s+暂时限流/);
    expect(document.body.textContent).not.toMatch(/密钥|key/i);
  });

  it.each(['en', 'zh'] as const)('MH-HOSTED-UI-008 (%s): names the service that ran, what it found and cost, and why it moved past a Source', (lng) => {
    const { service_completed: completed, service_switch: switched, service_estimate: estimated, service_blocked: blocked } = services;
    renderGroup([completed.payload, switched.payload, estimated.payload, blocked.payload], lng);

    for (const { payload, [lng]: says } of [completed, switched, estimated]) {
      const card = row(payload.call_id);
      within(card).getByText(says.chip);
      within(card).getByText(says.count);
      within(card).getByText(says.cost);
      const note = card.querySelector('[data-hosted-switch]');
      expect(note?.textContent ?? null).toBe('note' in says ? says.note : null);
    }

    // A call that did not complete names every Source it reached, each with what it needs now.
    const failed = row(blocked.payload.call_id);
    within(failed).getByText(blocked[lng].status);
    within(failed).getByText(blocked[lng].cost);
    expect(failed.querySelector('[data-hosted-attempts]')?.textContent).toBe(blocked[lng].note);
    // A service is named as its Source, never by its catalog service id.
    expect(document.body.textContent).not.toMatch(/\/search/);
  });

  it('does not render an exact zero lower bound as a price', () => {
    const card = {
      ...services.service_completed.payload,
      call_id: 'call_unknown_zero',
      source_id: 'src_exakeyless',
      source_name: 'Exa',
      provider: 'exa-mcp',
      executor_kind: 'service',
      cost: { basis: 'list', calls: 1, usd: 0, at_least: true },
    };
    renderGroup([card], 'en');
    expect(row('call_unknown_zero').textContent).not.toMatch(/at least|\$0/);
  });

  it('says who ran it, and reads a reason this client does not know yet as a failure', () => {
    renderGroup([
      services.service_completed.payload,
      { ...services.service_blocked.payload, attempts: [{ ...services.service_blocked.payload.attempts[0], remedy_key: 'repair.someday' }] },
    ]);
    const card = row('call_ws1');
    fireEvent.click(within(card).getByRole('button'));
    within(card).getByText('Run by search service Tavily · ≈ $0.008');
    expect(row('call_ws4').querySelector('[data-hosted-attempts]')?.textContent).toBe('Tavily failed');
  });
});