Files
openclaw/apps/macos/Tests/OpenClawIPCTests/NodeServiceManagerTests.swift
Peter Steinberger 0dbdf994b3 feat(macos): isolate named app profiles (#121136)
* feat(macos): isolate named app profiles

* refactor(macos): isolate profile launch ownership

* fix(macos): avoid overlapping approvals socket access

* fix(macos): declare profile defaults concurrency ownership

* fix(macos): return profiled node launch arguments

* chore(i18n): refresh macOS profile source inventory

* fix(macos): gate profile startup before services

* test(macos): evaluate profile state before assertions

* fix(daemon): skip absent launchd deactivation

* fix(macos): fail closed on profile port conflicts

* chore(i18n): refresh profile conflict inventory

* fix(macos): ignore non-gateway launch agent claims

* test(macos): stabilize profile lifecycle timing

* fix(macos): remove stale dashboard URL

* chore(macos): refresh native source baseline
2026-08-09 14:50:15 -07:00

74 lines
3.4 KiB
Swift

import Foundation
import Testing
@testable import OpenClaw
@Suite(.serialized) struct NodeServiceManagerTests {
@Test func `active profile performs no persistent node service work`() async {
let profile = AppProfile(environment: ["OPENCLAW_PROFILE": "work"])
NodeServiceManager._testResetPersistentServiceCalls()
#expect(await NodeServiceManager.start(profile: profile) == nil)
#expect(await NodeServiceManager.stop(profile: profile) == nil)
#expect(await NodeServiceManager.restart(profile: profile) == nil)
#expect(NodeServiceManager.launchdProgramArguments(profile: profile) == [])
#expect(await !(NodeServiceManager.waitUntilRunning(profile: profile)))
let snapshot = NodeServiceManager._testPersistentServiceCallSnapshot()
#expect(snapshot.commands.isEmpty)
#expect(snapshot.ownershipReads == 0)
}
@Test func `builds node service commands with current CLI shape`() async throws {
try await TestIsolation.withUserDefaultsValues(["openclaw.gatewayProjectRootPath": nil]) {
let tmp = try makeTempDirForTests()
CommandResolver.setProjectRoot(tmp.path)
let openclawPath = tmp.appendingPathComponent("node_modules/.bin/openclaw")
try makeExecutableForTests(at: openclawPath)
let start = await NodeServiceManager._testServiceCommand(["start"])
#expect(start == [openclawPath.path, "node", "start", "--json"])
let stop = await NodeServiceManager._testServiceCommand(["stop"])
#expect(stop == [openclawPath.path, "node", "stop", "--json"])
let restart = await NodeServiceManager._testServiceCommand(["restart"])
#expect(restart == [openclawPath.path, "node", "restart", "--json"])
}
}
@Test func `reads node service ownership command directly from launchd`() throws {
let url = FileManager.default.temporaryDirectory
.appendingPathComponent("openclaw-node-\(UUID().uuidString).plist")
defer { try? FileManager.default.removeItem(at: url) }
let arguments = [
"/Users/Test/.openclaw/tools/node/bin/node",
"/Users/Test/.openclaw/lib/node_modules/openclaw/dist/index.js",
"node",
"run",
]
let data = try PropertyListSerialization.data(
fromPropertyList: ["ProgramArguments": arguments],
format: .xml,
options: 0)
try data.write(to: url, options: .atomic)
#expect(NodeServiceManager._testLaunchdProgramArguments(plistURL: url) == arguments)
try Data("not a plist".utf8).write(to: url, options: .atomic)
#expect(NodeServiceManager._testLaunchdProgramArguments(plistURL: url) == nil)
try FileManager.default.removeItem(at: url)
#expect(NodeServiceManager._testLaunchdProgramArguments(plistURL: url) == [])
}
@Test func `node status requires loaded running service`() {
#expect(NodeServiceManager._testRuntimeIsRunning(fromJSON: """
{"service":{"loaded":true,"runtime":{"status":"running"}}}
"""))
#expect(!NodeServiceManager._testRuntimeIsRunning(fromJSON: """
{"service":{"loaded":false,"runtime":{"status":"running"}}}
"""))
#expect(!NodeServiceManager._testRuntimeIsRunning(fromJSON: """
{"service":{"loaded":true,"runtime":{"status":"stopped"}}}
"""))
}
}