Files
MonkeyCode/monkeyai/web/test/table-export.test.mjs
T

53 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from "node:assert/strict"
import test from "node:test"
import { tableExportPath, tableCsv } from "../src/lib/table-export.ts"
test("导出只覆盖分页,保留已生效的筛选值", () => {
const params = new URLSearchParams({
actor_user_id: "abc",
content: "报表 + ? &",
since: "2026-09-01T08:15:00+08:00",
page: "3",
page_size: "20",
})
const path = tableExportPath("/api/admin/v1/audits?" + params)
const [route, query] = path.split("?")
assert.equal(route, "/api/admin/v1/audits")
assert.deepEqual(Object.fromEntries(new URLSearchParams(query)), {
...Object.fromEntries(params),
page: "1",
page_size: "5000",
})
})
const columns = [["正文", (v) => v]]
test("CSV 可由 Excel 读取,保留换行、引号和数值,不执行文本公式", () => {
const { csv } = tableCsv(
{
items: ['正文,带"引号"\n换行', " =1+1", "@SUM(A1:A9)", "-0.15", null, 0],
},
columns
)
assert.equal(
csv,
'"正文"\r\n"正文,带""引号""\n换行"\r\n"\' =1+1"\r\n"\'@SUM(A1:A9)"\r\n"-0.15"\r\n""\r\n"0"\r\n'
)
})
test("最多 5000 行数据,区分恰好达到上限和仍有更多", () => {
const items = Array.from({ length: 5000 }, (_, i) => i + 1)
for (const [meta, truncated] of [
[{ total: 5000 }, false],
[{ total: 5001 }, true],
[{ has_next: false }, false],
[{ has_next: true }, true],
]) {
const result = tableCsv({ items, ...meta }, columns)
assert.equal(result.count, 5000)
assert.equal(result.truncated, truncated)
assert.equal(result.csv.trimEnd().split("\r\n").length, 5001)
}
assert.equal(
tableCsv({ items: [...items, 5001] }, columns).csv.includes('"5001"'),
false
)
})