Files
watchman/web/apps/console/src/datasources/SecretInput.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

109 lines
3.7 KiB
TypeScript

import { flushPromises, mount } from "@vue/test-utils";
import { describe, expect, it } from "vitest";
import { defineComponent, ref } from "vue";
import SecretInput from "./SecretInput.vue";
import { buildUpdatePayload } from "./payload";
import { SECRET_MASK, type DatasourceForm } from "./types";
// A stand-in for a saved password. The API never returns it, so it must never be rendered.
const SAVED_SECRET = "saved-secret-should-never-render";
async function mountEdit() {
const Host = defineComponent({
components: { SecretInput },
setup() {
const password = ref("");
return { password };
},
template: '<SecretInput v-model="password" maskable />',
});
const wrapper = mount(Host, { attachTo: document.body });
// Element Plus attaches its focus listener after the first flush.
await flushPromises();
const input = wrapper.get("input");
const model = () => (wrapper.vm as unknown as { password: string }).password;
return { wrapper, input, model };
}
function payloadFor(password: string) {
const form: DatasourceForm = {
name: "n",
description: "",
engine: "postgresql",
host: "h",
port: 5432,
database_name: "d",
username: "u",
password,
circuit_failure_window_seconds: 300,
circuit_failure_threshold: 3,
circuit_open_cooldown_seconds: 600,
};
return buildUpdatePayload(form);
}
describe("SecretInput password mask", () => {
it("is an Element Plus password input with the reveal toggle", async () => {
const { wrapper, input } = await mountEdit();
expect(input.attributes("type")).toBe("password");
expect(wrapper.find(".el-input__password").exists()).toBe(true);
wrapper.unmount();
});
it("edit shows a fixed-length mask and never the saved secret", async () => {
const { wrapper, input } = await mountEdit();
expect((input.element as HTMLInputElement).value).toBe(SECRET_MASK);
expect(wrapper.html()).not.toContain(SAVED_SECRET);
wrapper.unmount();
});
it("focus clears the mask without changing the model", async () => {
const { wrapper, input, model } = await mountEdit();
await input.trigger("focus");
await flushPromises();
expect((input.element as HTMLInputElement).value).toBe("");
expect(model()).toBe("");
wrapper.unmount();
});
it("blank after focus restores the mask on blur", async () => {
const { wrapper, input } = await mountEdit();
await input.trigger("focus");
await flushPromises();
await input.trigger("blur");
await flushPromises();
expect((input.element as HTMLInputElement).value).toBe(SECRET_MASK);
wrapper.unmount();
});
it("blank means the payload omits the password", async () => {
const { wrapper, input, model } = await mountEdit();
await input.trigger("focus");
await flushPromises();
await input.trigger("blur");
await flushPromises();
expect("password" in payloadFor(model())).toBe(false);
wrapper.unmount();
});
it("a typed value reaches the model and the payload, and the mask stays away", async () => {
const { wrapper, input, model } = await mountEdit();
await input.trigger("focus");
await flushPromises();
await input.setValue("typed-new-secret");
await flushPromises();
await input.trigger("blur");
await flushPromises();
expect(model()).toBe("typed-new-secret");
expect((input.element as HTMLInputElement).value).toBe("typed-new-secret");
expect(payloadFor(model()).password).toBe("typed-new-secret");
wrapper.unmount();
});
it("create mode shows an empty box, not the mask", () => {
const wrapper = mount(SecretInput, { props: { modelValue: "" } });
expect((wrapper.get("input").element as HTMLInputElement).value).toBe("");
wrapper.unmount();
});
});