import { test } from 'node:test'; import assert from 'node:assert/strict'; import { LOCAL_WRITE_GRACE_MS, pickHeartbeatStatus } from './ownStatusSync'; 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', ); });