1
0

feat: 配置变量系统与 target id/name 双字段标识

- 新增顶层 variables 段支持 string/number/boolean 字面量
- target 字符串字段支持 、、{...} 转义语法
- 变量解析优先级: variables -> process.env -> 默认值 -> 报错
- 完整引用保留原始类型,部分引用拼接为字符串
- 变量替换在 YAML 解析后、AJV 校验前执行
- 替换仅作用于 targets,跳过 id/type 字段
- target 新增必填 id 字段作为唯一标识,name 改为可选展示名称
- 数据库存储/API/前端全面迁移到 id 标识
- 统一 checker 运行时类型检查为 es-toolkit predicates
- 同步 delta specs 到主 specs,归档 config-variables 变更
This commit is contained in:
2026-05-17 00:37:54 +08:00
parent 366b3211c8
commit 7926514986
53 changed files with 1538 additions and 333 deletions

View File

@@ -31,6 +31,7 @@ function makeTarget(
...cmd,
},
group: "default",
id: "test-cmd",
intervalMs: 60000,
name: "test-cmd",
timeoutMs: 5000,

View File

@@ -20,6 +20,7 @@ function makeTarget(db: Partial<ResolvedDbTarget["db"]>, overrides?: Partial<Res
...db,
},
group: "default",
id: "test-db",
intervalMs: 60000,
name: "test-db",
timeoutMs: 5000,

View File

