fix(activity): say how to fix a routing block the mode cannot enter

When an ordinary rule sits before a must rule, the refusal now tells the
person to move the must rules to the top, and the toast names the rule in
the way on its second line instead of a generic write failure.
This commit is contained in:
Zakk committed 2026-09-28 02:07:38 +10:00
1 parent 14fc28692a
commit 2e40bc1de1
8 files changed
+61 -11

No files matched your search

+24 -1
View File
@@ -537,6 +537,29 @@ test('interleaved mandatory rules reject mode changes without writing configurat
await page.goto('/#/activity');
await page.getByRole('radiogroup', {name: 'Outbound mode'}).getByRole('radio', {name: 'Direct', exact: true}).click();
await page.getByRole('button', {name: 'Apply', exact: true}).click();
await expect(page.locator('.rp-toast.negative')).toContainText('Ordinary rules precede must rules');
const toast = page.locator('.rp-toast.negative');
await expect(toast).toContainText('Move the must rules to the top of the routing block');
// The rule in the way is named on its own line.
await expect(toast).toContainText('domain(example.com) -> proxy');
expect(requests.filter(request => request.method() === 'PUT')).toEqual([]);
});
test('the demo switches the outbound mode and back through its own configuration', async ({page}) => {
await page.goto('/#/activity');
const modes = page.getByRole('radiogroup', {name: 'Outbound mode'});
await modes.getByRole('radio', {name: 'Direct', exact: true}).click();
await page.getByRole('button', {name: 'Apply', exact: true}).click();
await expect(page.locator('.rp-toast.positive')).toContainText('reloaded: Direct');
await expect(page.locator('.rp-toast.negative')).toHaveCount(0);
// The catch-all is a real rule of the new generation, right after the must rules.
await page.goto('/#/rules?tab=list');
const rules = page.locator('.rp-table [role=rowgroup]:last-child [role=row][data-key]');
await expect(rules.nth(2)).toContainText('l4proto(tcp, udp)');
await expect(rules.nth(2)).toContainText('direct');
await page.goto('/#/activity');
await expect(modes.getByRole('radio', {name: 'Direct', exact: true})).toBeChecked();
await modes.getByRole('radio', {name: 'Rule', exact: true}).click();
await page.getByRole('button', {name: 'Apply', exact: true}).click();
await expect(page.locator('.rp-toast.positive').filter({hasText: 'reloaded: Rule'})).toBeVisible();
await expect(modes.getByRole('radio', {name: 'Rule', exact: true})).toBeChecked();
});
+4 -3
View File
@@ -30,7 +30,7 @@ export const messages = defineMessages({
'act.apply': '套用',
'act.modeApplied': '出站模式已寫入組態並重載:{mode}',
'act.modeNoRouting': '主組態未包含 routing 區段',
'act.modeInterleaved': '一般規則位於 must 規則之前,無法安全切換出站模式。',
'act.modeInterleaved': '路由區段有一般規則排在 must 規則前面。請先把 must 規則移到路由區段最前面,再切換出站模式。',
'act.modeReadOnly': '唯讀',
'act.modeWhyReadOnly': '查看唯讀原因',
'act.modeReadOnlyReason':
@@ -89,7 +89,7 @@ export const messages = defineMessages({
'act.apply': '应用',
'act.modeApplied': '出站模式已写入配置并重载:{mode}',
'act.modeNoRouting': '主配置未包含 routing 区段',
'act.modeInterleaved': '普通规则位于 must 规则之前,无法安全切换出站模式。',
'act.modeInterleaved': '路由区段有普通规则排在 must 规则前面。请先把 must 规则移到路由区段最前面,再切换出站模式。',
'act.modeReadOnly': '只读',
'act.modeWhyReadOnly': '查看只读原因',
'act.modeReadOnlyReason':
@@ -148,7 +148,8 @@ export const messages = defineMessages({
'act.apply': 'Apply',
'act.modeApplied': 'Outbound mode written to the configuration and reloaded: {mode}',
'act.modeNoRouting': 'The main configuration has no routing section',
'act.modeInterleaved': 'Ordinary rules precede must rules; the outbound mode cannot be changed safely.',
'act.modeInterleaved':
'An ordinary rule comes before a must rule in the routing block. Move the must rules to the top of the routing block, then change the outbound mode.',
'act.modeReadOnly': 'Read-only',
'act.modeWhyReadOnly': 'Why is the mode read-only?',
'act.modeReadOnlyReason':
+14
View File
@@ -82,6 +82,20 @@ it('refuses an override when ordinary rules precede mandatory exceptions', () =>
expect(writeMode(source, {mode: 'rule'})).toBe(source);
});
it('names the first ordinary rule ahead of a must rule, or the text on the line of a must rule', () => {
const refusal = (source: string) => {
try {
writeMode(source, {mode: 'direct'});
} catch (error) {
return error;
}
};
const interleaved = 'routing {\n domain(a.example) -> proxy \n domain(b.example) -> block\n pname(system) -> direct(must)\n fallback: proxy\n}\n';
expect(refusal(interleaved)).toMatchObject({key: 'act.modeInterleaved', detail: 'domain(a.example) -> proxy'});
const shared = 'routing {\n pname(system) -> direct(must) domain(a.example) -> proxy\n fallback: proxy\n}\n';
expect(refusal(shared)).toMatchObject({key: 'act.modeInterleaved', detail: 'pname(system) -> direct(must) domain(a.example) -> proxy'});
});
it('finds a hand-edited marked line by its marker and replaces it rather than adding a second', () => {
const edited = 'routing {\n l4proto(udp,tcp) -> proxy # doona: outbound mode\n fallback: direct\n}\n';
expect(readMode(edited)).toEqual({mode: 'global', target: 'proxy'});
+9 -4
View File
@@ -42,7 +42,12 @@ export function writeMode(text: string, next: OutboundMode): string {
const indent = body.match(/\n([ \t]+)\S/)?.[1] ?? ' ';
let at = block.open + 1;
if (text[at] === '\n') at++;
let ordinary = false;
let ordinary: string | null = null;
// The line an arrow sits on, as the refusal names it.
const lineOf = (at: number) => {
const end = text.indexOf('\n', at);
return text.slice(text.lastIndexOf('\n', at - 1) + 1, end === -1 ? undefined : end).trim();
};
// Mandatory rules must form a prefix or the catch-all cannot preserve their precedence.
for (let i = 0; i < tokens.length; i++) {
const arrow = tokens[i];
@@ -60,14 +65,14 @@ export function writeMode(text: string, next: OutboundMode): string {
text.slice(modifier.from, modifier.to) === 'must' &&
text.slice(close.from, close.to) === ')';
if (!mandatory) {
ordinary = true;
ordinary ??= lineOf(arrow.from);
continue;
}
if (ordinary) throw new LocalError('act.modeInterleaved');
if (ordinary !== null) throw new LocalError('act.modeInterleaved', ordinary);
const end = text.indexOf('\n', close.to);
at = end !== -1 && end < block.close ? end + 1 : close.to;
const following = tokens[i + 5];
if (following && following.from < at && following.kind !== 'comment') throw new LocalError('act.modeInterleaved');
if (following && following.from < at && following.kind !== 'comment') throw new LocalError('act.modeInterleaved', lineOf(following.from));
}
const outbound = next.mode === 'direct' ? 'direct' : next.target;
return text.slice(0, at) + (text[at - 1] === '\n' ? '' : '\n') + `${indent}l4proto(tcp, udp) -> ${outbound} ${MODE_MARK}\n` + text.slice(at);
+6
View File
@@ -8,6 +8,7 @@ import {useDraftGuard} from '../../shell/draft';
import {readMode, writeMode, type OutboundMode} from './mode';
import {modeLabels, modeView} from './view';
import {offered} from '../../api/capabilities';
import {LocalError} from '../../api/error';
export function useMode() {
const t = useT();
@@ -29,6 +30,11 @@ export function useMode() {
setStaged(current => (current === submitted ? null : current));
toast('positive', t('act.modeApplied', {mode: t(modeLabels[submitted.mode])}));
}
// doona's own refusal is the whole story: its advice leads and the rule in the way is the second line.
if (result.kind === 'failed' && result.error instanceof LocalError && result.error.key === 'act.modeInterleaved') {
toast('negative', t(result.error.key), {detail: result.error.detail ?? undefined});
return;
}
const problem = editProblem(result, t);
if (problem) toast(problem.kind, problem.text, {detail: problem.detail});
};
+2 -1
View File
@@ -31,7 +31,8 @@ export const messages = {
'act.apply': 'Apply',
'act.modeApplied': 'Outbound mode written to the configuration and reloaded: {mode}',
'act.modeNoRouting': 'The main configuration has no routing section',
'act.modeInterleaved': 'Ordinary rules precede must rules; the outbound mode cannot be changed safely.',
'act.modeInterleaved':
'An ordinary rule comes before a must rule in the routing block. Move the must rules to the top of the routing block, then change the outbound mode.',
'act.modeReadOnly': 'Read-only',
'act.modeWhyReadOnly': 'Why is the mode read-only?',
'act.modeReadOnlyReason':
+1 -1
View File
@@ -31,7 +31,7 @@ export const messages = {
'act.apply': '应用',
'act.modeApplied': '出站模式已写入配置并重载:{mode}',
'act.modeNoRouting': '主配置未包含 routing 区段',
'act.modeInterleaved': '普通规则位于 must 规则之前,无法安全切换出站模式。',
'act.modeInterleaved': '路由区段有普通规则排在 must 规则前面。请先把 must 规则移到路由区段最前面,再切换出站模式。',
'act.modeReadOnly': '只读',
'act.modeWhyReadOnly': '查看只读原因',
'act.modeReadOnlyReason':
+1 -1
View File
@@ -30,7 +30,7 @@ export const messages = {
'act.apply': '套用',
'act.modeApplied': '出站模式已寫入組態並重載:{mode}',
'act.modeNoRouting': '主組態未包含 routing 區段',
'act.modeInterleaved': '一般規則位於 must 規則之前,無法安全切換出站模式。',
'act.modeInterleaved': '路由區段有一般規則排在 must 規則前面。請先把 must 規則移到路由區段最前面,再切換出站模式。',
'act.modeReadOnly': '唯讀',
'act.modeWhyReadOnly': '查看唯讀原因',
'act.modeReadOnlyReason':