peers.test.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const { eq, ok } = require('../../helpers/assert');
  2. const mainViewsPath = require.resolve('../../../src/views/main_views');
  3. const peersViewPath = require.resolve('../../../src/views/peers_view');
  4. describe('peers: view module loads cleanly', (t) => {
  5. t('peers_view requires without throwing', () => {
  6. delete require.cache[peersViewPath];
  7. const mod = require('../../../src/views/peers_view');
  8. ok(typeof mod.peersView === 'function', 'exports peersView');
  9. });
  10. t('main_views exports template + i18n needed by peersView', () => {
  11. delete require.cache[mainViewsPath];
  12. const mod = require('../../../src/views/main_views');
  13. ok(typeof mod.template === 'function');
  14. ok(mod.i18n && typeof mod.i18n === 'object');
  15. });
  16. });
  17. describe('peers: i18n keys present for source chips and LAN status', (t) => {
  18. const i18nBase = require('../../../src/client/assets/translations/i18n');
  19. const langs = ['en', 'es', 'de', 'fr', 'it', 'pt', 'ru', 'zh', 'ar', 'hi', 'eu'];
  20. const required = [
  21. 'lanBroadcastLabel',
  22. 'lanBroadcastActive',
  23. 'lanBroadcastDisabled',
  24. 'peerSourceRpc',
  25. 'peerSourceGossip',
  26. 'peerSourceEbt',
  27. 'peerSourceRecent',
  28. 'peerSourceLan'
  29. ];
  30. for (const lang of langs) {
  31. t(`${lang} has all required keys`, () => {
  32. const dict = i18nBase[lang];
  33. ok(dict, `dict for ${lang} loaded`);
  34. for (const k of required) ok(typeof dict[k] === 'string' && dict[k].length > 0, `${lang}.${k}`);
  35. });
  36. }
  37. });
  38. describe('peers: deduplication invariant', (t) => {
  39. const dedup = (peers) => {
  40. const seen = new Set();
  41. return peers.filter(p => {
  42. const key = p[1]?.key;
  43. if (!key || seen.has(key)) return false;
  44. seen.add(key);
  45. return true;
  46. });
  47. };
  48. t('removes duplicate keys', () => {
  49. const dup = [
  50. ['addr1', { key: '@A.ed25519', name: 'A' }],
  51. ['addr2', { key: '@A.ed25519', name: 'A-again' }],
  52. ['addr3', { key: '@B.ed25519', name: 'B' }]
  53. ];
  54. const out = dedup(dup);
  55. eq(out.length, 2);
  56. eq(out[0][1].key, '@A.ed25519');
  57. eq(out[1][1].key, '@B.ed25519');
  58. });
  59. t('drops entries with missing key', () => {
  60. const malformed = [
  61. ['addr1', { key: null }],
  62. ['addr2', {}],
  63. ['addr3', { key: '@C.ed25519' }]
  64. ];
  65. const out = dedup(malformed);
  66. eq(out.length, 1);
  67. eq(out[0][1].key, '@C.ed25519');
  68. });
  69. });