Files
doona/src/features/shared/link.test.ts
T
Zakk aec2ebae90 feat(rules): prefill the trace from a link and trace a connection
The trace tab reads its target, port, network and source IP from the
address, so a link fills the form in; the person still runs it. The
connection detail opens it for the shown connection.
2026-09-29 07:53:04 +10:00

73 lines
3.9 KiB
TypeScript

import {expect, it} from 'vitest';
import type {ConfigSource} from '../../api/model';
import {groupQuery, nodeHref, parseRuleSeed, parseTraceLink, ruleSeedHref, sectionSourceHref, traceQuery} from './link';
it('round trips IPv6 and reserved URL characters without reinterpreting the condition kind', () => {
for (const seed of [
{kind: 'dip' as const, value: '2001:db8::1'},
{kind: 'domainSuffix' as const, value: 'a+b&c.example'}
]) {
const query = ruleSeedHref(seed).split('?')[1];
expect(parseRuleSeed(new URLSearchParams(query).get('add'))).toEqual(seed);
}
expect(parseRuleSeed('unknown:value')).toBeNull();
expect(parseRuleSeed('domainSuffix')).toBeNull();
expect(parseRuleSeed(null)).toBeNull();
});
it('seeds a DNS request rule on the DNS tab, readable only with the DNS condition kinds', () => {
const href = ruleSeedHref({kind: 'qnameSuffix', value: 'example.com'}, 'dns');
const params = new URLSearchParams(href.split('?')[1]);
expect(params.get('tab')).toBe('dns');
expect(parseRuleSeed(params.get('add'), ['qnameSuffix', 'qnameFull'])).toEqual({kind: 'qnameSuffix', value: 'example.com'});
expect(parseRuleSeed(params.get('add'))).toBeNull();
});
const source = (id: string, kind: ConfigSource['kind'], content: string | undefined): ConfigSource => ({
id,
kind,
path: `/etc/honk/${id}.dae`,
content,
writable: true,
content_sha256: '',
bytes: content?.length ?? 0,
line_count: content?.split('\n').length ?? 0,
loaded_at: ''
});
it('opens the first line of a section wherever an authored file holds it, else the main file', () => {
const query = (link: string) => Object.fromEntries(new URLSearchParams(link.split('?')[1]));
const main = source('main', 'main', 'global {}\nrouting {\n dns {}\n}\n');
const include = source('dns', 'include', '# upstreams\n\ndns {\n upstream {}\n}\n');
expect(query(sectionSourceHref([main, include], 'dns'))).toEqual({tab: 'source', source: 'dns', line: '3'});
expect(query(sectionSourceHref([main, include], 'routing'))).toEqual({tab: 'source', source: 'main', line: '2'});
// A nested block of the same name is not the section; a generated source and withheld text are not searched.
const generated = source('gen', 'generated', 'dns {}\n');
const withheld = source('secret', 'include', undefined);
expect(query(sectionSourceHref([generated, withheld, main], 'dns'))).toEqual({tab: 'source', source: 'main'});
expect(query(sectionSourceHref([], 'dns'))).toEqual({tab: 'source'});
});
it('opens a node under the owner the nodes page files it under', () => {
expect(nodeHref({provider_id: 'sub', protocol: 'vmess', name: 'hk 01'}, [])).toBe('#/nodes?provider=sub&q=hk+01');
// A built-in node has no provider; the page's stand-in owner steps past a provider that took its id.
expect(nodeHref({provider_id: null, protocol: 'direct', name: 'direct'}, [{id: 'builtin'}])).toBe('#/nodes?provider=builtin-&q=direct');
});
it('focuses a group by its backend id, and opens the page unfocused before the list holds it', () => {
const groups = [{id: 'group-proxy', name: 'proxy'}];
expect(groupQuery(groups, 'proxy')).toBe('group=group-proxy');
expect(groupQuery(groups, 'new')).toBe('');
expect(groupQuery(undefined, 'proxy')).toBe('');
});
it('carries a trace target and source through the address and back', () => {
const link = {network: 'udp', domain: 'example.com', dst_ip: '2001:db8::1', dst_port: '443', src_ip: '10.0.0.2'} as const;
expect(parseTraceLink(traceQuery(link))).toEqual(link);
// Empty values stay out of the address; a missing network is TCP.
expect(traceQuery({dst_ip: '1.1.1.1', domain: '', dst_port: '53'})).toBe('tab=trace&dst_ip=1.1.1.1&dst_port=53');
expect(parseTraceLink('tab=trace&dst_ip=1.1.1.1')).toEqual({network: 'tcp', domain: '', dst_ip: '1.1.1.1', dst_port: '', src_ip: ''});
expect(parseTraceLink('tab=trace&src_ip=10.0.0.2')).toBeNull();
expect(parseTraceLink('tab=trace')).toBeNull();
});