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.

12.0 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { act, fireEvent, render, screen, waitFor } from '../../../tests/helpers/render';
import { ToastContainer } from '../../components/shared/Toast';
import { saveSharedLink, useSharedImportStore } from '../../native/sharedFiles';
import { tripAddonsRepo } from '../../repo/tripAddonsRepo';
import { tripRepo } from '../../repo/tripRepo';
import { useAddonStore } from '../../store/addonStore';
import { useTripAddonsStore } from '../../store/tripAddonsStore';
import NativeImportSheet from './NativeImportSheet';

const navigate = vi.hoisted(() => vi.fn());
vi.mock('../../native/sharedFiles', async (importOriginal) => ({
  ...(await importOriginal<typeof import('../../native/sharedFiles')>()),
  saveSharedLink: vi.fn(),
}));
vi.mock('react-router', async (importOriginal) => ({
  ...(await importOriginal<typeof import('react-router')>()),
  useNavigate: () => navigate,
}));

beforeEach(() => {
  // A second spyOn returns the first spy with its calls; start every test clean.
  vi.restoreAllMocks();
  navigate.mockReset();
  useSharedImportStore.setState({ file: null, tripId: null, link: null, placeLink: null });
  useAddonStore.setState({ addons: [{ id: 'collab', enabled: true }] as never, loaded: true });
  useTripAddonsStore.setState({ trips: {} });
  // Every trip follows the instance switch unless a test says otherwise; answered at once, so no read outlives its test.
  vi.spyOn(tripAddonsRepo, 'list').mockResolvedValue([]);
  vi.mocked(saveSharedLink).mockReset().mockResolvedValue();
  vi.spyOn(tripRepo, 'list').mockResolvedValue({
    trips: [
      { id: 1, title: 'Past trip', start_date: '2020-01-01', end_date: '2020-01-05' },
      { id: 2, title: 'Japan', start_date: '2099-03-01', end_date: '2099-03-10' },
    ],
    archivedTrips: [],
  } as unknown as Awaited<ReturnType<typeof tripRepo.list>>);
});

const page = () => new File(['<html></html>'], 'hotel.html', { type: 'text/html' });
const bareLink = () => new File(['https://hotel.example/stay'], 'link.url', { type: 'text/uri-list' });
/** Trip 2's own Collab choice; trip 1 follows the instance. */
const collabOnlyFor2 = (enabled: boolean) =>
  vi
    .spyOn(tripAddonsRepo, 'list')
    .mockImplementation(async (tripId: number) =>
      tripId === 2
        ? [{ id: 'collab', name: 'Collab', icon: null, enabled, instanceEnabled: !enabled, overridden: true }]
        : []
    );
const link = (presetTripId: number | null = null) => ({
  url: 'https://hotel.example/stay',
  title: 'Hotel Kanra',
  presetTripId,
});

