2026-08-18 11:10:27 +08:00
|
|
|
import { test } from 'node:test'
|
|
|
|
|
import assert from 'node:assert/strict'
|
|
|
|
|
import { addPoint, createMatch, setFirstServer, settleMatch } from '../src/match.js'
|
2026-08-24 22:24:16 +08:00
|
|
|
import { buildHistoryPayload, dayStartUnix, loadTodayGames, parseGameRow, saveHistory } from '../src/history.js'
|
2026-08-18 11:10:27 +08:00
|
|
|
|
|
|
|
|
test('buildHistoryPayload:欄位對齊 web 版 history(雙打)', () => {
|
|
|
|
|
const m = createMatch(['Gary', '蓉蓉'], ['柏威', '小念'])
|
|
|
|
|
setFirstServer(m, 0)
|
|
|
|
|
addPoint(m, 0)
|
|
|
|
|
addPoint(m, 1)
|
|
|
|
|
settleMatch(m)
|
|
|
|
|
const now = new Date('2026-08-17T20:30:00+08:00') // 週一
|
|
|
|
|
const p = buildHistoryPayload(m, now)
|
|
|
|
|
assert.deepEqual(p, {
|
|
|
|
|
time: Math.floor(now.getTime() / 1000),
|
|
|
|
|
dayOfWeek: 1,
|
|
|
|
|
score: [1, 1],
|
|
|
|
|
winScore: 21,
|
|
|
|
|
type: 0,
|
|
|
|
|
players: ['Gary', '蓉蓉', '柏威', '小念'],
|
|
|
|
|
team: [['Gary', '蓉蓉'], ['柏威', '小念']],
|
|
|
|
|
scoreList: [[0, 0, 0, 0], [1, 1, 0, 1]],
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('buildHistoryPayload:單打 players 以 [p1,p1,p3,p3] 讓 players[starter] 對得到人', () => {
|
|
|
|
|
const m = createMatch(['A'], ['B'], 15)
|
|
|
|
|
setFirstServer(m, 1)
|
|
|
|
|
addPoint(m, 1)
|
|
|
|
|
const p = buildHistoryPayload(m)
|
|
|
|
|
assert.equal(p.type, 1)
|
|
|
|
|
assert.equal(p.winScore, 15)
|
|
|
|
|
assert.deepEqual(p.players, ['A', 'A', 'B', 'B'])
|
|
|
|
|
assert.deepEqual(p.team, [['A'], ['B']])
|
|
|
|
|
assert.equal(p.players[p.scoreList[0][1]], 'B')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('saveHistory:先 ensure table 再 INSERT,回傳 insertId', async () => {
|
|
|
|
|
const sqls = []
|
|
|
|
|
const query = async (sql, params) => {
|
|
|
|
|
sqls.push({ sql: sql.replace(/\s+/g, ' ').trim(), params })
|
|
|
|
|
return sql.includes('INSERT') ? { insertId: 42 } : []
|
|
|
|
|
}
|
|
|
|
|
const m = createMatch(['A', 'B'], ['C', 'D'])
|
|
|
|
|
setFirstServer(m, 0)
|
|
|
|
|
addPoint(m, 0)
|
|
|
|
|
const id = await saveHistory(m, { query, table: 'history_test' })
|
|
|
|
|
assert.equal(id, 42)
|
|
|
|
|
assert.match(sqls[0].sql, /CREATE TABLE IF NOT EXISTS `history_test`/)
|
|
|
|
|
assert.match(sqls[1].sql, /INSERT INTO `history_test` \(time, dayOfWeek, score, winScore, type, players, team, scoreList\)/)
|
|
|
|
|
assert.equal(sqls[1].params[2], '[1,0]')
|
|
|
|
|
assert.equal(sqls[1].params[5], '["A","B","C","D"]')
|
|
|
|
|
assert.equal(sqls[1].params[6], '[["A","B"],["C","D"]]')
|
|
|
|
|
assert.equal(sqls[1].params[7], '[[0,0,0,0]]')
|
|
|
|
|
})
|
2026-08-24 22:24:16 +08:00
|
|
|
|
|
|
|
|
test('loadTodayGames:只抓當天(台北時間)的 history,依時間排序並解析 team;壞掉的列略過', async () => {
|
|
|
|
|
assert.equal(dayStartUnix('20260824'), Math.floor(Date.parse('2026-08-24T00:00:00+08:00') / 1000))
|
|
|
|
|
assert.deepEqual(parseGameRow({ id: 1, time: '10', team: '[["A","B"],["C","D"]]' }), {
|
|
|
|
|
id: 1,
|
|
|
|
|
time: 10,
|
|
|
|
|
teams: [['A', 'B'], ['C', 'D']],
|
|
|
|
|
})
|
|
|
|
|
assert.deepEqual(parseGameRow({ id: 2, time: 10, team: '[["A"],["B"]]' }).teams, [['A'], ['B']], '單打')
|
|
|
|
|
assert.equal(parseGameRow({ id: 3, time: 10, team: 'oops' }), null)
|
|
|
|
|
assert.equal(parseGameRow({ id: 4, time: 10, team: '[["A","B"]]' }), null)
|
|
|
|
|
|
|
|
|
|
const sqls = []
|
|
|
|
|
const query = async (sql, params) => {
|
|
|
|
|
sqls.push({ sql: sql.replace(/\s+/g, ' ').trim(), params })
|
|
|
|
|
return [
|
|
|
|
|
{ id: 7, time: 100, team: '[["A","B"],["C","D"]]' },
|
|
|
|
|
{ id: 8, time: 200, team: 'bad' },
|
|
|
|
|
{ id: 9, time: 300, team: '[["E"],["F"]]' },
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
const games = await loadTodayGames({ query, table: 'history_test', date: '20260824' })
|
|
|
|
|
const start = dayStartUnix('20260824')
|
|
|
|
|
assert.match(sqls[0].sql, /SELECT id, time, team FROM `history_test` WHERE time >= \? AND time < \? ORDER BY time ASC, id ASC/)
|
|
|
|
|
assert.deepEqual(sqls[0].params, [start, start + 86400])
|
|
|
|
|
assert.deepEqual(games.map((g) => g.id), [7, 9])
|
|
|
|
|
assert.deepEqual(games[1].teams, [['E'], ['F']])
|
|
|
|
|
})
|