修复:清理导入历史失败提示并识别旧文件年份
This commit is contained in:
@@ -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, []);
|
||||
|
||||
Reference in New Issue
Block a user