Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 19 additions & 32 deletions src/rules.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { loadRuleSet, mergeRuleSets, loadAndMergeDomains, RuleSet } from './rules';
import { loadRuleSet, loadAndMergeDomains } from './rules';

describe('rules', () => {
let testDir: string;
Expand Down Expand Up @@ -152,37 +152,6 @@ rules: []
});
});

describe('mergeRuleSets', () => {
it('should merge multiple rulesets and deduplicate', () => {
const ruleSet1: RuleSet = {
version: 1,
rules: [
{ domain: 'github.com', subdomains: true },
{ domain: 'npmjs.org', subdomains: true },
],
};
const ruleSet2: RuleSet = {
version: 1,
rules: [
{ domain: 'github.com', subdomains: true }, // duplicate
{ domain: 'pypi.org', subdomains: true },
],
};

const result = mergeRuleSets([ruleSet1, ruleSet2]);
expect(result).toEqual(['github.com', 'npmjs.org', 'pypi.org']);
});

it('should handle empty rulesets', () => {
expect(mergeRuleSets([])).toEqual([]);
});

it('should handle rulesets with empty rules', () => {
const ruleSet: RuleSet = { version: 1, rules: [] };
expect(mergeRuleSets([ruleSet])).toEqual([]);
});
});

describe('loadAndMergeDomains', () => {
it('should merge file domains with CLI domains', () => {
const filePath = writeRuleFile('rules.yml', `
Expand Down Expand Up @@ -226,6 +195,24 @@ rules:
expect(result).toEqual(['github.com', 'npmjs.org']);
});

it('should deduplicate domains across multiple ruleset files', () => {
const file1 = writeRuleFile('rules1.yml', `
version: 1
rules:
- domain: github.com
- domain: npmjs.org
`);
const file2 = writeRuleFile('rules2.yml', `
version: 1
rules:
- domain: github.com
- domain: pypi.org
`);

const result = loadAndMergeDomains([file1, file2], []);
expect(result).toEqual(['github.com', 'npmjs.org', 'pypi.org']);
});

it('should work with no CLI domains', () => {
const filePath = writeRuleFile('rules.yml', `
version: 1
Expand Down
2 changes: 1 addition & 1 deletion src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function expandRule(rule: Rule): string[] {
* @param ruleSets - Array of parsed RuleSet objects
* @returns Array of unique domain strings
*/
export function mergeRuleSets(ruleSets: RuleSet[]): string[] {
function mergeRuleSets(ruleSets: RuleSet[]): string[] {
const domains = new Set<string>();

for (const ruleSet of ruleSets) {
Expand Down
Loading