import { test } from 'node:test'; import assert from 'node:assert/strict'; import { LOCAL_WRITE_GRACE_MS, pickHeartbeatStatus } from './ownStatusSync'; import { NOW_PLAYING_PREFIX } from './nowPlaying'; const now = 1_000_000; const old = { lastLocalWriteAt: 0, now }; test('a status cleared on another device is not re-published (DP3)', () => { assert.equal( pickHeartbeatStatus({ ...old, local: 'stale', server: { presence: 'online', status_msg: '' } }), '', ); assert.equal(pickHeartbeatStatus({ ...old, local: 'stale', server: { presence: 'online' } }), ''); }); test('a status set on another device is kept', () => { assert.equal( pickHeartbeatStatus({ ...old, local: 'stale', server: { presence: 'unavailable', status_msg: 'new' }, }), 'new', ); }); test('falls back to the local copy when the read failed or we are invisible', () => { assert.equal(pickHeartbeatStatus({ ...old, local: 'mine', server: undefined }), 'mine'); assert.equal( pickHeartbeatStatus({ ...old, local: 'mine', server: { presence: 'offline', status_msg: '' } }), 'mine', ); }); test("a fresh local save wins over a server read that hasn't caught up", () => { const server = { presence: 'online', status_msg: 'previous' }; assert.equal( pickHeartbeatStatus({ local: 'just saved', server, lastLocalWriteAt: now - 2000, now }), 'just saved', ); assert.equal( pickHeartbeatStatus({ local: 'just saved', server, lastLocalWriteAt: now - LOCAL_WRITE_GRACE_MS - 1, now, }), 'previous', ); }); test('pickHeartbeatStatus: the "dnd" sentinel is never adopted as the user status (#283)', () => { const base = { lastLocalWriteAt: 0, now: 100_000 }; // leaving DND: the server still says "dnd" → restore the real local status assert.equal( pickHeartbeatStatus({ ...base, local: '🎮 Gaming', server: { presence: 'unavailable', status_msg: 'dnd' }, }), '🎮 Gaming', ); // a local copy that had absorbed the sentinel becomes empty, not "dnd" assert.equal( pickHeartbeatStatus({ ...base, local: 'dnd', server: { presence: 'online', status_msg: 'dnd' }, }), '', ); // ordinary statuses are unaffected assert.equal( pickHeartbeatStatus({ ...base, local: 'old', server: { presence: 'online', status_msg: 'new' }, }), 'new', ); }); test('[Gitea #491] music text on the server is never adopted as a typed status', () => { const music = `${NOW_PLAYING_PREFIX}Maroon 5 – Animals`; // Music is only published with no typed status, so it reads as "none" — // including when another device has since cleared the typed status. assert.equal( pickHeartbeatStatus({ ...old, local: '', server: { presence: 'online', status_msg: music } }), '', ); assert.equal( pickHeartbeatStatus({ ...old, local: 'cleared elsewhere', server: { presence: 'online', status_msg: music }, }), '', ); // A fresh local save still wins over a server read showing the old music. assert.equal( pickHeartbeatStatus({ local: 'studying', server: { presence: 'online', status_msg: music }, lastLocalWriteAt: now - 1000, now, }), 'studying', ); // A status the user typed that merely starts with 🎵 is theirs. assert.equal( pickHeartbeatStatus({ ...old, local: '', server: { presence: 'online', status_msg: '🎵 vibes' }, }), '🎵 vibes', ); });