Five settings cards (Fingerprinting, Loudness, Public address, Proxies, Notification email) each hand-rolled the same cycle: load into a stored copy and an editable one, say when they differ, save, adopt what the server stored, toast. lib/settings/settingsForm.svelte.ts now runs it once (createSettingsForm), and SettingsLoadStatus draws the "Loading…" / "Couldn't load … Try again" line for those five and for Library folders and AcoustID. The Proxies card dropped the server's reason when a save was refused and toasted a fixed "Couldn't save proxy depth."; it now says why, as the other cards do. Two cards' "Loading <thing>…" lines now read "Loading…" like the rest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
4.9 KiB
TypeScript
131 lines
4.9 KiB
TypeScript
import { describe, expect, test, vi, beforeEach } from 'vitest';
|
|
import { render, screen, fireEvent, waitFor } from '@testing-library/svelte';
|
|
import NetworkSettingsCard from './NetworkSettingsCard.svelte';
|
|
import { pushToast } from '#lib/stores/toast.svelte.js';
|
|
|
|
const getNetworkSettings = vi.fn();
|
|
const updateNetworkSettings = vi.fn();
|
|
|
|
vi.mock('#lib/api/admin.js', () => ({
|
|
getNetworkSettings: () => getNetworkSettings(),
|
|
updateNetworkSettings: (hops: number) => updateNetworkSettings(hops)
|
|
}));
|
|
|
|
vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() }));
|
|
|
|
function settings(over: Record<string, unknown> = {}) {
|
|
return {
|
|
trusted_proxy_hops: 1,
|
|
max_hops: 10,
|
|
detected_client_ip: '198.51.100.7',
|
|
forwarded_chain: '198.51.100.7',
|
|
remote_addr: '172.18.0.1:40000',
|
|
...over
|
|
};
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe('NetworkSettingsCard', () => {
|
|
// The detected address is the card's verification affordance — the number
|
|
// is abstract, this is checkable against the machine you're sitting at.
|
|
test('shows the address the current setting resolves to', async () => {
|
|
getNetworkSettings.mockResolvedValue(settings());
|
|
render(NetworkSettingsCard);
|
|
|
|
// The address legitimately appears twice — as the detected client and
|
|
// inside the forwarded chain — so wait on the unique label, not the value.
|
|
await screen.findByText('Your address right now');
|
|
expect(screen.getAllByText('198.51.100.7').length).toBeGreaterThan(0);
|
|
expect(screen.getByText('172.18.0.1:40000')).toBeTruthy();
|
|
});
|
|
|
|
test('save is inert until the value actually changes', async () => {
|
|
getNetworkSettings.mockResolvedValue(settings({ trusted_proxy_hops: 1 }));
|
|
render(NetworkSettingsCard);
|
|
|
|
const save = await screen.findByRole('button', { name: /Save/ });
|
|
expect(save).toBeDisabled();
|
|
|
|
const input = screen.getByRole('spinbutton');
|
|
await fireEvent.input(input, { target: { value: '2' } });
|
|
await waitFor(() => expect(save).not.toBeDisabled());
|
|
});
|
|
|
|
test('saving sends the new depth and adopts the echoed value', async () => {
|
|
getNetworkSettings.mockResolvedValue(settings({ trusted_proxy_hops: 1 }));
|
|
updateNetworkSettings.mockResolvedValue(
|
|
settings({ trusted_proxy_hops: 2, detected_client_ip: '203.0.113.9' })
|
|
);
|
|
render(NetworkSettingsCard);
|
|
|
|
const input = await screen.findByRole('spinbutton');
|
|
await fireEvent.input(input, { target: { value: '2' } });
|
|
await fireEvent.click(screen.getByRole('button', { name: /Save/ }));
|
|
|
|
await waitFor(() => expect(updateNetworkSettings).toHaveBeenCalledWith(2));
|
|
// The recomputed address proves the change took effect on this request.
|
|
expect(await screen.findByText('203.0.113.9')).toBeTruthy();
|
|
});
|
|
|
|
// Counting proxies is an admin's job and the hint is how they do it
|
|
// without guessing.
|
|
test('hints the likely depth when it disagrees with the arriving chain', async () => {
|
|
getNetworkSettings.mockResolvedValue(
|
|
settings({ trusted_proxy_hops: 1, forwarded_chain: '198.51.100.7, 203.0.113.50' })
|
|
);
|
|
render(NetworkSettingsCard);
|
|
|
|
expect(await screen.findByText(/arrived with 2 forwarded addresses/)).toBeTruthy();
|
|
});
|
|
|
|
test('no hint when the setting already matches the chain length', async () => {
|
|
getNetworkSettings.mockResolvedValue(
|
|
settings({ trusted_proxy_hops: 1, forwarded_chain: '198.51.100.7' })
|
|
);
|
|
render(NetworkSettingsCard);
|
|
|
|
await screen.findByText('Your address right now');
|
|
expect(screen.queryByText(/arrived with/)).toBeNull();
|
|
});
|
|
|
|
test('states the mis-set risk rather than only exposing a number', async () => {
|
|
getNetworkSettings.mockResolvedValue(settings());
|
|
render(NetworkSettingsCard);
|
|
|
|
expect(await screen.findByText(/Count your proxies/)).toBeTruthy();
|
|
});
|
|
|
|
test('a refused save says why, in the server\'s words', async () => {
|
|
getNetworkSettings.mockResolvedValue(settings());
|
|
updateNetworkSettings.mockRejectedValue({
|
|
code: 'invalid_setting',
|
|
message: 'trusted proxy hops must be from 0 to 10'
|
|
});
|
|
render(NetworkSettingsCard);
|
|
|
|
const input = await screen.findByRole('spinbutton');
|
|
await fireEvent.input(input, { target: { value: '2' } });
|
|
const save = screen.getByRole('button', { name: /Save/ });
|
|
await waitFor(() => expect(save).not.toBeDisabled());
|
|
await fireEvent.click(save);
|
|
|
|
await waitFor(() => expect(pushToast).toHaveBeenCalled());
|
|
const [text, kind] = vi.mocked(pushToast).mock.calls[0];
|
|
expect(kind).toBe('error');
|
|
expect(text).toContain('trusted proxy hops must be from 0 to 10');
|
|
});
|
|
|
|
test('offers a retry when loading fails', async () => {
|
|
getNetworkSettings.mockRejectedValue(new Error('boom'));
|
|
render(NetworkSettingsCard);
|
|
|
|
const retry = await screen.findByRole('button', { name: 'Try again' });
|
|
getNetworkSettings.mockResolvedValue(settings());
|
|
await fireEvent.click(retry);
|
|
await screen.findByText('Your address right now');
|
|
});
|
|
});
|