Files
minstrel/web/src/lib/components/NetworkSettingsCard.test.ts
T
bvandeusen a07fb3867a
test-web / test (push) Successful in 42s
test-go / test (push) Failing after 43s
test-go / integration (push) Failing after 4m22s
fix(net): thread hops into session creation; disambiguate card tests — #2453
Two CI failures from 381e9ced, both mine.

**Go (vet, which cascaded into the integration job).** Widening
auth.ClientIP to take a hop count, I updated the middleware that TOUCHES a
session but missed the two places that CREATE one — handleLogin and
handleRegister. So `created_ip`, the frozen origin address that the whole
"address changed" comparison rests on, was the one value still being
computed the old way. Both now read h.netSettings.Hops(), which is nil-safe
so test handlers constructed without the service still work.

Worth noting the shape of this miss: I checked call sites by searching for
the middleware's own usage and stopped there, rather than for every caller of
the function whose signature I changed. vet found it in seconds; a grep for
`auth.ClientIP(` would have too.

**Web (vitest).** Three tests waited on `findByText('198.51.100.7')`, which
matches TWO elements in the fixture — the detected client address and the
forwarded chain, identical strings for a single-proxy setup — and findByText
throws on multiple matches. Now they wait on the unique "Your address right
now" label and assert the address with getAllByText where duplication is
legitimate. The duplication is correct behaviour, so the test moved rather
than the component.
2026-08-05 10:14:38 -04:00

110 lines
4.1 KiB
TypeScript

import { describe, expect, test, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/svelte';
import NetworkSettingsCard from './NetworkSettingsCard.svelte';
const getNetworkSettings = vi.fn();
const updateNetworkSettings = vi.fn();
vi.mock('$lib/api/admin', () => ({
getNetworkSettings: () => getNetworkSettings(),
updateNetworkSettings: (hops: number) => updateNetworkSettings(hops)
}));
vi.mock('$lib/stores/toast.svelte', () => ({ 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 the operator'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('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');
});
});