The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { IS_MAC } from '@/lib/keybinds/combo'
import { setAlwaysExternalLinks } from '@/store/external-links'
import { $previewTabs, closeRightRail } from '@/store/preview'
import {
__resetLinkTitleCache,
ExternalLink,
fetchLinkTitle,
hostPathLabel,
hudForcesNativeLinks,
isTitleFetchable,
LinkifiedText,
MarkdownLinkText,
PrettyLink,
urlSlugTitleLabel
} from './external-link'
const desktopWindow = window as unknown as { hermesDesktop?: Window['hermesDesktop'] }
const initialHermesDesktop = desktopWindow.hermesDesktop
function installDesktopBridge(partial: Partial<Window['hermesDesktop']> = {}) {
desktopWindow.hermesDesktop = {
fetchLinkTitle: vi.fn().mockResolvedValue(''),
openExternal: vi.fn().mockResolvedValue(undefined),
...partial
} as unknown as Window['hermesDesktop']
}
const FORGEJO_URL = 'https://forgejo.home.example/homelab/homelab-ops/issues/101'
function installTitleBridge(title: string) {
const bridge = vi.fn().mockResolvedValue(title)
installDesktopBridge({ fetchLinkTitle: bridge as unknown as Window['hermesDesktop']['fetchLinkTitle'] })
return bridge
}
afterEach(() => {
__resetLinkTitleCache()
closeRightRail()
globalThis.document.documentElement.removeAttribute('data-hermes-desktop-host')
setAlwaysExternalLinks(false)
vi.restoreAllMocks()
cleanup()
if (initialHermesDesktop) {
desktopWindow.hermesDesktop = initialHermesDesktop
} else {
delete desktopWindow.hermesDesktop
}
})
describe('external link helpers', () => {
it('formats URL fallbacks as host + path', () => {
expect(
hostPathLabel(
'https://www.getyourguide.com/culebra-island-l145468/from-fajardo-full-day-cordillera-islands-catamaran-tour-t19894/'
)
).toBe('getyourguide.com/culebra-island-l145468/from-fajardo-full-day-cordillera-islands-catamaran-tour-t19894')
})
it('derives readable title fallbacks from URL slugs', () => {
expect(
urlSlugTitleLabel(
'https://www.getyourguide.com/fajardo-l882/from-fajardo-icacos-island-full-day-catamaran-trip-t19891/'
)
).toBe('From Fajardo Icacos Island Full Day Catamaran Trip')
})
// Regression for #121321: a separator-less slug token that looks like a
// case-sensitive identifier (a digit, a dot, or mixed case — release
// tags, filenames) must keep its exact casing; title-casing it invented a
// different identifier (`V1.0.1`, `README.Md`) than the one authored. A
// plain lowercase word token is not an identifier and still title-cases.
it('keeps identifier casing in separator-less slug tokens but still title-cases words', () => {
expect(urlSlugTitleLabel('https://example.com/releases/tag/v1.0.1')).toBe('v1.0.1')
expect(urlSlugTitleLabel('https://example.com/repository/blob/main/README.md')).toBe('README.md')
expect(urlSlugTitleLabel('https://example.com/p/quantumcomputing')).toBe('Quantumcomputing')
})
it('filters out local/non-http targets for title fetches', () => {
expect(isTitleFetchable('https://www.expedia.com/things-to-do/foo')).toBe(true)
expect(isTitleFetchable('http://localhost:5174')).toBe(false)
expect(isTitleFetchable('file:///tmp/demo.html')).toBe(false)
expect(isTitleFetchable('mailto:[email protected]')).toBe(false)
})
it('deduplicates in-flight title fetches and caches results', async () => {
const bridge = vi.fn().mockResolvedValue('El Yunque Tour Water Slide, Rope Swing & Pickup')
installDesktopBridge({ fetchLinkTitle: bridge as unknown as Window['hermesDesktop']['fetchLinkTitle'] })
const url =
'https://www.expedia.com/things-to-do/puerto-rico-el-yunque-rainforest-adventure-with-transport.a46272756.activity-details'
const [first, second] = await Promise.all([fetchLinkTitle(url), fetchLinkTitle(url)])
expect(first).toBe('El Yunque Tour Water Slide, Rope Swing & Pickup')
expect(second).toBe('El Yunque Tour Water Slide, Rope Swing & Pickup')
expect(bridge).toHaveBeenCalledTimes(1)
const third = await fetchLinkTitle(url)
expect(third).toBe('El Yunque Tour Water Slide, Rope Swing & Pickup')
expect(bridge).toHaveBeenCalledTimes(1)
})
it('shares cache across protocol/www URL variants', async () => {
const bridge = vi.fn().mockResolvedValue('Shared Canonical Title')
installDesktopBridge({ fetchLinkTitle: bridge as unknown as Window['hermesDesktop']['fetchLinkTitle'] })
const first = 'https://www.getyourguide.com/san-juan-puerto-rico-l355/sunset-tours-tc306/'
const second = 'http://getyourguide.com/san-juan-puerto-rico-l355/sunset-tours-tc306/'
const [a, b] = await Promise.all([fetchLinkTitle(first), fetchLinkTitle(second)])
expect(a).toBe('Shared Canonical Title')
expect(b).toBe('Shared Canonical Title')
expect(bridge).toHaveBeenCalledTimes(1)
})
// In Electron a web link belongs in the in-app browser; the OS browser is the
// ⌘/Ctrl-click escape hatch. The connection mode does not change that.
it('opens an Electron web link in-app', async () => {
const openExternal = vi.fn().mockResolvedValue(undefined)
installDesktopBridge({ openExternal: openExternal as unknown as Window['hermesDesktop']['openExternal'] })
render(<ExternalLink href="https://example.com/path/to/resource">Example link</ExternalLink>)
fireEvent.click(screen.getByRole('link', { name: 'Example link' }))
expect(openExternal).not.toHaveBeenCalled()
await waitFor(() => expect($previewTabs.get().at(-1)?.target.url).toBe('https://example.com/path/to/resource'))
})
it.each([false, true])(
'opens a Webapp web link synchronously without adding a preview when always external is %s',
alwaysExternal => {
globalThis.document.documentElement.dataset.hermesDesktopHost = 'browser'
setAlwaysExternalLinks(alwaysExternal)
const openExternal = vi.fn().mockResolvedValue(undefined)
installDesktopBridge({ openExternal })
render(<ExternalLink href="https://example.com/path/to/resource">Example link</ExternalLink>)
fireEvent.click(screen.getByRole('link', { name: 'Example link' }))
expect(openExternal).toHaveBeenCalledExactlyOnceWith('https://example.com/path/to/resource')
expect($previewTabs.get()).toHaveLength(0)
}
)
// Platform-specific on purpose (same rule as terminal links / middle-click):
// ⌘ on macOS, Ctrl elsewhere. The suite runs as non-mac.
it('escapes to the OS browser on the platform open-elsewhere modifier', () => {
const openExternal = vi.fn().mockResolvedValue(undefined)
installDesktopBridge({ openExternal: openExternal as unknown as Window['hermesDesktop']['openExternal'] })
render(<ExternalLink href="https://example.com/path/to/resource">Example link</ExternalLink>)
fireEvent.click(screen.getByRole('link', { name: 'Example link' }), IS_MAC ? { metaKey: true } : { ctrlKey: true })
expect(openExternal).toHaveBeenCalledWith('https://example.com/path/to/resource')
expect($previewTabs.get()).toHaveLength(0)
})
it('sends an Electron plain click to the OS browser when always external is on', () => {
const openExternal = vi.fn().mockResolvedValue(undefined)
installDesktopBridge({ openExternal: openExternal as unknown as Window['hermesDesktop']['openExternal'] })
setAlwaysExternalLinks(true)
render(<ExternalLink href="https://example.com/path/to/resource">Example link</ExternalLink>)
fireEvent.click(screen.getByRole('link', { name: 'Example link' }))
expect(openExternal).toHaveBeenCalledExactlyOnceWith('https://example.com/path/to/resource')
expect($previewTabs.get()).toHaveLength(0)
})
it('treats only the HUD renderer as a native-link surface', () => {
expect(hudForcesNativeLinks('')).toBe(false)
expect(hudForcesNativeLinks('?win=secondary')).toBe(false)
expect(hudForcesNativeLinks('?win=browser&tab=1')).toBe(false)
expect(hudForcesNativeLinks('?win=hud')).toBe(true)
expect(hudForcesNativeLinks('?profile=work&win=hud')).toBe(true)
})
// The HUD has no in-app browser. A click that opened a preview tile would
// try to paint a webview into the transparent overlay (OAuth, consoles).
it('sends every HUD web link to the OS browser', () => {
const originalLocation = window.location
Object.defineProperty(window, 'location', {
configurable: true,
value: { ...originalLocation, search: '?win=hud' }
})
try {
const openExternal = vi.fn().mockResolvedValue(undefined)
installDesktopBridge({ openExternal: openExternal as unknown as Window['hermesDesktop']['openExternal'] })
render(<ExternalLink href="https://accounts.google.com/o/oauth2/auth">Sign in</ExternalLink>)
fireEvent.click(screen.getByRole('link', { name: 'Sign in' }))
expect(openExternal).toHaveBeenCalledWith('https://accounts.google.com/o/oauth2/auth')
expect($previewTabs.get()).toHaveLength(0)
} finally {
Object.defineProperty(window, 'location', { configurable: true, value: originalLocation })
}
})
// A setup step sends you to a console you are signed into in your own
// browser, to fill in a form and copy a secret back. The in-app pane has
// none of that session and is the wrong destination even for web URLs.
it('sends a setup-step link straight to the OS browser', () => {
const openExternal = vi.fn().mockResolvedValue(undefined)
installDesktopBridge({ openExternal: openExternal as unknown as Window['hermesDesktop']['openExternal'] })
render(<MarkdownLinkText text="Enable the [Docs API](https://console.cloud.google.com/apis/library) first." />)
fireEvent.click(screen.getByRole('link', { name: 'Docs API' }))
expect(openExternal).toHaveBeenCalledWith('https://console.cloud.google.com/apis/library')
expect($previewTabs.get()).toHaveLength(0)
})
// A webview can't do anything useful with these, so they always hand off.
it('hands a non-web scheme to the OS', () => {
const openExternal = vi.fn().mockResolvedValue(undefined)
installDesktopBridge({ openExternal: openExternal as unknown as Window['hermesDesktop']['openExternal'] })
render(<ExternalLink href="mailto:[email protected]">Mail</ExternalLink>)
fireEvent.click(screen.getByRole('link', { name: 'Mail' }))
expect(openExternal).toHaveBeenCalledWith('mailto:[email protected]')
})
it('renders pretty links with fetched titles and no host suffix', async () => {
const bridge = vi.fn().mockResolvedValue('From Fajardo: Full-Day Culebra Islands Catamaran Tour')
installDesktopBridge({ fetchLinkTitle: bridge as unknown as Window['hermesDesktop']['fetchLinkTitle'] })
const url =
'https://www.getyourguide.com/culebra-island-l145468/from-fajardo-full-day-cordillera-islands-catamaran-tour-t19894/'
render(<LinkifiedText text={`Read ${url}`} />)
const link = screen.getByTitle(url)
expect(link.textContent).toContain('From Fajardo Full Day Cordillera Islands Catamaran Tour')
await waitFor(() => {
expect(link.textContent).toContain('From Fajardo: Full-Day Culebra Islands Catamaran Tour')
})
expect(link.textContent).not.toContain('getyourguide.com')
})
it('ignores error-like fetched titles and falls back to slug label', async () => {
const bridge = vi.fn().mockResolvedValue('GetYourGuide – Error')
installDesktopBridge({ fetchLinkTitle: bridge as unknown as Window['hermesDesktop']['fetchLinkTitle'] })
const url =
'https://www.getyourguide.com/culebra-island-l145468/from-fajardo-full-day-cordillera-islands-catamaran-tour-t19894/'
render(<PrettyLink href={url} />)
const link = screen.getByTitle(url)
await waitFor(() => {
expect(link.textContent).toBe('From Fajardo Full Day Cordillera Islands Catamaran Tour')
})
})
it('treats not-found fetched titles as unusable', async () => {
const bridge = installTitleBridge('Page not found - Forgejo')
await expect(fetchLinkTitle(FORGEJO_URL)).resolves.toBe('')
expect(bridge).toHaveBeenCalledTimes(1)
})
it('keeps an authored fallbackLabel ahead of a fetched title, and skips the fetch', async () => {
const bridge = installTitleBridge('Kinkolino Forgejo')
// Chat markdown passes authored link text as `fallbackLabel`, not `label`.
render(<PrettyLink fallbackLabel="FJ #101" href={FORGEJO_URL} />)
const link = screen.getByTitle(FORGEJO_URL)
await waitFor(() => {
expect(link.textContent).toContain('FJ #101')
})
expect(link.textContent).not.toContain('Kinkolino Forgejo')
expect(bridge).not.toHaveBeenCalled()
})
it('still resolves a title when no label was authored', async () => {
const bridge = installTitleBridge('Homelab Ops Issue 101')
render(<PrettyLink href={FORGEJO_URL} />)
await waitFor(() => {
expect(screen.getByTitle(FORGEJO_URL).textContent).toContain('Homelab Ops Issue 101')
})
expect(bridge).toHaveBeenCalledTimes(1)
})
it('normalizes scheme-less links before opening', () => {
installDesktopBridge()
render(<LinkifiedText text="Source expedia.com/things-to-do/puerto-rico-el-yunque-rainforest-adventure" />)
const link = screen.getByRole('link')
expect(link.getAttribute('href')).toBe(
'https://expedia.com/things-to-do/puerto-rico-el-yunque-rainforest-adventure'
)
})
it('explicitOnly skips bare filename/domain tokens and only links explicit URLs', () => {
installDesktopBridge()
render(
<LinkifiedText
explicitOnly
pretty={false}
text={'Report https://paste.rs/abc\nagent.log https://paste.rs/def\nerrors.log'}
/>
)
const links = screen.getAllByRole('link')
expect(links.map(a => a.getAttribute('href'))).toEqual(['https://paste.rs/abc', 'https://paste.rs/def'])
// Bare filename-shaped tokens stay as plain text, not links.
expect(screen.queryByText(content => content.includes('agent.log'))).toBeTruthy()
expect(links.some(a => (a.textContent ?? '').includes('.log'))).toBe(false)
})
it('explicitOnly keeps www.-prefixed local filenames literal but links a standalone www. URL', () => {
installDesktopBridge()
render(
<LinkifiedText
explicitOnly
pretty={false}
text={'wrote /tmp/www.config and C:\\Users\\test-user\\Documents\\www.config; docs at www.example.com'}
/>
)
const links = screen.getAllByRole('link')
expect(links.map(a => a.getAttribute('href'))).toEqual(['https://www.example.com'])
})
it('without explicitOnly, bare filename tokens are still linkified (default behavior)', () => {
installDesktopBridge()
render(<LinkifiedText pretty={false} text="open agent.log please" />)
const link = screen.getByRole('link', { name: 'agent.log' })
expect(link.getAttribute('href')).toBe('https://agent.log')
})
})