Files
Filip131311 8c2a313c76 chore: format codebase with Prettier
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-03 14:53:58 +02:00

123 lines
4.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { Registry, ServiceState } from "@argent/registry";
import { createStopSimulatorServerTool } from "../src/tools/simulator/stop-simulator-server";
import { createStopAllSimulatorServersTool } from "../src/tools/simulator/stop-all-simulator-servers";
import { stopMetroTool } from "../src/tools/simulator/stop-metro";
function createMockRegistry(services: Map<string, { state: ServiceState; dependents: string[] }>) {
return {
getSnapshot: vi.fn(() => ({
services,
namespaces: [],
tools: [],
})),
disposeService: vi.fn(async () => {}),
} as unknown as Registry;
}
describe("stop-simulator-server", () => {
it("disposes the correct URN for a running simulator", async () => {
const services = new Map([
["SimulatorServer:AAAA-BBBB", { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: "AAAA-BBBB" });
expect(result).toEqual({ stopped: true, udid: "AAAA-BBBB" });
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:AAAA-BBBB");
});
it("returns stopped: false for a UDID with no running server", async () => {
const services = new Map<string, { state: ServiceState; dependents: string[] }>();
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: "CCCC-DDDD" });
expect(result).toEqual({ stopped: false, udid: "CCCC-DDDD" });
expect(registry.disposeService).not.toHaveBeenCalled();
});
it("returns stopped: false for an IDLE simulator", async () => {
const services = new Map([
["SimulatorServer:EEEE-FFFF", { state: ServiceState.IDLE, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: "EEEE-FFFF" });
expect(result).toEqual({ stopped: false, udid: "EEEE-FFFF" });
expect(registry.disposeService).not.toHaveBeenCalled();
});
});
describe("stop-all-simulator-servers", () => {
it("disposes all running SimulatorServer URNs", async () => {
const services = new Map([
["SimulatorServer:AAA", { state: ServiceState.RUNNING, dependents: [] }],
["SimulatorServer:BBB", { state: ServiceState.RUNNING, dependents: [] }],
["JsRuntimeDebugger:CCC", { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopAllSimulatorServersTool(registry);
const result = await tool.execute!({}, undefined);
expect(result).toEqual({
stopped: ["SimulatorServer:AAA", "SimulatorServer:BBB"],
});
expect(registry.disposeService).toHaveBeenCalledTimes(2);
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:AAA");
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:BBB");
});
it("returns empty list when no simulators are running", async () => {
const services = new Map<string, { state: ServiceState; dependents: string[] }>();
const registry = createMockRegistry(services);
const tool = createStopAllSimulatorServersTool(registry);
const result = await tool.execute!({}, undefined);
expect(result).toEqual({ stopped: [] });
expect(registry.disposeService).not.toHaveBeenCalled();
});
it("skips IDLE simulators", async () => {
const services = new Map([
["SimulatorServer:AAA", { state: ServiceState.IDLE, dependents: [] }],
["SimulatorServer:BBB", { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopAllSimulatorServersTool(registry);
const result = await tool.execute!({}, undefined);
expect(result).toEqual({ stopped: ["SimulatorServer:BBB"] });
expect(registry.disposeService).toHaveBeenCalledOnce();
});
});
describe("stop-metro", () => {
it("defaults to port 8081", () => {
expect(stopMetroTool.zodSchema).toBeDefined();
const parsed = stopMetroTool.zodSchema!.parse({});
expect(parsed.port).toBe(8081);
});
it("accepts a custom port", () => {
const parsed = stopMetroTool.zodSchema!.parse({ port: 9090 });
expect(parsed.port).toBe(9090);
});
it("returns stopped: false when no process on port", async () => {
// Use a high port unlikely to have anything listening
const result = await stopMetroTool.execute!({}, { port: 59999 });
expect(result.stopped).toBe(false);
expect(result.port).toBe(59999);
expect(result.pids).toEqual([]);
});
});