51 lines
3.2 KiB
TypeScript
51 lines
3.2 KiB
TypeScript
import { albumTracks, fakeApi, makeUser } from './fakeApi';
|
|
import { mobileFixtures } from './mobileFixtures';
|
|
|
|
/** Подменяет fetch на фейковый API. Включается только при сборке с VITE_E2E=1. */
|
|
export function installE2EFetch() {
|
|
const api = fakeApi([
|
|
makeUser('admin', 'admin', { must_change_credentials: true }),
|
|
makeUser('alice', 'user'),
|
|
makeUser('update-manager', 'user', { manages_updates: true }),
|
|
makeUser('mobile-admin', 'admin', { reviews_uploads: true }),
|
|
makeUser('moderator', 'moderator', { reviews_uploads: true }),
|
|
]);
|
|
// A ready conversation lets browser tests exercise messages and music sharing.
|
|
const alice = [...api.users.values()].find(u => u.username === 'alice')!;
|
|
const friend = [...api.users.values()].find(u => u.username === 'moderator')!;
|
|
api.friendships.set([alice.id, friend.id].sort().join(':'), { sender: friend.id, accepted: true });
|
|
const listeningScenario = localStorage.getItem('pipu.e2e.listening');
|
|
if (listeningScenario) {
|
|
const device = crypto.randomUUID();
|
|
api.presence.set(friend.id, Date.now());
|
|
api.activityVisibility.set(friend.id, 'friends');
|
|
api.activity.set(friend.id, { device_id: device, track_id: albumTracks[0]!.id, track_title: albumTracks[0]!.title, artist: 'Кино', context: 'other', position_ms: 0 });
|
|
api.listeningPreferences.set(friend.id, { requests: listeningScenario === 'restricted' ? 'nobody' : 'friends', auto_accept: listeningScenario === 'automatic' ? 'friends' : 'nobody', autoplay: true });
|
|
if (listeningScenario === 'incoming') {
|
|
const roomId = crypto.randomUUID();
|
|
const requestId = crypto.randomUUID();
|
|
api.listeningRooms.set(roomId, { id: roomId, owner_id: friend.id, owner_name: friend.username, device_id: device, track_id: albumTracks[0]!.id, position_ms: 0, playing: false, updated_at: new Date().toISOString(), participants: [] });
|
|
api.listeningMembers.set(friend.id, roomId);
|
|
api.listeningRequests.set(requestId, { id: requestId, owner_id: friend.id, guest_id: alice.id, sender_id: friend.id, sender_name: friend.username, recipient_id: alice.id, kind: 'invite', expires_at: new Date(Date.now() + 120_000).toISOString() });
|
|
}
|
|
}
|
|
if (localStorage.getItem('pipu.e2e.notifications') === '1') {
|
|
const kinds = ['message', 'friend_request', 'friend_accepted', 'music'] as const;
|
|
for (let index = 0; index < 28; index++) api.notifications.push({
|
|
id: `event-${index}`, recipient: alice.id, actor_id: friend.id,
|
|
actor_name: index % 3 === 0 ? 'JoyRacoon' : index % 3 === 1 ? 'Александра' : 'Константин',
|
|
kind: kinds[index % kinds.length]!, created_at: new Date(Date.now() - index * 36e5).toISOString(), read: index >= 4,
|
|
});
|
|
}
|
|
const orig = window.fetch.bind(window);
|
|
window.fetch = async (input, init) => {
|
|
const req = new Request(input, init);
|
|
const path = new URL(req.url, window.location.origin).pathname;
|
|
if (req.method === 'GET' && path in mobileFixtures) {
|
|
return new Response(JSON.stringify(mobileFixtures[path]), { headers: { 'Content-Type': 'application/json' } });
|
|
}
|
|
if (new URL(req.url, window.location.origin).pathname.startsWith('/api/')) return api.fetch(req);
|
|
return orig(input, init);
|
|
};
|
|
}
|