22 lines
1.2 KiB
JavaScript
22 lines
1.2 KiB
JavaScript
import { loadConfig } from '../src/config.js';
|
|
import { AccessStore } from '../src/store.js';
|
|
import { AccessService } from '../src/bots/service.js';
|
|
|
|
export function fixture(options = {}) {
|
|
let time = Date.now();
|
|
const config = { ...loadConfig({ SESSION_SECRET: 'test-secret-with-at-least-32-characters', TELEGRAM_ADMIN_IDS: '100,101', DISCORD_ADMIN_IDS: '200' }), ...options };
|
|
const store = new AccessStore(config, { memory: true, now: () => time });
|
|
const messages = [];
|
|
const provider = { async sendUser(userId, text, buttons = []) { messages.push({ userId, text, buttons }); } };
|
|
const service = new AccessService(config, store, { telegram: provider, discord: provider });
|
|
return { config, store, service, messages, provider, advance: ms => { time += ms; }, now: () => time };
|
|
}
|
|
|
|
export async function grant(f, userId = '5') {
|
|
await f.service.request({ platform: 'telegram', user_id: userId, username: 'adventurer' });
|
|
const request = f.store.state.requests.at(-1);
|
|
await f.service.decide('telegram', '100', request.id, 'approve');
|
|
const code = f.messages.findLast(m => m.text.includes('Your number:')).text.match(/Your number: (\d+)/)[1];
|
|
return { code, request: f.store.getRequest(request.id) };
|
|
}
|