diff --git a/e2e/activity.spec.ts b/e2e/activity.spec.ts index d6d5253d..fff7af15 100644 --- a/e2e/activity.spec.ts +++ b/e2e/activity.spec.ts @@ -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(); +}); diff --git a/src/features/activity/messages.ts b/src/features/activity/messages.ts index 48d80bd4..241f1d66 100644 --- a/src/features/activity/messages.ts +++ b/src/features/activity/messages.ts @@ -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': diff --git a/src/features/activity/mode.test.ts b/src/features/activity/mode.test.ts index 750492c1..c931edcf 100644 --- a/src/features/activity/mode.test.ts +++ b/src/features/activity/mode.test.ts @@ -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'}); diff --git a/src/features/activity/mode.ts b/src/features/activity/mode.ts index 8aad3234..f6c0ea12 100644 --- a/src/features/activity/mode.ts +++ b/src/features/activity/mode.ts @@ -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); diff --git a/src/features/activity/useMode.ts b/src/features/activity/useMode.ts index d064c3cf..d35aefd5 100644 --- a/src/features/activity/useMode.ts +++ b/src/features/activity/useMode.ts @@ -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}); }; diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts index a3c1eb6a..39f91732 100644 --- a/src/i18n/locales/en.ts +++ b/src/i18n/locales/en.ts @@ -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': diff --git a/src/i18n/locales/zh-CN.ts b/src/i18n/locales/zh-CN.ts index d86f6a1b..18790e2f 100644 --- a/src/i18n/locales/zh-CN.ts +++ b/src/i18n/locales/zh-CN.ts @@ -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': diff --git a/src/i18n/locales/zh-TW.ts b/src/i18n/locales/zh-TW.ts index eb9d3abe..8d3bd6b3 100644 --- a/src/i18n/locales/zh-TW.ts +++ b/src/i18n/locales/zh-TW.ts @@ -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':