Files
bot/apps/macos/Tests/BotIPCTests/CanvasFileWatcherTests.swift
zeekay 08582cc303 Rebrand OpenClaw → Hanzo Bot across entire codebase
Complete rename of all OpenClaw/OPENCLAW/openclaw references to
Hanzo Bot/BOT/bot naming convention:

- Env vars: OPENCLAW_* → BOT_* (18 patterns)
- Config: ~/.openclaw/ → ~/.bot/, openclaw.json → bot.json
- Packages: @openclaw/* → @hanzo/bot-*
- Domains: openclaw.ai → hanzo.bot
- Mobile: OpenClawKit → BotKit, bundle IDs ai.openclaw → ai.hanzo.bot
- mDNS: _openclaw-gw._tcp → _bot-gw._tcp
- Docker: CMD hanzo-bot.mjs, BOT_PREFER_PNPM, BOT_DOCKER_APT_PACKAGES
- CI: docker-release.yml adds :latest tag on main branch
2026-02-11 11:13:08 -08:00

79 lines
2.8 KiB
Swift

import Foundation
import os
import Testing
@testable import HanzoBot
@Suite(.serialized) struct CanvasFileWatcherTests {
private func makeTempDir() throws -> URL {
let base = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let dir = base.appendingPathComponent("hanzo-bot-canvaswatch-\(UUID().uuidString)", isDirectory: true)
try FileManager().createDirectory(at: dir, withIntermediateDirectories: true)
return dir
}
@Test func detectsInPlaceFileWrites() async throws {
let dir = try self.makeTempDir()
defer { try? FileManager().removeItem(at: dir) }
let file = dir.appendingPathComponent("index.html")
try "hello".write(to: file, atomically: false, encoding: .utf8)
let fired = OSAllocatedUnfairLock(initialState: false)
let waitState = OSAllocatedUnfairLock<(fired: Bool, cont: CheckedContinuation<Void, Never>?)>(
initialState: (false, nil))
func waitForFire(timeoutNs: UInt64) async -> Bool {
await withTaskGroup(of: Bool.self) { group in
group.addTask {
await withCheckedContinuation { cont in
let resumeImmediately = waitState.withLock { state in
if state.fired { return true }
state.cont = cont
return false
}
if resumeImmediately {
cont.resume()
}
}
return true
}
group.addTask {
try? await Task.sleep(nanoseconds: timeoutNs)
return false
}
let result = await group.next() ?? false
group.cancelAll()
return result
}
}
let watcher = CanvasFileWatcher(url: dir) {
fired.withLock { $0 = true }
let cont = waitState.withLock { state in
state.fired = true
let cont = state.cont
state.cont = nil
return cont
}
cont?.resume()
}
watcher.start()
defer { watcher.stop() }
// Give the stream a moment to start.
try await Task.sleep(nanoseconds: 150 * 1_000_000)
// Modify the file in-place (no rename). This used to be missed when only watching the directory vnode.
let handle = try FileHandle(forUpdating: file)
try handle.seekToEnd()
try handle.write(contentsOf: Data(" world".utf8))
try handle.close()
let ok = await waitForFire(timeoutNs: 2_000_000_000)
#expect(ok == true)
#expect(fired.withLock { $0 } == true)
}
}