Files
RedditBot/tests/conversation-read-state.test.ts
T
Jamie BruntonandClaude Opus 5 725b5fc4e6 feat: application-level read state and inbox triage for conversations
Conversations were hard to manage because "unread" came straight from
Matrix's notification_count, and nothing in the app ever sends a read
receipt — so opening a conversation in the dashboard could never clear it
and the unread set only ever grew. The list also collapsed three
orthogonal facts into one word, labelling "they spoke last and we have
not replied" as "Up to date".

Read state is now ours:

- conversation_read_state table (migration 19) plus
  ConversationReadStateRepository, kept separate from conversation_states
  so a read write can never be lost to mergeConversationState. The
  watermark is monotonic, so a stale poll cannot resurrect an unread
  badge.
- hasUnreadInbound and needsReply are derived server-side and returned on
  every conversation. Matrix's `unread` stays on the contract but the
  dashboard no longer reads it.
- POST /conversations/{id}/read|unread|status and
  POST /conversations/bulk-read, with optimistic cache updates.

Inbox rework:

- List rows are a real grid (checkbox, open button, chip strip) instead of
  a full-bleed absolute overlay button, which previously forced a
  pointer-events escape hatch for anything interactive in a row. Rows use
  aria-current rather than aria-pressed. Not a listbox: options must not
  contain interactive descendants.
- Counted filter pills replace the select, independent state chips replace
  the collapsed status word, plus a sort control (including oldest
  unanswered), a per-account filter, age-coloured timestamps, and an
  explicit count of rows hidden by the seller filter.
- Keyboard: j/k/arrows, Enter, u, e, / and Escape. Shortcuts yield to any
  open dialog so Escape dismisses the modal, not the conversation behind
  it. Multi-select with bulk mark-read and mark-done.
- Detail panel: auto-mark-read on open, a "New messages" divider pinned at
  mount (deriving it from the live watermark made it vanish a second after
  opening), triage actions in the header, the automation banner moved out
  of the composer, day dividers with time-only stamps, and a flex column
  so history scrolls in its own region instead of painting over the
  composer.
- Row order freezes while a conversation is open, with a "N new — refresh"
  pill, so a background poll cannot reorder rows mid-triage.

Two database tests rewound user_version by an offset from the latest
version, which silently re-targets a different migration whenever one is
appended. They now pin absolute versions.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-10 10:06:03 +01:00

111 lines
3.8 KiB
TypeScript

import Database from 'better-sqlite3';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { runMigrations } from '../src/database/client.js';
import { ConversationReadStateRepository } from '../src/database/repositories.js';
describe('ConversationReadStateRepository', () => {
let db: Database.Database;
let repo: ConversationReadStateRepository;
let other: ConversationReadStateRepository;
beforeEach(() => {
db = new Database(':memory:');
runMigrations(db);
repo = new ConversationReadStateRepository(db, 'account-one');
other = new ConversationReadStateRepository(db, 'account-two');
});
afterEach(() => db.close());
it('returns nothing for a conversation that has never been read', () => {
expect(repo.get('room-1')).toBeUndefined();
expect(repo.list()).toEqual([]);
});
it('records a read watermark and defaults to an open status', () => {
repo.markRead('room-1', 500, 1_000);
expect(repo.get('room-1')).toEqual({
conversationId: 'room-1',
lastReadMessageTs: 500,
lastReadAt: 1_000,
status: 'open',
snoozedUntil: null,
});
});
it('never moves the watermark backwards, so a stale write cannot resurrect an unread badge', () => {
repo.markRead('room-1', 900, 1_000);
repo.markRead('room-1', 400, 2_000);
const record = repo.get('room-1');
expect(record?.lastReadMessageTs).toBe(900);
// The read *time* still advances even when the watermark does not.
expect(record?.lastReadAt).toBe(2_000);
});
it('rewinds the watermark just before the named message when marking unread', () => {
repo.markRead('room-1', 900, 1_000);
repo.markUnread('room-1', 900, 2_000);
expect(repo.get('room-1')?.lastReadMessageTs).toBe(899);
});
it('reopens and clears any snooze when marking unread', () => {
repo.setStatus('room-1', 'snoozed', 5_000, 1_000);
repo.markUnread('room-1', 100, 2_000);
expect(repo.get('room-1')).toMatchObject({ status: 'open', snoozedUntil: null });
});
it('clamps a negative watermark to zero', () => {
repo.markUnread('room-1', 0, 1_000);
expect(repo.get('room-1')?.lastReadMessageTs).toBe(0);
});
it('stores a snooze deadline and drops it when the status changes', () => {
repo.setStatus('room-1', 'snoozed', 5_000, 1_000);
expect(repo.get('room-1')).toMatchObject({ status: 'snoozed', snoozedUntil: 5_000 });
repo.setStatus('room-1', 'done', null, 2_000);
expect(repo.get('room-1')).toMatchObject({ status: 'done', snoozedUntil: null });
});
it('ignores a snooze deadline for non-snoozed statuses', () => {
repo.setStatus('room-1', 'open', 5_000, 1_000);
expect(repo.get('room-1')?.snoozedUntil).toBeNull();
});
it('preserves the read watermark when only the status changes', () => {
repo.markRead('room-1', 900, 1_000);
repo.setStatus('room-1', 'done', null, 2_000);
expect(repo.get('room-1')).toMatchObject({ lastReadMessageTs: 900, status: 'done' });
});
it('reopens only snoozes whose deadline has passed', () => {
repo.setStatus('room-past', 'snoozed', 1_000, 1);
repo.setStatus('room-future', 'snoozed', 9_000, 1);
expect(repo.expireSnoozes(5_000)).toEqual(['room-past']);
expect(repo.get('room-past')).toMatchObject({ status: 'open', snoozedUntil: null });
expect(repo.get('room-future')).toMatchObject({ status: 'snoozed', snoozedUntil: 9_000 });
});
it('does not reopen an indefinite snooze', () => {
repo.setStatus('room-1', 'snoozed', null, 1);
expect(repo.expireSnoozes(9_999)).toEqual([]);
});
it('keeps read state isolated per account', () => {
repo.markRead('room-1', 500, 1_000);
expect(other.get('room-1')).toBeUndefined();
expect(other.list()).toEqual([]);
expect(repo.list()).toHaveLength(1);
});
});