describe('NativeImportSheet', () => {
  it('asks which trip a shared document goes to, upcoming trips first, and opens it there', async () => {
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(new File(['x'], 'booking.pdf', { type: 'application/pdf' })));

    const rows = await screen.findAllByRole('button', { name: /Japan|Past trip/ });
    expect(rows.map((r) => r.textContent)).toEqual(['Japan', 'Past trip']);
    expect(screen.getByText('booking.pdf')).toBeInTheDocument();

    fireEvent.click(rows[0]);
    expect(useSharedImportStore.getState().tripId).toBe(2);
    expect(navigate).toHaveBeenCalledWith('/trips/2');
  });

  it('goes straight to the trip the share sheet picked, without asking', () => {
    render(<NativeImportSheet />);
    act(() => {
      useSharedImportStore.getState().setFile(new File(['x'], 'booking.pdf', { type: 'application/pdf' }));
      useSharedImportStore.getState().chooseTrip(2);
    });
    expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
    expect(navigate).toHaveBeenCalledWith('/trips/2');
    expect(navigate).toHaveBeenCalledTimes(1);
  });

  it('asks what a shared page is for, and keeps it among the links of the trip picked', async () => {
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(page(), link()));
    expect(await screen.findByText('What should TREK do with this link?')).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /Japan/ })).not.toBeInTheDocument();

    fireEvent.click(screen.getByRole('button', { name: /Save to the trip's links/ }));
    expect(screen.getByText('Save the link to which trip?')).toBeInTheDocument();
    fireEvent.click(await screen.findByRole('button', { name: /Japan/ }));
    await waitFor(() => expect(navigate).toHaveBeenCalledWith('/trips/2?tab=collab'));
    expect(saveSharedLink).toHaveBeenCalledWith(2, link());
    expect(useSharedImportStore.getState().file).toBeNull();
  });

  it('reads a booking from the page when that is asked for', async () => {
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(page(), link()));
    fireEvent.click(await screen.findByRole('button', { name: /Read a booking from it/ }));
    fireEvent.click(await screen.findByRole('button', { name: /Japan/ }));
    expect(useSharedImportStore.getState().tripId).toBe(2);
    expect(navigate).toHaveBeenCalledWith('/trips/2');
    expect(saveSharedLink).not.toHaveBeenCalled();
  });

  it('keeps a bare link in the trip the share sheet picked, with nothing to ask', async () => {
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(bareLink(), link(2)));
    await waitFor(() => expect(saveSharedLink).toHaveBeenCalledWith(2, link(2)));
    await waitFor(() => expect(navigate).toHaveBeenCalledWith('/trips/2?tab=collab'));
  });

  it('with Collab off, a page is read as a booking and a bare link is explained', async () => {
    useAddonStore.setState({ addons: [], loaded: true });
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(page(), link()));
    expect(screen.queryByText('What should TREK do with this link?')).not.toBeInTheDocument();
    expect(await screen.findAllByRole('button', { name: /Japan|Past trip/ })).toHaveLength(2);

    act(() => useSharedImportStore.getState().setFile(bareLink(), link()));
    expect(screen.getByText('Turn on Collab to keep links in a trip.')).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /Japan/ })).not.toBeInTheDocument();
  });

  it('says so when the link cannot be saved, and offers the trips again', async () => {
    vi.mocked(saveSharedLink).mockRejectedValue(new Error('offline'));
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(bareLink(), link(2)));
    expect(await screen.findByRole('button', { name: /Japan/ })).toBeEnabled();
    expect(navigate).not.toHaveBeenCalled();
    expect(useSharedImportStore.getState().file).not.toBeNull();
  });

  it('keeps a link in a trip that switched Collab on while the instance has it off, offering only that trip', async () => {
    useAddonStore.setState({ addons: [], loaded: true });
    collabOnlyFor2(true);
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(page(), link()));

    fireEvent.click(await screen.findByRole('button', { name: /Save to the trip's links/ }));
    expect(await screen.findByRole('button', { name: /Japan/ })).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /Past trip/ })).not.toBeInTheDocument();
  });

  it('leaves out a trip that switched Collab off, and shows why when the server refuses the link', async () => {
    collabOnlyFor2(false);
    vi.mocked(saveSharedLink).mockRejectedValue({ response: { data: { error: 'Collab addon is not enabled' } } });
    render(
      <>
        <ToastContainer />
        <NativeImportSheet />
      </>
    );
    act(() => useSharedImportStore.getState().setFile(bareLink(), link(2)));

    expect(await screen.findByText('Collab addon is not enabled')).toBeInTheDocument();
    expect(await screen.findByRole('button', { name: /Past trip/ })).toBeInTheDocument();
    await waitFor(() => expect(screen.queryByRole('button', { name: /Japan/ })).not.toBeInTheDocument());
  });

  it('waits for the trips’ own Collab before a page shared into a trip is read as a booking', async () => {
    useAddonStore.setState({ addons: [], loaded: true });
    let answer: () => void = () => {};
    vi.spyOn(tripAddonsRepo, 'list').mockImplementation(
      (tripId: number) =>
        new Promise((resolve) => {
          const own = [
            { id: 'collab', name: 'Collab', icon: null, enabled: true, instanceEnabled: false, overridden: true },
          ];
          if (tripId === 2) answer = () => resolve(own);
          else resolve([]);
        })
    );
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(page(), link(2)));
    await waitFor(() => expect(tripAddonsRepo.list).toHaveBeenCalledWith(2));
    expect(useSharedImportStore.getState().tripId).toBeNull();
    expect(screen.queryByRole('button', { name: /Japan|Past trip/ })).not.toBeInTheDocument();

    act(() => answer());
    expect(await screen.findByText('What should TREK do with this link?')).toBeInTheDocument();
    expect(useSharedImportStore.getState().tripId).toBeNull();
  });

  it('reads every trip’s Collab again when it opens, so a switch this device missed counts', async () => {
    useAddonStore.setState({ addons: [], loaded: true });
    // The app still holds trip 2 as following the instance; its owner has since switched Collab on.
    useTripAddonsStore.getState().publish(2, []);
    collabOnlyFor2(true);
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(bareLink(), link()));
    expect(await screen.findByRole('button', { name: /Japan/ })).toBeInTheDocument();
    expect(tripAddonsRepo.list).toHaveBeenCalledWith(1);
    expect(tripAddonsRepo.list).toHaveBeenCalledWith(2);
  });

  it('keeps the trips and does not save again when a list reloads after a refused link', async () => {
    vi.mocked(saveSharedLink).mockRejectedValue(new Error('offline'));
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(bareLink(), link(2)));
    expect(await screen.findByRole('button', { name: /Japan/ })).toBeInTheDocument();
    expect(saveSharedLink).toHaveBeenCalledTimes(1);

    let answer: () => void = () => {};
    vi.spyOn(tripAddonsRepo, 'list').mockImplementation(
      () =>
        new Promise((resolve) => {
          answer = () => resolve([]);
        })
    );
    await act(async () => {
      void useTripAddonsStore.getState().load(1);
    });
    expect(screen.getByRole('button', { name: /Japan/ })).toBeInTheDocument();
    await act(async () => answer());
    expect(screen.getByRole('button', { name: /Japan/ })).toBeInTheDocument();
    expect(saveSharedLink).toHaveBeenCalledTimes(1);
  });

  it('offers a shared map link as a place first, and hands it to the trip picked (#2582)', async () => {
    const maps = {
      url: 'https://www.google.com/maps/place/Time+Out+Market',
      title: 'Time Out Market',
      presetTripId: null,
    };
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(page(), maps));
    const choices = await screen.findAllByRole('button', {
      name: /Add it as a place|Save to the trip's links|Read a booking/,
    });
    expect(choices[0]).toHaveAccessibleName(/Add it as a place/);

    fireEvent.click(choices[0]);
    expect(screen.getByText('Add the place to which trip?')).toBeInTheDocument();
    fireEvent.click(await screen.findByRole('button', { name: /Japan/ }));
    expect(useSharedImportStore.getState().placeLink).toEqual({ tripId: 2, url: maps.url });
    expect(useSharedImportStore.getState().file).toBeNull();
    expect(navigate).toHaveBeenCalledWith('/trips/2');
    expect(saveSharedLink).not.toHaveBeenCalled();
  });

  it('offers no place for a link that is not a map', async () => {
    render(<NativeImportSheet />);
    act(() => useSharedImportStore.getState().setFile(page(), link()));
    await screen.findByText('What should TREK do with this link?');
    expect(screen.queryByRole('button', { name: /Add it as a place/ })).not.toBeInTheDocument();
  });

  it('stays closed with nothing shared', () => {
    render(<NativeImportSheet />);
    expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
    expect(tripRepo.list).not.toHaveBeenCalled();
  });
});