mirror of
https://github.com/bitbaum/threadkit.git
synced 2026-12-03 20:57:08 +01:00
* chore: add a formatter, matching the style this repo already writes singleQuote=true was chosen by counting this repo's own imports, not by fleet decree. The fleet is genuinely split and the two repos that already had a .prettierrc disagreed with each other, so there was no standard to restore. Quote style does not cross repo boundaries; having a gate does. Markdown is ignored for now — prettier rewraps prose, which would bury the real diff. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * style: format with prettier (12 files) Mechanical. No behaviour change. This SHA is listed in .git-blame-ignore-revs so `git blame` skips it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * chore: teach git blame to skip the reformat Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
84 lines
3.3 KiB
JavaScript
84 lines
3.3 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { unreadCount, unreadMessages, unreadThreadCount } from '../dist/index.js';
|
|
|
|
const T0 = new Date('2026-01-01T09:00:00Z');
|
|
const T1 = new Date('2026-01-01T10:00:00Z');
|
|
const T2 = new Date('2026-01-01T11:00:00Z');
|
|
|
|
function msg(id, authorId, createdAt) {
|
|
return { id, threadId: 'thread-1', authorId, body: 'x', createdAt };
|
|
}
|
|
|
|
test('one person opening the thread does not clear the badge for everyone else', () => {
|
|
// This is the bug a `read_at` column on the message row cannot avoid: the
|
|
// first reader marks it read for all of them.
|
|
const patient = { actorId: 'patient-1', kind: 'human', joinedAt: T0, lastReadAt: T2 };
|
|
const drA = { actorId: 'dr-a', kind: 'human', joinedAt: T0, lastReadAt: T2 };
|
|
const drB = { actorId: 'dr-b', kind: 'human', joinedAt: T0, lastReadAt: null };
|
|
const thread = { id: 'thread-1', participants: [patient, drA, drB], createdAt: T0 };
|
|
const messages = [msg('m1', 'patient-1', T1)];
|
|
|
|
assert.equal(unreadCount(thread, 'dr-a', messages), 0, 'dr-a has caught up');
|
|
assert.equal(unreadCount(thread, 'dr-b', messages), 1, 'dr-b has not, and still owes a read');
|
|
});
|
|
|
|
test('your own messages are never unread to you', () => {
|
|
const patient = { actorId: 'patient-1', kind: 'human', joinedAt: T0, lastReadAt: null };
|
|
const drA = { actorId: 'dr-a', kind: 'human', joinedAt: T0, lastReadAt: null };
|
|
const thread = { id: 'thread-1', participants: [patient, drA], createdAt: T0 };
|
|
const messages = [msg('m1', 'patient-1', T1)];
|
|
|
|
assert.equal(unreadCount(thread, 'patient-1', messages), 0);
|
|
assert.equal(unreadCount(thread, 'dr-a', messages), 1);
|
|
});
|
|
|
|
test('a participant who has never read owes every visible message from someone else', () => {
|
|
const patient = { actorId: 'patient-1', kind: 'human', joinedAt: T0 };
|
|
const drA = { actorId: 'dr-a', kind: 'human', joinedAt: T0 };
|
|
const thread = { id: 'thread-1', participants: [patient, drA], createdAt: T0 };
|
|
const messages = [msg('m1', 'patient-1', T1), msg('m2', 'patient-1', T2)];
|
|
|
|
assert.deepEqual(
|
|
unreadMessages(thread, 'dr-a', messages).map((m) => m.id),
|
|
['m1', 'm2'],
|
|
);
|
|
});
|
|
|
|
test('unread never reaches back past the day you joined', () => {
|
|
const patient = { actorId: 'patient-1', kind: 'human', joinedAt: T0 };
|
|
const drB = { actorId: 'dr-b', kind: 'human', joinedAt: T2, lastReadAt: null };
|
|
const thread = { id: 'thread-1', participants: [patient, drB], createdAt: T0 };
|
|
const messages = [msg('m1', 'patient-1', T1)];
|
|
|
|
assert.equal(
|
|
unreadCount(thread, 'dr-b', messages),
|
|
0,
|
|
'a message from before dr-b joined is not theirs to owe',
|
|
);
|
|
});
|
|
|
|
test('the nav badge counts threads that want you, not messages to read', () => {
|
|
const me = { actorId: 'me', kind: 'human', joinedAt: T0, lastReadAt: null };
|
|
const other = { actorId: 'other', kind: 'human', joinedAt: T0 };
|
|
const mk = (id) => ({ id, participants: [me, other], createdAt: T0 });
|
|
|
|
const entries = [
|
|
{
|
|
thread: mk('t1'),
|
|
messages: [
|
|
{ id: 'a', threadId: 't1', authorId: 'other', body: 'x', createdAt: T1 },
|
|
{ id: 'b', threadId: 't1', authorId: 'other', body: 'x', createdAt: T2 },
|
|
],
|
|
},
|
|
{ thread: mk('t2'), messages: [] },
|
|
];
|
|
|
|
assert.equal(
|
|
unreadThreadCount(entries, 'me'),
|
|
1,
|
|
'two unread messages in one thread is one badge',
|
|
);
|
|
});
|