|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { afterEach, expect, test, vi } from 'vitest'; |
| 6 | +import type { DaemonStopResult } from '../../../daemon/daemon-stop.ts'; |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ |
| 9 | + cleanupRunnerLeasesForOwner: vi.fn(async () => undefined), |
| 10 | + readDaemonShutdownReport: vi.fn(), |
| 11 | + readDaemonStopIdentity: vi.fn(), |
| 12 | + stopDaemon: vi.fn(), |
| 13 | + writeCommandOutput: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('../../../daemon/daemon-stop.ts', () => ({ |
| 17 | + readDaemonStopIdentity: mocks.readDaemonStopIdentity, |
| 18 | + stopDaemon: mocks.stopDaemon, |
| 19 | +})); |
| 20 | +vi.mock('../../../daemon/daemon-shutdown-report.ts', () => ({ |
| 21 | + readDaemonShutdownReport: mocks.readDaemonShutdownReport, |
| 22 | +})); |
| 23 | +vi.mock('../../../platforms/apple/core/runner/runner-lease.ts', () => ({ |
| 24 | + cleanupRunnerLeasesForOwner: mocks.cleanupRunnerLeasesForOwner, |
| 25 | +})); |
| 26 | +vi.mock('../../../platforms/apple/core/runner/runner-disposal.ts', () => ({ |
| 27 | + runnerLeaseCleanupAdapter: {}, |
| 28 | +})); |
| 29 | +vi.mock('../shared.ts', () => ({ writeCommandOutput: mocks.writeCommandOutput })); |
| 30 | + |
| 31 | +import { daemonCommand } from '../daemon.ts'; |
| 32 | + |
| 33 | +const GRACEFUL_RESULT: DaemonStopResult = { |
| 34 | + stopped: true, |
| 35 | + mode: 'graceful', |
| 36 | + cleanupConfidence: 'known', |
| 37 | + claimsReleased: [], |
| 38 | + claimsOrphaned: [], |
| 39 | + providerReleases: { status: 'completed', released: [], pending: [] }, |
| 40 | + warnings: [], |
| 41 | +}; |
| 42 | + |
| 43 | +afterEach(() => { |
| 44 | + vi.clearAllMocks(); |
| 45 | +}); |
| 46 | + |
| 47 | +test('accepts only daemon stop', async () => { |
| 48 | + await assert.rejects( |
| 49 | + async () => |
| 50 | + await daemonCommand({ |
| 51 | + positionals: [], |
| 52 | + flags: { help: false, json: false, version: false }, |
| 53 | + client: {} as never, |
| 54 | + }), |
| 55 | + (error: { code?: string }) => error.code === 'INVALID_ARGS', |
| 56 | + ); |
| 57 | + await assert.rejects( |
| 58 | + async () => |
| 59 | + await daemonCommand({ |
| 60 | + positionals: ['stop', 'extra'], |
| 61 | + flags: { help: false, json: false, version: false }, |
| 62 | + client: {} as never, |
| 63 | + }), |
| 64 | + (error: { code?: string }) => error.code === 'INVALID_ARGS', |
| 65 | + ); |
| 66 | +}); |
| 67 | + |
| 68 | +test('merges a graceful shutdown report and cleans runner leases with the start-time identity', async () => { |
| 69 | + const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-daemon-command-')); |
| 70 | + mocks.readDaemonStopIdentity.mockReturnValue({ pid: 123, processStartTime: 'start-time' }); |
| 71 | + mocks.stopDaemon.mockResolvedValue(GRACEFUL_RESULT); |
| 72 | + mocks.readDaemonShutdownReport.mockReturnValue({ |
| 73 | + providerReleases: { |
| 74 | + released: [{ leaseId: 'lease-1', provider: 'limrun' }], |
| 75 | + pending: [], |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + try { |
| 80 | + await daemonCommand({ |
| 81 | + positionals: ['stop'], |
| 82 | + flags: { clean: true, help: false, json: false, stateDir, version: false }, |
| 83 | + client: {} as never, |
| 84 | + }); |
| 85 | + |
| 86 | + expect(mocks.cleanupRunnerLeasesForOwner).toHaveBeenCalledWith( |
| 87 | + { pid: 123, startTime: 'start-time' }, |
| 88 | + {}, |
| 89 | + ); |
| 90 | + expect(mocks.writeCommandOutput).toHaveBeenCalledWith( |
| 91 | + expect.objectContaining({ clean: true, json: false }), |
| 92 | + expect.objectContaining({ |
| 93 | + clean: true, |
| 94 | + providerReleases: { |
| 95 | + status: 'completed', |
| 96 | + released: [{ leaseId: 'lease-1', provider: 'limrun' }], |
| 97 | + pending: [], |
| 98 | + }, |
| 99 | + }), |
| 100 | + expect.any(Function), |
| 101 | + ); |
| 102 | + } finally { |
| 103 | + fs.rmSync(stateDir, { recursive: true, force: true }); |
| 104 | + } |
| 105 | +}); |
| 106 | + |
| 107 | +test('reports graceful provider cleanup as unknown when the shutdown report is unavailable', async () => { |
| 108 | + const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-daemon-command-')); |
| 109 | + mocks.readDaemonStopIdentity.mockReturnValue(null); |
| 110 | + mocks.stopDaemon.mockResolvedValue(GRACEFUL_RESULT); |
| 111 | + mocks.readDaemonShutdownReport.mockReturnValue(null); |
| 112 | + |
| 113 | + try { |
| 114 | + await daemonCommand({ |
| 115 | + positionals: ['stop'], |
| 116 | + flags: { help: false, json: false, stateDir, version: false }, |
| 117 | + client: {} as never, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(mocks.writeCommandOutput).toHaveBeenCalledWith( |
| 121 | + expect.objectContaining({ json: false }), |
| 122 | + expect.objectContaining({ |
| 123 | + clean: false, |
| 124 | + cleanupConfidence: 'unknown', |
| 125 | + providerReleases: { status: 'unknown', released: [], pending: null }, |
| 126 | + warnings: [expect.stringContaining('provider cleanup state is unknown')], |
| 127 | + }), |
| 128 | + expect.any(Function), |
| 129 | + ); |
| 130 | + } finally { |
| 131 | + fs.rmSync(stateDir, { recursive: true, force: true }); |
| 132 | + } |
| 133 | +}); |
0 commit comments