import { useState } from 'react'; import { fireEvent, screen, waitFor, within } from '@testing-library/react'; import { expect, it, vi } from 'vitest'; import NodeFormModal from '@/pages/nodes/NodeFormModal'; import type { NodeRecord, ProbeResult } from '@/schemas/node'; import { Msg } from '@/utils'; import { chooseSelectOption, renderWithProviders } from './test-utils'; const stored: NodeRecord = { id: 11, name: 'Primary', address: 'primary.example.test', port: 2053, status: 'online', enable: true, hasApiToken: true, }; const other: NodeRecord = { ...stored, id: 22, name: 'Secondary', tlsVerifyMode: 'pin' }; const unexpectedValue = 'unrelated-fixture'; const apiTokenLabel = { selector: 'input' }; function setup() { const probe = vi.fn(async (payload: Partial) => payload.apiToken === 'incorrect-fixture' ? new Msg(false, 'Credential rejected') : new Msg(true, '', { status: 'online', latencyMs: 1 }), ); const save = vi.fn(async (_payload: Partial) => new Msg(true)); const fingerprint = vi.fn( async (_payload: Partial) => new Msg(true, '', 'fixture-pin'), ); const inbounds = vi.fn(async (_payload: Partial) => new Msg(true, '', [])); function Page() { const [node, setNode] = useState(null); const [open, setOpen] = useState(false); return ( <> ); } const view = renderWithProviders(); return { ...view, probe, save, fingerprint, inbounds }; } function openForm(name: string) { fireEvent.click(screen.getByRole('button', { name })); return within(screen.getByRole('dialog')); } it('keeps the stored token on first open even if a password input receives an unexpected value', async () => { for (let i = 0; i < 2; i++) { const { probe, save, unmount } = setup(); const dialog = openForm('Edit primary'); const unexpectedInput = dialog.queryByLabelText('API Token', apiTokenLabel); if (unexpectedInput) { fireEvent.change(unexpectedInput, { target: { value: unexpectedValue } }); } fireEvent.click(dialog.getByRole('button', { name: 'Test Connection' })); await waitFor(() => expect(probe).toHaveBeenCalledTimes(1)); expect(probe.mock.calls[0]?.[0]).toMatchObject({ id: 11 }); expect(probe.mock.calls[0]?.[0]).not.toHaveProperty('apiToken'); fireEvent.click(dialog.getByRole('button', { name: 'Save' })); await waitFor(() => expect(save).toHaveBeenCalledTimes(1)); expect(save.mock.calls[0]?.[0]).toMatchObject({ id: 11 }); expect(save.mock.calls[0]?.[0]).not.toHaveProperty('apiToken'); unmount(); } }); it('ignores autofill in replacement mode, probes explicit edits, and clears them across opens', async () => { const { probe, save, fingerprint, inbounds } = setup(); const dialog = openForm('Edit primary'); fireEvent.click(dialog.getByRole('checkbox', { name: 'Replace API Token' })); const input = dialog.getByLabelText('API Token', apiTokenLabel); expect(input.value).toBe(''); fireEvent.input(input, { target: { value: unexpectedValue }, inputType: 'insertReplacementText', }); expect(input.value).toBe(''); fireEvent.click(dialog.getByRole('button', { name: 'Test Connection' })); await waitFor(() => expect(probe).toHaveBeenCalledTimes(1)); expect(probe.mock.calls[0]?.[0]).not.toHaveProperty('apiToken'); fireEvent.input(input, { target: { value: 'incorrect-fixture' }, inputType: 'insertFromPaste', }); fireEvent.click(dialog.getByRole('button', { name: 'Save' })); await dialog.findByText('Credential rejected'); expect(probe.mock.calls[1]?.[0]).toMatchObject({ id: 11, apiToken: 'incorrect-fixture' }); expect(save).not.toHaveBeenCalled(); fireEvent.click(dialog.getByRole('button', { name: 'Cancel' })); const reopened = openForm('Edit primary'); expect(reopened.queryByLabelText('API Token', apiTokenLabel)).toBeNull(); expect(reopened.queryByText('Credential rejected')).toBeNull(); fireEvent.click(reopened.getByRole('button', { name: 'Cancel' })); const secondary = openForm('Edit secondary'); fireEvent.click(secondary.getByRole('button', { name: 'Fetch' })); await waitFor(() => expect(fingerprint).toHaveBeenCalledTimes(1)); expect(fingerprint.mock.calls[0]?.[0]).toMatchObject({ id: 22 }); expect(fingerprint.mock.calls[0]?.[0]).not.toHaveProperty('apiToken'); chooseSelectOption(secondary.getByLabelText('Inbound import').id, 'Selected inbounds'); fireEvent.mouseDown(secondary.getByRole('combobox', { name: 'Inbounds' })); fireEvent.click(screen.getByRole('button', { name: 'Load inbounds from node' })); await waitFor(() => expect(inbounds).toHaveBeenCalledTimes(1)); expect(inbounds.mock.calls[0]?.[0]).toMatchObject({ id: 22 }); expect(inbounds.mock.calls[0]?.[0]).not.toHaveProperty('apiToken'); }); it('updates the probe credential when undoing and redoing a replacement', async () => { const { probe } = setup(); const dialog = openForm('Edit primary'); fireEvent.click(dialog.getByRole('checkbox', { name: 'Replace API Token' })); const input = dialog.getByLabelText('API Token', apiTokenLabel); const replacement = 'replacement-fixture'; fireEvent.input(input, { target: { value: replacement }, inputType: 'insertFromPaste' }); fireEvent.input(input, { target: { value: '' }, inputType: 'historyUndo' }); expect(input.value).toBe(''); fireEvent.click(dialog.getByRole('button', { name: 'Test Connection' })); await waitFor(() => expect(probe).toHaveBeenCalledTimes(1)); expect(probe.mock.calls[0]?.[0]).toMatchObject({ id: 11 }); expect(probe.mock.calls[0]?.[0]).not.toHaveProperty('apiToken'); fireEvent.input(input, { target: { value: replacement }, inputType: 'historyRedo' }); expect(input.value).toBe(replacement); fireEvent.click(dialog.getByRole('button', { name: 'Test Connection' })); await waitFor(() => expect(probe).toHaveBeenCalledTimes(2)); expect(probe.mock.calls[1]?.[0]).toMatchObject({ id: 11, apiToken: replacement }); }); it('requires an entered token for ordinary additions and allows mTLS additions without one', async () => { const { probe, save } = setup(); const editing = openForm('Edit primary'); fireEvent.click(editing.getByRole('checkbox', { name: 'Replace API Token' })); fireEvent.input(editing.getByLabelText('API Token', apiTokenLabel), { target: { value: 'explicit-fixture' }, inputType: 'insertFromPaste', }); fireEvent.click(editing.getByRole('button', { name: 'Save' })); await waitFor(() => expect(save).toHaveBeenCalledTimes(1)); expect(save.mock.calls[0]?.[0]).toMatchObject({ id: 11, apiToken: 'explicit-fixture' }); const adding = openForm('Add node'); const token = adding.getByLabelText('API Token', apiTokenLabel); expect(token.value).toBe(''); fireEvent.change(adding.getByLabelText('Name'), { target: { value: 'New node' } }); fireEvent.change(adding.getByLabelText('Address'), { target: { value: 'new.example.test' } }); fireEvent.change(token, { target: { value: unexpectedValue } }); fireEvent.click(adding.getByRole('button', { name: 'Save' })); await screen.findByText('Name, address, port and API token are required'); expect(probe).toHaveBeenCalledTimes(1); chooseSelectOption( adding.getByLabelText('TLS verification').id, 'Mutual TLS (client certificate)', ); fireEvent.click(adding.getByRole('button', { name: 'Save' })); await waitFor(() => expect(save).toHaveBeenCalledTimes(2)); expect(save.mock.calls[1]?.[0]).toMatchObject({ id: 0, tlsVerifyMode: 'mtls' }); expect(save.mock.calls[1]?.[0]).not.toHaveProperty('apiToken'); });