Files
cinny/src/app/utils/ownStatusSync.test.ts
T

82 lines
2.3 KiB
TypeScript
Raw Normal View History

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',
);
});
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',
);
});