import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import { fileURLToPath } from 'node:url' const detailViewTemplate = readFileSync( fileURLToPath(new URL('../src/views/TravelRequestDetailView.vue', import.meta.url)), 'utf8' ) const detailViewScript = readFileSync( fileURLToPath(new URL('../src/views/scripts/TravelRequestDetailView.js', import.meta.url)), 'utf8' ) const detailExpenseModelScript = readFileSync( fileURLToPath(new URL('../src/views/scripts/travelRequestDetailExpenseModel.js', import.meta.url)), 'utf8' ) const confirmDialogComponent = readFileSync( fileURLToPath(new URL('../src/components/shared/ConfirmDialog.vue', import.meta.url)), 'utf8' ) function extractFunction(source, name) { let signatureIndex = source.indexOf(`function ${name}(`) if (signatureIndex === -1) { signatureIndex = source.indexOf(`async function ${name}(`) } assert.notEqual(signatureIndex, -1, `${name} should exist`) const bodyStart = source.indexOf('{', signatureIndex) assert.notEqual(bodyStart, -1, `${name} should have a body`) let depth = 0 for (let index = bodyStart; index < source.length; index += 1) { const char = source[index] if (char === '{') { depth += 1 } else if (char === '}') { depth -= 1 if (depth === 0) { return source.slice(signatureIndex, index + 1) } } } assert.fail(`${name} body should be closed`) } test('detail submit opens a confirmation dialog before calling submit API', () => { assert.match(detailViewTemplate, / { assert.match(detailViewTemplate, /:open="riskOverrideDialogOpen"/) assert.match(detailViewTemplate, /:open="riskOverrideDialogOpen"[\s\S]*size="review"/) assert.match(detailViewTemplate, /异常说明/) assert.match(detailViewTemplate, /:title="riskOverrideDialogTitle"/) assert.match(detailViewTemplate, /:description="riskOverrideDialogDescription"/) assert.match(detailViewTemplate, /:confirm-text="riskOverrideConfirmText"/) assert.match(detailViewTemplate, /@confirm="confirmRiskOverrideDialog"/) assert.match(detailViewTemplate, /class="risk-override-card-shell"/) assert.match(detailViewTemplate, /class="risk-override-side-nav risk-override-side-nav--previous"/) assert.match(detailViewTemplate, /class="risk-override-side-nav risk-override-side-nav--next"/) assert.match(detailViewTemplate, /class="risk-override-guidance"/) assert.match(detailViewTemplate, /goToPreviousSubmitRisk/) assert.match(detailViewTemplate, /goToNextSubmitRisk/) assert.doesNotMatch(detailViewTemplate, /class="risk-override-nav"/) assert.doesNotMatch(detailViewTemplate, /v-model="riskOverrideReasons\[currentSubmitRiskWarning\.id\]"/) assert.doesNotMatch(detailViewTemplate, /risk-override-save-btn/) assert.doesNotMatch(detailViewTemplate, /confirmRiskOverrideReasons/) assert.match(detailViewScript, /const submitRiskWarnings = computed/) assert.match(detailViewScript, /const submitExplainedRiskWarnings = computed/) assert.match(detailViewScript, /const submitRiskReviewWarnings = computed/) assert.match(detailViewScript, /const hasMissingSubmitRiskWarnings = computed/) assert.match(detailViewScript, /const riskOverrideConfirmText = computed\(\(\) =>[\s\S]*确认说明/) const handleSubmit = extractFunction(detailViewScript, 'handleSubmit') const confirmSubmitRequest = extractFunction(detailViewScript, 'confirmSubmitRequest') assert.match(handleSubmit, /submitRiskReviewWarnings\.value\.length[\s\S]*openRiskOverrideDialog\(\)/) assert.doesNotMatch(confirmSubmitRequest, /openRiskOverrideDialog/) assert.doesNotMatch(detailViewScript, /riskOverrideReasons/) assert.doesNotMatch(detailViewScript, /function confirmRiskOverrideReasons\(\)/) assert.match(detailViewScript, /function confirmRiskOverrideDialog\(\)/) assert.match(detailViewScript, /function confirmRiskExplanation\(\)/) assert.match(detailViewScript, /function confirmStandardAdjustment\(\)/) assert.match(detailViewTemplate, /v-if="standardAdjustmentBusy" class="expense-recognition-banner standard-adjustment-banner"/) assert.match(detailViewScript, /const standardAdjustmentBusy = ref\(false\)/) const confirmRiskExplanation = extractFunction(detailViewScript, 'confirmRiskExplanation') assert.match(confirmRiskExplanation, /riskOverrideDialogOpen\.value = false[\s\S]*submitConfirmDialogOpen\.value = true/) const confirmStandardAdjustment = extractFunction(detailViewScript, 'confirmStandardAdjustment') assert.match(confirmStandardAdjustment, /const claimId = String\(request\.value\?\.claimId/) assert.match(confirmStandardAdjustment, /riskOverrideDialogOpen\.value = false[\s\S]*standardAdjustmentBusy\.value = true[\s\S]*void runStandardAdjustmentRecalculation\(claimId, taskSeq\)/) assert.doesNotMatch(confirmStandardAdjustment, /await /) assert.doesNotMatch(confirmStandardAdjustment, /acceptExpenseClaimStandardAdjustment/) const runStandardAdjustmentRecalculation = extractFunction(detailViewScript, 'runStandardAdjustmentRecalculation') assert.match(runStandardAdjustmentRecalculation, /acceptExpenseClaimStandardAdjustment\(claimId, payload\)/) assert.doesNotMatch(runStandardAdjustmentRecalculation, /submitConfirmDialogOpen\.value = true/) assert.match(detailViewScript, /buildStandardAdjustmentPayloadModel\(\{[\s\S]*warnings:\s*submitRiskCards\.value/) const actionBusyStart = detailViewScript.indexOf('const actionBusy = computed') const actionBusyEnd = detailViewScript.indexOf('const profile = computed', actionBusyStart) assert.ok(actionBusyStart > -1 && actionBusyEnd > actionBusyStart) assert.doesNotMatch(detailViewScript.slice(actionBusyStart, actionBusyEnd), /standardAdjustmentBusy/) assert.match(detailExpenseModelScript, /STANDARD_ADJUSTMENT_RISK_SOURCE = 'reimbursement_standard_adjustment'/) }) test('submit confirm dialog is constrained for laptop viewport height', () => { assert.match(confirmDialogComponent, /max-height:\s*calc\(100vh - 40px\)/) assert.match(confirmDialogComponent, /max-height:\s*calc\(100dvh - 40px\)/) assert.match(confirmDialogComponent, /\.shared-confirm-body \{[\s\S]*min-height: 0;[\s\S]*overflow-y: auto;/) assert.match(confirmDialogComponent, /\.shared-confirm-actions \{[\s\S]*flex: 0 0 auto;/) assert.match(confirmDialogComponent, /\.shared-confirm-card--review \{[\s\S]*width: min\(560px, calc\(100vw - 40px\)\);/) assert.match(confirmDialogComponent, /@media \(max-width: 720px\) \{[\s\S]*max-height: calc\(100dvh - 28px\)/) }) test('detail header and fallback progress use reimbursement wording', () => { assert.match(detailViewScript, /label:\s*'单据申请日期'/) assert.match(detailExpenseModelScript, /label:\s*'关联单据'/) assert.match(detailExpenseModelScript, /label:\s*'已归档'/) assert.doesNotMatch(detailViewScript, /label:\s*'保存草稿'/) }) test('detail delete action is gated by admin-only permission', () => { assert.match(detailViewScript, /const canDeleteRequest = computed\(\(\) => isPlatformAdminUser\(currentUser\.value\)\)/) assert.match(detailViewTemplate, /v-else-if="canReturnRequest \|\| canApproveRequest \|\| canPayRequest \|\| canDeleteRequest"/) assert.doesNotMatch(detailViewTemplate, /v-if="canManageCurrentClaim"/) }) test('detail delete action does not allow applicant or claim manager fallback', () => { assert.doesNotMatch(detailViewScript, /const canDeleteRequest = computed\(\(\) => \{[\s\S]*isCurrentApplicant[\s\S]*\}\)/) assert.doesNotMatch(detailViewScript, /const canDeleteRequest = computed\(\(\) => \{[\s\S]*canManageCurrentClaim[\s\S]*\}\)/) assert.match(detailViewScript, /if \(isApplicationDocument\.value\) {\s*return '删除申请'\s*}/) assert.match(detailViewScript, /当前申请单已进入审批流程,只有退回后申请人本人或系统管理员可以删除。/) })