Files
MyIP/tests/globalping-inventory.test.js
jason5ng32andClaude Opus 5.5 c29e48687b Refactor(layout): split backend-only code out of common/, group frontend utils
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>
2026-10-05 13:10:02 +08:00

51 lines
2.2 KiB
JavaScript

// Unit tests for the Globalping probe inventory aggregation
// (server/globalping-inventory.js). Fixtures mirror the raw /v1/probes
// shape: one entry per online probe with location.country / .continent.
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { buildProbeInventory } from '../server/globalping-inventory.js';
const probe = (country, continent) => ({ location: { country, continent } });
describe('buildProbeInventory', () => {
it('returns empty shapes for empty or non-array input', () => {
assert.deepEqual(buildProbeInventory([]), { countries: [], continents: [] });
assert.deepEqual(buildProbeInventory(null), { countries: [], continents: [] });
});
it('dedupes countries and sorts codes alphabetically', () => {
const result = buildProbeInventory([
probe('DE', 'EU'), probe('DE', 'EU'), probe('AT', 'EU'), probe('CH', 'EU'),
]);
assert.deepEqual(result.countries, ['AT', 'CH', 'DE']);
assert.deepEqual(result.continents, [{ key: 'europe', countries: ['AT', 'CH', 'DE'] }]);
});
it('merges NA and SA into the americas bucket', () => {
const result = buildProbeInventory([
probe('US', 'NA'), probe('BR', 'SA'), probe('CA', 'NA'),
]);
assert.deepEqual(result.continents, [{ key: 'americas', countries: ['BR', 'CA', 'US'] }]);
});
it('keeps continents in display order (asia → europe → africa → americas → oceania)', () => {
const result = buildProbeInventory([
probe('AU', 'OC'), probe('US', 'NA'), probe('ZA', 'AF'), probe('DE', 'EU'), probe('JP', 'AS'),
]);
assert.deepEqual(result.continents.map((c) => c.key), ['asia', 'europe', 'africa', 'americas', 'oceania']);
});
it('skips probes without a country; unknown continents still count as available', () => {
const result = buildProbeInventory([
probe(undefined, 'EU'),
probe('AQ', 'AN'), // Antarctica: no display bucket
{ location: null },
probe('JP', 'AS'),
]);
assert.deepEqual(result.countries, ['AQ', 'JP']);
assert.deepEqual(result.continents, [{ key: 'asia', countries: ['JP'] }]);
});
});