@@ -11,7 +11,7 @@ describe("validateDbConfig", () => {
test("缺少 db.url 返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ name: "test", type: "db" }],
targets: [{ id: "test", name: "test", type: "db" }],
});
expect(result.length).toBeGreaterThan(0);
const dbError = result.find((e) => e.path.includes("db"));
@@ -22,7 +22,7 @@ describe("validateDbConfig", () => {
test("db.url 为空字符串返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ db: { url: "" }, name: "test", type: "db" }],
targets: [{ db: { url: "" }, id: "test", name: "test", type: "db" }],
});
const urlError = result.find((e) => e.path.includes("db.url"));
expect(urlError).toBeDefined();
@@ -32,7 +32,7 @@ describe("validateDbConfig", () => {
test("db.query 为空字符串返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ db: { query: "", url: "sqlite://:memory:" }, name: "test", type: "db" }],
targets: [{ db: { query: "", url: "sqlite://:memory:" }, id: "test", name: "test", type: "db" }],
});
const queryError = result.find((e) => e.path.includes("db.query"));
expect(queryError).toBeDefined();
@@ -42,7 +42,7 @@ describe("validateDbConfig", () => {
test("db 分组未知字段返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ db: { timeout: 5, url: "sqlite://:memory:" }, name: "test", type: "db" }],
targets: [{ db: { timeout: 5, url: "sqlite://:memory:" }, id: "test", name: "test", type: "db" }],
});
const unknownError = result.find((e) => e.path.includes("db.timeout"));
expect(unknownError).toBeDefined();
@@ -52,7 +52,15 @@ describe("validateDbConfig", () => {
test("expect.maxDurationMs 非数字返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ db: { url: "sqlite://:memory:" }, expect: { maxDurationMs: "invalid" }, name: "test", type: "db" }],
targets: [
{
db: { url: "sqlite://:memory:" },
expect: { maxDurationMs: "invalid" },
id: "test",
name: "test",
type: "db",
},
],
});
const durationError = result.find((e) => e.path.includes("expect.maxDurationMs"));
expect(durationError).toBeDefined();
@@ -62,7 +70,9 @@ describe("validateDbConfig", () => {
test("expect.rowCount 非法 operator 返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ db: { url: "sqlite://:memory:" }, expect: { rowCount: { foo: 1 } }, name: "test", type: "db" }],
targets: [
{ db: { url: "sqlite://:memory:" }, expect: { rowCount: { foo: 1 } }, id: "test", name: "test", type: "db" },
],
});
const rowCountError = result.find((e) => e.path.includes("expect.rowCount"));
expect(rowCountError).toBeDefined();
@@ -72,7 +82,9 @@ describe("validateDbConfig", () => {
test("expect.rows 不是数组返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ db: { url: "sqlite://:memory:" }, expect: { rows: "not-array" }, name: "test", type: "db" }],
targets: [
{ db: { url: "sqlite://:memory:" }, expect: { rows: "not-array" }, id: "test", name: "test", type: "db" },
],
});
const rowsError = result.find((e) => e.path.includes("expect.rows"));
expect(rowsError).toBeDefined();
@@ -82,7 +94,9 @@ describe("validateDbConfig", () => {
test("expect.rows 元素不是对象返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ db: { url: "sqlite://:memory:" }, expect: { rows: ["not-object"] }, name: "test", type: "db" }],
targets: [
{ db: { url: "sqlite://:memory:" }, expect: { rows: ["not-object"] }, id: "test", name: "test", type: "db" },
],
});
const rowError = result.find((e) => e.path.includes("expect.rows[0]"));
expect(rowError).toBeDefined();
@@ -96,6 +110,7 @@ describe("validateDbConfig", () => {
{
db: { url: "sqlite://:memory:" },
expect: { rows: [{ name: { match: "[invalid" } }] },
id: "test",
name: "test",
type: "db",
},
@@ -109,7 +124,7 @@ describe("validateDbConfig", () => {
test("expect 未知字段返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ db: { url: "sqlite://:memory:" }, expect: { status: [200] }, name: "test", type: "db" }],
targets: [{ db: { url: "sqlite://:memory:" }, expect: { status: [200] }, id: "test", name: "test", type: "db" }],
});
const unknownError = result.find((e) => e.path.includes("expect.status"));
expect(unknownError).toBeDefined();
@@ -123,6 +138,7 @@ describe("validateDbConfig", () => {
{
db: { query: "SELECT 1", url: "sqlite://:memory:" },
expect: { maxDurationMs: 5000, rowCount: { gte: 1 }, rows: [{ cnt: { gte: 1 } }] },
id: "test",
name: "test",
type: "db",
},
@@ -134,7 +150,7 @@ describe("validateDbConfig", () => {
test("忽略非 db 类型 target", () => {
const result = validateDbConfig({
defaults: {},
targets: [{ name: "test", type: "http" }],
targets: [{ id: "test", name: "test", type: "http" }],
});
expect(result).toHaveLength(0);
});
@@ -143,8 +159,8 @@ describe("validateDbConfig", () => {
const result = validateDbConfig({
defaults: {},
targets: [
{ db: { url: "sqlite://:memory:" }, name: "db1", type: "db" },
{ db: { url: "" }, name: "db2", type: "db" },
{ db: { url: "sqlite://:memory:" }, id: "db1", name: "db1", type: "db" },
{ db: { url: "" }, id: "db2", name: "db2", type: "db" },
],
});
expect(result.length).toBeGreaterThan(0);

View File

@@ -166,6 +166,7 @@ describe("HttpChecker", () => {
method: overrides.method ?? "GET",
url: overrides.url ?? `${baseUrl}/ok`,
},
id: "test-http",
intervalMs: 60000,
name: "test-http",
timeoutMs: overrides.timeoutMs ?? 5000,
@@ -850,6 +851,7 @@ describe("HttpChecker.resolve", () => {
const errors = validateHttpTarget({
expect: { status: ["abc"] },
http: { url: "https://example.com" },
id: "test",
name: "test",
type: "http",
});
@@ -858,7 +860,7 @@ describe("HttpChecker.resolve", () => {
test("ignoreSSL 默认值为 false", () => {
const result = checker.resolve(
{ http: { url: "https://example.com" }, name: "test", type: "http" },
{ http: { url: "https://example.com" }, id: "test", name: "test", type: "http" },
makeResolveContext(),
);
expect(result.http.ignoreSSL).toBe(false);
@@ -866,7 +868,7 @@ describe("HttpChecker.resolve", () => {
test("maxRedirects 默认值为 0", () => {
const result = checker.resolve(
{ http: { url: "https://example.com" }, name: "test", type: "http" },
{ http: { url: "https://example.com" }, id: "test", name: "test", type: "http" },
makeResolveContext(),
);
expect(result.http.maxRedirects).toBe(0);
@@ -874,7 +876,13 @@ describe("HttpChecker.resolve", () => {
test("合法 status 范围模式通过校验", () => {
const result = checker.resolve(
{ expect: { status: ["2xx", 301] }, http: { url: "https://example.com" }, name: "test", type: "http" },
{
expect: { status: ["2xx", 301] },
http: { url: "https://example.com" },
id: "test",
name: "test",
type: "http",
},
makeResolveContext(),
);
expect(result.expect?.status).toEqual(["2xx", 301]);
@@ -882,7 +890,12 @@ describe("HttpChecker.resolve", () => {
test("显式 ignoreSSL 和 maxRedirects 正确解析", () => {
const result = checker.resolve(
{ http: { ignoreSSL: true, maxRedirects: 3, url: "https://example.com" }, name: "test", type: "http" },
{
http: { ignoreSSL: true, maxRedirects: 3, url: "https://example.com" },
id: "test",
name: "test",
type: "http",
},
makeResolveContext(),
);
expect(result.http.ignoreSSL).toBe(true);