astro_avtourist/frontend/scripts/test-votes.ts

54 lines
No EOL
2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Тест голосования
import PocketBase from 'pocketbase';
const PB_URL = process.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
// Симулируем токен пользователя (нужно получить реальный)
const testToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb2xsZWN0aW9uSWQiOiJfcGJfdXNlcnNfYXV0aF8iLCJleHAiOjE3NzY5NTI4NjUsImlkIjoidGR0Z2JuNGFrb3BsZGhvIiwicmVmcmVzaGFibGUiOnRydWUsInR5cGUiOiJhdXRoIn0.A1oacYog9de5GjZj5aNkHeRDWyjQKXvTSkEBFr4hi9Q';
async function testVoting() {
const pb = new PocketBase(PB_URL);
// Симулируем авторизацию
pb.authStore.save(testToken, { id: 'tdtgbn4akopldho', email: 'redibedi2019@gmail.com' });
console.log('Auth valid:', pb.authStore.isValid);
console.log('User ID:', pb.authStore.model?.id);
// Тест: получить голоса поста
try {
const postId = 'test-post-id';
const votes = await pb.collection('post_votes').getList(1, 1000, {
filter: `post_id="${postId}"`,
});
console.log('\n--- Тест получения голосов ---');
console.log('Всего голосов:', votes.totalItems);
const likes = votes.items.filter(v => v.vote_type === 'like').length;
const dislikes = votes.items.filter(v => v.vote_type === 'dislike').length;
console.log('Likes:', likes);
console.log('Dislikes:', dislikes);
} catch (e) {
console.error('Ошибка:', e);
}
// Тест: создать голос
try {
console.log('\n--- Тест создания голоса ---');
const newVote = await pb.collection('post_votes').create({
post_id: 'test-post-123',
user_id: 'tdtgbn4akopldho',
vote_type: 'like',
});
console.log('Создан голос:', newVote.id);
// Удаляем тестовый голос
await pb.collection('post_votes').delete(newVote.id);
console.log('Удален тестовый голос');
} catch (e) {
console.error('Ошибка создания:', e);
}
}
testVoting();