Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

5.8 KB
Raw Blame
import { renderHook } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { useAssistantStore } from '../../store/assistantStore';
import type { Day } from '../../types';
import { useAssistantDeepLink, useAssistantTabLink } from './useAssistantDeepLink';

const day = (id: number, tripId = 5) => ({ id, trip_id: tripId }) as Day;

function setup(query: string, days: Day[]) {
  const searchParams = new URLSearchParams(query);
  const setSearchParams = vi.fn((update: (params: URLSearchParams) => URLSearchParams) => update(searchParams));
  const selectDay = vi.fn();
  const hook = renderHook(
    ({ list }) =>
      useAssistantDeepLink({
        searchParams,
        setSearchParams: setSearchParams as never,
        tripId: 5,
        days: list,
        selectDay,
      }),
    { initialProps: { list: days } }
  );
  return { searchParams, setSearchParams, selectDay, hook };
}

afterEach(() => {
  useAssistantStore.setState({ window: { open: false, minimized: false } });
});

describe('useAssistantDeepLink', () => {
  it('FE-PLANNER-ASSISTLINK-001: ?assistant=1 opens the assistant and leaves the URL', () => {
    const { searchParams, setSearchParams } = setup('tab=plan&assistant=1', []);
    expect(useAssistantStore.getState().window).toEqual({ open: true, minimized: false });
    expect(searchParams.has('assistant')).toBe(false);
    expect(searchParams.get('tab')).toBe('plan');
    expect(setSearchParams).toHaveBeenCalledWith(expect.any(Function), { replace: true });
  });

  it('FE-PLANNER-ASSISTLINK-002: ?day= selects a day of this trip', () => {
    const { searchParams, selectDay } = setup('day=8', [day(7), day(8)]);
    expect(selectDay).toHaveBeenCalledWith(8);
    expect(searchParams.has('day')).toBe(false);
  });

  it('FE-PLANNER-ASSISTLINK-003: waits for the days of this trip, then drops a day it does not hold', () => {
    const { searchParams, selectDay, hook } = setup('day=8', [day(8, 4)]);
    expect(searchParams.get('day')).toBe('8');
    hook.rerender({ list: [day(7)] });
    expect(selectDay).not.toHaveBeenCalled();
    expect(searchParams.has('day')).toBe(false);
  });

  it('FE-PLANNER-ASSISTLINK-004: does nothing without a link', () => {
    const { setSearchParams, selectDay } = setup('tab=plan&place=3', [day(7)]);
    expect(setSearchParams).not.toHaveBeenCalled();
    expect(selectDay).not.toHaveBeenCalled();
    expect(useAssistantStore.getState().window.open).toBe(false);
  });
});

describe('useAssistantTabLink', () => {
  function setupTab(query: string, activeTab: string) {
    const searchParams = new URLSearchParams(query);
    const setSearchParams = vi.fn((update: (params: URLSearchParams) => URLSearchParams) => update(searchParams));
    const selectTab = vi.fn();
    const onLinked = vi.fn();
    renderHook(() =>
      useAssistantTabLink({ searchParams, setSearchParams: setSearchParams as never, activeTab, selectTab, onLinked })
    );
    return { searchParams, setSearchParams, selectTab, onLinked };
  }

  it('FE-PLANNER-ASSISTLINK-005: a ?tab= the chat opens while the planner is open switches the tab, once', () => {
    const { searchParams, selectTab } = setupTab('tab=finanzplan&expense=7', 'plan');
    expect(selectTab).toHaveBeenCalledWith('finanzplan');
    expect(searchParams.has('tab')).toBe(false);
    // What the tab reads beside it stays for the tab to take.
    expect(searchParams.get('expense')).toBe('7');
  });

  it('FE-PLANNER-ASSISTLINK-006: the tab on screen is not switched again; a tab it cannot open is dropped from the URL', () => {
    const same = setupTab('tab=finanzplan', 'finanzplan');
    expect(same.selectTab).not.toHaveBeenCalled();
    expect(same.searchParams.has('tab')).toBe(false);

    const unknown = setupTab('tab=nowhere&place=3', 'plan');
    expect(unknown.selectTab).not.toHaveBeenCalled();
    expect(unknown.onLinked).not.toHaveBeenCalled();
    expect(unknown.searchParams.has('tab')).toBe(false);
    expect(unknown.searchParams.get('place')).toBe('3');
  });

  it('FE-PLANNER-ASSISTLINK-007: every link it takes is reported, also one to the tab already on screen', () => {
    expect(setupTab('tab=collab', 'plan').onLinked).toHaveBeenCalledWith('collab');
    expect(setupTab('tab=collab', 'collab').onLinked).toHaveBeenCalledWith('collab');
  });

  it('FE-PLANNER-ASSISTLINK-008: a link is taken once while the cleaned URL is still on its way', () => {
    // React Router lands the URL without the link as a transition: until then
    // the hook keeps reading the link, with a new selectTab on every render.
    const searchParams = new URLSearchParams('tab=collab');
    const setSearchParams = vi.fn();
    const onLinked = vi.fn();
    const selects: ReturnType<typeof vi.fn>[] = [];
    const { rerender } = renderHook(() => {
      const selectTab = vi.fn();
      selects.push(selectTab);
      useAssistantTabLink({
        searchParams,
        setSearchParams: setSearchParams as never,
        activeTab: 'plan',
        selectTab,
        onLinked,
      });
    });
    rerender();
    rerender();
    expect(selects.filter((select) => select.mock.calls.length > 0)).toHaveLength(1);
    expect(onLinked).toHaveBeenCalledTimes(1);
    expect(setSearchParams).toHaveBeenCalledTimes(1);
  });

  it('FE-PLANNER-ASSISTLINK-009: the same link in a new URL is taken again', () => {
    const setSearchParams = vi.fn();
    const onLinked = vi.fn();
    const { rerender } = renderHook(
      ({ query }) =>
        useAssistantTabLink({
          searchParams: new URLSearchParams(query),
          setSearchParams: setSearchParams as never,
          activeTab: 'plan',
          selectTab: vi.fn(),
          onLinked,
        }),
      { initialProps: { query: 'tab=collab' } }
    );
    rerender({ query: 'tab=collab' });
    expect(onLinked).toHaveBeenCalledTimes(2);
  });
});