You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dianshang-qianduan/tests/training-step-columns.test.cjs

67 lines
4.5 KiB
JavaScript

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, compileTemplate } = require('@vue/compiler-sfc');
const babel = require('@babel/parser');
const fields = require('../src/views/training/trainingStepAnswerFields.json');
const files = {
'target-user-profile': 'demand/people.vue', 'consumer-behavior': 'demand/consumer-behavior.vue',
'consumer-scenario': 'demand/consumer-scenario.vue', 'feature-conversion': 'demand/feature-conversion.vue',
'product-value-positioning': 'product/differentiation.vue', 'purchase-demand-forecast': 'channel/index.vue',
'comprehensive-case-training': 'training/GenericTrainingPage.vue',
};
for (const [taskKey, file] of Object.entries(files)) test(taskKey + ' saves each panel into its own step column and restores it', async () => {
const helpers = await import('../src/views/training/trainingStepAnswers.mjs');
const combined = {};
fields[taskKey].flat().forEach(key => { combined[key] = { evidence: taskKey + ':' + key }; });
const filename = path.resolve(__dirname, '../src/views/' + file);
const descriptor = parse(fs.readFileSync(filename, 'utf8')).descriptor;
compileScript(descriptor, { id: 'step-columns' });
assert.deepEqual(compileTemplate({ source: descriptor.template.content, filename, id: 'step-columns' }).errors, []);
const script = descriptor.scriptSetup.content;
const nodes = babel.parse(script, { sourceType: 'module' }).program.body;
const functions = nodes.filter(n => n.type === 'FunctionDeclaration' && ['buildAnswerPayload', 'buildOutcome', 'buildProgress'].includes(n.id.name));
const ref = value => ({ value });
const context = { buildProgress: () => combined, ...helpers, TASK_KEY: taskKey, taskConfig: ref({ id: 'task' }), taskTitle: ref('实训'),
flowSteps: ref(['一', '二', '三', '四']), configuredSteps: ref(['一', '二', '三', '四']), currentStep: ref(2),
form: ref({ ...combined, activeStep: taskKey === 'consumer-behavior' ? 1 : 2 }), syncQuestionnaireText() {},
phases: [], journeyCheckedCorrectly: ref(true), pageKey: ref(taskKey), page: ref({ title: '综合实训' }),
workRows: ref([]), processRows: ref([]), activeStep: ref(2),
linearForecastRows: ref(combined.linearForecastRows), procurementPlanRows: ref(combined.procurementPlanRows),
demandForecastChart: ref(combined.demandForecastChart), forecastMethod: ref(combined.forecastMethod), purchasePlan: ref(combined.purchasePlan), tableData: ref([]), tableData1: ref([]), forecastData: ref(combined.forecastData),
};
if (taskKey === 'comprehensive-case-training') {
context.form = ref(combined.form);
for (const [key, value] of Object.entries(combined)) if (key.startsWith('comprehensiveStep')) context[key] = ref(value);
}
vm.runInNewContext(functions.map(n => script.slice(n.start, n.end)).join('\n') + '\nglobalThis.outcome = typeof buildOutcome === "function" ? buildOutcome : buildProgress;', context);
// Channel persistence is inline; the other six use the real page payload builder.
const payload = taskKey === 'purchase-demand-forecast'
? helpers.splitTrainingStepAnswers(taskKey, context.outcome()) : context.buildAnswerPayload('IN_PROGRESS');
const roundTrip = JSON.parse(helpers.restoreTrainingStepAnswers(taskKey, { ...payload, currentStep: 2 }));
fields[taskKey].forEach((panel, index) => {
const actual = JSON.parse(payload['step' + (index + 1) + 'Answer']);
panel.forEach(key => {
const original = context.outcome ? context.outcome()[key] : context.form.value[key];
assert.deepEqual(actual[key], JSON.parse(JSON.stringify(original)), taskKey + ' / ' + key);
assert.deepEqual(roundTrip[key], actual[key]);
fields[taskKey].forEach((_, other) => {
if (other !== index) assert.equal(Object.hasOwn(JSON.parse(payload['step' + (other + 1) + 'Answer']), key), false);
});
});
});
assert.equal(roundTrip.activeStep, taskKey === 'consumer-behavior' ? 1 : 2);
});
test('restoring a later saved step works without any first-step answer', async () => {
const { restoreTrainingStepAnswers } = await import('../src/views/training/trainingStepAnswers.mjs');
const restored = JSON.parse(restoreTrainingStepAnswers('comprehensive-case-training', {
currentStep: 10, step10Answer: JSON.stringify({ form: { validation: '最终复盘' } }),
}));
assert.equal(restored.form.validation, '最终复盘');
assert.equal(restored.currentStep, 10);
});