move tests to src/

This commit is contained in:
2026-03-13 17:42:34 +01:00
parent 893309b308
commit 470b92d0bd
2 changed files with 4 additions and 4 deletions

View File

@@ -0,0 +1,97 @@
import { beforeEach, describe, expect, test, mock } from 'bun:test';
import { MATCH_STATES } from './types';
const mocks = {
db: {
query: {
match: { findFirst: mock() },
matchParticipant: { findMany: mock() },
matchGame: { findMany: mock() },
mappoolSlot: { findFirst: mock() }
},
insert: mock(),
update: mock(),
delete: mock(),
select: mock()
}
};
mock.module('$lib/server/db', () => ({ db: mocks.db }));
mock.module('drizzle-orm', () => ({
eq: mock(() => ({})),
and: mock(() => ({})),
asc: mock(() => ({})),
lt: mock(() => ({})),
inArray: mock(() => ({})),
sql: mock(() => ({})),
isNotNull: mock(() => ({}))
}));
mock.module('$lib/server/db/schema', () => ({
match: {},
matchParticipant: {},
matchParticipantPlayer: {},
matchGame: {},
matchGameScore: {},
matchQueue: {},
mappoolSlot: {},
playerRating: {},
teamMember: {}
}));
mock.module('$lib/server/db/auth.schema', () => ({ account: {} }));
mock.module('$lib/server/discord/client', () => ({
notifyMatchCreated: mock(),
notifyMatchFinished: mock()
}));
mock.module('$lib/server/osu/api', () => ({
getUser: mock()
}));
const { pickMap } = await import('./engine');
describe('pickMap', () => {
beforeEach(() => {
mocks.db.query.match.findFirst.mockReset();
mocks.db.query.matchParticipant.findMany.mockReset();
mocks.db.query.matchGame.findMany.mockReset();
mocks.db.query.mappoolSlot.findFirst.mockReset();
mocks.db.insert.mockReset();
mocks.db.update.mockReset();
mocks.db.delete.mockReset();
mocks.db.select.mockReset();
mocks.db.query.match.findFirst.mockResolvedValue({
id: 'match-1',
state: MATCH_STATES.PICKING,
config: { bestOf: 5, teamSize: 1, scoringType: 'score_v2' }
});
mocks.db.query.matchParticipant.findMany.mockResolvedValue([
{ id: 'p1', pickOrder: 1, score: 1 },
{ id: 'p2', pickOrder: 2, score: 1 }
]);
mocks.db.query.matchGame.findMany.mockResolvedValue([]);
});
test('throws when a non-current picker tries to pick', async () => {
await expect(pickMap('match-1', 'p2', 'slot-1')).rejects.toThrow('Not your turn to pick');
expect(mocks.db.query.mappoolSlot.findFirst).not.toHaveBeenCalled();
});
test('rejects tiebreaker picks before match point', async () => {
mocks.db.query.mappoolSlot.findFirst.mockResolvedValue({
id: 'slot-tb',
category: 'TB'
});
await expect(pickMap('match-1', 'p1', 'slot-tb')).rejects.toThrow(
'Tiebreaker can only be picked at match point'
);
expect(mocks.db.insert).not.toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,97 @@
import { beforeEach, describe, expect, test, mock } from 'bun:test';
import { MATCH_STATES } from './types';
const mocks = {
db: {
query: {
matchParticipantPlayer: { findMany: mock() },
playerRating: { findFirst: mock() },
matchQueue: { findFirst: mock() }
},
delete: mock()
}
};
mock.module('$lib/server/db', () => ({ db: mocks.db }));
mock.module('drizzle-orm', () => ({
eq: mock(() => ({})),
and: mock(() => ({})),
asc: mock(() => ({})),
lt: mock(() => ({})),
inArray: mock(() => ({})),
sql: mock(() => ({})),
isNotNull: mock(() => ({}))
}));
mock.module('$lib/server/db/schema', () => ({
match: {},
matchQueue: {},
matchParticipantPlayer: {},
playerRating: {}
}));
mock.module('$lib/server/db/auth.schema', () => ({ account: {} }));
mock.module('$lib/server/osu/api', () => ({
getUser: mock()
}));
mock.module('$lib/server/match/engine', () => ({
createMatch: mock()
}));
const { joinQueue } = await import('./queue');
describe('joinQueue', () => {
beforeEach(() => {
mocks.db.query.matchParticipantPlayer.findMany.mockReset();
mocks.db.query.playerRating.findFirst.mockReset();
mocks.db.query.matchQueue.findFirst.mockReset();
mocks.db.delete.mockReset();
});
test('throws when user is already in an active match', async () => {
mocks.db.delete.mockReturnValue({
where: mock(() => ({ returning: mock(() => Promise.resolve([])) }))
});
mocks.db.query.matchParticipantPlayer.findMany.mockResolvedValue([
{
participant: {
match: { id: 'match-1', state: MATCH_STATES.PLAYING, createdAt: new Date().toISOString() }
}
}
]);
await expect(joinQueue('user-1', 'team-1')).rejects.toThrow(
'You are already in an active match'
);
expect(mocks.db.query.playerRating.findFirst).not.toHaveBeenCalled();
});
test('throws when user has no rating', async () => {
mocks.db.delete.mockReturnValue({
where: mock(() => ({ returning: mock(() => Promise.resolve([])) }))
});
mocks.db.query.matchParticipantPlayer.findMany.mockResolvedValue([]);
mocks.db.query.playerRating.findFirst.mockResolvedValue(null);
await expect(joinQueue('user-1', 'team-1')).rejects.toThrow(
'No rating found — please re-register'
);
});
test('throws when user is already in queue', async () => {
mocks.db.delete.mockReturnValue({
where: mock(() => ({ returning: mock(() => Promise.resolve([])) }))
});
mocks.db.query.matchParticipantPlayer.findMany.mockResolvedValue([]);
mocks.db.query.playerRating.findFirst.mockResolvedValue({ elo: 1500 });
mocks.db.query.matchQueue.findFirst.mockResolvedValue({ userId: 'user-1', teamId: 'team-1' });
await expect(joinQueue('user-1', 'team-1')).rejects.toThrow('Already in queue');
});
});