45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
|
|
import { describe, expect, it } from 'vitest'
|
||
|
|
import { cleanName, summarize, type ScoreBoard } from '../server/utils/puzzles'
|
||
|
|
|
||
|
|
describe('leaderboard names', () => {
|
||
|
|
it('keeps letters, digits and a little punctuation', () => {
|
||
|
|
expect(cleanName(' Jörg der Elektriker ')).toBe('Jörg der Elektriker')
|
||
|
|
expect(cleanName('<script>alert(1)</script>')).toBe('scriptalert1script')
|
||
|
|
expect(cleanName("O'Neil #1 & co!")).toBe("O'Neil #1 & co!")
|
||
|
|
})
|
||
|
|
|
||
|
|
it('is at most 24 characters, and empty for anything but text', () => {
|
||
|
|
expect(cleanName('x'.repeat(40))).toHaveLength(24)
|
||
|
|
expect(cleanName(42)).toBe('')
|
||
|
|
expect(cleanName(null)).toBe('')
|
||
|
|
})
|
||
|
|
})
|
||
|
|
|
||
|
|
describe('leaderboard top ten', () => {
|
||
|
|
const board: ScoreBoard = {}
|
||
|
|
for (let i = 0; i < 14; i++) board[`player-${String(i).padStart(4, '0')}`] = { t: 40 + (i % 5) * 4, d: 10 - (i % 3), at: 1000 + i }
|
||
|
|
|
||
|
|
it('ranks fewest transistors, then shortest delay, then who got there first', () => {
|
||
|
|
const s = summarize(board, undefined, { 'player-0000': 'Ada' })
|
||
|
|
expect(s.players).toBe(14)
|
||
|
|
expect(s.top).toHaveLength(10)
|
||
|
|
// t = 40 for i = 0, 5, 10; delays 10, 8, 9.
|
||
|
|
expect(s.top.slice(0, 3).map(e => [e.t, e.d])).toEqual([
|
||
|
|
[40, 8],
|
||
|
|
[40, 9],
|
||
|
|
[40, 10],
|
||
|
|
])
|
||
|
|
expect(s.top[2]!.name).toBe('Ada')
|
||
|
|
expect(s.top[0]!.name).toBe('')
|
||
|
|
for (let i = 1; i < s.top.length; i++) expect(s.top[i]!.t).toBeGreaterThanOrEqual(s.top[i - 1]!.t)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('marks your own entry and your place', () => {
|
||
|
|
const s = summarize(board, 'player-0004', {})
|
||
|
|
expect(s.you).toEqual({ t: 56, d: 9, rank: 13 })
|
||
|
|
expect(s.top.some(e => e.you)).toBe(false)
|
||
|
|
const t = summarize(board, 'player-0005', {})
|
||
|
|
expect(t.top.find(e => e.you)).toMatchObject({ t: 40, d: 8 })
|
||
|
|
})
|
||
|
|
})
|