修复:清理导入历史失败提示并识别旧文件年份

This commit is contained in:
2026-11-04 12:06:48 +08:00
parent f04d4e96e3
commit 866ba21bb8
6 changed files with 108 additions and 21 deletions
@@ -0,0 +1,20 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import ts from 'typescript';
const source=ts.transpileModule(readFileSync(new URL('../src/sourceWorkbookDetection.ts',import.meta.url),'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}}).outputText.replace(/^import .*;\n/gm,'').replace(/export /g,'');
const {sourceFilenamePeriod:period,sourceBatchYear:year}=new Function(source+';return {sourceFilenamePeriod,sourceBatchYear};')();
test('actual historical quality workbook names identify 2025 without confusing split-date ranges',()=>{
assert.deepEqual(period('1月亮片质量数据统计表25.1.xlsx'),{year:2025,month:1});
assert.deepEqual(period('2月黑片质量数据统计表25.2上.xlsx'),{year:2025,month:2});
assert.deepEqual(period('7月亮片质量数据统计表25.7.29-7.31.xlsx'),{year:2025,month:7});
assert.deepEqual(period('3月黑片质量数据统计表3.16~3.31.xlsx'),{year:undefined,month:3});
assert.deepEqual(period('2025年12月黑片.xlsx'),{year:2025,month:12});
assert.deepEqual(period('6月质量数据统计表25.5.xlsx'),{year:undefined,month:6});
});
test('a consistent batch year also applies to same-batch files without a year; mixed explicit years keep individual identities',()=>{
assert.equal(year(['1月亮片质量数据统计表25.1.xlsx','12月黑片质量数据统计表12月1日-12月31日.xlsx'],2026),2025);
assert.equal(year(['2025年1月黑片.xlsx','2026年1月黑片.xlsx'],2026),2026);
assert.equal(year(['3月黑片质量数据统计表3.16~3.31.xlsx'],2026),2026);
assert.equal(period('2025年1月黑片.xlsx').year,2025);
});
@@ -7,15 +7,16 @@ const source = ts.transpileModule(readFileSync(new URL('../src/useDataWorkflowOp
const tick = () => new Promise(resolve => setImmediate(resolve));
const deferred = () => { let resolve; const promise = new Promise(done => { resolve = done; }); return { promise, resolve }; };
function operationHook(api) {
const events = [], callbacks = [], storage = new Map();
function operationHook(api, restoredOperationId = '') {
const events = [], callbacks = [], storage = new Map(), state = [];
if (restoredOperationId) storage.set('quality-data-workflow:project', JSON.stringify({ operationId: restoredOperationId, startedAt: '2026-11-04T00:00:00Z' }));
const window = { localStorage: storage, dispatchEvent: event => events.push(event) };
const localStorage = { getItem: key => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, value), removeItem: key => storage.delete(key) };
const hooks = { useState: value => [value, () => {}], useRef: current => ({ current }), useCallback: fn => fn, useEffect: fn => { fn(); } };
const hooks = { useState: value => { const index = state.length; state.push(value); return [value, next => { state[index] = next; }]; }, useRef: current => ({ current }), useCallback: fn => fn, useEffect: fn => { fn(); } };
const load = new Function('api', 'window', 'localStorage', 'WORKSPACE_INVALIDATED', 'usePageCache', 'useVisiblePolling', 'hooks', `const {useState,useRef,useCallback,useEffect}=hooks;${source};return useDataWorkflowOperation;`);
const useDataWorkflowOperation = load(api, window, localStorage, 'quality-workspace-invalidated', () => ({ active: true }), () => {}, hooks);
const hook = useDataWorkflowOperation('project', value => callbacks.push(value));
return { hook, events, callbacks, storage };
return { hook, events, callbacks, storage, state };
}
test('a completed workflow invalidates retained data exactly once after the server marks it complete', async () => {
@@ -39,14 +40,34 @@ test('a completed workflow invalidates retained data exactly once after the serv
assert.equal(events.length, 1, 'a terminal result cannot invalidate a second time');
});
test('failed workflows never invalidate published data and keep the operation id available for recovery', async () => {
test('a cached failed operation is explicitly marked as historical and is not restored again on the next visit', async () => {
const { hook, storage, state } = operationHook({ dataWorkflowOperation: async () => ({ operationId: 'old-job', status: 'FAILED', errors: [{ message: 'old failure' }] }) }, 'old-job');
hook.resume(); await tick(); await tick();
assert.equal(state[0].status, 'FAILED');
assert.equal(state[0].restoredFromPreviousVisit, true);
assert.equal(state[2], 'old failure');
assert.equal(storage.size, 0);
});
test('an old terminal response cannot remove a newer task saved by another tab', async () => {
const final = deferred();
const { hook, storage } = operationHook({ dataWorkflowOperation: async () => final.promise });
await hook.start(async () => ({ operationId: 'old-job', status: 'QUEUED' }));
hook.resume(); await tick();
storage.set('quality-data-workflow:project', JSON.stringify({ operationId: 'new-job' }));
final.resolve({ operationId: 'old-job', status: 'FAILED', errors: [{ message: 'old failure' }] });
await tick(); await tick();
assert.equal(JSON.parse(storage.get('quality-data-workflow:project')).operationId, 'new-job');
});
test('failed workflows keep their visible error but remove the pending pointer so another visit does not revive an old failure', async () => {
let calls = 0;
const { hook, events, callbacks, storage } = operationHook({ dataWorkflowOperation: async () => { calls++; return { operationId: 'job', status: 'FAILED', errors: [{ message: 'rejected' }] }; } });
await hook.start(async () => ({ operationId: 'job', status: 'QUEUED' }));
hook.resume(); await tick(); await tick();
assert.deepEqual(events, []);
assert.deepEqual(callbacks, []);
assert.equal(storage.size, 1);
assert.equal(storage.size, 0, 'only unfinished operations belong in reload recovery storage');
hook.resume(); await tick();
assert.equal(calls, 1, 'a terminal failure is not polled repeatedly');
assert.deepEqual(events, []);
@@ -0,0 +1,15 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import ts from 'typescript';
import React from 'react';
const source=readFileSync(new URL('../src/NoCodeDataWorkflow.tsx',import.meta.url),'utf8');
const body=source.slice(source.indexOf('function WorkflowOperationFeedback('),source.indexOf('export type PendingSourceDeletion'));
const code=ts.transpileModule(body+'\nreturn WorkflowOperationFeedback;', {compilerOptions:{target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.React}}).outputText;
const Feedback=new Function('React','Alert','Button','Progress','workflowStageLabel',code)(React,'Alert','Button','Progress',{FAILED:'处理失败'});
function nodes(element){if(!React.isValidElement(element))return [];return [element,...React.Children.toArray(element.props.children).flatMap(nodes)];}
for(const restored of [false,true])test(restored?'historical failure is labelled and dismissible without a misleading processing progress bar':'new failure retains its concrete reason and can be dismissed',()=>{
let dismissed=false;
const tree=Feedback({operation:{operationId:'job',status:'FAILED',stage:'FAILED',restoredFromPreviousVisit:restored,errors:[{message:'具体校验原因'}]},error:'具体校验原因',busy:false,onResume(){throw Error('terminal failure must not offer progress retry');},onDismiss(){dismissed=true;}});
const all=nodes(tree),alerts=all.filter(n=>n.type==='Alert');assert.equal(alerts.length,1);assert.equal(alerts[0].props.message,restored?'上次操作未完成':'本次操作未完成');assert.ok(JSON.stringify(alerts[0].props.description).includes('具体校验原因'));assert.equal(all.filter(n=>n.type==='Progress').length,0);alerts[0].props.onClose();assert.equal(dismissed,true);
});