Files
gold_land_user/test/castle-exam.test.ts
T

42 lines
1.8 KiB
TypeScript
Raw Normal View History

import assert from 'node:assert/strict'
import test from 'node:test'
import { ApiNetworkError, StaleSessionError } from '../src/api/errors'
import { invalidateSession } from '../src/api/authSession'
import { bindMeMutationApply, resetMeMutationQueueForTests, runMeMutation } from '../src/api/meMutationQueue'
import { clearExamAnswers, sectionIds, unansweredCount } from '../src/modules/gold-land/castle-exam/castleState'
const questions = [
{ id: 'castle-q1', sectionId: 'basics' },
{ id: 'castle-q2', sectionId: 'basics' },
{ id: 'castle-q5', sectionId: 'market' },
{ id: 'castle-q9', sectionId: 'choices' },
]
test('sections keep answers while moving and incomplete exams do not submit', () => {
assert.deepEqual(sectionIds(questions), ['basics', 'market', 'choices'])
let answers: Record<string, string> = { 'castle-q1': 'exchange' }
answers = { ...answers, 'castle-q5': 'no' }
assert.equal(answers['castle-q1'], 'exchange')
assert.equal(unansweredCount(questions.map((question) => question.id), answers), 2)
assert.equal(unansweredCount(questions.map((question) => question.id), answers) === 0, false)
})
test('network and session failures keep or clear exam answers without applying me', async () => {
let answers: Record<string, string> = { 'castle-q1': 'exchange' }
resetMeMutationQueueForTests()
let applied = 0
bindMeMutationApply(() => {
applied += 1
return true
})
await assert.rejects(() => runMeMutation(async () => { throw new ApiNetworkError() }), ApiNetworkError)
assert.equal(answers['castle-q1'], 'exchange')
await assert.rejects(() => runMeMutation(async () => {
invalidateSession()
answers = clearExamAnswers()
return { me: { user: { coins: 1 } } }
}), StaleSessionError)
assert.equal(applied, 0)
assert.deepEqual(answers, {})
})