Keep the news layer inside its day and its memory bounds

From review of the news layer:
- An event's other sides older than 24 hours (or undated) are no longer
  listed, and a report dated more than 15 minutes ahead is outside the
  window, as in the regional brief.
- Picture cards are one canvas per event in a bounded cache (24), repainted
  in place when the age changes, with each card's picture dropped with it;
  the age in the key had made a new canvas every minute.
- The feed list drops reports that leave the window and is capped at 600.
- "In view" is filtered from the reports the layer already holds; it reads
  the feed only with minor news on, once per refresh, never per pan.
- Infinite scroll restarts after a filter change or a failed read.
- The two filter selects stack, so "Significant and above" is not cut off.
- The timeout tests hold the event loop until aborted, so they pass on
  Node 22 as well.
This commit is contained in:
Denis Groshev committed 2026-11-06 00:45:25 +01:00
1 parent 138cc77a11
commit e95ae7b80c
11 files changed
+560 -90

No files matched your search

+123 -3
View File
@@ -61,8 +61,20 @@ const event = (extra = {}) => ({
severity: 3,
lastAt: '2026-10-28T09:30:00Z',
sides: [
{ side: 'UA', title: 'a', url: 'https://a.test/', publisher: 'A' },
{ side: 'RU', title: 'b', url: 'https://b.test/', publisher: 'B' },
{
side: 'UA',
title: 'a',
url: 'https://a.test/',
publisher: 'A',
publishedAt: '2026-10-28T09:00:00Z',
},
{
side: 'RU',
title: 'b',
url: 'https://b.test/',
publisher: 'B',
publishedAt: '2026-10-28T09:20:00Z',
},
],
image: { url: 'https://a.test/p.jpg', publisher: 'A' },
...extra,
@@ -189,12 +201,18 @@ test('feed pages merge newest first without duplicates', () => {
{ items: [] },
{ reports: [a, b], nextBefore: 'B' },
'replace',
NOW,
);
assert.deepEqual(
[feed.items.map((r) => r.id), feed.nextBefore],
[['a', 'b'], 'B'],
);
feed = mergeFeedPage(feed, { reports: [b, c], nextBefore: null }, 'append');
feed = mergeFeedPage(
feed,
{ reports: [b, c], nextBefore: null },
'append',
NOW,
);
assert.deepEqual(
[feed.items.map((r) => r.id), feed.nextBefore],
[['a', 'b', 'c'], null],
@@ -204,6 +222,7 @@ test('feed pages merge newest first without duplicates', () => {
{ ...feed, nextBefore: 'C' },
{ reports: [newer, a], nextBefore: 'X' },
'head',
NOW + 3_600_000,
);
assert.deepEqual(
[feed.items.map((r) => r.id), feed.nextBefore],
@@ -215,6 +234,46 @@ test('feed pages merge newest first without duplicates', () => {
);
});
test('the feed list keeps only the last 24 hours and at most its cap', () => {
const fresh = report('f', '2026-10-28T09:00:00Z');
const stale = report('s', '2026-10-27T09:00:00Z');
const ahead = report('x', '2026-10-28T11:00:00Z');
// A long session: yesterday's rows already held fall out on the next merge.
const feed = mergeFeedPage(
{ items: [stale], nextBefore: 'S' },
{ reports: [fresh, ahead], nextBefore: 'F' },
'head',
NOW,
);
assert.deepEqual(
feed.items.map((r) => r.id),
['f'],
);
const many = Array.from({ length: 30 }, (_, i) =>
report(`r${i}`, new Date(NOW - (i + 1) * 60_000).toISOString()),
);
const capped = mergeFeedPage(
{ items: [] },
{ reports: many, nextBefore: null },
'replace',
NOW,
10,
);
assert.equal(capped.items.length, 10);
assert.equal(capped.items[0].id, 'r0');
assert.equal(capped.items.at(-1).id, 'r9');
// Everything outside the window: an empty list, not an error.
assert.deepEqual(
mergeFeedPage(
{ items: [] },
{ reports: [stale], nextBefore: null },
'replace',
NOW,
).items,
[],
);
});
test('filters default to significant news and map to a last-24-hours query', () => {
const hotspots = [{ id: 'ua' }, { id: 'me' }];
const filters = normalizeFilters(
@@ -252,6 +311,11 @@ test('only the last 24 hours pass, and the severity floor applies', () => {
assert.equal(withinNewsWindow(fresh, NOW), true);
assert.equal(withinNewsWindow(old, NOW), false);
assert.equal(withinNewsWindow({ publishedAt: 'x' }, NOW), false);
// Clock skew up to 15 min ahead passes; further ahead is not news yet.
const skew = report('k', '2026-10-28T10:14:00Z');
const future = report('z', '2026-10-28T10:16:00Z');
assert.equal(withinNewsWindow(skew, NOW), true);
assert.equal(withinNewsWindow(future, NOW), false);
assert.deepEqual(
[fresh, minor, old]
.filter((r) => passesFilters(r, filters, NOW))
@@ -417,6 +481,62 @@ test('feed cards read like Breaking: headline, meta parts, sides and chips', ()
assert.equal(sidesLine({ side: 'unknown' }), '');
});
test('event sides older than a day are left out of the card and its sides line', () => {
const r = report('w', '2026-10-28T09:00:00Z', {
event: event({
sides: [
{
side: 'UA',
title: 'a',
url: 'https://a.test/',
publisher: 'A',
publishedAt: '2026-10-28T08:00:00Z',
},
{
side: 'RU',
title: 'old',
url: 'https://b.test/',
publisher: 'B',
publishedAt: '2026-10-26T08:00:00Z',
},
{
side: 'unknown',
title: 'c',
url: 'https://c.test/',
publisher: 'C',
publishedAt: '2026-10-28T09:10:00Z',
},
// No date: nothing shows it is recent.
{ side: 'IL', title: 'd', url: 'https://d.test/', publisher: 'D' },
],
}),
});
const view = feedCardView(r, NOW);
assert.deepEqual(
view.others.map((o) => [o.side, o.title]),
[['Unattributed', 'c']],
);
// The unattributed side is not named in the sides line.
assert.equal(view.sides, 'Reported by UA sources');
assert.equal(cardView(r, NOW).meta, '30 min · 6 sources · UA, Unattributed');
// All sides outside the window: no others, the report's own side remains.
const stale = report('v', '2026-10-28T09:00:00Z', {
event: event({
sides: [
{
side: 'RU',
title: 'old',
url: 'https://b.test/',
publisher: 'B',
publishedAt: '2026-10-26T08:00:00Z',
},
],
}),
});
assert.deepEqual(feedCardView(stale, NOW).others, []);
assert.equal(feedCardView(stale, NOW).sides, 'Reported by UA sources');
});
test('in view: half-degree cells and reports within 500 km of the centre', () => {
assert.equal(viewCell({ lat: 50.2, lon: 36.7 }), '100:73');
assert.equal(viewCell({ lat: 50.4, lon: 36.9 }), '100:73');