feat: migrate from npm to Bun
- Install Bun via mise - Update package.json scripts to use Bun - Migrate unit tests from Vitest to Bun test runner - Migrate component tests from Vitest to Bun test runner - Configure Bun with DOM environment for React Testing Library - Keep Playwright for E2E tests (as planned) - 93/100 tests passing (7 flaky tests when running all together, pass individually)
This commit is contained in:
@@ -5,25 +5,25 @@
|
||||
* Regression tests for the issue where tournament_admin users were redirected to login
|
||||
*/
|
||||
|
||||
import { describe, test, expect, vi, beforeEach } from 'vitest';
|
||||
import { describe, test, expect, mock, beforeEach } from 'bun:test';
|
||||
import { canManageTournament, ownsTournament, getManageableTournaments } from '@/lib/permissions';
|
||||
import { getSession } from '@/lib/auth-simple';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import type { User, Event } from '@prisma/client';
|
||||
|
||||
// Mock the getSession and prisma functions
|
||||
vi.mock('@/lib/auth-simple', () => ({
|
||||
getSession: vi.fn(),
|
||||
mock.module('@/lib/auth-simple', () => ({
|
||||
getSession: mock(() => {}),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
prisma: {
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
findUnique: mock(() => {}),
|
||||
},
|
||||
event: {
|
||||
findUnique: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
findUnique: mock(() => {}),
|
||||
findMany: mock(() => {}),
|
||||
},
|
||||
},
|
||||
}));
|
||||
@@ -61,16 +61,16 @@ const createMockTournament = (id: number, ownerId: string | null): Event => ({
|
||||
|
||||
describe('Tournament Permissions', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mock.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('canManageTournament', () => {
|
||||
test('should allow club_admin to manage any tournament', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
||||
);
|
||||
|
||||
@@ -79,14 +79,14 @@ describe('Tournament Permissions', () => {
|
||||
});
|
||||
|
||||
test('should allow tournament_admin to manage their own tournament', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'tour-admin-1', email: 'tour@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('tour-admin-1', 'tour@example.com', 'tournament_admin')
|
||||
);
|
||||
vi.mocked(prisma.event.findUnique).mockResolvedValue(
|
||||
prisma.event.findUnique.mockImplementation(async () =>
|
||||
createMockTournament(1, 'tour-admin-1')
|
||||
);
|
||||
|
||||
@@ -95,14 +95,14 @@ describe('Tournament Permissions', () => {
|
||||
});
|
||||
|
||||
test('should deny tournament_admin from managing other users tournaments', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'tour-admin-1', email: 'tour@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('tour-admin-1', 'tour@example.com', 'tournament_admin')
|
||||
);
|
||||
vi.mocked(prisma.event.findUnique).mockResolvedValue(
|
||||
prisma.event.findUnique.mockImplementation(async () =>
|
||||
createMockTournament(1, 'other-user-1')
|
||||
);
|
||||
|
||||
@@ -112,11 +112,11 @@ describe('Tournament Permissions', () => {
|
||||
});
|
||||
|
||||
test('should deny player from managing tournaments', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'player-1', email: 'player@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('player-1', 'player@example.com', 'player')
|
||||
);
|
||||
|
||||
@@ -126,7 +126,7 @@ describe('Tournament Permissions', () => {
|
||||
});
|
||||
|
||||
test('should deny unauthenticated user', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue(null);
|
||||
getSession.mockImplementation(async () => null);
|
||||
|
||||
const result = await canManageTournament(999);
|
||||
expect(result.allowed).toBe(false);
|
||||
@@ -136,11 +136,11 @@ describe('Tournament Permissions', () => {
|
||||
|
||||
describe('ownsTournament', () => {
|
||||
test('should return true if user owns tournament', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'owner-1', email: 'owner@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.event.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.event.findUnique.mockImplementation(async () =>
|
||||
createMockTournament(1, 'owner-1')
|
||||
);
|
||||
|
||||
@@ -149,11 +149,11 @@ describe('Tournament Permissions', () => {
|
||||
});
|
||||
|
||||
test('should return false if user does not own tournament', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'non-owner-1', email: 'nonowner@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.event.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.event.findUnique.mockImplementation(async () =>
|
||||
createMockTournament(1, 'owner-1')
|
||||
);
|
||||
|
||||
@@ -165,11 +165,11 @@ describe('Tournament Permissions', () => {
|
||||
|
||||
describe('getManageableTournaments', () => {
|
||||
test('should return all tournaments for club_admin', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
||||
);
|
||||
|
||||
@@ -178,7 +178,7 @@ describe('Tournament Permissions', () => {
|
||||
createMockTournament(2, 'user-2'),
|
||||
createMockTournament(3, 'user-3'),
|
||||
];
|
||||
vi.mocked(prisma.event.findMany).mockResolvedValue(mockTournaments);
|
||||
prisma.event.findMany.mockImplementation(async () => mockTournaments);
|
||||
|
||||
const result = await getManageableTournaments();
|
||||
expect(result).toEqual(mockTournaments);
|
||||
@@ -190,11 +190,11 @@ describe('Tournament Permissions', () => {
|
||||
});
|
||||
|
||||
test('should return only owned tournaments for tournament_admin', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'tour-admin-1', email: 'tour@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('tour-admin-1', 'tour@example.com', 'tournament_admin')
|
||||
);
|
||||
|
||||
@@ -202,7 +202,7 @@ describe('Tournament Permissions', () => {
|
||||
createMockTournament(1, 'tour-admin-1'),
|
||||
createMockTournament(2, 'tour-admin-1'),
|
||||
];
|
||||
vi.mocked(prisma.event.findMany).mockResolvedValue(mockTournaments);
|
||||
prisma.event.findMany.mockImplementation(async () => mockTournaments);
|
||||
|
||||
const result = await getManageableTournaments();
|
||||
expect(result).toEqual(mockTournaments);
|
||||
@@ -217,11 +217,11 @@ describe('Tournament Permissions', () => {
|
||||
});
|
||||
|
||||
test('should return only non-draft tournaments for players', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'player-1', email: 'player@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('player-1', 'player@example.com', 'player')
|
||||
);
|
||||
|
||||
@@ -229,7 +229,7 @@ describe('Tournament Permissions', () => {
|
||||
createMockTournament(1, 'user-1'),
|
||||
createMockTournament(2, 'user-2'),
|
||||
];
|
||||
vi.mocked(prisma.event.findMany).mockResolvedValue(mockTournaments);
|
||||
prisma.event.findMany.mockImplementation(async () => mockTournaments);
|
||||
|
||||
const result = await getManageableTournaments();
|
||||
expect(result).toEqual(mockTournaments);
|
||||
@@ -249,14 +249,14 @@ describe('Tournament Permissions', () => {
|
||||
// This simulates the scenario where a tournament_admin user
|
||||
// clicks "Edit" on a tournament they own
|
||||
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'tour-admin-1', email: 'tour@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('tour-admin-1', 'tour@example.com', 'tournament_admin')
|
||||
);
|
||||
vi.mocked(prisma.event.findUnique).mockResolvedValue(
|
||||
prisma.event.findUnique.mockImplementation(async () =>
|
||||
createMockTournament(1, 'tour-admin-1')
|
||||
);
|
||||
|
||||
@@ -271,11 +271,11 @@ describe('Tournament Permissions', () => {
|
||||
test('club_admin should still be able to manage any tournament', async () => {
|
||||
// This ensures we didn't break the existing club_admin functionality
|
||||
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'club-admin-1', email: 'club@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('club-admin-1', 'club@example.com', 'club_admin')
|
||||
);
|
||||
|
||||
@@ -286,11 +286,11 @@ describe('Tournament Permissions', () => {
|
||||
test('players should still be denied from managing tournaments', async () => {
|
||||
// This ensures we didn't accidentally grant players access
|
||||
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'player-1', email: 'player@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('player-1', 'player@example.com', 'player')
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user