import type { PrimaryTableCellParams, PrimaryTableCol } from "tdesign-react"; import { describe, expect, test } from "bun:test"; import type { TargetStatus } from "../../../src/shared/api"; import { createTargetTableColumns } from "../../../src/web/constants/target-table-columns"; interface TableFilter { list?: Array<{ label: string; value: string }>; type?: string; } function getColumn(columns: Array>, colKey: string): PrimaryTableCol { const column = columns.find((item) => item.colKey === colKey); expect(column).toBeDefined(); return column!; } function makeTarget(overrides: Partial = {}): TargetStatus { return { group: "default", id: 1, interval: "5s", latestCheck: null, name: "test", recentSamples: [], stats: { availability: 100, totalChecks: 0 }, target: "https://example.com", type: "http", ...overrides, }; } describe("createTargetTableColumns", () => { test("生成 7 个目标表格列", () => { const columns = createTargetTableColumns(["http", "command"]); expect(columns.map((column) => column.colKey)).toEqual([ "latestCheck.matched", "name", "type", "stats.availability", "recentSamples", "latestCheck.durationMs", "interval", ]); }); test("根据 checkerTypes 生成类型筛选器", () => { const typeColumn = getColumn(createTargetTableColumns(["http", "command", "tcp"]), "type"); const filter = typeColumn.filter as TableFilter; expect(filter.type).toBe("single"); expect(filter.list).toEqual([ { label: "全部", value: "" }, { label: "http", value: "http" }, { label: "command", value: "command" }, { label: "tcp", value: "tcp" }, ]); }); test("checkerTypes 为空时只保留全部选项", () => { const typeColumn = getColumn(createTargetTableColumns([]), "type"); const filter = typeColumn.filter as TableFilter; expect(filter.list).toEqual([{ label: "全部", value: "" }]); }); test("类型列直接渲染原始 type 文本", () => { const typeColumn = getColumn(createTargetTableColumns(["tcp"]), "type"); const renderCell = typeColumn.cell as (params: PrimaryTableCellParams) => { props: { children: unknown }; }; const element = renderCell({ col: typeColumn, colIndex: 2, row: makeTarget({ type: "tcp" }), rowIndex: 0, }); expect(element.props.children).toBe("tcp"); }); });