Spaces:
Paused
Paused
File size: 5,385 Bytes
a5784e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | /**
* PortStatus Component Tests
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
// Define types locally to avoid module resolution issues
interface ProcessInfo {
pid: number;
name: string;
}
interface PortStatusInfo {
port: number;
port_type: string;
in_use: boolean;
processes: ProcessInfo[];
}
describe("PortStatus Logic", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("Data Fetching", () => {
it("returns loading state structure", () => {
const result = { data: undefined, isLoading: true, error: null };
expect(result.isLoading).toBe(true);
});
it("returns port status when loaded", () => {
const mockStatus = {
ports: [
{
port: 2048,
port_type: "fastapi",
in_use: true,
processes: [{ pid: 1234, name: "python" }],
},
{
port: 3120,
port_type: "stream_proxy",
in_use: false,
processes: [],
},
] as PortStatusInfo[],
};
const result = { data: mockStatus, isLoading: false, error: null };
expect(result.data.ports).toHaveLength(2);
expect(result.data.ports[0].in_use).toBe(true);
});
it("handles empty ports list", () => {
const mockStatus = { ports: [] as PortStatusInfo[] };
const result = { data: mockStatus, isLoading: false, error: null };
expect(result.data.ports).toHaveLength(0);
});
});
describe("Mutations", () => {
it("killProcess accepts pid", () => {
const pid = 1234;
expect(typeof pid).toBe("number");
});
it("tracks pending state during kill", () => {
const mutation = { isPending: true, variables: 1234 };
expect(mutation.isPending).toBe(true);
expect(mutation.variables).toBe(1234);
});
});
describe("Kill Confirmation Flow", () => {
it("enters confirmation mode on first click", () => {
let confirmKill: number | null = null;
const pid = 1234;
// First click - enter confirmation
confirmKill = pid;
expect(confirmKill).toBe(1234);
});
it("executes kill on second click", () => {
const confirmKill: number | null = 1234;
const pid = 1234;
let killExecuted = false;
// Second click when already confirmed
if (confirmKill === pid) {
killExecuted = true;
}
expect(killExecuted).toBe(true);
});
it("auto-cancels confirmation after timeout", () => {
let confirmKill: number | null = 1234;
const pid = 1234;
// Simulate timeout callback
if (confirmKill === pid) {
confirmKill = null;
}
expect(confirmKill).toBeNull();
});
it("does not cancel if different pid confirmed", () => {
let confirmKill: number | null = 5678;
const originalPid = 1234;
// Timeout for originalPid should not affect confirmKill for 5678
if (confirmKill === originalPid) {
confirmKill = null;
}
expect(confirmKill).toBe(5678);
});
it("resets confirmation after successful kill", () => {
let confirmKill: number | null = 1234;
// onSuccess callback
confirmKill = null;
expect(confirmKill).toBeNull();
});
});
describe("Port Status Display", () => {
it("identifies ports in use", () => {
const port: PortStatusInfo = {
port: 2048,
port_type: "fastapi",
in_use: true,
processes: [{ pid: 1234, name: "python" }],
};
expect(port.in_use).toBe(true);
});
it("identifies unused ports", () => {
const port: PortStatusInfo = {
port: 3120,
port_type: "stream_proxy",
in_use: false,
processes: [],
};
expect(port.in_use).toBe(false);
expect(port.processes).toHaveLength(0);
});
it("extracts process info correctly", () => {
const port: PortStatusInfo = {
port: 2048,
port_type: "fastapi",
in_use: true,
processes: [
{ pid: 1234, name: "python" },
{ pid: 5678, name: "node" },
],
};
expect(port.processes).toHaveLength(2);
expect(port.processes[0].pid).toBe(1234);
expect(port.processes[1].name).toBe("node");
});
});
describe("Port List Processing", () => {
it("extracts ports array from response", () => {
const data = {
ports: [
{ port: 2048, port_type: "fastapi", in_use: true, processes: [] },
] as PortStatusInfo[],
};
const ports = data.ports;
expect(ports).toHaveLength(1);
});
it("handles undefined data gracefully", () => {
const data: { ports: PortStatusInfo[] } | undefined = undefined;
const ports = data?.ports || [];
expect(ports).toHaveLength(0);
});
it("sorts ports by number", () => {
const ports: PortStatusInfo[] = [
{ port: 9222, port_type: "debug", in_use: false, processes: [] },
{ port: 2048, port_type: "fastapi", in_use: true, processes: [] },
{ port: 3120, port_type: "stream", in_use: false, processes: [] },
];
const sorted = [...ports].sort((a, b) => a.port - b.port);
expect(sorted[0].port).toBe(2048);
expect(sorted[1].port).toBe(3120);
expect(sorted[2].port).toBe(9222);
});
});
});
|