47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import { validatePackEntries, type ZipEntry } from "./validate.js"
|
|
|
|
function entries(attachment: Partial<ZipEntry> = {}): ZipEntry[] {
|
|
return [
|
|
{ fileName: "信息表.xlsx", uncompressedSize: 100, compressedSize: 80, raw: {} },
|
|
{
|
|
fileName: "DOC-001/合同/合同.pdf",
|
|
uncompressedSize: 500,
|
|
compressedSize: 400,
|
|
raw: {},
|
|
...attachment
|
|
}
|
|
]
|
|
}
|
|
|
|
describe("material pack attachment file policy", () => {
|
|
test("accepts the default 500-byte minimum and common PDF extension", () => {
|
|
expect(() => validatePackEntries(entries())).not.toThrow()
|
|
})
|
|
|
|
test("rejects attachments smaller than the configured minimum", () => {
|
|
expect(() => validatePackEntries(entries({ uncompressedSize: 499 }))).toThrow(
|
|
"附件文件过小"
|
|
)
|
|
})
|
|
|
|
test("rejects attachments larger than the configured maximum", () => {
|
|
expect(() => validatePackEntries(entries({ uncompressedSize: 1001 }), {
|
|
maxFileSizeBytes: 1000
|
|
})).toThrow("附件文件过大")
|
|
})
|
|
|
|
test("rejects extensions outside the configured whitelist", () => {
|
|
expect(() => validatePackEntries(entries({ fileName: "DOC-001/合同/合同.exe" }), {
|
|
allowedExtensions: [".pdf"]
|
|
})).toThrow("附件后缀不在白名单中")
|
|
})
|
|
|
|
test("rejects an invalid size range before any upload can start", () => {
|
|
expect(() => validatePackEntries(entries(), {
|
|
minFileSizeBytes: 1000,
|
|
maxFileSizeBytes: 999
|
|
})).toThrow("最大文件大小不能小于最小文件大小")
|
|
})
|
|
})
|