Файли
openidea/src/workbench/iosSimulators.test.ts
T
LU XIANGRUIтаwildcatDownstairs 2a7975a077 fix(xcode): select the root scheme and the backend's default simulator in Xcode projects (#7366)
An Xcode project with an npm package in a subfolder opened with that package's dev script selected, and the simulator chooser showed an iPad when nothing was booted.

- defaultSelection scored a root Xcode scheme the same as any run/dev script, so list order decided. A root scheme (kind xcode, no cwd) now scores like a Spring Boot application; schemes in subfolders and other project types keep their order, and a saved selection still wins.
- currentIosSimulator fell back to the first listed simulator, while the backend's pick_simulator takes a booted simulator, then the newest iPhone. The toolbar now applies the same rule, and pick_simulator ranks over simulator::parse_list so both sides agree on ties.

Fixes #7354

Co-authored-by: wildcatDownstairs <[email protected]>
2026-12-02 12:35:54 +08:00

84 рядки
3.6 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import type { IosSimulator, RunConfig } from "@/platform/ipc";
import { currentIosSimulator, iosSimulatorFor, refreshIosSimulators, useIosSimulators } from "./iosSimulators";
vi.mock("./contributions", () => ({ notifications: () => ({ notify: vi.fn() }) }));
const iosSimulators = vi.fn();
vi.mock("@/platform/ipc", async (original) => ({
...(await original<typeof import("@/platform/ipc")>()),
commands: { iosSimulators: () => iosSimulators() },
}));
const sim = (udid: string, state = "Shutdown"): IosSimulator => ({
udid,
name: udid,
runtime: "iOS 27.0",
state,
physical: false,
});
const config = (params?: Record<string, string>) =>
({ id: "xcode:App", kind: "xcode", params }) as unknown as RunConfig;
describe("iosSimulatorFor", () => {
it("prefers the configuration's pinned simulator, then the toolbar's choice, then the first listed", () => {
useIosSimulators.setState({ list: [sim("BOOTED", "Booted"), sim("OTHER")], preferred: null });
expect(iosSimulatorFor(config())).toBe("BOOTED");
useIosSimulators.setState({ preferred: "OTHER" });
expect(iosSimulatorFor(config())).toBe("OTHER");
expect(iosSimulatorFor(config({ simulator: "PINNED" }))).toBe("PINNED");
useIosSimulators.setState({ preferred: "GONE" });
expect(iosSimulatorFor(config())).toBe("BOOTED");
useIosSimulators.setState({ list: null });
expect(iosSimulatorFor(config())).toBeNull();
});
});
describe("currentIosSimulator", () => {
// Ordered as the backend lists them: booted, then newest runtime, then by name, then devices.
const named = (udid: string, name: string, runtime = "iOS 27.0", state = "Shutdown") => ({
...sim(udid, state),
name,
runtime,
});
const pad = named("PAD", "iPad (A16)");
const phone = named("PHONE", "iPhone 18");
const oldPhone = named("OLD", "iPhone 16", "iOS 18.2");
const device = { ...named("DEVICE", "iPhone", "iOS 27.0", "Available"), physical: true };
it("defaults to a booted simulator, then the newest iPhone, as the backend does", () => {
useIosSimulators.setState({ list: [pad, phone, oldPhone, device], preferred: null });
expect(currentIosSimulator()?.udid).toBe("PHONE");
const booted = { ...oldPhone, state: "Booted" };
useIosSimulators.setState({ list: [booted, pad, phone, device] });
expect(currentIosSimulator()?.udid).toBe("OLD");
useIosSimulators.setState({ list: [pad, device] });
expect(currentIosSimulator()?.udid).toBe("PAD");
});
it("keeps the simulator the user picked", () => {
useIosSimulators.setState({ list: [pad, phone], preferred: "PAD" });
expect(currentIosSimulator()?.udid).toBe("PAD");
});
});
describe("refreshIosSimulators", () => {
it("keeps the chooser hidden when the first listing fails, and reports later failures", async () => {
useIosSimulators.setState({ root: "/p", list: null, error: null });
iosSimulators.mockResolvedValueOnce({
status: "error",
error: { kind: "unavailable", message: "Restricted Mode" },
});
await refreshIosSimulators();
expect(useIosSimulators.getState()).toMatchObject({ list: null, error: null });
iosSimulators.mockResolvedValueOnce({ status: "ok", data: [sim("A")] });
await refreshIosSimulators();
expect(useIosSimulators.getState()).toMatchObject({ list: [sim("A")], error: null });
iosSimulators.mockResolvedValueOnce({ status: "error", error: { kind: "unavailable", message: "simctl failed" } });
await refreshIosSimulators();
expect(useIosSimulators.getState()).toMatchObject({ list: [sim("A")], error: "simctl failed" });
});
});