mirror of
https://github.com/certd/certd.git
synced 2026-05-14 20:17:32 +08:00
chore: 补充单元测试
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
"dev-build": "npm run build",
|
||||
"preview": "vite preview",
|
||||
"test": "mocha --loader=ts-node/esm",
|
||||
"test:unit": "mocha --node-option no-warnings --node-option loader=ts-node/esm \"src/**/*.test.ts\"",
|
||||
"pub": "npm publish",
|
||||
"compile": "tsc --skipLibCheck --watch"
|
||||
},
|
||||
@@ -42,8 +43,10 @@
|
||||
"eslint": "^8.41.0",
|
||||
"eslint-config-prettier": "^8.8.0",
|
||||
"eslint-plugin-prettier": "^4.2.1",
|
||||
"mocha": "^10.2.0",
|
||||
"prettier": "^2.8.8",
|
||||
"rimraf": "^5.0.5",
|
||||
"ts-node": "^10.9.2",
|
||||
"tslib": "^2.8.1",
|
||||
"typescript": "^5.4.2"
|
||||
},
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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}$/);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -7,7 +7,7 @@
|
||||
"esModuleInterop": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"inlineSourceMap":false,
|
||||
"inlineSourceMap": false,
|
||||
"sourceMap": false,
|
||||
"noImplicitThis": true,
|
||||
"noUnusedLocals": true,
|
||||
@@ -22,21 +22,11 @@
|
||||
"composite": false,
|
||||
"useDefineForClassFields": true,
|
||||
"strict": true,
|
||||
"typeRoots": [ "./typings", "./node_modules/@types"],
|
||||
"typeRoots": ["./typings", "./node_modules/@types"],
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": false,
|
||||
"lib": ["ESNext", "DOM"],
|
||||
"lib": ["ESNext", "DOM"]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"src/**/*.json"
|
||||
],
|
||||
"exclude": [
|
||||
"*.js",
|
||||
"*.ts",
|
||||
"*.spec.ts",
|
||||
"dist",
|
||||
"node_modules",
|
||||
"test"
|
||||
],
|
||||
"include": ["src/**/*.ts", "src/**/*.json"],
|
||||
"exclude": ["*.js", "*.ts", "*.spec.ts", "dist", "node_modules", "src/**/*.test.ts", "test"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user