Files
watchman/web/apps/console/src/datasources/payload.spec.ts
T
unknownandCursor e6d37d176d feat(datasource): 完成 M03 数据源配置与熔断巡检
交付三种引擎登记、草稿/已保存连接测试、可逆启用、每源熔断与 Cron 巡检,以及控制台页面;用户确认收尾后标 DONE_LOCAL。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 12:50:46 +08:00

99 lines
3.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { buildCreatePayload, buildDraftTestPayload, buildUpdatePayload } from "./payload";
import { SECRET_MASK, type DatasourceForm } from "./types";
function form(overrides: Partial<DatasourceForm> = {}): DatasourceForm {
return {
name: " 产线工艺库 ",
description: "",
engine: "postgresql",
host: "db.example.test",
port: 5432,
database_name: "process_demo",
username: "readonly_demo",
password: "",
circuit_failure_window_seconds: 300,
circuit_failure_threshold: 3,
circuit_open_cooldown_seconds: 600,
...overrides,
};
}
describe("datasource payloads", () => {
it("create sends the typed password and trims text fields", () => {
const payload = buildCreatePayload(form({ password: "typed-by-user" }));
expect(payload.password).toBe("typed-by-user");
expect(payload.name).toBe("产线工艺库");
expect(payload.description).toBeNull();
});
it("edit with a blank password omits the password key", () => {
const payload = buildUpdatePayload(form({ password: "" }));
expect("password" in payload).toBe(false);
expect(JSON.stringify(payload)).not.toContain("password");
});
it("edit with a spaces-only password omits the password key", () => {
expect("password" in buildUpdatePayload(form({ password: " " }))).toBe(false);
});
it("edit with a typed password sends it", () => {
expect(buildUpdatePayload(form({ password: "new-secret-value" })).password).toBe("new-secret-value");
});
it("never carries the mask placeholder unless the user typed it into the model", () => {
// The mask lives only inside SecretInput; the form model stays blank until the user types.
expect(JSON.stringify(buildUpdatePayload(form()))).not.toContain(SECRET_MASK);
});
it("never sends the read-only intent", () => {
expect("read_only_intent" in buildCreatePayload(form({ password: "x" }))).toBe(false);
expect("read_only_intent" in buildUpdatePayload(form())).toBe(false);
});
it("create sends no circuit fields (the server defaults apply)", () => {
const body = JSON.stringify(buildCreatePayload(form({ password: "x" })));
expect(body).not.toContain("circuit");
});
it("edit sends the three circuit parameters as numbers", () => {
const payload = buildUpdatePayload(
form({
circuit_failure_window_seconds: 120,
circuit_failure_threshold: 5,
circuit_open_cooldown_seconds: 900,
}),
);
expect(payload.circuit_failure_window_seconds).toBe(120);
expect(payload.circuit_failure_threshold).toBe(5);
expect(payload.circuit_open_cooldown_seconds).toBe(900);
});
});
describe("draft test payload", () => {
it("create sends the typed password and no datasource_id", () => {
const payload = buildDraftTestPayload(form({ password: "typed-by-user" }));
expect(payload.password).toBe("typed-by-user");
expect("datasource_id" in payload).toBe(false);
expect(payload.host).toBe("db.example.test");
});
it("edit with a blank password sends the id and no password key", () => {
const payload = buildDraftTestPayload(form({ password: " " }), "ds-1");
expect(payload.datasource_id).toBe("ds-1");
expect("password" in payload).toBe(false);
});
it("edit with a typed password sends both, never the mask", () => {
const payload = buildDraftTestPayload(form({ password: "new-pw" }), "ds-1");
expect(payload.password).toBe("new-pw");
expect(payload.datasource_id).toBe("ds-1");
expect(JSON.stringify(payload)).not.toContain(SECRET_MASK);
});
it("carries only connection fields", () => {
const keys = Object.keys(buildDraftTestPayload(form({ password: "x" }), "ds-1")).sort();
expect(keys).toEqual(["database_name", "datasource_id", "engine", "host", "password", "port", "username"]);
});
});