Files
Termix/src/ui/tests/sidebar/tree/HostItem/host-status-tooltip.test.ts
T

110 lines
3.7 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { registerHostProtocol } from "@/sidebar/host-protocols";
import type { Host } from "@/types/ui-types";
import {
buildStatusTooltip,
statusCheckEnabled,
} from "@/sidebar/tree/HostItem/HostItem";
// Minimal host factory, only the fields buildStatusTooltip reads.
function makeHost(overrides: Partial<Host> = {}): Host {
return {
id: 1,
name: "test-host",
ip: "127.0.0.1",
username: "user",
enableSsh: true,
enableRdp: false,
enableVnc: false,
enableTelnet: false,
statusCheckEnabled: true,
...overrides,
} as unknown as Host;
}
// Translator that returns human-readable labels, not key paths.
// This is what the review asked for: assert the rendered labels,
// not the key paths the translator falls back to when a resource is missing.
const t = (key: string): string =>
({
"hosts.status.online": "Online",
"hosts.status.checking": "Checking",
"hosts.status.offline": "Offline",
"hosts.status.monitoringDisabled": "Monitoring disabled",
})[key] ?? key;
describe("buildStatusTooltip", () => {
it("returns the translated 'Online' label for online status", () => {
const host = makeHost();
const tooltip = buildStatusTooltip(host, "online", t);
expect(tooltip).toContain("Online");
expect(tooltip).not.toContain("hosts.status.");
});
it("returns 'Checking' before the host has been checked", () => {
const host = makeHost();
const tooltip = buildStatusTooltip(host, "unknown", t);
expect(tooltip).toContain("Checking");
expect(tooltip).not.toContain("hosts.status.");
});
it("returns the translated 'Offline' label for offline status", () => {
const host = makeHost();
const tooltip = buildStatusTooltip(host, "offline", t);
expect(tooltip).toContain("Offline");
expect(tooltip).not.toContain("hosts.status.");
});
it("returns 'Monitoring disabled' when status check is disabled", () => {
const host = makeHost({ statusCheckEnabled: false });
const tooltip = buildStatusTooltip(host, "online", t);
expect(tooltip).toBe("Monitoring disabled");
});
it("includes protocol names in the tooltip when protocols are enabled", () => {
const dispose = registerHostProtocol({
id: "demo-desktop",
pluginId: "demo",
settingKey: "enableDemo",
defaultPort: 3389,
titleKey: "Demo Desktop",
icon: () => null,
});
const host = makeHost({
enableSsh: true,
pluginSettings: { demo: { enableDemo: true } },
});
const tooltip = buildStatusTooltip(host, "online", t);
dispose();
expect(tooltip).toContain("SSH");
expect(tooltip).toContain("Demo Desktop");
expect(tooltip).toContain("Online");
});
it("returns just the status label when no protocols are enabled", () => {
const host = makeHost({ enableSsh: false });
const tooltip = buildStatusTooltip(host, "online", t);
expect(tooltip).toBe("Online");
});
it("does not render key paths when a translator is supplied", () => {
const host = makeHost();
const tooltip = buildStatusTooltip(host, "online", t);
// The tooltip must never show the raw key path, that means the
// translation resource is missing, which is the bug #1265 fixed.
expect(tooltip).not.toMatch(/hosts\.status\./);
});
});
describe("statusCheckEnabled", () => {
it("returns true when statusCheckEnabled is not set (default)", () => {
const host = makeHost({ statusCheckEnabled: undefined });
expect(statusCheckEnabled(host)).toBe(true);
});
it("returns false when statusCheckEnabled is explicitly false", () => {
const host = makeHost({ statusCheckEnabled: false });
expect(statusCheckEnabled(host)).toBe(false);
});
});