Skip to content

Commit cfc3831

Browse files
fix(server): only accept a rejected iOS shutdown when the hub reports the simulator off
A failure from serve-sim on a still-running simulator now surfaces instead of falling through to the hub route, which would skip the capture release. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 1c0d84e commit cfc3831

2 files changed

Lines changed: 103 additions & 88 deletions

File tree

‎apps/server/src/device/DeviceService.test.ts‎

Lines changed: 93 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type DeviceServiceState,
88
} from "@t3tools/contracts";
99
import * as Effect from "effect/Effect";
10+
import * as Exit from "effect/Exit";
1011
import * as Deferred from "effect/Deferred";
1112
import * as Fiber from "effect/Fiber";
1213
import * as PubSub from "effect/PubSub";
@@ -470,87 +471,97 @@ it.effect.each(["shutdown", "close"] as const)(
470471
),
471472
);
472473

473-
it.effect("shuts down an iOS simulator that is already off through the tolerant hub route", () =>
474-
Effect.gen(function* () {
475-
const deviceId = DeviceId.make("22222222-2222-2222-2222-222222222222");
476-
const paths: string[] = [];
477-
// Stale: the simulator was powered off outside T3.
478-
let booted = true;
479-
const ready: DeviceHost.DeviceHostReady = {
480-
nodePath: process.execPath,
481-
hub: { origin: "http://device.test" },
482-
helpers: { serveSimAxSettings: null, serveSimCli: null },
483-
run: () => Effect.succeed({ code: 0, stdout: "", stderr: "" }),
484-
};
485-
const host: DeviceHost.DeviceHost["Service"] = {
486-
id: LOCAL_DEVICE_HOST_ID,
487-
summary: Effect.succeed({
474+
it.effect.each([
475+
{ hubReportsBooted: false, outcome: "succeeds" },
476+
{ hubReportsBooted: true, outcome: "fails" },
477+
] as const)(
478+
"iOS shutdown $outcome when serve-sim rejects it and the hub reports booted=$hubReportsBooted",
479+
({ hubReportsBooted, outcome }) =>
480+
Effect.gen(function* () {
481+
const deviceId = DeviceId.make("22222222-2222-2222-2222-222222222222");
482+
const paths: string[] = [];
483+
// The device list is stale until shutdown re-reads it from the hub.
484+
let booted = true;
485+
const ready: DeviceHost.DeviceHostReady = {
486+
nodePath: process.execPath,
487+
hub: { origin: "http://device.test" },
488+
helpers: { serveSimAxSettings: null, serveSimCli: null },
489+
run: () => Effect.succeed({ code: 0, stdout: "", stderr: "" }),
490+
};
491+
const host: DeviceHost.DeviceHost["Service"] = {
488492
id: LOCAL_DEVICE_HOST_ID,
489-
kind: "local",
490-
label: "Simulator host",
491-
platforms: [{ platform: "ios", available: true }],
492-
hubInstalled: true,
493-
agentDeviceInstalled: false,
494-
}),
495-
platformAvailability: (platform) => Effect.succeed({ platform, available: true }),
496-
ensureReady: () => Effect.succeed(ready),
497-
ensureAgentReady: () => Effect.die("Agent access is not used in this test"),
498-
current: Effect.succeed(ready),
499-
stopAgent: Effect.void,
500-
stop: Effect.void,
501-
};
502-
const http = HttpClient.make((request) =>
503-
Effect.sync(() => {
504-
const path = new URL(request.url).pathname;
505-
paths.push(path);
506-
if (path === "/api/devices") {
507-
return HttpClientResponse.fromWeb(
508-
request,
509-
Response.json({
510-
emulators: [],
511-
simulators: [
512-
{
513-
id: deviceId,
514-
name: "iPhone",
515-
platform: "ios",
516-
version: "26",
517-
physical: false,
518-
booted,
519-
},
520-
],
521-
}),
522-
);
523-
}
524-
if (path === "/vendor/serve-sim/grid/api/shutdown") {
525-
// serve-sim runs `simctl shutdown` bare and fails on an already-off device.
526-
return HttpClientResponse.fromWeb(
527-
request,
528-
Response.json(
529-
{ ok: false, error: "Unable to shutdown device in current state: Shutdown" },
530-
{ status: 500 },
531-
),
532-
);
533-
}
534-
if (path === "/api/devices/shutdown") {
535-
booted = false;
536-
return HttpClientResponse.fromWeb(request, Response.json({ ok: true, id: deviceId }));
537-
}
538-
throw new Error(`Unexpected hub path: ${path}`);
539-
}),
540-
);
541-
const service = yield* makeWithHosts(new Map([[host.id, host]])).pipe(
542-
Effect.provideService(HttpClient.HttpClient, http),
543-
);
544-
yield* service.shutdown({ deviceId, platform: "ios" });
545-
expect(paths.filter((path) => path.endsWith("shutdown"))).toEqual([
546-
"/vendor/serve-sim/grid/api/shutdown",
547-
"/api/devices/shutdown",
548-
]);
549-
expect((yield* service.state).devices.find((device) => device.id === deviceId)?.booted).toBe(
550-
false,
551-
);
552-
}).pipe(
553-
Effect.provide(ServerSettingsService.layerTest({ enableDeviceSupport: true })),
554-
Effect.scoped,
555-
),
493+
summary: Effect.succeed({
494+
id: LOCAL_DEVICE_HOST_ID,
495+
kind: "local",
496+
label: "Simulator host",
497+
platforms: [{ platform: "ios", available: true }],
498+
hubInstalled: true,
499+
agentDeviceInstalled: false,
500+
}),
501+
platformAvailability: (platform) => Effect.succeed({ platform, available: true }),
502+
ensureReady: () => Effect.succeed(ready),
503+
ensureAgentReady: () => Effect.die("Agent access is not used in this test"),
504+
current: Effect.succeed(ready),
505+
stopAgent: Effect.void,
506+
stop: Effect.void,
507+
};
508+
const http = HttpClient.make((request) =>
509+
Effect.sync(() => {
510+
const path = new URL(request.url).pathname;
511+
paths.push(path);
512+
if (path === "/api/devices") {
513+
return HttpClientResponse.fromWeb(
514+
request,
515+
Response.json({
516+
emulators: [],
517+
simulators: [
518+
{
519+
id: deviceId,
520+
name: "iPhone",
521+
platform: "ios",
522+
version: "26",
523+
physical: false,
524+
booted,
525+
},
526+
],
527+
}),
528+
);
529+
}
530+
if (path === "/vendor/serve-sim/grid/api/shutdown") {
531+
// serve-sim runs `simctl shutdown` bare and returns its failure as-is.
532+
booted = hubReportsBooted;
533+
return HttpClientResponse.fromWeb(
534+
request,
535+
Response.json(
536+
{ ok: false, error: "Unable to shutdown device in current state: Shutdown" },
537+
{ status: 500 },
538+
),
539+
);
540+
}
541+
throw new Error(`Unexpected hub path: ${path}`);
542+
}),
543+
);
544+
const service = yield* makeWithHosts(new Map([[host.id, host]])).pipe(
545+
Effect.provideService(HttpClient.HttpClient, http),
546+
);
547+
yield* service.list;
548+
const exit = yield* Effect.exit(service.shutdown({ deviceId, platform: "ios" }));
549+
expect(paths.filter((path) => path.endsWith("shutdown"))).toEqual([
550+
"/vendor/serve-sim/grid/api/shutdown",
551+
]);
552+
if (outcome === "succeeds") {
553+
expect(Exit.isSuccess(exit)).toBe(true);
554+
expect(
555+
(yield* service.state).devices.find((device) => device.id === deviceId)?.booted,
556+
).toBe(false);
557+
} else {
558+
expect(Exit.isFailure(exit)).toBe(true);
559+
expect(
560+
(yield* service.state).devices.find((device) => device.id === deviceId)?.booted,
561+
).toBe(true);
562+
}
563+
}).pipe(
564+
Effect.provide(ServerSettingsService.layerTest({ enableDeviceSupport: true })),
565+
Effect.scoped,
566+
),
556567
);

‎apps/server/src/device/DeviceService.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -690,20 +690,24 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
690690
),
691691
),
692692
);
693-
const hubShutdown = postShutdown("/api/devices/shutdown", { platform, id: deviceId });
694693
// serve-sim's shutdown closes its in-process capture session before it runs
695694
// `simctl shutdown`; the hub's generic shutdown can leave that session cached
696695
// across a reboot. serve-sim runs simctl bare, though, so a simulator that is
697-
// already off fails there and the hub route, which tolerates that, takes over.
696+
// already off fails there. Accept that failure only when the hub confirms
697+
// the simulator is off; a failure on a running one still surfaces.
698698
yield* platform === "ios"
699699
? postShutdown(`${vendorPrefix("ios")}/grid/api/shutdown`, { udid: deviceId }).pipe(
700700
Effect.catch((cause) =>
701-
Effect.logWarning("serve-sim shutdown failed; retrying through the device hub", {
702-
cause,
703-
}).pipe(Effect.andThen(hubShutdown)),
701+
fetchDevices(ready).pipe(
702+
Effect.flatMap(({ devices }) =>
703+
devices.some((device) => device.id === deviceId && device.booted)
704+
? Effect.fail(cause)
705+
: Effect.logInfo("iOS simulator was already shut down", { deviceId }),
706+
),
707+
),
704708
),
705709
)
706-
: hubShutdown;
710+
: postShutdown("/api/devices/shutdown", { platform, id: deviceId });
707711
yield* publish((state) => ({
708712
...state,
709713
devices: state.devices.map((device) =>

0 commit comments

Comments
 (0)