const assert = require('node:assert/strict')
const {
	applyGenerationStatus,
	replaceFirstPage,
	extractReportId,
	shouldResumePolling,
	reconcileStatusQueryError,
	createStatusRequestState,
	setStatusPageVisible,
	beginStatusRequest,
	shouldApplyStatusResponse,
	finishStatusRequest,
	createListRefreshState,
	beginListRefresh,
	createListRequestMeta,
	shouldApplyListResponse,
	beginListPageRequest,
	finishListPageRequest
} = require('../utils/reportGenerationStatus')

const failedState = applyGenerationStatus({ state:-1, errorMessage:'Step 4 failed' })
assert.equal(failedState.title, '报告生成失败')
assert.equal(failedState.errorMessage, 'Step 4 failed')
assert.equal(failedState.polling, false)

assert.deepEqual(
	replaceFirstPage([{ reportId:9, state:0 }], [{ reportId:9, state:-1 }]),
	[{ reportId:9, state:-1 }]
)
const nextFirstPage = [{ reportId:10, state:1 }]
const replacedFirstPage = replaceFirstPage([{ reportId:9, state:0 }], nextFirstPage)
assert.deepEqual(replacedFirstPage, nextFirstPage)
assert.notEqual(replacedFirstPage, nextFirstPage)
assert.equal(shouldResumePolling(9, { state:0 }), true)
assert.equal(shouldResumePolling(9, { state:1 }), false)
assert.equal(shouldResumePolling('', { state:0 }), false)

const retryAfterTransientError = reconcileStatusQueryError({ reportId:9, state:0, status:'RUNNING' })
assert.equal(retryAfterTransientError.state, 0)
assert.equal(retryAfterTransientError.polling, true)
assert.equal(retryAfterTransientError.title, '报告正在生成中~')
assert.equal(retryAfterTransientError.imageKey, 'success')

const statusState = createStatusRequestState()
assert.equal(beginStatusRequest(statusState), null)
setStatusPageVisible(statusState, true)
const firstStatusRequest = beginStatusRequest(statusState)
assert.deepEqual(firstStatusRequest, { requestId:1, visibilityToken:0 })
assert.equal(beginStatusRequest(statusState), null)
assert.equal(shouldApplyStatusResponse(statusState, firstStatusRequest), true)
setStatusPageVisible(statusState, false)
assert.equal(shouldApplyStatusResponse(statusState, firstStatusRequest), false)
setStatusPageVisible(statusState, true)
const resumedStatusRequest = beginStatusRequest(statusState)
assert.deepEqual(resumedStatusRequest, { requestId:2, visibilityToken:1 })
finishStatusRequest(statusState, resumedStatusRequest)
assert.equal(statusState.statusRequestInFlight, false)

const listRefreshState = createListRefreshState()
const firstGeneration = beginListRefresh(listRefreshState)
const secondGeneration = beginListRefresh(listRefreshState)
assert.equal(firstGeneration, 1)
assert.equal(secondGeneration, 2)
assert.equal(shouldApplyListResponse(listRefreshState.refreshGeneration, createListRequestMeta(firstGeneration, 1)), false)
assert.equal(shouldApplyListResponse(listRefreshState.refreshGeneration, createListRequestMeta(secondGeneration, 1)), true)
const pageRequest = beginListPageRequest(listRefreshState, secondGeneration, 2)
assert.deepEqual(pageRequest, { generation:2, page:2, key:'2:2' })
assert.equal(beginListPageRequest(listRefreshState, secondGeneration, 2), null)
finishListPageRequest(listRefreshState, pageRequest)
assert.deepEqual(beginListPageRequest(listRefreshState, secondGeneration, 2), { generation:2, page:2, key:'2:2' })

assert.equal(extractReportId(12), 12)
assert.equal(extractReportId({ reportId:13 }), 13)
assert.equal(extractReportId({ dto:{ reportId:14 } }), 14)
assert.equal(extractReportId({ info:{ id:15 } }), 15)

console.log('report generation status rules: PASS')
