11// Tests system command version probing for presence checks.
22import os from "node:os" ;
33import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures" ;
4- import { afterEach , describe , expect , it , vi } from "vitest" ;
4+ import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
55import { withEnvAsync } from "../test-utils/env.js" ;
66import { VERSION as runtimeVersion } from "../version.js" ;
77
88vi . unmock ( "../version.js" ) ;
99
10+ const spawnSyncMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
11+
12+ vi . mock ( "node:child_process" , async ( ) => {
13+ const { mockNodeChildProcessSpawnSync } = await import ( "openclaw/plugin-sdk/test-node-mocks" ) ;
14+ return mockNodeChildProcessSpawnSync ( spawnSyncMock , ( ) =>
15+ vi . importActual < typeof import ( "node:child_process" ) > ( "node:child_process" ) ,
16+ ) ;
17+ } ) ;
18+
1019async function withPresenceModule < T > (
1120 env : Record < string , string | undefined > ,
1221 run : ( module : typeof import ( "./system-presence.js" ) ) => Promise < T > | T ,
@@ -29,8 +38,20 @@ async function withPresenceModule<T>(
2938}
3039
3140describe ( "system-presence version fallback" , ( ) => {
41+ beforeEach ( ( ) => {
42+ spawnSyncMock . mockReturnValue ( {
43+ stdout : "" ,
44+ stderr : "" ,
45+ pid : 1 ,
46+ output : [ ] ,
47+ status : 0 ,
48+ signal : null ,
49+ } ) ;
50+ } ) ;
51+
3252 afterEach ( ( ) => {
3353 vi . restoreAllMocks ( ) ;
54+ spawnSyncMock . mockReset ( ) ;
3455 } ) ;
3556
3657 async function expectSelfVersion (
@@ -114,4 +135,37 @@ describe("system-presence version fallback", () => {
114135 expect ( selfEntry ?. ip ) . toBe ( "test-host" ) ;
115136 } ) ;
116137 } ) ;
138+
139+ it ( "bounds macOS self-presence metadata probes" , async ( ) => {
140+ vi . spyOn ( os , "platform" ) . mockReturnValue ( "darwin" ) ;
141+ vi . spyOn ( os , "release" ) . mockReturnValue ( "25.0.0" ) ;
142+ vi . spyOn ( os , "arch" ) . mockReturnValue ( "arm64" ) ;
143+ vi . spyOn ( os , "hostname" ) . mockReturnValue ( "test-mac" ) ;
144+ vi . spyOn ( os , "networkInterfaces" ) . mockReturnValue ( { } ) ;
145+ spawnSyncMock . mockImplementation ( ( command : string ) => ( {
146+ stdout : command === "sysctl" ? "Mac16,1\n" : "26.0\n" ,
147+ stderr : "" ,
148+ pid : 1 ,
149+ output : [ ] ,
150+ status : 0 ,
151+ signal : null ,
152+ } ) ) ;
153+
154+ await withPresenceModule ( { } , ( { listSystemPresence } ) => {
155+ const selfEntry = listSystemPresence ( ) . find ( ( entry ) => entry . reason === "self" ) ;
156+ expect ( selfEntry ?. modelIdentifier ) . toBe ( "Mac16,1" ) ;
157+ expect ( selfEntry ?. platform ) . toBe ( "macos 26.0" ) ;
158+ } ) ;
159+
160+ expect ( spawnSyncMock ) . toHaveBeenCalledWith ( "sysctl" , [ "-n" , "hw.model" ] , {
161+ encoding : "utf-8" ,
162+ timeout : 5_000 ,
163+ killSignal : "SIGKILL" ,
164+ } ) ;
165+ expect ( spawnSyncMock ) . toHaveBeenCalledWith ( "sw_vers" , [ "-productVersion" ] , {
166+ encoding : "utf-8" ,
167+ timeout : 5_000 ,
168+ killSignal : "SIGKILL" ,
169+ } ) ;
170+ } ) ;
117171} ) ;
0 commit comments