import Database from 'better-sqlite3'; import { describe, expect, it } from 'vitest'; import { allowsConversationSync, allowsDiscovery, allowsSending, isBotMode, parseBotMode, } from '../src/services/bot-mode.js'; import { RuntimeStateRepository } from '../src/database/candidate-queue-repository.js'; import { runMigrations } from '../src/database/client.js'; describe('bot mode capabilities', () => { it('lets scan sync conversations without sending or discovering', () => { expect(allowsConversationSync('scan')).toBe(true); expect(allowsSending('scan')).toBe(false); expect(allowsDiscovery('scan')).toBe(false); }); it('lets running do everything and stopped do nothing', () => { for (const capability of [allowsConversationSync, allowsSending, allowsDiscovery]) { expect(capability('running')).toBe(true); expect(capability('stopped')).toBe(false); } }); it('falls back to stopped rather than guessing a sending mode', () => { expect(parseBotMode('scan')).toBe('scan'); expect(parseBotMode(null)).toBe('stopped'); expect(parseBotMode('paused')).toBe('stopped'); expect(parseBotMode(undefined, 'running')).toBe('running'); expect(isBotMode('paused')).toBe(false); }); }); describe('runtime state persistence', () => { function db(): Database.Database { const database = new Database(':memory:'); runMigrations(database); return database; } function repo(): RuntimeStateRepository { return new RuntimeStateRepository(db()); } it('round-trips every mode', () => { const state = repo(); for (const mode of ['running', 'scan', 'stopped'] as const) { state.setBotMode(mode); expect(state.getBotMode()).toBe(mode); } }); it('reports scan as not-running for the legacy boolean readers', () => { const state = repo(); state.setBotMode('scan'); expect(state.isBotRunning()).toBe(false); state.setBotRunning(true); expect(state.getBotMode()).toBe('running'); }); it('defaults to stopped before anything has been persisted', () => { expect(repo().getBotMode()).toBe('stopped'); }); it('reads a pre-migration row that only has the boolean', () => { const database = db(); database.prepare('INSERT INTO runtime_state (id, bot_running, bot_mode, updated_at) VALUES (1, 1, NULL, 0)').run(); expect(new RuntimeStateRepository(database).getBotMode()).toBe('running'); }); });