chore: 补充单元测试

This commit is contained in:
xiaojunnuo
2026-05-01 09:16:46 +08:00
parent 80092823db
commit 0a0f1e90e1
24 changed files with 656 additions and 187 deletions
@@ -0,0 +1,31 @@
/// <reference types="mocha" />
import { expect } from "chai";
import { amountUtils } from "./util.amount.js";
describe("amountUtils", () => {
describe("toCent", () => {
it("converts yuan values to cents", () => {
expect(amountUtils.toCent(1)).to.equal(100);
expect(amountUtils.toCent(12.34)).to.equal(1234);
});
it("rounds to the nearest cent", () => {
expect(amountUtils.toCent(1.235)).to.equal(124);
expect(amountUtils.toCent(1.234)).to.equal(123);
});
});
describe("toYuan", () => {
it("converts cent values to yuan", () => {
expect(amountUtils.toYuan(100)).to.equal(1);
expect(amountUtils.toYuan(1234)).to.equal(12.34);
});
it("rounds yuan values to two decimal places", () => {
expect(amountUtils.toYuan(1235)).to.equal(12.35);
expect(amountUtils.toYuan(1)).to.equal(0.01);
});
});
});
@@ -0,0 +1,43 @@
/// <reference types="mocha" />
import { expect } from "chai";
import { domainUtils } from "./util.domain.js";
describe("domainUtils", () => {
describe("match", () => {
it("matches exact domains", () => {
expect(domainUtils.match("example.com", ["example.com"])).to.equal(true);
expect(domainUtils.match("api.example.com", ["example.com"])).to.equal(false);
});
it("matches wildcard domains by suffix", () => {
expect(domainUtils.match("api.example.com", ["*.example.com"])).to.equal(true);
expect(domainUtils.match("deep.api.example.com", ["*.example.com"])).to.equal(false);
expect(domainUtils.match("example.com", ["*.example.com"])).to.equal(false);
});
it("requires every target domain to match", () => {
expect(domainUtils.match(["api.example.com", "admin.example.com"], ["*.example.com"])).to.equal(true);
expect(domainUtils.match(["api.example.com", "other.com"], ["*.example.com"])).to.equal(false);
});
});
describe("isIp", () => {
it("detects valid IPv4 addresses", () => {
expect(domainUtils.isIpv4("127.0.0.1")).to.equal(true);
expect(domainUtils.isIpv4("255.255.255.255")).to.equal(true);
});
it("rejects invalid IPv4 addresses", () => {
expect(domainUtils.isIpv4("999.1.1.1")).to.equal(false);
expect(domainUtils.isIpv4("1.2.3")).to.equal(false);
expect(domainUtils.isIpv4("example.com")).to.equal(false);
});
it("detects IPv6 addresses", () => {
expect(domainUtils.isIpv6("2001:db8::1")).to.equal(true);
expect(domainUtils.isIp("2001:db8::1")).to.equal(true);
});
});
});
+4 -1
View File
@@ -51,7 +51,10 @@ function isIpv4(d: string) {
return false;
}
const isIPv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
return isIPv4Regex.test(d);
if (!isIPv4Regex.test(d)) {
return false;
}
return d.split(".").every(item => Number(item) <= 255);
}
function isIpv6(d: string) {
@@ -0,0 +1,40 @@
/// <reference types="mocha" />
import { expect } from "chai";
import { optionsUtils } from "./util.options.js";
describe("optionsUtils", () => {
describe("groupByDomain", () => {
it("splits options by domain match", () => {
const matchedOption = { value: "matched", domain: "api.example.com" };
const wildcardMatchedOption = { value: "wildcard", domain: "admin.example.com" };
const unmatchedOption = { value: "unmatched", domain: "other.com" };
const result = optionsUtils.groupByDomain([matchedOption, wildcardMatchedOption, unmatchedOption], ["api.example.com", "*.example.com"]);
expect(result.matched).to.deep.equal([matchedOption, wildcardMatchedOption]);
expect(result.notMatched).to.deep.equal([unmatchedOption]);
});
it("treats options without matching domains as not matched", () => {
const optionWithoutDomain = { value: "empty" };
const result = optionsUtils.groupByDomain([optionWithoutDomain], ["example.com"]);
expect(result.matched).to.deep.equal([]);
expect(result.notMatched).to.deep.equal([optionWithoutDomain]);
});
});
describe("buildGroupOptions", () => {
it("builds disabled group labels around matched and unmatched options", () => {
const matchedOption = { value: "matched", domain: "api.example.com" };
const unmatchedOption = { value: "unmatched", domain: "other.com" };
const result = optionsUtils.buildGroupOptions([matchedOption, unmatchedOption], ["api.example.com"]);
expect(result).to.deep.equal([{ value: "matched", disabled: true, label: "----已匹配----" }, matchedOption, { value: "unmatched", disabled: true, label: "----未匹配----" }, unmatchedOption]);
});
});
});
@@ -0,0 +1,36 @@
/// <reference types="mocha" />
import { expect } from "chai";
import { stringUtils } from "./util.string.js";
describe("stringUtils", () => {
describe("maxLength", () => {
it("returns an empty string for empty input", () => {
expect(stringUtils.maxLength()).to.equal("");
expect(stringUtils.maxLength("")).to.equal("");
});
it("returns the original string when it is within the limit", () => {
expect(stringUtils.maxLength("certd", 5)).to.equal("certd");
expect(stringUtils.maxLength("certd", 6)).to.equal("certd");
});
it("truncates strings longer than the limit and appends ellipsis", () => {
expect(stringUtils.maxLength("certificate", 4)).to.equal("cert...");
});
});
describe("appendTimeSuffix", () => {
it("returns an empty string for empty input", () => {
expect(stringUtils.appendTimeSuffix()).to.equal("");
expect(stringUtils.appendTimeSuffix("")).to.equal("");
});
it("appends a millisecond timestamp suffix", () => {
const result = stringUtils.appendTimeSuffix("certd");
expect(result).to.match(/^certd-\d{17}$/);
});
});
});