37 lines
1.3 KiB
TypeScript
37 lines
1.3 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { buildPasswordCsv, csvQuote, guardFormula } from "./csv";
|
|
|
|
describe("csvQuote", () => {
|
|
it("给所有字段加引号并双写内部引号", () => {
|
|
expect(csvQuote("a")).toBe('"a"');
|
|
expect(csvQuote('say "hi"')).toBe('"say ""hi"""');
|
|
expect(csvQuote("a,b")).toBe('"a,b"');
|
|
expect(csvQuote("a\nb")).toBe('"a\nb"');
|
|
});
|
|
});
|
|
|
|
describe("guardFormula", () => {
|
|
it("名称以公式字符开头时前缀单引号", () => {
|
|
expect(guardFormula("=1+1")).toBe("'=1+1");
|
|
expect(guardFormula("+cmd")).toBe("'+cmd");
|
|
expect(guardFormula("-1")).toBe("'-1");
|
|
expect(guardFormula("@SUM")).toBe("'@SUM");
|
|
expect(guardFormula("门口")).toBe("门口");
|
|
});
|
|
});
|
|
|
|
describe("buildPasswordCsv", () => {
|
|
it("带 BOM、表头、CRLF,密码原样加引号", () => {
|
|
const out = buildPasswordCsv([
|
|
{ id: "e1", name: "门,口", login_password: 'p"w' },
|
|
{ id: "e2", name: "=cmd", login_password: "=1+1" },
|
|
]);
|
|
expect(out.startsWith("\uFEFF")).toBe(true);
|
|
const body = out.slice(1);
|
|
expect(body.startsWith('"id","name","login_password"\r\n')).toBe(true);
|
|
const rows = body.split("\r\n");
|
|
expect(rows[1]).toBe('"e1","门,口","p""w"');
|
|
expect(rows[2]).toBe('"e2","\'=cmd","=1+1"');
|
|
});
|
|
});
|