修复:禁止批量铺货误读多工作表内容

- 按 workbook 关系定位唯一 XLSX 工作表并拒绝多表或损坏结构
- 重置表格文件控件以支持同一路径文件重新选择
- 补齐应用层、HTTP 与前端文件选择回归测试
This commit is contained in:
Christ committed 2026-08-20 21:15:21 +08:00
1 parent 58f91ac77d
commit 3b67ab81ee
18 files changed
+422 -26

No files matched your search

@@ -0,0 +1,36 @@
// @vitest-environment jsdom
// 本测试需要浏览器文件控件与 File API,因此固定使用 jsdom 环境。
import { describe,expect,test } from 'vitest';
import { consumeSelectedFile } from './fileInput';
// ItemList 文件选择测试覆盖同一路径的表格在保存后能够被重新读取。
describe('consumeSelectedFile',
// 测试组回调验证读取文件后重置原生 input 的浏览器兼容行为。
() => {
// 连续两次选择同名文件时,每次都返回对应 File 快照并清空上一轮原生选择值。
test('clears the native input after each selected file',
// 测试回调模拟浏览器在用户重新选择同一路径前写入的文件列表和伪路径值。
() => {
// input 保存可被测试替换 files 与 value 行为的文件控件。
const input = document.createElement('input');
// currentValue 保存浏览器文件控件的当前伪路径值,空值代表下次同一路径选择会重新触发 change。
let currentValue = 'C:\\fakepath\\products.xlsx';
Object.defineProperty(input, 'value', {
configurable: true,
get: /* 当前访问器返回模拟的文件控件伪路径。 */ () => currentValue,
set: /* 当前访问器记录业务代码清空原生控件的操作。 */ value => { currentValue = String(value); },
});
// firstFile 保存首次选择时浏览器创建的文件快照。
const firstFile = new File(['old-content'], 'products.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
Object.defineProperty(input, 'files', { configurable: true, value: [firstFile] as unknown as FileList });
expect(consumeSelectedFile(input)).toBe(firstFile);
expect(currentValue).toBe('');
// secondFile 保存同名文件修改后再次选择时浏览器创建的新快照。
const secondFile = new File(['new-content'], 'products.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
currentValue = 'C:\\fakepath\\products.xlsx';
Object.defineProperty(input, 'files', { configurable: true, value: [secondFile] as unknown as FileList });
expect(consumeSelectedFile(input)).toBe(secondFile);
expect(currentValue).toBe('');
});
});
+11
View File
@@ -0,0 +1,11 @@
/**
* consumeSelectedFile 读取文件控件当前选择的首个文件,并立即重置原生值。
* input 是批量铺货表格控件;返回值是可由 React 状态持有的文件快照,重置让用户可再次选择相同路径的已修改文件。
*/
export const consumeSelectedFile = (input: HTMLInputElement): File | null => {
// selectedFile 保存本次 change 事件携带的文件快照;无选择或用户取消时为 null。
const selectedFile = input.files?.[0] || null;
// 原生控件必须在读取后清空,否则浏览器可能将相同路径的再次选择视为未变化而不触发 change。
input.value = '';
return selectedFile;
};
@@ -10,6 +10,7 @@ getShippingRules,
} from '../api';
import { batchStatusClass,batchStatusText } from '../batchState';
import { BatchPhaseIndicator } from '../components/BatchPhaseIndicator';
import { consumeSelectedFile } from '../fileInput';
import { useItemPublishBatch } from '../hooks';
import { useItemActions } from '../itemActions';
import type { ItemListProps } from '../types';
@@ -676,7 +677,7 @@ const ItemList: React.FC<ItemListProps> = ({ onConfigureDelivery }) => {
className="hidden"
type="file"
accept=".xlsx,.csv,.tsv"
onChange={/* 当前回调处理用户交互或异步状态变化。 */ e => setBatchFile(e.target.files?.[0] || null)}
onChange={/* 当前回调读取本次文件快照并重置原生控件,以便同一路径文件修改后可再次选择。 */ event => setBatchFile(consumeSelectedFile(event.currentTarget))}
/>
</label>
<label className="flex min-h-[150px] cursor-pointer flex-col items-center justify-center rounded-2xl border-2 border-dashed border-gray-200 bg-gray-50 px-4 py-6 text-center hover:border-emerald-300 hover:bg-emerald-50 transition-colors">