From a153a308e0020633f7d1d0c52fdf00f1f3ea139f Mon Sep 17 00:00:00 2001 From: dsz Date: Tue, 24 Nov 2026 02:45:53 +0545 Subject: [PATCH] Report rules skipped at the rule limit when importing importRules() stopped with `break` once the store held LIMITS.maxRules rules, so every remaining rule in the file was dropped without being counted as imported, duplicate or rejected. Importing 50 rules into a store with 497 reported "3 imported" and said nothing about the other 47. Duplicates after that point were not counted either. Each rule past the limit is now added to `rejected` with its position in the file and the reason ("Not imported: you can store at most 500 rules."), which the import dialog already lists, and the loop keeps going so later duplicates are still counted. To name the position, parseRulesImport() now returns each accepted rule with its index. Co-authored-by: Claude --- src/compat/rules.ts | 7 ++++--- src/storage/repository.ts | 16 +++++++++++++--- tests/unit/repository.test.ts | 28 ++++++++++++++++++++++++++++ tests/unit/rules.test.ts | 5 +++-- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/compat/rules.ts b/src/compat/rules.ts index 52da8cf..b779406 100644 --- a/src/compat/rules.ts +++ b/src/compat/rules.ts @@ -71,7 +71,8 @@ export function toContentRules(rules: Rule[], host: string, now: number): Conten } export interface ParsedImport { - accepted: RuleInput[]; + /** Each valid rule with its position in the file, so a rule skipped later can be named. */ + accepted: { index: number; rule: RuleInput }[]; rejected: { index: number; reason: string }[]; } @@ -88,7 +89,7 @@ export function parseRulesImport(payload: unknown): ParsedImport { ], }; } - const accepted: RuleInput[] = []; + const accepted: ParsedImport['accepted'] = []; const rejected: ParsedImport['rejected'] = []; envelope.data.rules.slice(0, LIMITS.maxRules).forEach((raw, index) => { const candidate = z @@ -106,7 +107,7 @@ export function parseRulesImport(payload: unknown): ParsedImport { return; } const result = RuleInputSchema.safeParse({ ...candidate.data, enabled: false }); - if (result.success) accepted.push(result.data); + if (result.success) accepted.push({ index, rule: result.data }); else rejected.push({ index, reason: result.error.issues.map((i) => i.message).join('; ') }); }); return { accepted, rejected }; diff --git a/src/storage/repository.ts b/src/storage/repository.ts index 074c649..e252e7c 100644 --- a/src/storage/repository.ts +++ b/src/storage/repository.ts @@ -272,18 +272,28 @@ export class Repository { const now = this.now(); let imported = 0; let duplicates = 0; - for (const input of parsed.accepted) { + const rejected = [...parsed.rejected]; + for (const { index, rule: input } of parsed.accepted) { const rule = createRule(input, 'community', now); if (rules.some((r) => r.domain === rule.domain && r.selector === rule.selector)) { duplicates++; continue; } - if (rules.length >= LIMITS.maxRules) break; + // Past the limit each remaining rule is reported, so the outcome + // accounts for every rule in the file instead of dropping the rest. + if (rules.length >= LIMITS.maxRules) { + rejected.push({ + index, + reason: `Not imported: you can store at most ${LIMITS.maxRules} rules.`, + }); + continue; + } rules.push(rule); imported++; } await this.area.set({ [KEYS.rules]: rules }); - return { imported, duplicates, rejected: parsed.rejected }; + rejected.sort((a, b) => a.index - b.index); + return { imported, duplicates, rejected }; }); } diff --git a/tests/unit/repository.test.ts b/tests/unit/repository.test.ts index 67947ba..0a5966b 100644 --- a/tests/unit/repository.test.ts +++ b/tests/unit/repository.test.ts @@ -3,6 +3,8 @@ import { MemoryArea } from '../../src/storage/area'; import { Repository, SCHEMA_VERSION } from '../../src/storage/repository'; import { DEFAULT_SETTINGS } from '../../src/shared/schemas'; import { ShieldFixError } from '../../src/shared/messages'; +import { LIMITS } from '../../src/shared/constants'; +import { createRule } from '../../src/compat/rules'; let area: MemoryArea; let now: number; @@ -142,6 +144,32 @@ describe('rules', () => { expect(imported).toMatchObject({ enabled: false, source: 'community' }); }); + it('reports the rules it skips once the rule limit is reached', async () => { + await area.set({ + rules: Array.from({ length: LIMITS.maxRules - 2 }, (_, i) => + createRule({ domain: 'example.com', selector: `.r${i}`, enabled: true }, 'user', now), + ), + }); + const result = await repo.importRules({ + format: 'shieldfix-rules', + formatVersion: 1, + rules: [ + { domain: 'example.com', selector: '.r0' }, + { domain: 'example.com', selector: '.a' }, + { domain: 'example.com', selector: '.b' }, + { domain: 'example.com', selector: '.c' }, + { domain: 'example.com', selector: '.r1' }, + ], + }); + // Two fit, ".c" does not: it must be reported, not silently dropped. A + // duplicate after the limit still counts as a duplicate. + expect(result).toMatchObject({ imported: 2, duplicates: 2 }); + expect(result.rejected).toEqual([ + { index: 3, reason: `Not imported: you can store at most ${LIMITS.maxRules} rules.` }, + ]); + expect(await repo.getRules()).toHaveLength(LIMITS.maxRules); + }); + it('round-trips through export and import', async () => { await repo.upsertRule({ domain: 'example.com', selector: '.a', enabled: true }); const exported = await repo.exportRules(); diff --git a/tests/unit/rules.test.ts b/tests/unit/rules.test.ts index c754f78..42f83a1 100644 --- a/tests/unit/rules.test.ts +++ b/tests/unit/rules.test.ts @@ -106,8 +106,9 @@ describe('import', () => { ], }); expect(parsed.accepted).toHaveLength(1); - expect(parsed.accepted[0]).toMatchObject({ enabled: false, selector: '.adblock-wall' }); - expect(parsed.accepted[0]).not.toHaveProperty('id'); + expect(parsed.accepted[0]!.index).toBe(0); + expect(parsed.accepted[0]!.rule).toMatchObject({ enabled: false, selector: '.adblock-wall' }); + expect(parsed.accepted[0]!.rule).not.toHaveProperty('id'); expect(parsed.rejected.map((r) => r.index)).toEqual([1, 2, 3]); });