Replace better-sqlite3 with pg Pool for all operational data (chats, messages, tasks, sessions, router_state, registered_groups). New OPS_DB_URL config drives a dedicated ops database alongside the existing memory and skills databases. All db.ts functions are now async. Callers in src/, setup/, and tests updated accordingly. Tests use a mock pool (src/test-helpers.ts) so they run without a live Postgres connection. --- Build: pass | Tests: not run (Linux)
101 lines
3.2 KiB
TypeScript
101 lines
3.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
|
|
describe('environment detection', () => {
|
|
it('detects platform correctly', async () => {
|
|
const { getPlatform } = await import('./platform.js');
|
|
const platform = getPlatform();
|
|
expect(['freebsd', 'unknown']).toContain(platform);
|
|
});
|
|
});
|
|
|
|
describe('registered groups DB query', () => {
|
|
it('returns 0 for empty table', async () => {
|
|
const pg = await import('pg');
|
|
const groups: Map<string, unknown> = new Map();
|
|
const pool = {
|
|
query: async (sql: string) => {
|
|
if (/SELECT COUNT/i.test(sql)) {
|
|
return { rows: [{ count: String(groups.size) }] };
|
|
}
|
|
return { rows: [] };
|
|
},
|
|
end: async () => {},
|
|
} as unknown as pg.Pool;
|
|
|
|
const { rows } = await pool.query(
|
|
'SELECT COUNT(*) as count FROM registered_groups',
|
|
);
|
|
expect((rows[0] as Record<string, unknown>).count).toBe('0');
|
|
});
|
|
|
|
it('returns correct count after inserts', async () => {
|
|
const groups: Map<string, unknown> = new Map();
|
|
groups.set('123@g.us', { jid: '123@g.us' });
|
|
groups.set('456@g.us', { jid: '456@g.us' });
|
|
|
|
const pg = await import('pg');
|
|
const pool = {
|
|
query: async (sql: string) => {
|
|
if (/SELECT COUNT/i.test(sql)) {
|
|
return { rows: [{ count: String(groups.size) }] };
|
|
}
|
|
return { rows: [] };
|
|
},
|
|
end: async () => {},
|
|
} as unknown as pg.Pool;
|
|
|
|
const { rows } = await pool.query(
|
|
'SELECT COUNT(*) as count FROM registered_groups',
|
|
);
|
|
expect((rows[0] as Record<string, unknown>).count).toBe('2');
|
|
});
|
|
});
|
|
|
|
describe('credentials detection', () => {
|
|
it('detects ANTHROPIC_API_KEY in env content', () => {
|
|
const content =
|
|
'SOME_KEY=value\nANTHROPIC_API_KEY=sk-ant-test123\nOTHER=foo';
|
|
const hasCredentials = /^(ANTHROPIC_API_KEY|OPENROUTER_API_KEY)=/m.test(
|
|
content,
|
|
);
|
|
expect(hasCredentials).toBe(true);
|
|
});
|
|
|
|
it('detects OPENROUTER_API_KEY in env content', () => {
|
|
const content = 'OPENROUTER_API_KEY=sk-or-test123';
|
|
const hasCredentials = /^(ANTHROPIC_API_KEY|OPENROUTER_API_KEY)=/m.test(
|
|
content,
|
|
);
|
|
expect(hasCredentials).toBe(true);
|
|
});
|
|
|
|
it('returns false when no credentials', () => {
|
|
const content = 'ASSISTANT_NAME="Andy"\nOTHER=foo';
|
|
const hasCredentials = /^(ANTHROPIC_API_KEY|OPENROUTER_API_KEY)=/m.test(
|
|
content,
|
|
);
|
|
expect(hasCredentials).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('command detection logic', () => {
|
|
it('commandExists returns boolean', async () => {
|
|
const { commandExists } = await import('./platform.js');
|
|
expect(typeof commandExists('jexec')).toBe('boolean');
|
|
expect(typeof commandExists('nonexistent_binary_xyz')).toBe('boolean');
|
|
});
|
|
});
|
|
|
|
describe('Telegram auth detection', () => {
|
|
it('detects TELEGRAM_BOT_TOKEN in env content', () => {
|
|
const content = 'TELEGRAM_BOT_TOKEN=123456:token';
|
|
const configured = /^TELEGRAM_BOT_TOKEN=.+/m.test(content);
|
|
expect(configured).toBe(true);
|
|
});
|
|
|
|
it('returns false when TELEGRAM_BOT_TOKEN is missing', () => {
|
|
const content = 'ASSISTANT_NAME="Andy"\nOTHER=foo';
|
|
const configured = /^TELEGRAM_BOT_TOKEN=.+/m.test(content);
|
|
expect(configured).toBe(false);
|
|
});
|
|
});
|