| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- /**
- * Tests for the provider selection UI in GitHubBackupSettings.
- */
- import { describe, it, expect, vi, beforeEach } from 'vitest';
- import { fireEvent, screen, waitFor } from '@testing-library/react';
- import { render } from '../utils';
- import { GitHubBackupSettings } from '../../components/GitHubBackupSettings';
- import { http, HttpResponse } from 'msw';
- import { server } from '../mocks/server';
- const baseHandlers = () => [
- http.get('/api/v1/github-backup/config', () => HttpResponse.json(null)),
- http.get('/api/v1/github-backup/status', () =>
- HttpResponse.json({
- configured: false,
- enabled: false,
- is_running: false,
- progress: null,
- last_backup_at: null,
- last_backup_status: null,
- next_scheduled_run: null,
- })
- ),
- http.get('/api/v1/github-backup/logs', () => HttpResponse.json([])),
- http.get('/api/v1/local-backup/status', () =>
- HttpResponse.json({
- enabled: false,
- schedule: 'daily',
- time: '03:00',
- retention: 5,
- path: '',
- default_path: '/data/backups',
- is_running: false,
- last_backup_at: null,
- last_status: null,
- last_message: null,
- next_run: null,
- })
- ),
- http.get('/api/v1/local-backup/backups', () => HttpResponse.json([])),
- http.get('/api/v1/cloud/status', () => HttpResponse.json({ is_authenticated: false })),
- http.get('/api/v1/printers', () => HttpResponse.json([])),
- http.put('/api/v1/settings/', () => HttpResponse.json({})),
- ];
- describe('GitHubBackupSettings - Provider Selection', () => {
- beforeEach(() => {
- vi.clearAllMocks();
- server.use(...baseHandlers());
- });
- it('renders provider dropdown with GitHub selected by default', async () => {
- render(<GitHubBackupSettings />);
- await waitFor(() => {
- expect(screen.getByText('Git Provider')).toBeInTheDocument();
- });
- const select = screen.getByRole('combobox', { name: /git provider/i });
- expect(select).toHaveValue('github');
- });
- it('does not show instance URL field for any provider', async () => {
- render(<GitHubBackupSettings />);
- await waitFor(() => {
- expect(screen.getByText('Git Provider')).toBeInTheDocument();
- });
- expect(screen.queryByText('Instance URL')).not.toBeInTheDocument();
- });
- it('loads provider from existing config', async () => {
- server.use(
- http.get('/api/v1/github-backup/config', () =>
- HttpResponse.json({
- id: 1,
- repository_url: 'https://git.example.com/owner/repo',
- has_token: true,
- branch: 'main',
- provider: 'gitea',
- schedule_enabled: false,
- schedule_type: 'daily',
- backup_kprofiles: true,
- backup_cloud_profiles: true,
- backup_settings: false,
- backup_spools: false,
- backup_archives: false,
- enabled: true,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- })
- )
- );
- render(<GitHubBackupSettings />);
- await waitFor(() => {
- const select = screen.getByRole('combobox', { name: /git provider/i });
- expect(select).toHaveValue('gitea');
- });
- });
- it('renders Forgejo as a separate dropdown option', async () => {
- render(<GitHubBackupSettings />);
- await waitFor(() => {
- expect(screen.getByRole('combobox', { name: /git provider/i })).toBeInTheDocument();
- });
- const select = screen.getByRole('combobox', { name: /git provider/i });
- const options = Array.from((select as HTMLSelectElement).options).map((o) => o.value);
- expect(options).toContain('gitea');
- expect(options).toContain('forgejo');
- });
- it('loads forgejo provider from existing config', async () => {
- server.use(
- http.get('/api/v1/github-backup/config', () =>
- HttpResponse.json({
- id: 2,
- repository_url: 'https://forgejo.example.com/owner/repo',
- has_token: true,
- branch: 'main',
- provider: 'forgejo',
- schedule_enabled: false,
- schedule_type: 'daily',
- backup_kprofiles: true,
- backup_cloud_profiles: true,
- backup_settings: false,
- backup_spools: false,
- backup_archives: false,
- enabled: true,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- })
- )
- );
- render(<GitHubBackupSettings />);
- await waitFor(() => {
- const select = screen.getByRole('combobox', { name: /git provider/i });
- expect(select).toHaveValue('forgejo');
- });
- });
- it('autosaves existing config changes after debounce', async () => {
- let patchBody: Record<string, unknown> | null = null;
- server.use(
- http.get('/api/v1/github-backup/config', () =>
- HttpResponse.json({
- id: 3,
- repository_url: 'https://git.example.com/owner/repo',
- has_token: true,
- branch: 'main',
- provider: 'gitea',
- allow_insecure_http: false,
- schedule_enabled: false,
- schedule_type: 'daily',
- backup_kprofiles: true,
- backup_cloud_profiles: true,
- backup_settings: false,
- backup_spools: false,
- backup_archives: false,
- enabled: true,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- })
- ),
- http.patch('/api/v1/github-backup/config', async ({ request }) => {
- patchBody = await request.json() as Record<string, unknown>;
- return HttpResponse.json({
- id: 3,
- repository_url: patchBody.repository_url,
- has_token: true,
- branch: patchBody.branch,
- provider: patchBody.provider,
- allow_insecure_http: patchBody.allow_insecure_http,
- schedule_enabled: patchBody.schedule_enabled,
- schedule_type: patchBody.schedule_type,
- backup_kprofiles: patchBody.backup_kprofiles,
- backup_cloud_profiles: patchBody.backup_cloud_profiles,
- backup_settings: patchBody.backup_settings,
- backup_spools: patchBody.backup_spools,
- backup_archives: patchBody.backup_archives,
- enabled: patchBody.enabled,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- });
- })
- );
- render(<GitHubBackupSettings />);
- const branchInput = await screen.findByDisplayValue('main');
- await waitFor(() => expect(branchInput).toHaveValue('main'));
- fireEvent.change(branchInput, { target: { value: 'dev' } });
- await waitFor(() => {
- expect(patchBody).toEqual({ branch: 'dev' });
- }, { timeout: 2000 });
- });
- it('autosaves provider changes after debounce', async () => {
- let patchBody: Record<string, unknown> | null = null;
- server.use(
- http.get('/api/v1/github-backup/config', () =>
- HttpResponse.json({
- id: 4,
- repository_url: 'https://git.example.com/owner/repo',
- has_token: true,
- branch: 'main',
- provider: 'gitea',
- allow_insecure_http: false,
- schedule_enabled: false,
- schedule_type: 'daily',
- backup_kprofiles: true,
- backup_cloud_profiles: true,
- backup_settings: false,
- backup_spools: false,
- backup_archives: false,
- enabled: true,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- })
- ),
- http.patch('/api/v1/github-backup/config', async ({ request }) => {
- patchBody = await request.json() as Record<string, unknown>;
- return HttpResponse.json({
- id: 4,
- repository_url: patchBody.repository_url,
- has_token: true,
- branch: patchBody.branch,
- provider: patchBody.provider,
- allow_insecure_http: patchBody.allow_insecure_http,
- schedule_enabled: patchBody.schedule_enabled,
- schedule_type: patchBody.schedule_type,
- backup_kprofiles: patchBody.backup_kprofiles,
- backup_cloud_profiles: patchBody.backup_cloud_profiles,
- backup_settings: patchBody.backup_settings,
- backup_spools: patchBody.backup_spools,
- backup_archives: patchBody.backup_archives,
- enabled: patchBody.enabled,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- });
- })
- );
- render(<GitHubBackupSettings />);
- const providerSelect = await screen.findByRole('combobox', { name: /git provider/i });
- await waitFor(() => expect(providerSelect).toHaveValue('gitea'));
- fireEvent.change(providerSelect, { target: { value: 'forgejo' } });
- await waitFor(() => {
- expect(patchBody).toEqual({ provider: 'forgejo' });
- }, { timeout: 2000 });
- });
- it('autosaves repository URL changes after debounce', async () => {
- let patchBody: Record<string, unknown> | null = null;
- server.use(
- http.get('/api/v1/github-backup/config', () =>
- HttpResponse.json({
- id: 5,
- repository_url: 'https://git.example.com/owner/repo',
- has_token: true,
- branch: 'main',
- provider: 'gitea',
- allow_insecure_http: false,
- schedule_enabled: false,
- schedule_type: 'daily',
- backup_kprofiles: true,
- backup_cloud_profiles: true,
- backup_settings: false,
- backup_spools: false,
- backup_archives: false,
- enabled: true,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- })
- ),
- http.patch('/api/v1/github-backup/config', async ({ request }) => {
- patchBody = await request.json() as Record<string, unknown>;
- return HttpResponse.json({
- id: 5,
- repository_url: patchBody.repository_url,
- has_token: true,
- branch: patchBody.branch,
- provider: patchBody.provider,
- allow_insecure_http: patchBody.allow_insecure_http,
- schedule_enabled: patchBody.schedule_enabled,
- schedule_type: patchBody.schedule_type,
- backup_kprofiles: patchBody.backup_kprofiles,
- backup_cloud_profiles: patchBody.backup_cloud_profiles,
- backup_settings: patchBody.backup_settings,
- backup_spools: patchBody.backup_spools,
- backup_archives: patchBody.backup_archives,
- enabled: patchBody.enabled,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- });
- })
- );
- render(<GitHubBackupSettings />);
- const repoInput = await screen.findByDisplayValue('https://git.example.com/owner/repo');
- fireEvent.change(repoInput, { target: { value: 'https://git.example.com/owner/other-repo' } });
- await waitFor(() => {
- expect(patchBody).toEqual({
- repository_url: 'https://git.example.com/owner/other-repo',
- });
- }, { timeout: 2000 });
- });
- it('does not let pending token autosave cancel provider settings autosave', async () => {
- let patchBody: Record<string, unknown> | null = null;
- let postBody: Record<string, unknown> | null = null;
- server.use(
- http.get('/api/v1/github-backup/config', () =>
- HttpResponse.json({
- id: 6,
- repository_url: 'http://git.example.com/owner/repo',
- has_token: true,
- branch: 'main',
- provider: 'gitea',
- allow_insecure_http: true,
- schedule_enabled: false,
- schedule_type: 'daily',
- backup_kprofiles: true,
- backup_cloud_profiles: true,
- backup_settings: false,
- backup_spools: false,
- backup_archives: false,
- enabled: true,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- })
- ),
- http.patch('/api/v1/github-backup/config', async ({ request }) => {
- patchBody = await request.json() as Record<string, unknown>;
- return HttpResponse.json({
- id: 6,
- repository_url: 'http://git.example.com/owner/repo',
- has_token: true,
- branch: 'main',
- provider: patchBody.provider ?? 'gitea',
- allow_insecure_http: true,
- schedule_enabled: false,
- schedule_type: 'daily',
- backup_kprofiles: true,
- backup_cloud_profiles: true,
- backup_settings: false,
- backup_spools: false,
- backup_archives: false,
- enabled: true,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- });
- }),
- http.post('/api/v1/github-backup/config', async ({ request }) => {
- postBody = await request.json() as Record<string, unknown>;
- return HttpResponse.json({
- id: 6,
- repository_url: postBody.repository_url,
- has_token: true,
- branch: postBody.branch,
- provider: postBody.provider,
- allow_insecure_http: postBody.allow_insecure_http,
- schedule_enabled: postBody.schedule_enabled,
- schedule_type: postBody.schedule_type,
- backup_kprofiles: postBody.backup_kprofiles,
- backup_cloud_profiles: postBody.backup_cloud_profiles,
- backup_settings: postBody.backup_settings,
- backup_spools: postBody.backup_spools,
- backup_archives: postBody.backup_archives,
- enabled: postBody.enabled,
- last_backup_at: null,
- last_backup_status: null,
- last_backup_message: null,
- last_backup_commit_sha: null,
- next_scheduled_run: null,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- });
- })
- );
- render(<GitHubBackupSettings />);
- const tokenInput = await screen.findByPlaceholderText('Enter new token to update');
- fireEvent.change(tokenInput, { target: { value: 'new-token' } });
- const providerSelect = await screen.findByRole('combobox', { name: /git provider/i });
- fireEvent.change(providerSelect, { target: { value: 'forgejo' } });
- await waitFor(() => {
- expect(patchBody).toEqual({ provider: 'forgejo' });
- }, { timeout: 2000 });
- });
- });
|