MotorSignal-App/website/tests/community-personal-rides.te...

28 lines
1.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { NextRequest } from 'next/server';
import { GET } from '../app/api/group-rides/route';
const state = vi.hoisted(() => ({ rides: [] as Array<{ id: string; data: () => Record<string, unknown>; ref: unknown }> }));
vi.mock('../lib/firebase-admin', () => ({
getAdminAuth: () => ({ verifyIdToken: async () => ({ uid: 'viewer' }) }),
getAdminFirestore: () => ({ collection: (name: string) => {
if (name === 'users') return { doc: () => ({ collection: () => ({ get: async () => ({ docs: [] }), doc: () => ({ get: async () => ({ exists: false }) }) }) }) };
const query = { where: () => query, orderBy: () => query, limit: () => query, get: async () => ({ docs: state.rides }) };
return query;
} }),
}));
beforeEach(() => { state.rides = []; });
describe('community personal ride schema', () => {
it('discovers both absent and null clubId while excluding club-affiliated rides', async () => {
for (const [id, extra] of [['absent', {}], ['null', { clubId: null }], ['club', { clubId: 'real-club' }]] as const) {
state.rides.push({ id, data: () => ({ title: id, organizerUid: 'organizer', visibility: 'public', status: 'scheduled',
scheduledStartAt: new Date(Date.now() + 86_400_000).toISOString(), ...extra }),
ref: { collection: () => ({ doc: () => ({ get: async () => ({ data: () => undefined }) }) }) } });
}
const response = await GET(new NextRequest('http://localhost/api/group-rides', { headers: { authorization: 'Bearer isolated-token' } }));
expect(response.status).toBe(200);
expect((await response.json()).rides.map((ride: { id: string }) => ride.id)).toEqual(['absent', 'null']);
});
});