61 lines
2.5 KiB
JavaScript
61 lines
2.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import {
|
|
DICTIONARY_CSV_HEADER,
|
|
parseDictionaryImport,
|
|
planDictionaryImport,
|
|
serializeDictionaryCSV,
|
|
} from "../../src/lib/dictionary-batch.js";
|
|
|
|
test("dictionary CSV export carries BOM, header and one name per row", () => {
|
|
const csv = serializeDictionaryCSV(["商品检查", "拿取前", ""]);
|
|
assert.equal(csv, `\ufeff${DICTIONARY_CSV_HEADER}\r\n商品检查\r\n拿取前\r\n`);
|
|
});
|
|
|
|
test("dictionary CSV export escapes commas, quotes and newlines", () => {
|
|
const csv = serializeDictionaryCSV(["含,逗号", '含"引号', "含\n换行"]);
|
|
const rows = csv.split("\r\n");
|
|
assert.equal(rows[1], '"含,逗号"');
|
|
assert.equal(rows[2], '"含""引号"');
|
|
assert.equal(rows.length, 5);
|
|
});
|
|
|
|
test("dictionary import parses export round-trip and plain text", () => {
|
|
const roundTrip = parseDictionaryImport(serializeDictionaryCSV(["商品检查", "调整,头环"]));
|
|
assert.deepEqual(roundTrip.names, ["商品检查", "调整,头环"]);
|
|
assert.deepEqual(roundTrip.invalid, []);
|
|
const plain = parseDictionaryImport("拿取前\r\n\r\n 商品 检查 \n调整头环");
|
|
assert.deepEqual(plain.names, ["拿取前", "商品 检查", "调整头环"]);
|
|
});
|
|
|
|
test("dictionary import reports multi-column rows as invalid instead of truncating", () => {
|
|
const parsed = parseDictionaryImport("名称\r\n商品检查,多余列\r\n正常名称");
|
|
assert.deepEqual(parsed.names, ["正常名称"]);
|
|
assert.deepEqual(parsed.invalid, ["商品检查,多余列"]);
|
|
});
|
|
|
|
test("dictionary import plan skips selected, collapses duplicates by name key", () => {
|
|
const items = [
|
|
{ id: "a", name: "商品检查", status: "active" },
|
|
{ id: "b", name: "拿取 前", status: "active" },
|
|
{ id: "c", name: "已停用", status: "inactive" },
|
|
];
|
|
// "拿取 前" collapses to the single-space key of the selected "拿取 前";
|
|
// "商品 检查" (inner space) stays a distinct name, matching server keys.
|
|
const plan = planDictionaryImport({
|
|
kind: "slice",
|
|
names: ["商品检查", "拿取 前", "新名称", "新名称"],
|
|
items,
|
|
enabledIds: ["a", "b", "c"],
|
|
});
|
|
assert.deepEqual(plan.alreadySelected, ["商品检查", "拿取 前"]);
|
|
assert.deepEqual(plan.toImport, ["新名称"]);
|
|
assert.deepEqual(plan.duplicates, ["新名称"]);
|
|
});
|
|
|
|
test("dictionary import parser never revives cross-line quoting confusion", () => {
|
|
const parsed = parseDictionaryImport('"未闭合引号');
|
|
assert.deepEqual(parsed.names, []);
|
|
assert.equal(parsed.invalid.length, 1);
|
|
});
|