const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const { parse, compileScript } = require('@vue/compiler-sfc'); const babel = require('@babel/parser'); const ref = value => ({ value }); function page(file, names, context, mounted = false) { const descriptor = parse(fs.readFileSync(path.resolve(__dirname, '../src/views/' + file), 'utf8')).descriptor; compileScript(descriptor, { id: 'remaining-columns' }); const script = descriptor.scriptSetup.content; const nodes = babel.parse(script, { sourceType: 'module' }).program.body; const selected = nodes.filter(n => n.type === 'FunctionDeclaration' && names.includes(n.id.name) || mounted && n.type === 'ExpressionStatement' && n.expression?.callee?.name === 'onMounted'); vm.createContext(context); vm.runInContext(selected.map(n => script.slice(n.start, n.end)).join('\n'), context); return context; } test('market selection drafts, transition and submission persist columns 5/6, then restore them', async () => { const writes = []; const c = page('product/market-opportunity-selection.vue', ['saveCurrentProgress', 'saveAndGoToStepSix', 'saveAndCompleteTraining', 'loadSavedAnswer'], { TASK_KEY: 'market-opportunity-selection', saving: ref(false), currentStep: ref(5), proxy: null, stepOnePassed: ref(true), stepTwoPassed: ref(true), stepThreePassed: ref(true), stepFourPassed: ref(true), stepFivePassed: ref(true), products: [], reviewInsights: ref({}), developmentConclusion: ref(''), createReviewInsights: () => ({}), hasCompleteReviewInsights: () => true, buildStepFiveOutcome: () => ({ reviewInsights: { product: { painPoint: '便携' } } }), buildStepSixOutcome: () => ({ developmentConclusion: '开发化妆镜' }), checkMarketOpportunitySelectionStepFive: async () => {}, checkMarketOpportunitySelectionStepSix: async () => {}, saveStudentTrainingAnswer: async (_, payload) => writes.push(payload), getStudentTrainingAnswer: async () => ({ data: { currentStep: 6, ...writes.at(-1) } }), }); await c.saveCurrentProgress(); await c.saveAndGoToStepSix(); await c.saveCurrentProgress(); await c.saveAndCompleteTraining(); for (const payload of writes) { assert.equal(JSON.parse(payload.step5Answer).reviewInsights.product.painPoint, '便携'); assert.equal(Object.hasOwn(payload, 'dynamicStepAnswers'), false); } assert.equal(JSON.parse(writes.at(-1).step6Answer).developmentConclusion, '开发化妆镜'); await c.loadSavedAnswer(); assert.equal(c.reviewInsights.value.product.painPoint, '便携'); assert.equal(c.developmentConclusion.value, '开发化妆镜'); }); test('pricing saves draft and final answers to column 5 and restores later-only data', async () => { const writes = []; const c = page('product/price.vue', ['saveStepFiveDraft', 'saveStepFive', 'buildStepFiveProgress', 'applyStepFiveProgress', 'loadSavedProgress', 'parseSavedJson'], { TASK_KEY: 'product-pricing', saving: ref(false), hasSavedAnswer: ref(false), currentStep: ref(1), proxy: null, selectedCostMarkupChannel: ref('线上'), finalPricingAnalysis: ref('建议售价399元'), stepFiveMethodResults: ref([{ measuredPrice: 399 }]), validateStepFive: () => '', validationMessage: ref(''), labels: {}, validateProductPricingStepFive: async () => ({}), saveStudentTrainingAnswer: async (_, payload) => writes.push(payload), getStudentTrainingAnswer: async () => ({ data: writes.at(-1) }), }); await c.saveStepFiveDraft(); await c.saveStepFive(); c.finalPricingAnalysis.value = ''; await c.loadSavedProgress(); assert.equal(c.finalPricingAnalysis.value, '建议售价399元'); assert.equal(c.currentStep.value, 5); for (const payload of writes) { assert.equal(JSON.parse(payload.step5Answer).methodResults[0].measuredPrice, 399); assert.equal(Object.hasOwn(payload, 'dynamicStepAnswers'), false); } }); test('category optimization saves and restores its fifth column without changing earlier answers', async () => { let saved, mounted; const earlier = { imageUrl: '/file/pareto.png' }; const c = page('assessment/optimization.vue', ['persistProgress', 'parseSavedJson'], { TASK_KEY: 'category-optimization', activeStep: ref(5), taskConfig: ref(null), buildProgress: () => ({ categoryRows: [1] }), buildStepTwoProgress: () => ({ rows: [2] }), buildStepThreeProgress: () => earlier, buildStepFourProgress: () => ({ rows: [4] }), buildStepFiveProgress: () => ({ rows: [{ layer: 'A', resourcePriority: '优先' }] }), saveStudentTrainingAnswer: async (_, payload) => { saved = payload; }, getStudentTrainingAnswer: async () => ({ data: { step5Answer: saved.step5Answer } }), getTrainingTaskByKey: async () => ({}), normalizeActiveStep: value => value, applyStepFiveProgress: value => { c.restored = value; }, onMounted: callback => { mounted = callback; }, }, true); await c.persistProgress('SUBMIT'); await mounted(); assert.deepEqual(JSON.parse(saved.step3Answer), earlier); assert.equal(c.restored.rows[0].resourcePriority, '优先'); assert.equal(saved.submitted, true); assert.equal(Object.hasOwn(saved, 'dynamicStepAnswers'), false); });