Files
RedditBot/tests/database.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

259 lines
11 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import Database from 'better-sqlite3';
import { createDatabase, runMigrations } from '../src/database/client.js';
import { getTestConfig } from '../tests/testUtils.js';
describe('Database Client', () => {
describe('createDatabase', () => {
it('creates an in-memory database', () => {
const config = getTestConfig({ database: { path: ':memory:' } });
const db = createDatabase(config);
expect(db).toBeDefined();
db.close();
});
it('enables WAL mode', () => {
const config = getTestConfig({ database: { path: ':memory:' } });
const db = createDatabase(config);
const journalMode = db.pragma('journal_mode', { simple: true }) as string;
// SQLite uses an in-memory journal for the special :memory: database;
// file-backed databases use WAL in production.
expect(['wal', 'memory']).toContain(journalMode);
db.close();
});
it('enables foreign keys', () => {
const config = getTestConfig({ database: { path: ':memory:' } });
const db = createDatabase(config);
const fk = db.pragma('foreign_keys', { simple: true }) as number;
expect(fk).toBe(1);
db.close();
});
});
describe('runMigrations', () => {
let db: Database.Database;
beforeEach(() => {
db = new Database(':memory:');
db.pragma('foreign_keys = ON');
});
afterEach(() => {
db.close();
});
it('creates processed_posts table', () => {
runMigrations(db);
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all() as { name: string }[];
const tableNames = tables.map(t => t.name);
expect(tableNames).toContain('processed_posts');
});
it('creates sent_messages table', () => {
runMigrations(db);
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all() as { name: string }[];
const tableNames = tables.map(t => t.name);
expect(tableNames).toContain('sent_messages');
});
it('creates successful_messages table', () => {
runMigrations(db);
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all() as { name: string }[];
const tableNames = tables.map(t => t.name);
expect(tableNames).toContain('successful_messages');
});
it('creates indexes', () => {
runMigrations(db);
const indexes = db.prepare("SELECT name FROM sqlite_master WHERE type='index'").all() as { name: string }[];
const indexNames = indexes.map(i => i.name);
expect(indexNames).toContain('idx_posts_subreddit');
expect(indexNames).toContain('idx_posts_processed_at');
expect(indexNames).toContain('idx_posts_message_sent');
expect(indexNames).toContain('idx_sent_messages_response');
expect(indexNames).toContain('idx_sent_messages_recipient');
});
it('adds got_response column', () => {
runMigrations(db);
const columns = db.prepare('PRAGMA table_info(sent_messages)').all() as { name: string }[];
const columnNames = columns.map(c => c.name);
expect(columnNames).toContain('got_response');
expect(columnNames).toContain('response_checked_at');
expect(columnNames).toContain('got_image');
});
it('adds strategy_id column', () => {
runMigrations(db);
const columns = db.prepare('PRAGMA table_info(sent_messages)').all() as { name: string }[];
const columnNames = columns.map(c => c.name);
expect(columnNames).toContain('strategy_id');
});
it('adds subreddit column to successful_messages', () => {
runMigrations(db);
const columns = db.prepare('PRAGMA table_info(successful_messages)').all() as { name: string }[];
const columnNames = columns.map(c => c.name);
expect(columnNames).toContain('subreddit');
});
it('is idempotent (safe to run multiple times)', () => {
runMigrations(db);
runMigrations(db);
runMigrations(db);
const tables = db
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")
.all() as { name: string }[];
// Existing domain/config tables plus account, conversation, sync and media ownership stores.
expect(tables.length).toBe(21);
expect(tables.map((table) => table.name)).toEqual(expect.arrayContaining([
'candidate_queue',
'runtime_state',
'media_library_assets',
'media_library_sends',
'conversation_read_state',
]));
});
it('tracks schema version and migrates a legacy sent_messages table transactionally', () => {
db.exec(`
CREATE TABLE processed_posts (id TEXT PRIMARY KEY, subreddit TEXT NOT NULL, author TEXT NOT NULL, title TEXT NOT NULL, image_url TEXT, image_analysis TEXT, generated_message TEXT, processed_at INTEGER NOT NULL, message_sent INTEGER DEFAULT 0, error TEXT);
CREATE TABLE sent_messages (id INTEGER PRIMARY KEY AUTOINCREMENT, post_id TEXT NOT NULL, recipient TEXT NOT NULL, subject TEXT NOT NULL, message TEXT NOT NULL, sent_at INTEGER NOT NULL);
CREATE TABLE activity_log (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER NOT NULL, type TEXT NOT NULL, message TEXT NOT NULL, recipient TEXT, image_url TEXT);
INSERT INTO processed_posts VALUES ('post-1', 'test', 'author', 'title', NULL, NULL, NULL, 1, 0, NULL);
INSERT INTO sent_messages (post_id, recipient, subject, message, sent_at) VALUES ('post-1', 'recipient', 'subject', 'message', 2);
INSERT INTO activity_log (timestamp, type, message, recipient) VALUES (2, 'sent', 'Sent message to u/recipient (r/test)', 'recipient');
`);
runMigrations(db);
expect(db.pragma('user_version', { simple: true })).toBeGreaterThan(0);
expect(db.prepare('SELECT post_id FROM sent_messages').get()).toEqual({ post_id: 'post-1' });
const postId = db.prepare('PRAGMA table_info(sent_messages)').all().find((column: { name: string }) => column.name === 'post_id') as { notnull: number };
expect(postId.notnull).toBe(0);
expect(db.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'delivery_outbox'").get()).toBeDefined();
expect(db.prepare('SELECT post_title, post_url FROM activity_log').get()).toEqual({
post_title: 'title',
post_url: 'https://www.reddit.com/r/test/comments/post-1/',
});
});
it('backfills global recipient suppression from sent messages and known conversations', () => {
runMigrations(db);
db.prepare(`
INSERT INTO reddit_conversations (
account_id, conversation_id, username, room_name, payload_json, updated_at
) VALUES (?, 'old-room', 'Old_Contact', 'Old Contact', '{}', 20)
`).run('__legacy_unassigned__');
db.prepare(`
INSERT INTO processed_posts (account_id, id, subreddit, author, title, processed_at)
VALUES (?, 'old-post', 'test', 'sent-contact', 'title', 10)
`).run('__legacy_unassigned__');
db.prepare(`
INSERT INTO sent_messages (
account_id, post_id, recipient, subject, message, sent_at
) VALUES (?, 'old-post', 'Sent_Contact', 'subject', 'message', 11)
`).run('__legacy_unassigned__');
db.exec(`
DELETE FROM recipient_suppressions;
DROP TABLE conversation_automation_claims;
DROP TABLE ai_usage;
`);
// Rewind to just before the suppression backfill migration. This must be an
// absolute version, not an offset from the latest — an offset silently
// re-targets a different migration every time a new one is appended.
db.pragma('user_version = 11');
runMigrations(db);
expect(db.prepare('SELECT recipient, reason FROM recipient_suppressions ORDER BY recipient COLLATE NOCASE').all())
.toEqual([
{ recipient: 'Old_Contact', reason: 'historical-conversation' },
{ recipient: 'Sent_Contact', reason: 'sent-message-history' },
]);
});
it('preserves pending reply claims when adding auditable superseded status', () => {
runMigrations(db);
db.exec(`
DROP TABLE conversation_automation_claims;
CREATE TABLE conversation_automation_claims (
account_id TEXT NOT NULL,
conversation_id TEXT NOT NULL,
incoming_key TEXT NOT NULL,
status TEXT NOT NULL CHECK(status IN ('pending', 'sent')),
claimed_at INTEGER NOT NULL,
sent_at INTEGER,
reply_text TEXT,
incoming_at INTEGER,
last_attempt_at INTEGER,
attempt_count INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (account_id, conversation_id, incoming_key),
FOREIGN KEY (account_id) REFERENCES reddit_accounts(account_id) ON DELETE CASCADE
);
INSERT INTO conversation_automation_claims (
account_id, conversation_id, incoming_key, status, claimed_at,
reply_text, incoming_at, last_attempt_at, attempt_count
) VALUES (
'__legacy_unassigned__', 'room', 'event', 'pending', 10,
'durable reply', 5, 11, 1
);
`);
// Absolute version: rewind to just before the superseded-status rebuild.
db.pragma('user_version = 16');
runMigrations(db);
expect(db.prepare(`
SELECT status, reply_text, incoming_at, last_attempt_at, attempt_count, superseded_at
FROM conversation_automation_claims
`).get()).toEqual({
status: 'pending', reply_text: 'durable reply', incoming_at: 5,
last_attempt_at: 11, attempt_count: 1, superseded_at: null,
});
});
it('processed_posts has correct schema', () => {
runMigrations(db);
const columns = db.prepare('PRAGMA table_info(processed_posts)').all() as { name: string; notnull: number }[];
const columnMap = new Map(columns.map(c => [c.name, c]));
expect(columnMap.get('account_id')?.notnull).toBe(1);
expect(columnMap.get('id')?.notnull).toBe(1); // Composite account-scoped primary key
expect(columnMap.get('subreddit')?.notnull).toBe(1);
expect(columnMap.get('author')?.notnull).toBe(1);
expect(columnMap.get('title')?.notnull).toBe(1);
expect(columnMap.get('post_created_at')).toBeDefined();
expect(columnMap.get('processed_at')?.notnull).toBe(1);
expect(columnMap.get('message_sent')).toBeDefined();
expect(columnMap.get('error')).toBeDefined();
});
it('sent_messages has correct schema', () => {
runMigrations(db);
const columns = db.prepare('PRAGMA table_info(sent_messages)').all() as { name: string; notnull: number }[];
const columnMap = new Map(columns.map(c => [c.name, c]));
expect(columnMap.get('recipient')?.notnull).toBe(1);
expect(columnMap.get('subject')?.notnull).toBe(1);
expect(columnMap.get('message')?.notnull).toBe(1);
expect(columnMap.get('sent_at')?.notnull).toBe(1);
expect(columnMap.get('post_id')?.notnull).toBe(0); // nullable — chat rows have no post
expect(columnMap.get('chat_room_id')).toBeDefined();
expect(columnMap.get('got_image')).toBeDefined();
});
it('successful_messages has correct schema', () => {
runMigrations(db);
const columns = db.prepare('PRAGMA table_info(successful_messages)').all() as { name: string; notnull: number }[];
const columnMap = new Map(columns.map(c => [c.name, c]));
expect(columnMap.get('original_message')?.notnull).toBe(1);
expect(columnMap.get('created_at')?.notnull).toBe(1);
expect(columnMap.get('sent_message_id')?.notnull).toBe(0); // nullable
});
});
});