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.
89 lines
5.9 KiB
JavaScript
89 lines
5.9 KiB
JavaScript
|
1 month ago
|
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 babel = require('@babel/parser');
|
||
|
|
const { parse, compileTemplate } = require('@vue/compiler-sfc');
|
||
|
|
const ref = value => ({ value });
|
||
|
|
|
||
|
|
function pageFunctions(file, names, context, constants = []) {
|
||
|
|
const filename = path.resolve(__dirname, '../src/views/' + file);
|
||
|
|
const descriptor = parse(fs.readFileSync(filename, 'utf8')).descriptor;
|
||
|
|
assert.deepEqual(compileTemplate({ source: descriptor.template.content, filename, id: 'field-display' }).errors, []);
|
||
|
|
const script = descriptor.scriptSetup.content;
|
||
|
|
const nodes = babel.parse(script, { sourceType: 'module' }).program.body.filter(n =>
|
||
|
|
n.type === 'FunctionDeclaration' && names.includes(n.id.name) ||
|
||
|
|
n.type === 'VariableDeclaration' && n.declarations.some(d => constants.includes(d.id.name)));
|
||
|
|
vm.createContext(context); vm.runInContext(nodes.map(n => script.slice(n.start, n.end)).join('\n'), context);
|
||
|
|
context.template = descriptor.template.content;
|
||
|
|
return context;
|
||
|
|
}
|
||
|
|
|
||
|
|
test('later step fields cannot overwrite first step inputs during display restoration', async () => {
|
||
|
|
const { restoreTrainingStepAnswers } = await import('../src/views/training/trainingStepAnswers.mjs');
|
||
|
|
const form = JSON.parse(restoreTrainingStepAnswers('feature-conversion', {
|
||
|
|
step1Answer: JSON.stringify({ productName: '用户产品', rows: [{ voice: '第一步原文' }] }),
|
||
|
|
step2Answer: JSON.stringify({ scoreRows: [{ reach: 4 }], rows: [{ voice: '错误覆盖' }], activeStep: 99 }),
|
||
|
|
step3Answer: JSON.stringify({ priorityRows: [{ priority: 'P0' }], rows: [{ voice: '错误覆盖' }] }), currentStep: 3,
|
||
|
|
}));
|
||
|
|
assert.equal(form.rows[0].voice, '第一步原文');
|
||
|
|
assert.equal(form.scoreRows[0].reach, 4); assert.equal(form.priorityRows[0].priority, 'P0'); assert.equal(form.activeStep, 3);
|
||
|
|
});
|
||
|
|
|
||
|
|
function profilePage() {
|
||
|
|
return pageFunctions('demand/people.vue', ['applySavedAnswer', 'normalizeExcelPreview', 'normalizeRfmCharts', 'normalizeRfmInsights',
|
||
|
|
'normalizeKeyUserRecords', 'normalizeMarketSegments', 'createKeyUserRecords', 'createMarketSegments', 'createEmptyMarketSegment',
|
||
|
|
'createForm', 'createEmptyPreview', 'createEmptyRfmCharts', 'createEmptyRfmInsights'],
|
||
|
|
{ form: ref({}), currentStep: ref(1), steps: ref([1, 2, 3, 4, 5]), MAX_MARKET_SEGMENTS: 6 }, ['RFM_CHARTS', 'RFM_INSIGHT_ROWS']);
|
||
|
|
}
|
||
|
|
|
||
|
|
test('profile page displays all three saved image types, Excel preview and fifth step', async () => {
|
||
|
|
const { restoreTrainingStepAnswers } = await import('../src/views/training/trainingStepAnswers.mjs');
|
||
|
|
const c = profilePage();
|
||
|
|
const charts = Object.fromEntries(['segment-share', 'average-spend', 'score-heatmap'].map(id => [id, { url: '/file/' + id + '.png', name: id }]));
|
||
|
|
const answer = { currentStep: 5, step1Answer: JSON.stringify({ profileFileUrl: '/file/data.xlsx', profileFileName: 'data.xlsx', profilePreview: { columns: ['R'], rows: [['5']] } }),
|
||
|
|
step2Answer: JSON.stringify({ rfmCharts: charts }), step3Answer: '{}', step4Answer: '{}',
|
||
|
|
step5Answer: JSON.stringify({ marketSegments: [{ market: '白领' }], marketSelection: '优先白领' }) };
|
||
|
|
c.applySavedAnswer(restoreTrainingStepAnswers('target-user-profile', answer));
|
||
|
|
assert.equal(c.form.value.profilePreview.rows[0][0], '5');
|
||
|
|
for (const [key, value] of Object.entries(charts)) assert.equal(c.form.value.rfmCharts[key].url, value.url);
|
||
|
|
assert.equal(c.form.value.marketSelection, '优先白领');
|
||
|
|
assert.equal(c.currentStep.value, 5);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('survey page retains more than five report rows and renders persisted attachments', async () => {
|
||
|
|
const c = pageFunctions('demand/consumer-behavior.vue', ['normalizeSurveyReportRows', 'handleFileChange'], {
|
||
|
|
form: ref({ chartFiles: { 均值对比: { url: '/file/old.png', name: '旧图' } } }), uploading: ref(false),
|
||
|
|
uploadContext: ref({ type: 'chart', key: '' }), proxy: null, FormData: class { append() {} },
|
||
|
|
uploadStudentTrainingFile: async () => ({ url: '/file/new.png' }),
|
||
|
|
});
|
||
|
|
const rows = Array.from({ length: 8 }, (_, n) => ({ title: '模块' + n, content: '内容' + n }));
|
||
|
|
assert.equal(c.normalizeSurveyReportRows(rows).length, 8);
|
||
|
|
assert.match(c.template, /v-for="\(file, key\) in form\.chartFiles"/);
|
||
|
|
assert.match(c.template, /:src="file\.url"/); assert.match(c.template, /:href="form\.dataExcelFileUrl"/);
|
||
|
|
await c.handleFileChange({ target: { files: [{ name: '新图.png' }], value: 'selected' } });
|
||
|
|
assert.equal(c.form.value.chartFiles['新图.png'].url, '/file/new.png');
|
||
|
|
assert.equal(c.form.value.chartFiles.均值对比.url, '/file/old.png');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('the fifth implemented profile step is not displayed as an empty custom step', () => {
|
||
|
|
const c = pageFunctions('components/DynamicTrainingStepNavigator.vue', ['parseSteps'], {
|
||
|
|
props: { taskKey: 'target-user-profile' }, getStudentTrainingStepCount: () => 5,
|
||
|
|
});
|
||
|
|
const result = c.parseSteps(Array.from({ length: 6 }, (_, n) => ({ id: String(n), name: '步骤' + n, kind: n >= 4 ? 'custom' : 'builtin' })));
|
||
|
|
assert.equal(result[4].kind, 'builtin'); assert.equal(result[5].kind, 'custom');
|
||
|
|
});
|
||
|
|
|
||
|
|
if (process.env.TRAINING_FIELD_FIXTURES) test('actual migrated profile attachments survive the real page loader', async () => {
|
||
|
|
const { restoreTrainingStepAnswers } = await import('../src/views/training/trainingStepAnswers.mjs');
|
||
|
|
const records = JSON.parse(fs.readFileSync(process.env.TRAINING_FIELD_FIXTURES, 'utf8'));
|
||
|
|
const a = records.find(x => x.taskKey === 'target-user-profile');
|
||
|
|
assert.ok(a);
|
||
|
|
const c = profilePage(); c.applySavedAnswer(restoreTrainingStepAnswers(a.taskKey, a));
|
||
|
|
const savedCharts = JSON.parse(a.step2Answer).rfmCharts;
|
||
|
|
for (const [key, value] of Object.entries(savedCharts)) assert.equal(c.form.value.rfmCharts[key].url, value.url);
|
||
|
|
assert.ok(c.form.value.profilePreview.rows.length > 0);
|
||
|
|
assert.ok(c.form.value.marketSegments.length > 0); assert.ok(c.form.value.marketSelection);
|
||
|
|
});
|