Files
Nodecast/test/channelVariantGrouping.test.js
blomios a7a83a6faa feat: group variants of the same IPTV channel into a logical channel (#9)
Add a channelVariantGrouping service that normalizes channel names
(strips region prefixes and quality tokens) to fingerprint duplicate
stream entries and cluster them under a single logical channel with
quality/source variants. Wire it into the Live TV channel list and
player variant selection, reducing duplicates without destructively
merging streams.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-02 08:25:42 +02:00

45 lines
2.2 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const grouping = require('../public/js/services/channelVariantGrouping.js');
const channel = (id, name, extra = {}) => ({ id, streamId: id, sourceId: 1, name, groupTitle: 'Sports', ...extra });
test('normalizes regional prefixes and quality markers into one logical channel', () => {
const names = ['|FR| BEIN SPORTS uhd 1', '|FR| BEIN SPORTS 1 4K', 'FR-CAR| bein sports 1'];
assert.deepEqual(names.map(grouping.normalizeChannelName), ['bein sports 1', 'bein sports 1', 'bein sports 1']);
const logical = grouping.groupChannels(names.map((name, index) => channel(String(index + 1), name)));
assert.equal(logical.length, 1);
assert.equal(logical[0].displayName, 'beIN Sports 1');
assert.equal(logical[0].variants.length, 3);
});
test('does not merge distinct numbered channels', () => {
const logical = grouping.groupChannels([
channel('1', 'beIN Sports 1 HD'),
channel('2', 'beIN Sports 2 HD')
]);
assert.deepEqual(logical.map(item => item.fingerprint), ['bein sports 1', 'bein sports 2']);
});
test('logical favorite and hidden state are derived from individual streams', () => {
const variants = [channel('1', 'BEIN SPORTS 1 HD'), channel('2', 'BEIN SPORTS 1 4K')];
const logical = grouping.groupChannels(variants, {
isFavorite: item => item.id === '1',
isHidden: item => item.id === '2'
})[0];
assert.equal(logical.isFavorite, true);
assert.equal(logical.isHidden, false);
assert.equal(logical.preferredVariant.id, '1');
assert.equal(grouping.groupChannels(variants, { isHidden: () => true }).length, 0);
assert.equal(grouping.groupChannels(variants, { isHidden: () => true, showHidden: true })[0].isHidden, true);
});
test('an explicit visible selection overrides the automatic quality preference', () => {
const hd = channel('1', 'BEIN SPORTS 1 HD');
const uhd = channel('2', 'BEIN SPORTS 1 UHD');
const selectedVariants = new Map([['bein sports 1', grouping.getVariantKey(hd)]]);
const logical = grouping.groupChannels([hd, uhd], { selectedVariants })[0];
assert.equal(logical.preferredVariant.id, '1');
});