openclaw/apps/macos/Tests/ClawdbotIPCTests/ClawdbotConfigFileTests.swift

80 lines
2.8 KiB
Swift
Raw Normal View History

import Foundation
import Testing
@testable import Moltbot
@Suite(.serialized)
struct MoltbotConfigFileTests {
@Test
2026-01-07 20:13:24 +00:00
func configPathRespectsEnvOverride() async {
let override = FileManager().temporaryDirectory
.appendingPathComponent("moltbot-config-\(UUID().uuidString)")
.appendingPathComponent("moltbot.json")
.path
2026-01-07 20:13:24 +00:00
await TestIsolation.withEnvValues(["CLAWDBOT_CONFIG_PATH": override]) {
#expect(MoltbotConfigFile.url().path == override)
}
}
2026-01-07 11:00:21 +00:00
@MainActor
@Test
2026-01-07 20:13:24 +00:00
func remoteGatewayPortParsesAndMatchesHost() async {
let override = FileManager().temporaryDirectory
.appendingPathComponent("moltbot-config-\(UUID().uuidString)")
.appendingPathComponent("moltbot.json")
2026-01-07 11:00:21 +00:00
.path
2026-01-07 20:13:24 +00:00
await TestIsolation.withEnvValues(["CLAWDBOT_CONFIG_PATH": override]) {
MoltbotConfigFile.saveDict([
2026-01-07 11:00:21 +00:00
"gateway": [
"remote": [
2026-01-19 04:50:07 +00:00
"url": "ws://gateway.ts.net:19999",
2026-01-07 11:00:21 +00:00
],
],
])
#expect(MoltbotConfigFile.remoteGatewayPort() == 19999)
#expect(MoltbotConfigFile.remoteGatewayPort(matchingHost: "gateway.ts.net") == 19999)
#expect(MoltbotConfigFile.remoteGatewayPort(matchingHost: "gateway") == 19999)
#expect(MoltbotConfigFile.remoteGatewayPort(matchingHost: "other.ts.net") == nil)
2026-01-07 11:00:21 +00:00
}
}
@MainActor
@Test
2026-01-07 20:13:24 +00:00
func setRemoteGatewayUrlPreservesScheme() async {
let override = FileManager().temporaryDirectory
.appendingPathComponent("moltbot-config-\(UUID().uuidString)")
.appendingPathComponent("moltbot.json")
2026-01-07 11:00:21 +00:00
.path
2026-01-07 20:13:24 +00:00
await TestIsolation.withEnvValues(["CLAWDBOT_CONFIG_PATH": override]) {
MoltbotConfigFile.saveDict([
2026-01-07 11:00:21 +00:00
"gateway": [
"remote": [
"url": "wss://old-host:111",
],
],
])
MoltbotConfigFile.setRemoteGatewayUrl(host: "new-host", port: 2222)
let root = MoltbotConfigFile.loadDict()
2026-01-07 11:00:21 +00:00
let url = ((root["gateway"] as? [String: Any])?["remote"] as? [String: Any])?["url"] as? String
#expect(url == "wss://new-host:2222")
}
}
@Test
2026-01-07 20:13:24 +00:00
func stateDirOverrideSetsConfigPath() async {
let dir = FileManager().temporaryDirectory
.appendingPathComponent("moltbot-state-\(UUID().uuidString)", isDirectory: true)
.path
2026-01-07 20:13:24 +00:00
await TestIsolation.withEnvValues([
"CLAWDBOT_CONFIG_PATH": nil,
"CLAWDBOT_STATE_DIR": dir,
]) {
#expect(MoltbotConfigFile.stateDirURL().path == dir)
#expect(MoltbotConfigFile.url().path == "\(dir)/moltbot.json")
}
}
}