Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

8.7 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import { isBareLinkLabel, isEntityIdLabel, parseInternalLink } from './internalLink';

describe('parseInternalLink', () => {
  it('parses official agent document links', () => {
    expect(
      parseInternalLink('https://app.lobehub.com/agent/agt_agent/docs/document?mode=preview#title'),
    ).toEqual({
      agentId: 'agt_agent',
      documentId: 'docs_document',
      pathname: '/agent/agt_agent/docs/document?mode=preview#title',
      type: 'document',
    });
  });

  it('parses page links as document references', () => {
    expect(parseInternalLink('/page/page-1')).toEqual({
      documentId: 'docs_page-1',
      pathname: '/page/page-1',
      type: 'document',
    });
  });

  it('parses global and agent-scoped tasks', () => {
    expect(parseInternalLink('https://app.lobehub.com/task/T-198')).toEqual({
      pathname: '/task/T-198',
      taskId: 'T-198',
      type: 'task',
    });
    expect(parseInternalLink('/agent/agent-1/task/T-199')).toEqual({
      agentId: 'agent-1',
      pathname: '/agent/agent-1/task/T-199',
      taskId: 'T-199',
      type: 'task',
    });
  });

  it('ignores the readable title slug tail when resolving a task link', () => {
    expect(parseInternalLink('https://app.lobehub.com/task/T-198/ship-the-thing')).toEqual({
      pathname: '/task/T-198/ship-the-thing',
      taskId: 'T-198',
      type: 'task',
    });
    // A CJK slug round-trips through `new URL`, so the pathname comes back
    // percent-encoded — still a valid link to the same task.
    expect(parseInternalLink('/agent/agent-1/task/T-199/飞书适配器')).toEqual({
      agentId: 'agent-1',
      pathname: '/agent/agent-1/task/T-199/%E9%A3%9E%E4%B9%A6%E9%80%82%E9%85%8D%E5%99%A8',
      taskId: 'T-199',
      type: 'task',
    });
  });

  it('parses verification report links', () => {
    expect(parseInternalLink('https://app.lobehub.com/verify/run-1')).toEqual({
      pathname: '/verify/run-1',
      runId: 'run-1',
      type: 'verify',
    });
    expect(parseInternalLink('/lobe-team/verify/run-2', undefined, ['lobe-team'])).toEqual({
      pathname: '/lobe-team/verify/run-2',
      runId: 'run-2',
      type: 'verify',
      workspaceSlug: 'lobe-team',
    });
  });

  it('parses acceptance links as dedicated entities', () => {
    expect(parseInternalLink('https://app.lobehub.com/acceptance/acceptance-1')).toEqual({
      acceptanceId: 'acceptance-1',
      pathname: '/acceptance/acceptance-1',
      type: 'acceptance',
    });
    expect(
      parseInternalLink('/lobe-team/acceptance/acceptance-2', undefined, ['lobe-team']),
    ).toEqual({
      acceptanceId: 'acceptance-2',
      pathname: '/lobe-team/acceptance/acceptance-2',
      type: 'acceptance',
      workspaceSlug: 'lobe-team',
    });
    expect(
      parseInternalLink('https://lobehub.com/acceptance/acceptance-3', 'https://app.lobehub.com'),
    ).toEqual({
      acceptanceId: 'acceptance-3',
      pathname: '/acceptance/acceptance-3',
      type: 'acceptance',
    });
  });

  it('parses goal links, global and agent-scoped, as dedicated entities', () => {
    expect(parseInternalLink('https://app.lobehub.com/goal/goal_JekDW3il7s4H')).toEqual({
      goalId: 'goal_JekDW3il7s4H',
      pathname: '/goal/goal_JekDW3il7s4H',
      type: 'goal',
    });
    expect(parseInternalLink('/agent/agt_1/goal/goal_abc')).toEqual({
      agentId: 'agt_1',
      goalId: 'goal_abc',
      pathname: '/agent/agt_1/goal/goal_abc',
      type: 'goal',
    });
    expect(parseInternalLink('/lobe-team/goal/goal_abc', undefined, ['lobe-team'])).toEqual({
      goalId: 'goal_abc',
      pathname: '/lobe-team/goal/goal_abc',
      type: 'goal',
      workspaceSlug: 'lobe-team',
    });
  });

  it('parses workspace-prefixed entity paths', () => {
    expect(
      parseInternalLink('/lobe-team/agent/agt_agent/docs/docs_document', undefined, ['lobe-team']),
    ).toEqual({
      agentId: 'agt_agent',
      documentId: 'docs_document',
      pathname: '/lobe-team/agent/agt_agent/docs/docs_document',
      type: 'document',
      workspaceSlug: 'lobe-team',
    });
  });

  it('normalizes away the active workspace slug so the link opens in-context', () => {
    expect(
      parseInternalLink(
        'https://app.lobehub.com/lobehub/page/docs_LgOiA6TLUUKQro6L',
        'app://renderer',
        ['lobehub'],
        'lobehub',
      ),
    ).toEqual({
      documentId: 'docs_LgOiA6TLUUKQro6L',
      pathname: '/page/docs_LgOiA6TLUUKQro6L',
      type: 'document',
    });
  });

  it('keeps the slug of a workspace other than the active one', () => {
    expect(
      parseInternalLink(
        '/lobe-team/page/docs_document',
        undefined,
        ['lobe-team', 'lobehub'],
        'lobehub',
      ),
    ).toMatchObject({ pathname: '/lobe-team/page/docs_document', workspaceSlug: 'lobe-team' });
  });

  it('accepts the current self-hosted origin', () => {
    expect(
      parseInternalLink('https://chat.example.com/task/T-200', 'https://chat.example.com'),
    ).toEqual({
      pathname: '/task/T-200',
      taskId: 'T-200',
      type: 'task',
    });
    expect(
      parseInternalLink('https://app.lobehub.com/task/T-200', 'https://chat.example.com'),
    ).toBeNull();
  });

  it('accepts official links from the Electron renderer origin', () => {
    expect(parseInternalLink('https://app.lobehub.com/task/T-201', 'app://renderer')).toEqual({
      pathname: '/task/T-201',
      taskId: 'T-201',
      type: 'task',
    });
    expect(
      parseInternalLink('https://lobehub.com/acceptance/acceptance-4', 'app://renderer'),
    ).toEqual({
      acceptanceId: 'acceptance-4',
      pathname: '/acceptance/acceptance-4',
      type: 'acceptance',
    });
  });

  it('preserves workspace context for workspace-prefixed SPA routes', () => {
    expect(parseInternalLink('/lobe-team/tasks', undefined, ['lobe-team'])).toEqual({
      pathname: '/lobe-team/tasks',
      type: 'route',
      workspaceSlug: 'lobe-team',
    });
  });

  it('rejects external hosts even when their path resembles an app route', () => {
    expect(
      parseInternalLink('https://example.com/task/T-200', 'https://chat.example.com'),
    ).toBeNull();
    expect(parseInternalLink('//example.com/task/T-200', 'https://chat.example.com')).toBeNull();
  });

  it('parses agent roots and keeps deeper routes as SPA routes', () => {
    expect(parseInternalLink('/agent/agent-1')).toEqual({
      agentId: 'agent-1',
      pathname: '/agent/agent-1',
      type: 'agent',
    });
    expect(parseInternalLink('/agent/custom-agent-id-123/docs/foo')).toEqual({
      agentId: 'custom-agent-id-123',
      documentId: 'docs_foo',
      pathname: '/agent/custom-agent-id-123/docs/foo',
      type: 'document',
    });
    expect(parseInternalLink('/agent/agent-1/topics')).toEqual({
      pathname: '/agent/agent-1/topics',
      type: 'route',
    });
    expect(parseInternalLink('/agent/inbox')).toEqual({
      pathname: '/agent/inbox',
      type: 'route',
    });
    expect(parseInternalLink('https://app.lobehub.com/settings/profile')).toEqual({
      pathname: '/settings/profile',
      type: 'route',
    });
  });

  it('leaves same-origin backend and framework links to the browser', () => {
    expect(parseInternalLink('/api/agent/stream?operationId=op_1')).toBeNull();
    expect(parseInternalLink('/f/generated-file-id')).toBeNull();
    expect(parseInternalLink('https://app.lobehub.com/f/generated-file-id')).toBeNull();
    expect(parseInternalLink('/trpc/lambda')).toBeNull();
    expect(parseInternalLink('/webapi/chat')).toBeNull();
    expect(parseInternalLink('/_next/static/chunks/app.js')).toBeNull();
    expect(parseInternalLink('/favicon.ico')).toBeNull();
    expect(parseInternalLink('/manifest.webmanifest')).toBeNull();
    expect(parseInternalLink('/.well-known/assetlinks.json')).toBeNull();
  });
});

describe('isEntityIdLabel', () => {
  it('treats a goal id label as saying no more than the URL', () => {
    const reference = parseInternalLink('/goal/goal_abc')!;
    expect(isEntityIdLabel('goal_abc', reference)).toBe(true);
    expect(isEntityIdLabel('the fix goal', reference)).toBe(false);
  });
});

describe('isBareLinkLabel', () => {
  it('treats a label that IS the address as bare', () => {
    expect(isBareLinkLabel('/acceptance/acc_1', '/acceptance/acc_1')).toBe(true);
    expect(
      isBareLinkLabel('https://app.lobehub.com/task/tsk_1', 'https://app.lobehub.com/task/tsk_1'),
    ).toBe(true);
  });

  it('treats authored text as not bare, even when it looks like a URL', () => {
    expect(isBareLinkLabel('验收报告', '/acceptance/acc_1')).toBe(false);
    // A URL-shaped authored label must survive: the author chose it.
    expect(isBareLinkLabel('https://docs.example', '/task/T-198')).toBe(false);
    expect(isBareLinkLabel('/project plan', '/task/T-198')).toBe(false);
  });
});