mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 08:55:47 +00:00
common/ had become the backend's library: 30 of its 41 modules were imported by nothing in the frontend, so the name no longer said what belonged there. It now holds only what the frontend or the build imports too; backend-only code moved to server/, flat except for datasets/ (the dataset engine and its readers). The *-db/ dataset directories stay in common/: self-hosting docs and Docker volumes mount those paths. frontend/utils/ keeps its app-wide modules at the root and gains ip/ (IP / ASN shaping and validation), report/ (the report pipeline) and features/ (the pure layer of a single feature). Paths only — no logic changes. The Dockerfile copies server/, and the AGENTS.md files say where a new module goes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
110 lines
4.8 KiB
JavaScript
110 lines
4.8 KiB
JavaScript
// Tests for server/as-relationships.js — relationship counts that follow
|
||
// the /api/asn-connectivity graph's own misinference rule: a distrusted
|
||
// non-Tier-1 provider is not counted (and not reclassified). The hypergiant
|
||
// fixture checks lists and graph agree; the graph itself is
|
||
// buildTopology, unchanged and covered in asn-connectivity.test.js.
|
||
|
||
import assert from 'node:assert/strict';
|
||
import { describe, it } from 'node:test';
|
||
import {
|
||
TIER1_PEERING_TRUSTED, TIER1_ADJACENCY_TRUSTED, distrustsNonTier1Providers,
|
||
countedRelationships,
|
||
} from '../server/as-relationships.js';
|
||
import { buildTopology, buildNeighbours, fillNeighbourNames } from '../api/asn-connectivity.js';
|
||
|
||
// p2c and p2p edges → an adjacency API like server/datasets/as-rel-db.js.
|
||
const makeRel = ({ p2c = [], p2p = [], tier1s = [] }) => {
|
||
const providers = new Map();
|
||
const customers = new Map();
|
||
const peers = new Map();
|
||
const add = (map, k, v) => map.set(k, [...(map.get(k) || []), v]);
|
||
for (const [provider, customer] of p2c) {
|
||
add(providers, customer, provider);
|
||
add(customers, provider, customer);
|
||
}
|
||
for (const [a, b] of p2p) {
|
||
add(peers, a, b);
|
||
add(peers, b, a);
|
||
}
|
||
const t1 = new Set(tier1s);
|
||
return {
|
||
providersOf: (asn) => providers.get(asn) || [],
|
||
peersOf: (asn) => peers.get(asn) || [],
|
||
customersOf: (asn) => customers.get(asn) || [],
|
||
isTier1: (asn) => t1.has(asn),
|
||
customerCountOf: (asn) => (customers.get(asn) || []).length,
|
||
};
|
||
};
|
||
|
||
// 101–108 are the clique. HYPER peers with seven of them and is (wrongly)
|
||
// listed as a customer of REGIONAL, which itself buys from Tier 1 101.
|
||
const HYPER = 13335;
|
||
const REGIONAL = 500;
|
||
const CLIQUE = [101, 102, 103, 104, 105, 106, 107, 108];
|
||
const rel = makeRel({
|
||
tier1s: CLIQUE,
|
||
p2c: [[101, REGIONAL], [REGIONAL, HYPER], [REGIONAL, 800], [HYPER, 600], [REGIONAL, 900], [101, 900]],
|
||
p2p: [...CLIQUE.slice(0, 7).map((t) => [HYPER, t]), [HYPER, 700]],
|
||
});
|
||
|
||
describe('distrustsNonTier1Providers', () => {
|
||
it('applies the graph bars', () => {
|
||
assert.equal(distrustsNonTier1Providers(HYPER, rel), true);
|
||
assert.equal(distrustsNonTier1Providers(REGIONAL, rel), false);
|
||
const peersOnly = makeRel({ tier1s: CLIQUE, p2p: CLIQUE.slice(0, TIER1_PEERING_TRUSTED - 1).map((t) => [1, t]) });
|
||
assert.equal(distrustsNonTier1Providers(1, peersOnly), false);
|
||
const mixed = makeRel({
|
||
tier1s: CLIQUE,
|
||
p2c: CLIQUE.slice(0, TIER1_ADJACENCY_TRUSTED - 4).map((t) => [t, 1]),
|
||
p2p: CLIQUE.slice(4, 8).map((t) => [1, t]),
|
||
});
|
||
assert.equal(distrustsNonTier1Providers(1, mixed), true);
|
||
});
|
||
});
|
||
|
||
describe('countedRelationships', () => {
|
||
it('a hypergiant: the distrusted non-Tier-1 provider is not counted at all', () => {
|
||
const { providers, peers, customers } = countedRelationships(HYPER, rel);
|
||
assert.deepEqual(providers, []);
|
||
assert.deepEqual(customers, [600]);
|
||
assert.deepEqual(peers, [...CLIQUE.slice(0, 7), 700]);
|
||
assert.equal(peers.includes(REGIONAL), false);
|
||
});
|
||
|
||
it('the other end is untouched: no customer-side inference', () => {
|
||
const { providers, peers, customers } = countedRelationships(REGIONAL, rel);
|
||
assert.deepEqual(providers, [101]);
|
||
assert.deepEqual(customers, [HYPER, 800, 900]);
|
||
assert.deepEqual(peers, []);
|
||
});
|
||
|
||
it('an ordinary AS keeps every provider; transit wins over peering', () => {
|
||
const ordinary = makeRel({ p2c: [[20, 10], [21, 10], [10, 50]], p2p: [[10, 20], [10, 40]] });
|
||
assert.deepEqual(countedRelationships(10, ordinary), { providers: [20, 21], peers: [40], customers: [50] });
|
||
});
|
||
});
|
||
|
||
describe('lists and graph agree', () => {
|
||
it('hypergiant: no upstream in the counts, none drawn', () => {
|
||
const neighbours = buildNeighbours(HYPER, rel, () => null);
|
||
assert.deepEqual(neighbours.counts, { providers: 0, peers: 8, customers: 1 });
|
||
const graph = buildTopology(HYPER, rel);
|
||
assert.equal(graph.nodes.some((n) => n.asn === REGIONAL), false);
|
||
assert.ok(graph.edges.every((e) => e.kind === 'peering'));
|
||
});
|
||
|
||
it('regional: counts match the lists', () => {
|
||
assert.deepEqual(buildNeighbours(REGIONAL, rel, () => null).counts, { providers: 1, peers: 0, customers: 3 });
|
||
});
|
||
});
|
||
|
||
describe('fillNeighbourNames', () => {
|
||
it('fills names as2org lacks from the graph nodes', () => {
|
||
const neighbours = { providers: [{ asn: 1, name: null }], peers: [{ asn: 2, name: 'Kept' }], customers: [{ asn: 3, name: null }] };
|
||
fillNeighbourNames(neighbours, [{ asn: 1, name: 'From graph' }, { asn: 2, name: 'Other' }]);
|
||
assert.equal(neighbours.providers[0].name, 'From graph');
|
||
assert.equal(neighbours.peers[0].name, 'Kept');
|
||
assert.equal(neighbours.customers[0].name, null);
|
||
});
|
||
});
|