11import path from "node:path" ;
2+ import type { HealthCheck } from "openclaw/plugin-sdk/health" ;
23import type { WorkerProfile , WorkerProvider } from "openclaw/plugin-sdk/plugin-entry" ;
4+ import * as processRuntime from "openclaw/plugin-sdk/process-runtime" ;
35import type { SpawnResult } from "openclaw/plugin-sdk/process-runtime" ;
46import { describe , expect , it , vi } from "vitest" ;
5- import { operationLeaseId , resolveCrabboxBinary } from "./crabbox-worker-profile.js" ;
7+ import * as doctorRuntime from "./crabbox-worker-doctor-runtime.js" ;
8+ import {
9+ findCrabboxBinary ,
10+ operationLeaseId ,
11+ resolveCrabboxBinary ,
12+ } from "./crabbox-worker-profile.js" ;
613import { createCrabboxWorkerProvider , resolveOpenClawRoot } from "./crabbox-worker-provider.js" ;
14+ import {
15+ CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID ,
16+ registerCrabboxWorkerProviderDoctorChecks ,
17+ } from "./doctor.js" ;
718
819const OPERATION_ID = `provision:v2:${ "0" . repeat ( 64 ) } ` ;
920const LEASE_ID = "cbx_6071fc2062a6" ;
@@ -1675,6 +1686,25 @@ describe("Crabbox binary resolution", () => {
16751686 ) . toBe ( "crabbox" ) ;
16761687 } ) ;
16771688
1689+ it ( "distinguishes executable discovery from the dispatch fallback" , ( ) => {
1690+ const explicitBinary = path . resolve ( path . sep , "custom" , "crabbox" ) ;
1691+
1692+ expect (
1693+ findCrabboxBinary ( {
1694+ explicit : explicitBinary ,
1695+ openclawRoot : OPENCLAW_ROOT ,
1696+ isExecutable : ( ) => false ,
1697+ } ) ,
1698+ ) . toBeUndefined ( ) ;
1699+ expect (
1700+ findCrabboxBinary ( {
1701+ openclawRoot : OPENCLAW_ROOT ,
1702+ pathEnv : path . resolve ( path . sep , "not-executable" ) ,
1703+ isExecutable : ( ) => false ,
1704+ } ) ,
1705+ ) . toBeUndefined ( ) ;
1706+ } ) ;
1707+
16781708 it ( "derives the package root from source and bundled plugin roots" , ( ) => {
16791709 expect ( resolveOpenClawRoot ( path . join ( OPENCLAW_ROOT , "extensions" , "crabbox" ) ) ) . toBe (
16801710 OPENCLAW_ROOT ,
@@ -1684,3 +1714,145 @@ describe("Crabbox binary resolution", () => {
16841714 ) ;
16851715 } ) ;
16861716} ) ;
1717+
1718+ function captureCrabboxDoctorCheck ( ) : HealthCheck {
1719+ let check : HealthCheck | undefined ;
1720+ registerCrabboxWorkerProviderDoctorChecks ( {
1721+ openclawRoot : OPENCLAW_ROOT ,
1722+ registerHealthCheck ( value ) {
1723+ check = value ;
1724+ } ,
1725+ } ) ;
1726+ if ( ! check ) {
1727+ throw new Error ( "Crabbox doctor check was not registered" ) ;
1728+ }
1729+ return check ;
1730+ }
1731+
1732+ describe ( "Crabbox worker doctor" , ( ) => {
1733+ it ( "reports a configured non-executable binary with a profile-specific repair" , async ( ) => {
1734+ const probe = vi . spyOn ( doctorRuntime , "probeCrabboxVersion" ) ;
1735+ const binary = path . resolve ( path . sep , "nonexistent" , "crabbox" ) ;
1736+ try {
1737+ await expect (
1738+ captureCrabboxDoctorCheck ( ) . detect ( {
1739+ cfg : {
1740+ cloudWorkers : {
1741+ profiles : {
1742+ aws : { provider : "crabbox" , settings : { binary } } ,
1743+ } ,
1744+ } ,
1745+ } ,
1746+ env : { PATH : "" } ,
1747+ } as never ) ,
1748+ ) . resolves . toEqual ( [
1749+ expect . objectContaining ( {
1750+ checkId : CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID ,
1751+ severity : "warning" ,
1752+ message : expect . stringContaining ( 'profile "aws"' ) ,
1753+ path : binary ,
1754+ target : "aws" ,
1755+ fixHint : expect . stringContaining ( "cloudWorkers.profiles.aws.settings.binary" ) ,
1756+ } ) ,
1757+ ] ) ;
1758+ expect ( probe ) . not . toHaveBeenCalled ( ) ;
1759+ } finally {
1760+ probe . mockRestore ( ) ;
1761+ }
1762+ } ) ;
1763+
1764+ it ( "emits no finding for a supported configured binary" , async ( ) => {
1765+ const probe = vi
1766+ . spyOn ( doctorRuntime , "probeCrabboxVersion" )
1767+ . mockResolvedValue ( { status : "supported" , version : "0.41.6" } ) ;
1768+ try {
1769+ await expect (
1770+ captureCrabboxDoctorCheck ( ) . detect ( {
1771+ cfg : {
1772+ cloudWorkers : {
1773+ profiles : {
1774+ aws : { provider : "crabbox" , settings : { binary : process . execPath } } ,
1775+ } ,
1776+ } ,
1777+ } ,
1778+ } as never ) ,
1779+ ) . resolves . toEqual ( [ ] ) ;
1780+ expect ( probe ) . toHaveBeenCalledOnce ( ) ;
1781+ } finally {
1782+ probe . mockRestore ( ) ;
1783+ }
1784+ } ) ;
1785+
1786+ it ( "reports an indeterminate version probe without asserting failure" , async ( ) => {
1787+ const probe = vi . spyOn ( doctorRuntime , "probeCrabboxVersion" ) . mockResolvedValue ( {
1788+ status : "indeterminate" ,
1789+ reason : "version command timed out after 2000 ms" ,
1790+ } ) ;
1791+ try {
1792+ await expect (
1793+ captureCrabboxDoctorCheck ( ) . detect ( {
1794+ cfg : {
1795+ cloudWorkers : {
1796+ profiles : {
1797+ aws : { provider : "crabbox" , settings : { binary : process . execPath } } ,
1798+ } ,
1799+ } ,
1800+ } ,
1801+ } as never ) ,
1802+ ) . resolves . toEqual ( [
1803+ expect . objectContaining ( {
1804+ severity : "info" ,
1805+ message : expect . stringContaining ( "could not determine its version" ) ,
1806+ fixHint : expect . stringContaining ( `${ process . execPath } --version` ) ,
1807+ } ) ,
1808+ ] ) ;
1809+ } finally {
1810+ probe . mockRestore ( ) ;
1811+ }
1812+ } ) ;
1813+
1814+ it ( "does not probe when no Crabbox cloud worker profile is configured" , async ( ) => {
1815+ const probe = vi . spyOn ( doctorRuntime , "probeCrabboxVersion" ) ;
1816+ try {
1817+ await expect ( captureCrabboxDoctorCheck ( ) . detect ( { cfg : { } } as never ) ) . resolves . toEqual ( [ ] ) ;
1818+ expect ( probe ) . not . toHaveBeenCalled ( ) ;
1819+ } finally {
1820+ probe . mockRestore ( ) ;
1821+ }
1822+ } ) ;
1823+ } ) ;
1824+
1825+ describe ( "Crabbox version probe" , ( ) => {
1826+ it . each ( [
1827+ { output : "0.41.1\n" , expected : { status : "supported" , version : "0.41.1" } } ,
1828+ { output : "crabbox 0.41.6\n" , expected : { status : "supported" , version : "0.41.6" } } ,
1829+ { output : "0.40.9\n" , expected : { status : "outdated" , version : "0.40.9" } } ,
1830+ ] ) ( "classifies $output" , async ( { output, expected } ) => {
1831+ const run = vi
1832+ . spyOn ( processRuntime , "runCommandWithTimeout" )
1833+ . mockResolvedValue ( commandResult ( { stdout : output } ) ) ;
1834+ try {
1835+ await expect ( doctorRuntime . probeCrabboxVersion ( "/opt/crabbox" ) ) . resolves . toEqual ( expected ) ;
1836+ expect ( run ) . toHaveBeenCalledWith (
1837+ [ "/opt/crabbox" , "--version" ] ,
1838+ expect . objectContaining ( { timeoutMs : 2_000 , killProcessTree : true } ) ,
1839+ ) ;
1840+ } finally {
1841+ run . mockRestore ( ) ;
1842+ }
1843+ } ) ;
1844+
1845+ it ( "turns timeout into an indeterminate result" , async ( ) => {
1846+ const run = vi
1847+ . spyOn ( processRuntime , "runCommandWithTimeout" )
1848+ . mockResolvedValue ( commandResult ( { code : 124 , termination : "timeout" } ) ) ;
1849+ try {
1850+ await expect ( doctorRuntime . probeCrabboxVersion ( "/opt/crabbox" ) ) . resolves . toEqual ( {
1851+ status : "indeterminate" ,
1852+ reason : "version command timed out after 2000 ms" ,
1853+ } ) ;
1854+ } finally {
1855+ run . mockRestore ( ) ;
1856+ }
1857+ } ) ;
1858+ } ) ;
0 commit comments