|
| 1 | +import { daemonPlatformServicesFixture } from '../../../../__tests__/support/platform-services-fixture.ts'; |
| 2 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 3 | +import { |
| 4 | + sessionCloseShutdownFixture, |
| 5 | + type SessionState, |
| 6 | +} from './session-close-shutdown.fixtures.ts'; |
| 7 | +import { |
| 8 | + mkdtempForTestSync, |
| 9 | + bindTestCommandHost, |
| 10 | + createTestHostEnvironment, |
| 11 | +} from '@agent-device/test-kit'; |
| 12 | +import { fakeCreateCommandSurface } from '../../../../__tests__/support/command-surface-fake.ts'; |
| 13 | +import { lifecycleOperationsSpy } from '../../../../__tests__/support/lifecycle-operations-spy.ts'; |
| 14 | +import { platformResourceCleanupFixture } from '../../../../__tests__/support/platform-resource-cleanup-fixture.ts'; |
| 15 | + |
| 16 | +bindTestCommandHost(); |
| 17 | + |
| 18 | +const { |
| 19 | + AppError, |
| 20 | + handleSessionCommands, |
| 21 | + makeSession, |
| 22 | + makeSessionStore, |
| 23 | + mockShutdownTargetRuntime, |
| 24 | + noopInvoke, |
| 25 | + path, |
| 26 | + resetSessionCloseShutdownMocks, |
| 27 | +} = sessionCloseShutdownFixture; |
| 28 | + |
| 29 | +beforeEach(resetSessionCloseShutdownMocks); |
| 30 | + |
| 31 | +const iosSimulator: SessionState['device'] = { |
| 32 | + platform: 'apple', |
| 33 | + appleOs: 'ios', |
| 34 | + id: 'sim-udid-implicit', |
| 35 | + name: 'iPhone 15', |
| 36 | + kind: 'simulator', |
| 37 | + // Selection records the boot from the stopped state, and the session keeps that snapshot. |
| 38 | + booted: false, |
| 39 | +}; |
| 40 | + |
| 41 | +const androidEmulator: SessionState['device'] = { |
| 42 | + platform: 'android', |
| 43 | + id: 'emulator-5554', |
| 44 | + name: 'Pixel_9_API_35', |
| 45 | + kind: 'emulator', |
| 46 | + booted: false, |
| 47 | +}; |
| 48 | + |
| 49 | +async function closeSession(params: { |
| 50 | + sessionName: string; |
| 51 | + device: SessionState['device']; |
| 52 | + bootedByOpen?: true; |
| 53 | + flags?: Record<string, unknown>; |
| 54 | + lifecycleOperationsLoader?: Parameters<typeof handleSessionCommands>[0]['lifecycleOperationsLoader']; |
| 55 | + platformResourceCleanup?: ReturnType<typeof platformResourceCleanupFixture>; |
| 56 | +}) { |
| 57 | + const { sessionName, device, bootedByOpen, flags } = params; |
| 58 | + const sessionStore = makeSessionStore(); |
| 59 | + const session = makeSession(sessionName, device); |
| 60 | + if (bootedByOpen) session.bootedByOpen = bootedByOpen; |
| 61 | + sessionStore.publish(sessionName, session); |
| 62 | + const response = await handleSessionCommands({ |
| 63 | + host: createTestHostEnvironment(), |
| 64 | + createCommandSurface: fakeCreateCommandSurface, |
| 65 | + platformServices: daemonPlatformServicesFixture(), |
| 66 | + ...(params.lifecycleOperationsLoader |
| 67 | + ? { lifecycleOperationsLoader: params.lifecycleOperationsLoader } |
| 68 | + : {}), |
| 69 | + ...(params.platformResourceCleanup |
| 70 | + ? { platformResourceCleanup: params.platformResourceCleanup } |
| 71 | + : {}), |
| 72 | + req: { |
| 73 | + token: 't', |
| 74 | + session: sessionName, |
| 75 | + command: 'close', |
| 76 | + positionals: [], |
| 77 | + flags: flags ?? {}, |
| 78 | + }, |
| 79 | + sessionName, |
| 80 | + logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'), |
| 81 | + sessionStore, |
| 82 | + invoke: noopInvoke, |
| 83 | + }); |
| 84 | + return { response, sessionStore }; |
| 85 | +} |
| 86 | + |
| 87 | +test.each([ |
| 88 | + { label: 'iOS simulator', device: iosSimulator }, |
| 89 | + { label: 'Android emulator', device: androidEmulator }, |
| 90 | +])( |
| 91 | + 'plain close stops the $label this session booted and reports it in data.shutdown', |
| 92 | + async ({ device }) => { |
| 93 | + const { response, sessionStore } = await closeSession({ |
| 94 | + sessionName: 'implicit-shutdown-session', |
| 95 | + device, |
| 96 | + bootedByOpen: true, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(response?.ok).toBe(true); |
| 100 | + expect(mockShutdownTargetRuntime).toHaveBeenCalledTimes(1); |
| 101 | + expect(sessionStore.get('implicit-shutdown-session')).toBeUndefined(); |
| 102 | + if (response?.ok) { |
| 103 | + expect(response.data?.session).toBe('implicit-shutdown-session'); |
| 104 | + expect(response.data?.shutdown).toEqual({ |
| 105 | + success: true, |
| 106 | + exitCode: 0, |
| 107 | + stdout: '', |
| 108 | + stderr: '', |
| 109 | + }); |
| 110 | + } |
| 111 | + }, |
| 112 | +); |
| 113 | + |
| 114 | +test.each([ |
| 115 | + { label: 'iOS simulator', device: { ...iosSimulator, booted: true } }, |
| 116 | + { label: 'Android emulator', device: { ...androidEmulator, booted: true } }, |
| 117 | +])( |
| 118 | + 'plain close leaves the already-booted $label running and reports no shutdown', |
| 119 | + async ({ device }) => { |
| 120 | + const { response, sessionStore } = await closeSession({ |
| 121 | + sessionName: 'prebooted-session', |
| 122 | + device, |
| 123 | + }); |
| 124 | + |
| 125 | + expect(response?.ok).toBe(true); |
| 126 | + expect(mockShutdownTargetRuntime).not.toHaveBeenCalled(); |
| 127 | + expect(sessionStore.get('prebooted-session')).toBeUndefined(); |
| 128 | + if (response?.ok) expect(response.data?.shutdown).toBeUndefined(); |
| 129 | + }, |
| 130 | +); |
| 131 | + |
| 132 | +test.each([ |
| 133 | + { |
| 134 | + label: 'a physical iOS device', |
| 135 | + device: { |
| 136 | + platform: 'apple' as const, |
| 137 | + appleOs: 'ios' as const, |
| 138 | + id: 'physical-implicit-1', |
| 139 | + name: 'My iPhone', |
| 140 | + kind: 'device' as const, |
| 141 | + booted: false, |
| 142 | + }, |
| 143 | + }, |
| 144 | + { |
| 145 | + label: 'a physical Android device', |
| 146 | + device: { |
| 147 | + platform: 'android' as const, |
| 148 | + id: 'R5CT123456A', |
| 149 | + name: 'Pixel 9', |
| 150 | + kind: 'device' as const, |
| 151 | + booted: false, |
| 152 | + }, |
| 153 | + }, |
| 154 | +])( |
| 155 | + 'plain close on $label leaves the stop to the platform, which reports none', |
| 156 | + async ({ device }) => { |
| 157 | + // Selection never records a boot for these kinds. The close may still ask the admitted platform |
| 158 | + // to finalize with a stop, and the platform answers with no shutdown result. |
| 159 | + const { loader, finalizeApplicationClose } = lifecycleOperationsSpy(); |
| 160 | + const { response } = await closeSession({ |
| 161 | + sessionName: 'physical-session', |
| 162 | + device, |
| 163 | + lifecycleOperationsLoader: loader, |
| 164 | + }); |
| 165 | + |
| 166 | + expect(response?.ok).toBe(true); |
| 167 | + expect(finalizeApplicationClose).toHaveBeenCalledWith( |
| 168 | + expect.objectContaining({ shutdownTarget: false }), |
| 169 | + ); |
| 170 | + expect(mockShutdownTargetRuntime).not.toHaveBeenCalled(); |
| 171 | + if (response?.ok) expect(response.data?.shutdown).toBeUndefined(); |
| 172 | + }, |
| 173 | +); |
| 174 | + |
| 175 | +test('plain close on a provider-owned session never asks the local-family stop', async () => { |
| 176 | + // The boot fact cannot come from a provider selection. Even a record that carries one must not |
| 177 | + // route a provider session through the local shutdown capability. |
| 178 | + const sessionStore = makeSessionStore(); |
| 179 | + const sessionName = 'provider-implicit-session'; |
| 180 | + const device: SessionState['device'] = { |
| 181 | + platform: 'apple', |
| 182 | + appleOs: 'ios', |
| 183 | + id: 'limrun:ios:lease-implicit', |
| 184 | + name: 'Limrun iOS', |
| 185 | + kind: 'simulator', |
| 186 | + booted: false, |
| 187 | + }; |
| 188 | + const session = makeSession(sessionName, device); |
| 189 | + session.bootedByOpen = true; |
| 190 | + sessionStore.publish(sessionName, session); |
| 191 | + const { loader, finalizeApplicationClose } = lifecycleOperationsSpy(); |
| 192 | + const facts = sessionCloseShutdownFixture.lifecycleRuntimeFacts(device); |
| 193 | + const providerFacts = { |
| 194 | + ...facts, |
| 195 | + device: { ...facts.device, providerMode: 'provider-runtime' as const }, |
| 196 | + }; |
| 197 | + sessionCloseShutdownFixture.mockInspectDeviceRuntimeFacts |
| 198 | + .mockResolvedValueOnce(providerFacts) |
| 199 | + .mockResolvedValueOnce(providerFacts); |
| 200 | + sessionCloseShutdownFixture.mockBindDeviceRuntime.mockImplementationOnce( |
| 201 | + async (candidate, use) => |
| 202 | + await sessionCloseShutdownFixture.narrowDeviceBinding( |
| 203 | + { |
| 204 | + device: candidate, |
| 205 | + owner: sessionCloseShutdownFixture.providerRuntimeOwner('limrun', 'lease-implicit'), |
| 206 | + facts: providerFacts, |
| 207 | + operations: await loader(candidate), |
| 208 | + [Symbol.asyncDispose]: async () => undefined, |
| 209 | + }, |
| 210 | + use, |
| 211 | + ), |
| 212 | + ); |
| 213 | + |
| 214 | + const response = await handleSessionCommands({ |
| 215 | + host: createTestHostEnvironment(), |
| 216 | + createCommandSurface: fakeCreateCommandSurface, |
| 217 | + platformServices: daemonPlatformServicesFixture(), |
| 218 | + req: { |
| 219 | + token: 't', |
| 220 | + session: sessionName, |
| 221 | + command: 'close', |
| 222 | + positionals: [], |
| 223 | + flags: {}, |
| 224 | + }, |
| 225 | + sessionName, |
| 226 | + logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'), |
| 227 | + sessionStore, |
| 228 | + invoke: noopInvoke, |
| 229 | + }); |
| 230 | + |
| 231 | + expect(response?.ok).toBe(true); |
| 232 | + expect(finalizeApplicationClose).toHaveBeenCalledWith( |
| 233 | + expect.objectContaining({ shutdownTarget: false }), |
| 234 | + ); |
| 235 | + expect(mockShutdownTargetRuntime).not.toHaveBeenCalled(); |
| 236 | +}); |
| 237 | + |
| 238 | +test('plain close on a boot-owned device finalizes without a retained runner and without retention', async () => { |
| 239 | + const { loader, finalizeApplicationClose } = lifecycleOperationsSpy(); |
| 240 | + const platformResourceCleanup = platformResourceCleanupFixture(); |
| 241 | + const { response } = await closeSession({ |
| 242 | + sessionName: 'implicit-retain-session', |
| 243 | + device: iosSimulator, |
| 244 | + bootedByOpen: true, |
| 245 | + lifecycleOperationsLoader: loader, |
| 246 | + platformResourceCleanup, |
| 247 | + }); |
| 248 | + |
| 249 | + expect(response?.ok).toBe(true); |
| 250 | + expect(finalizeApplicationClose).toHaveBeenCalledWith( |
| 251 | + expect.objectContaining({ shutdownTarget: true, retainRunner: false }), |
| 252 | + ); |
| 253 | + expect(platformResourceCleanup.retainExecutionHostAfterClose).toHaveBeenCalledWith( |
| 254 | + expect.objectContaining({ device: iosSimulator, shutdownRequested: true }), |
| 255 | + ); |
| 256 | +}); |
| 257 | + |
| 258 | +test('close --shutdown keeps asking for the stop on a session that did not boot its device', async () => { |
| 259 | + const { loader, finalizeApplicationClose } = lifecycleOperationsSpy(); |
| 260 | + const { response } = await closeSession({ |
| 261 | + sessionName: 'explicit-shutdown-session', |
| 262 | + device: { ...iosSimulator, booted: true }, |
| 263 | + flags: { shutdown: true }, |
| 264 | + lifecycleOperationsLoader: loader, |
| 265 | + }); |
| 266 | + |
| 267 | + expect(response?.ok).toBe(true); |
| 268 | + expect(finalizeApplicationClose).toHaveBeenCalledWith( |
| 269 | + expect.objectContaining({ shutdownTarget: true }), |
| 270 | + ); |
| 271 | +}); |
| 272 | + |
| 273 | +test('a policy denial of the implicit stop keeps close successful and reports the kept device', async () => { |
| 274 | + const policyDenied = new AppError('UNAUTHORIZED', "This daemon's policy denies device-shutdown.", { |
| 275 | + capability: 'device-shutdown', |
| 276 | + reason: 'DAEMON_POLICY_DENIED', |
| 277 | + rule: 'capability', |
| 278 | + retriable: false, |
| 279 | + }); |
| 280 | + const { loader, finalizeApplicationClose } = lifecycleOperationsSpy({ |
| 281 | + finalizeApplicationClose: vi.fn(async () => { |
| 282 | + throw policyDenied; |
| 283 | + }), |
| 284 | + }); |
| 285 | + const { response, sessionStore } = await closeSession({ |
| 286 | + sessionName: 'policy-denied-session', |
| 287 | + device: iosSimulator, |
| 288 | + bootedByOpen: true, |
| 289 | + lifecycleOperationsLoader: loader, |
| 290 | + }); |
| 291 | + |
| 292 | + expect(finalizeApplicationClose).toHaveBeenCalledWith( |
| 293 | + expect.objectContaining({ shutdownTarget: true }), |
| 294 | + ); |
| 295 | + expect(response?.ok).toBe(true); |
| 296 | + expect(sessionStore.get('policy-denied-session')).toBeUndefined(); |
| 297 | + if (response?.ok) { |
| 298 | + expect(response.data?.session).toBe('policy-denied-session'); |
| 299 | + expect(response.data?.shutdown).toBeUndefined(); |
| 300 | + expect(response.data?.warnings).toEqual([ |
| 301 | + "This session's open booted this device and close would stop it, but this daemon's policy denies device-shutdown. The device is still running.", |
| 302 | + ]); |
| 303 | + } |
| 304 | +}); |
| 305 | + |
| 306 | +test('the same policy denial still fails an explicit close --shutdown', async () => { |
| 307 | + const policyDenied = new AppError('UNAUTHORIZED', "This daemon's policy denies device-shutdown.", { |
| 308 | + capability: 'device-shutdown', |
| 309 | + reason: 'DAEMON_POLICY_DENIED', |
| 310 | + rule: 'capability', |
| 311 | + retriable: false, |
| 312 | + }); |
| 313 | + const { loader } = lifecycleOperationsSpy({ |
| 314 | + finalizeApplicationClose: vi.fn(async () => { |
| 315 | + throw policyDenied; |
| 316 | + }), |
| 317 | + }); |
| 318 | + const sessionStore = makeSessionStore(); |
| 319 | + const sessionName = 'policy-denied-explicit-session'; |
| 320 | + const session = makeSession(sessionName, { ...iosSimulator, booted: true }); |
| 321 | + sessionStore.publish(sessionName, session); |
| 322 | + |
| 323 | + // An explicit stop that the policy refuses is the caller's own failed request: close fails with |
| 324 | + // the incomplete-cleanup report, exactly as any other failed finalization does. |
| 325 | + await expect( |
| 326 | + handleSessionCommands({ |
| 327 | + host: createTestHostEnvironment(), |
| 328 | + createCommandSurface: fakeCreateCommandSurface, |
| 329 | + platformServices: daemonPlatformServicesFixture(), |
| 330 | + lifecycleOperationsLoader: loader, |
| 331 | + req: { |
| 332 | + token: 't', |
| 333 | + session: sessionName, |
| 334 | + command: 'close', |
| 335 | + positionals: [], |
| 336 | + flags: { shutdown: true }, |
| 337 | + }, |
| 338 | + sessionName, |
| 339 | + logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'), |
| 340 | + sessionStore, |
| 341 | + invoke: noopInvoke, |
| 342 | + }), |
| 343 | + ).rejects.toMatchObject({ |
| 344 | + details: { reason: 'session_cleanup_incomplete', failedSteps: ['application_lifecycle'] }, |
| 345 | + }); |
| 346 | +}); |
0 commit comments