Chore: sort Telegram DC Options
Some checks failed
Build / Build (push) Has been cancelled
Build / Diff output (push) Has been cancelled
Build / Deploy (push) Has been cancelled

This commit is contained in:
SukkaW
2026-08-14 20:21:58 +08:00
parent 54c6d411a7
commit 48b948c8eb
2 changed files with 86 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ import { expect } from 'earl';
import {
DC_OPTION_FLAG_IPV6,
DC_OPTION_FLAG_MEDIA_ONLY,
DC_OPTION_FLAG_STATIC,
mergeFallbackEndpoints,
TELEGRAM_BOOTSTRAP_ENDPOINTS
@@ -69,6 +70,53 @@ describe('MTProto DC config', () => {
expect(result.bootstrapAdded).toEqual(TELEGRAM_BOOTSTRAP_ENDPOINTS.length - 1);
});
it('emits a deterministic option order regardless of input order', () => {
const options = [
{ id: 2, ip: '149.154.167.222', port: 443, flags: DC_OPTION_FLAG_MEDIA_ONLY },
{ id: 1, ip: '2001:0b28:f23d:f001:0000:0000:0000:000a', port: 443, flags: DC_OPTION_FLAG_IPV6 },
// Numerically before .222 but lexically after it.
{ id: 2, ip: '149.154.167.41', port: 443, flags: DC_OPTION_FLAG_STATIC },
{ id: 1, ip: '149.154.175.56', port: 443, flags: 0 }
];
const build = (input: typeof options) => {
const config: MTProtoDCConfig = {
version: 1,
date: 1,
expires: 2,
this_dc: 1,
options: structuredClone(input)
};
mergeFallbackEndpoints(config, []);
return config.options;
};
const forward = build(options);
expect(build([...options].reverse())).toEqual(forward);
const ipsOfDc = (id: number) => forward.reduce<string[]>((acc, option) => {
if (option.id === id) acc.push(option.ip);
return acc;
}, []);
// IPv4 sorts numerically and precedes IPv6 within the same DC.
expect(ipsOfDc(1)).toEqual([
'149.154.175.50',
'149.154.175.56',
'2001:b28:f23d:f001::a'
]);
expect(ipsOfDc(2)).toEqual([
'95.161.76.100',
'149.154.167.41',
'149.154.167.50',
'149.154.167.51',
'149.154.167.222',
'2001:67c:4e8:f002::a'
]);
// DC ids are non-decreasing across the whole array.
expect(forward.every((option, i) => i === 0 || forward[i - 1].id <= option.id)).toEqual(true);
});
it('keeps functional variants separate and preserves backup secrets', () => {
const config: MTProtoDCConfig = {
version: 1,