Files
ytplayer/frontend/p2p-core.test.js

47 lines
2.1 KiB
JavaScript

'use strict';
const { test } = require('node:test');
const assert = require('node:assert');
const C = require('./p2p-core');
const NOW = Date.UTC(2026, 8, 29, 12);
const H = 3600_000;
const payload = (holders, extra = {}) => ({ ok: true, staleDays: 7, cids: [{ cid: 'a'.repeat(64), size: 10, holders, ...extra }] });
test('ago buckets', () => {
assert.strictEqual(C.ago(NOW - 30_000, NOW), 'just now');
assert.strictEqual(C.ago(NOW - 20 * 60_000, NOW), '20m ago');
assert.strictEqual(C.ago(NOW - 5 * H, NOW), '5h ago');
assert.strictEqual(C.ago(NOW - 72 * H, NOW), '3d ago');
});
test('no holders → nothing to show', () => {
assert.strictEqual(C.formatAvailability(payload([]), NOW), null);
assert.strictEqual(C.formatAvailability(null, NOW), null);
});
test('mixed holders: counts, newest check, not stale', () => {
const f = C.formatAvailability(payload([
{ peer: 'p1', online: true, lastVerifiedAt: NOW - 30 * 24 * H, stale: true, trust: 'reported' },
{ peer: 'p2', online: false, lastVerifiedAt: NOW - 48 * H, stale: false, trust: 'challenged' },
]), NOW);
assert.strictEqual(f.text, '📡 On 2 devices · 1 online now · last checked 2d ago');
assert.strictEqual(f.stale, false);
assert.match(f.title, /p1 · online · checked 30d ago \(stale\)/);
assert.match(f.title, /p2 · offline · checked 2d ago · spot-checked/);
});
test('every holder stale → still listed, flagged', () => {
const f = C.formatAvailability(payload([{ peer: 'p1', online: false, lastVerifiedAt: NOW - 20 * 24 * H, stale: true }]), NOW);
assert.strictEqual(f.text, '📡 On 1 device · none online · last checked 20d ago (not checked recently)');
assert.strictEqual(f.stale, true);
});
test('pickPeers: online only, spot-checked first', () => {
const p = payload([
{ peer: 'off', online: false, lastVerifiedAt: NOW, trust: 'challenged' },
{ peer: 'on1', online: true, lastVerifiedAt: NOW, trust: 'reported' },
{ peer: 'on2', online: true, lastVerifiedAt: NOW - H, trust: 'challenged' },
]);
assert.deepStrictEqual(C.pickPeers(p).map((x) => x.peer), ['on2', 'on1']);
});