Files
makelore/tests/unit/plugin-navigation.test.tsx

66 lines
2.8 KiB
TypeScript

import { fireEvent, render, screen } from '@testing-library/react';
import { MemoryRouter, Route, Routes, useLocation, useNavigate } from 'react-router-dom';
import { describe, expect, it } from 'vitest';
import {
LegacyPluginRedirect,
PluginWorkspaceRedirect,
PROJECT_SETTINGS_PLUGINS_PATH,
} from '@/pages/Plugins/legacy-plugin-redirect';
import { getGuardedAiModuleForPath, isProgrammingProviderRoute } from '@/lib/ai-modules';
function LocationProbe() {
const location = useLocation();
const navigate = useNavigate();
return (
<div>
<output data-testid="location">{location.pathname}{location.search}</output>
<button type="button" onClick={() => navigate(-1)}></button>
</div>
);
}
describe('plugin navigation cutover', () => {
it.each([
['/plugin-marketplace', `${PROJECT_SETTINGS_PLUGINS_PATH}?scope=all&source=all&state=available`],
['/my-plugins', `${PROJECT_SETTINGS_PLUGINS_PATH}?scope=all&state=mine`],
['/project-plugins', `${PROJECT_SETTINGS_PLUGINS_PATH}?scope=project&state=mine`],
] as const)('replace-redirects %s to %s', async (legacy, canonical) => {
render(
<MemoryRouter initialEntries={['/previous', legacy]} initialIndex={1}>
<Routes>
<Route path={legacy} element={<LegacyPluginRedirect from={legacy} />} />
<Route path={PROJECT_SETTINGS_PLUGINS_PATH} element={<LocationProbe />} />
<Route path="/previous" element={<output data-testid="previous">previous</output>} />
</Routes>
</MemoryRouter>,
);
expect(await screen.findByTestId('location')).toHaveTextContent(canonical);
fireEvent.click(screen.getByRole('button', { name: '返回' }));
expect(await screen.findByTestId('previous')).toBeVisible();
});
it('moves the former canonical URL into project settings without losing its query', async () => {
render(
<MemoryRouter initialEntries={['/previous', '/plugins?scope=all&state=mine']} initialIndex={1}>
<Routes>
<Route path="/plugins" element={<PluginWorkspaceRedirect />} />
<Route path={PROJECT_SETTINGS_PLUGINS_PATH} element={<LocationProbe />} />
<Route path="/previous" element={<output data-testid="previous">previous</output>} />
</Routes>
</MemoryRouter>,
);
expect(await screen.findByTestId('location')).toHaveTextContent(
`${PROJECT_SETTINGS_PLUGINS_PATH}?scope=all&state=mine`,
);
fireEvent.click(screen.getByRole('button', { name: '返回' }));
expect(await screen.findByTestId('previous')).toBeVisible();
});
it('classifies the project-settings plugin page as Code without making it provider-gated', () => {
expect(getGuardedAiModuleForPath(PROJECT_SETTINGS_PLUGINS_PATH)).toBe('programming');
expect(isProgrammingProviderRoute(PROJECT_SETTINGS_PLUGINS_PATH)).toBe(false);
});
});