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.

5.7 KB
Raw Blame
/**
 * @vitest-environment happy-dom
 */
import { renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { useTaskStore } from '@/store/task';

import { useCanonicalTaskSlug } from './useCanonicalTaskSlug';

const mocks = vi.hoisted(() => ({
  location: { hash: '', search: '' } as { hash: string; search: string; state?: unknown },
  navigate: vi.fn(),
  params: {} as { aid?: string; slug?: string; workspaceSlug?: string },
}));

vi.mock('react-router', () => ({
  useLocation: () => mocks.location,
  useNavigate: () => mocks.navigate,
  useParams: () => mocks.params,
}));

vi.mock('@/business/client/hooks/useActiveWorkspaceSlug', () => ({
  getActiveWorkspaceSlug: () => null,
  useActiveWorkspaceSlug: () => null,
}));

const setTaskDetail = (detail?: { instruction?: string; name?: string | null }) => {
  useTaskStore.setState({
    taskRefIndex: { 'T-1': 'task_1', 'task_1': 'task_1' },
    taskDetailMap:
      detail === undefined
        ? {}
        : ({ task_1: { ...detail, id: 'task_1', identifier: 'T-1' } } as never),
  });
};

const setTaskName = (name?: string) => setTaskDetail(name === undefined ? undefined : { name });

describe('useCanonicalTaskSlug', () => {
  beforeEach(() => {
    mocks.navigate.mockClear();
    mocks.params = {};
    mocks.location = { hash: '', search: '' };
    setTaskName(undefined);
  });

  it('upgrades a bare id link to the slugged path once the title lands', () => {
    setTaskName('Ship the Thing');

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).toHaveBeenCalledWith('/task/T-1/ship-the-thing', { replace: true });
  });

  it('repairs a stale slug left by a rename or a hand-edited link', () => {
    mocks.params = { slug: 'whatever-was-pasted' };
    setTaskName('Ship the Thing');

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).toHaveBeenCalledWith('/task/T-1/ship-the-thing', { replace: true });
  });

  it('keeps the workspace prefix and agent scope of the current URL', () => {
    mocks.params = { aid: 'agt_owner', workspaceSlug: 'lobehub' };
    setTaskName('Ship the Thing');

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).toHaveBeenCalledWith(
      '/lobehub/agent/agt_owner/task/T-1/ship-the-thing',
      {
        replace: true,
      },
    );
  });

  it('keeps the history state, so the task list origin survives a slug repair', () => {
    mocks.params = { slug: 'old-title' };
    mocks.location = {
      hash: '',
      search: '',
      state: { taskListOrigin: '/tasks?collection=mine' },
    };
    setTaskName('Ship the Thing');

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).toHaveBeenCalledWith('/task/T-1/ship-the-thing', {
      replace: true,
      state: { taskListOrigin: '/tasks?collection=mine' },
    });
  });

  it('preserves the query string and hash', () => {
    mocks.location = { hash: '#activity', search: '?tab=runs' };
    setTaskName('Ship the Thing');

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).toHaveBeenCalledWith('/task/T-1/ship-the-thing?tab=runs#activity', {
      replace: true,
    });
  });

  it('drops a slug the title no longer supports', () => {
    mocks.params = { slug: 'ship-the-thing' };
    setTaskName('!!!');

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).toHaveBeenCalledWith('/task/T-1', { replace: true });
  });

  it('collapses the URL once the title is cleared', () => {
    // Regression: an empty title is a resolved state, not an unloaded one —
    // clearing the input persists `name: ''`. Treating the two alike pinned the
    // URL to the slug of the title that had just been deleted.
    mocks.params = { slug: 'ship-the-thing' };
    setTaskName('');

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).toHaveBeenCalledWith('/task/T-1', { replace: true });
  });

  it('leaves a nameless task at its bare identifier instead of slugging the instruction', () => {
    // A task's instruction is a prompt body: putting it in the path would leak
    // it into history, analytics page views and every copied link. A task with
    // no name simply has no slug.
    setTaskDetail({ instruction: 'Fallback to instruction', name: null });

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).not.toHaveBeenCalled();
  });

  it('strips an instruction slug someone hand-edited into the URL', () => {
    mocks.params = { slug: 'fallback-to-instruction' };
    setTaskDetail({ instruction: 'Fallback to instruction', name: null });

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).toHaveBeenCalledWith('/task/T-1', { replace: true });
  });

  it('preserves a stable id URL even when cached label metadata may be stale', () => {
    mocks.params = { slug: 'stale-title', aid: 'agt_owner', workspaceSlug: 'team' };
    mocks.location = { hash: '#activity', search: '?tab=runs' };
    setTaskName('Ship the Thing');
    renderHook(() => useCanonicalTaskSlug('task_1'));
    expect(mocks.navigate).toHaveBeenCalledWith(
      '/team/agent/agt_owner/task/task_1/ship-the-thing?tab=runs#activity',
      { replace: true },
    );
  });

  it('leaves the URL alone while the title is still unknown', () => {
    mocks.params = { slug: 'an-incoming-slug' };

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).not.toHaveBeenCalled();
  });

  it('does not navigate when the slug already matches', () => {
    mocks.params = { slug: 'ship-the-thing' };
    setTaskName('Ship the Thing');

    renderHook(() => useCanonicalTaskSlug('T-1'));

    expect(mocks.navigate).not.toHaveBeenCalled();
  });
});