Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ typecheck-tests:
test-without-xcode:
@./scripts/run-tests-without-xcode.sh $(NAME)

# One-shot dev cycle: rebuild, reinstall the app, refresh notify.sh in
# One-shot dev cycle: rebuild, reinstall the app, refresh notify.sh and the CLI in
# ~/.stack-nudge so hook-side changes propagate, kickstart the daemon.
# Build output goes to $(BUILD_LOG); on failure, last 20 lines tail to stderr.
.PHONY: reload
Expand All @@ -95,6 +95,7 @@ reload:
cp -R "$(BUILT_APP)" "$(APP)"; \
if [ -d "$$HOME/.stack-nudge" ]; then \
cp notify.sh "$$HOME/.stack-nudge/notify.sh"; \
cp stack-nudge.sh "$$HOME/.stack-nudge/stack-nudge"; \
if [ -d phrases ]; then \
rm -rf "$$HOME/.stack-nudge/phrases"; \
cp -R phrases "$$HOME/.stack-nudge/phrases"; \
Expand Down
8 changes: 8 additions & 0 deletions PRIVACY.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ StackNudge identifier, because there isn't one.
about 350 MB and lands in `~/.cache/stackvox/`, outside `~/.stack-nudge/` —
worth knowing if you are removing stack-nudge by deleting that directory.

## The local command line

`~/.stack-nudge/stack-nudge` sends commands (`say`, `focus`, `state`, `poll`,
`busy`, `settings`, `ping`) to the running panel over `~/.stack-nudge/panel.sock`,
a Unix socket readable and writable only by your user. Nothing leaves your
machine. The macOS Focus state is pushed in by a Shortcuts automation you set
up; stack-nudge never reads it from the system.

## Tools it runs on your behalf

Usage tracking (`STACKNUDGE_QUOTA_TRACKING`) reads your quota by shelling out to
Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ let package = Package(
"install.sh",
"uninstall.sh",
"notify.sh",
"stack-nudge.sh",
"notify.conf.example",
"README.md",
"LICENSE",
Expand Down
30 changes: 30 additions & 0 deletions Tests/StackNudgePanelCoreTests/BootstrapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,33 @@ final class BootstrapTests: XCTestCase {
return dir
}
}

// The stack-nudge CLI is copied beside notify.sh and kept equal to the bundle's.
final class BootstrapCLITests: XCTestCase {

private func tempDir() throws -> URL {
let dir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
addTeardownBlock { try? FileManager.default.removeItem(at: dir) }
return dir
}

func test_installCLI_writesAnExecutableCopy() throws {
let path = try tempDir().appendingPathComponent("stack-nudge").path
XCTAssertTrue(Bootstrap.installCLI(bundled: "#!/bin/sh\necho new\n", at: path))
XCTAssertEqual(try String(contentsOfFile: path, encoding: .utf8), "#!/bin/sh\necho new\n")
XCTAssertTrue(FileManager.default.isExecutableFile(atPath: path))
}

func test_installCLI_replacesAnOutdatedCopyAndLeavesAnEqualOne() throws {
let path = try tempDir().appendingPathComponent("stack-nudge").path
try "old".write(toFile: path, atomically: true, encoding: .utf8)
XCTAssertTrue(Bootstrap.installCLI(bundled: "new", at: path))
XCTAssertFalse(Bootstrap.installCLI(bundled: "new", at: path))
}

func test_installCLI_skipsAMachineWithNoInstallDirectory() {
XCTAssertFalse(Bootstrap.installCLI(bundled: "x", at: "/nonexistent-\(UUID().uuidString)/stack-nudge"))
XCTAssertFalse(Bootstrap.installCLI(bundled: nil, at: "/tmp/never"))
}
}
300 changes: 300 additions & 0 deletions Tests/StackNudgePanelCoreTests/CommandChannelTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
import XCTest

@testable import StackNudgePanelCore

// The request/reply form of the panel socket: the router on its own, then the
// real listener and the stack-nudge CLI against it.
@MainActor
final class CommandChannelTests: XCTestCase {

private final class Recorder {
var posted: [NudgeEvent] = []
var focus: Bool?
var pose: PoseOverride?
var settings: SettingsCategory??
var busy: [BusyItem] = []
var poll: ((@escaping (Int) -> Void) -> Void)?
}

private let now = Date(timeIntervalSince1970: 1_800_000_000)

private func router(_ recorder: Recorder) -> CommandRouter {
var hooks = CommandHooks()
hooks.post = { recorder.posted.append($0) }
hooks.setFocus = { recorder.focus = $0 }
hooks.focusStatus = { ["focus": recorder.focus ?? false, "held": 0] }
hooks.poll = { recorder.poll }
hooks.setPose = { recorder.pose = $0 }
hooks.busy = { recorder.busy }
hooks.openSettings = { category in
recorder.settings = .some(category)
return category ?? .notifications
}
hooks.now = { self.now }
return CommandRouter(hooks: hooks)
}

private func run(_ router: CommandRouter, _ json: String) -> [String: Any] {
var out = ""
router.handle(Data(json.utf8)) { out = $0 }
return (try? JSONSerialization.jsonObject(with: Data(out.utf8)) as? [String: Any]) ?? [:]
}

// MARK: - Detecting a command

func test_isCommand_onlyALineWithACmdKey() {
XCTAssertTrue(CommandRouter.isCommand(Data(#"{"cmd":"ping"}"#.utf8)))
XCTAssertFalse(CommandRouter.isCommand(Data(#"{"agent":"claude-code","event":"stop","title":"x","message":"y"}"#.utf8)))
XCTAssertFalse(CommandRouter.isCommand(Data("not json".utf8)))
XCTAssertFalse(CommandRouter.isCommand(Data(#"{"cmd":5}"#.utf8)))
}

// MARK: - Commands

func test_ping() {
let reply = run(router(Recorder()), #"{"cmd":"ping"}"#)
XCTAssertEqual(reply["ok"] as? Bool, true)
XCTAssertEqual(reply["pong"] as? Bool, true)
}

func test_unknownCommandFails() {
let reply = run(router(Recorder()), #"{"cmd":"nope"}"#)
XCTAssertEqual(reply["ok"] as? Bool, false)
XCTAssertEqual(reply["error"] as? String, "unknown command nope")
}

func test_say_postsAScriptFeedEvent() {
let recorder = Recorder()
let reply = run(router(recorder), #"{"cmd":"say","title":"Deploy done","body":"v2","url":"https://example.com/d","severity":"action","tag":"deploy","key":"deploy:1"}"#)
XCTAssertEqual(reply["ok"] as? Bool, true)
let event = recorder.posted.first
XCTAssertEqual(recorder.posted.count, 1)
XCTAssertEqual(event?.title, "Deploy done")
XCTAssertEqual(event?.message, "v2")
XCTAssertEqual(event?.source, .script)
XCTAssertEqual(event?.kind, .other)
XCTAssertEqual(event?.severity, .action)
XCTAssertEqual(event?.tag, "deploy")
XCTAssertEqual(event?.key, "deploy:1")
XCTAssertEqual(event?.url?.absoluteString, "https://example.com/d")
}

func test_say_dropsANonWebLinkAndNeverMakesAnAgentEvent() {
let recorder = Recorder()
_ = run(router(recorder), #"{"cmd":"say","title":"x","url":"file:///etc/passwd"}"#)
XCTAssertNil(recorder.posted.first?.url)
XCTAssertTrue(recorder.posted.first?.isFeed ?? false)
}

func test_say_rejectsBadInput() {
let recorder = Recorder()
let r = router(recorder)
XCTAssertEqual(run(r, #"{"cmd":"say"}"#)["ok"] as? Bool, false)
XCTAssertEqual(run(r, #"{"cmd":"say","title":"x","severity":"loud"}"#)["ok"] as? Bool, false)
XCTAssertEqual(run(r, #"{"cmd":"say","title":"x","source":"agent"}"#)["ok"] as? Bool, false)
XCTAssertTrue(recorder.posted.isEmpty)
}

func test_focus_setsAndReadsState() {
let recorder = Recorder()
let r = router(recorder)
XCTAssertEqual(run(r, #"{"cmd":"focus","state":"on"}"#)["ok"] as? Bool, true)
XCTAssertEqual(recorder.focus, true)
XCTAssertEqual(run(r, #"{"cmd":"focus","state":"off"}"#)["focus"] as? Bool, false)
XCTAssertEqual(recorder.focus, false)
recorder.focus = nil
XCTAssertEqual(run(r, #"{"cmd":"focus"}"#)["ok"] as? Bool, true)
XCTAssertNil(recorder.focus, "status must not change the state")
XCTAssertEqual(run(r, #"{"cmd":"focus","state":"maybe"}"#)["ok"] as? Bool, false)
}

func test_poll_saysNoPollerUntilOneIsWired() {
let recorder = Recorder()
let r = router(recorder)
XCTAssertEqual(run(r, #"{"cmd":"poll"}"#)["error"] as? String, "no poller")
recorder.poll = { $0(3) }
let reply = run(r, #"{"cmd":"poll"}"#)
XCTAssertEqual(reply["ok"] as? Bool, true)
XCTAssertEqual(reply["newEvents"] as? Int, 3)
}

func test_state_storesTheOverrideWithItsExpiry() {
let recorder = Recorder()
let reply = run(router(recorder), #"{"cmd":"state","state":"sleeping","ttlSeconds":120}"#)
XCTAssertEqual(reply["ok"] as? Bool, true)
XCTAssertEqual(recorder.pose, PoseOverride(pose: "sleeping", expiresAt: now.addingTimeInterval(120)))
}

func test_state_defaultsAndClampsTheTTL() {
let recorder = Recorder()
let r = router(recorder)
_ = run(r, #"{"cmd":"state","state":"alert"}"#)
XCTAssertEqual(recorder.pose?.expiresAt, now.addingTimeInterval(30))
_ = run(r, #"{"cmd":"state","state":"alert","ttlSeconds":99999}"#)
XCTAssertEqual(recorder.pose?.expiresAt, now.addingTimeInterval(3600))
_ = run(r, #"{"cmd":"state","state":"alert","ttlSeconds":0}"#)
XCTAssertEqual(recorder.pose?.expiresAt, now.addingTimeInterval(1))
XCTAssertEqual(run(r, #"{"cmd":"state","state":"a b"}"#)["ok"] as? Bool, false)
XCTAssertEqual(run(r, #"{"cmd":"state"}"#)["ok"] as? Bool, false)
}

func test_poseOverride_isActiveUntilItExpires() {
let pose = PoseOverride(pose: "alert", expiresAt: now.addingTimeInterval(10))
XCTAssertTrue(pose.isActive(at: now))
XCTAssertFalse(pose.isActive(at: now.addingTimeInterval(10)))
}

func test_busy_reportsTheRegisteredSources() {
let recorder = Recorder()
let r = router(recorder)
XCTAssertEqual(run(r, #"{"cmd":"busy"}"#)["busy"] as? Bool, false)
recorder.busy = [BusyItem(kind: "session", target: "repo", since: nil)]
let reply = run(r, #"{"cmd":"busy"}"#)
XCTAssertEqual(reply["busy"] as? Bool, true)
let items = reply["items"] as? [[String: String]]
XCTAssertEqual(items?.first?["kind"], "session")
XCTAssertEqual(items?.first?["target"], "repo")
}

func test_busySources_mergeAndSortProviders() {
let sources = BusySources()
sources.register("b") { [BusyItem(kind: "run", target: "z", since: nil)] }
sources.register("a") { [BusyItem(kind: "poll", target: "", since: nil)] }
XCTAssertEqual(sources.items().map(\.kind), ["poll", "run"])
}

func test_settings_opensACategoryAndRejectsAnUnknownOne() {
let recorder = Recorder()
let r = router(recorder)
let reply = run(r, #"{"cmd":"settings","category":"focus"}"#)
XCTAssertEqual(reply["current"] as? String, "focus")
XCTAssertEqual(recorder.settings, .some(.focus))
XCTAssertTrue((reply["categories"] as? [String])?.contains("voice") ?? false)
_ = run(r, #"{"cmd":"settings"}"#)
XCTAssertEqual(recorder.settings, .some(nil))
XCTAssertEqual(run(r, #"{"cmd":"settings","category":"nope"}"#)["ok"] as? Bool, false)
}

// MARK: - Real socket

private final class Box: @unchecked Sendable {
private let lock = NSLock()
private var stored: String?
private var finished = false
func finish(_ value: String?) { lock.lock(); stored = value; finished = true; lock.unlock() }
var isFinished: Bool { lock.lock(); defer { lock.unlock() }; return finished }
var value: String? { lock.lock(); defer { lock.unlock() }; return stored }
}

// Runs `work` off the main thread while this one keeps servicing the main
// queue, which is where the listener hands commands to the router.
private func offMain(_ work: @escaping @Sendable () -> String?) -> String? {
let box = Box()
DispatchQueue.global().async { box.finish(work()) }
let deadline = Date().addingTimeInterval(10)
while !box.isFinished, Date() < deadline {
RunLoop.current.run(until: Date().addingTimeInterval(0.01))
}
return box.value
}

private nonisolated static func connect(_ path: String) -> Int32? {
let fd = socket(AF_UNIX, SOCK_STREAM, 0)
var addr = sockaddr_un()
addr.sun_family = sa_family_t(AF_UNIX)
let bytes = Array(path.utf8CString)
withUnsafeMutableBytes(of: &addr.sun_path) { $0.copyBytes(from: bytes.map { UInt8(bitPattern: $0) }) }
let status = withUnsafePointer(to: &addr) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
Darwin.connect(fd, $0, socklen_t(MemoryLayout<sockaddr_un>.size))
}
}
guard status == 0 else { close(fd); return nil }
return fd
}

// Next to this checkout's Tests directory; the no-Xcode runner stages the
// test files elsewhere, but runs from the repo root.
private static func cliScript() -> String {
let beside = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
.appendingPathComponent("stack-nudge.sh").path
if FileManager.default.fileExists(atPath: beside) { return beside }
return FileManager.default.currentDirectoryPath + "/stack-nudge.sh"
}

private func startListener(_ recorder: Recorder) throws -> (EventListener, String, EventStore) {
let path = "/tmp/sn-\(UUID().uuidString.prefix(8)).sock"
let store = EventStore()
let listener = EventListener(store: store, socketPath: path, commands: router(recorder))
try listener.start()
return (listener, path, store)
}

func test_socket_answersACommandWithoutTheClientClosing() throws {
let (listener, path, _) = try startListener(Recorder())
defer { listener.stop() }
let reply = offMain {
guard let fd = Self.connect(path) else { return nil }
defer { close(fd) }
let line = Array("{\"cmd\":\"ping\"}\n".utf8)
_ = write(fd, line, line.count)
var buf = [UInt8](repeating: 0, count: 256)
let n = read(fd, &buf, buf.count)
return n > 0 ? String(decoding: buf.prefix(n), as: UTF8.self) : nil
}
XCTAssertEqual(reply, "{\"ok\":true,\"pong\":true}\n")
}

func test_socket_stillTakesAnEventThatClosesWithoutReading() throws {
let (listener, path, store) = try startListener(Recorder())
defer { listener.stop() }
_ = offMain {
guard let fd = Self.connect(path) else { return nil }
let line = Array("{\"agent\":\"claude-code\",\"event\":\"stop\",\"title\":\"Done\",\"message\":\"ok\"}\n".utf8)
_ = write(fd, line, line.count)
close(fd)
return "sent"
}
let deadline = Date().addingTimeInterval(5)
while store.events.isEmpty, Date() < deadline {
RunLoop.current.run(until: Date().addingTimeInterval(0.01))
}
XCTAssertEqual(store.events.first?.title, "Done")
}

func test_cli_sendsCommandsThroughTheSocket() throws {
let recorder = Recorder()
let (listener, path, _) = try startListener(recorder)
defer { listener.stop() }
let script = Self.cliScript()

let reply = offMain {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/sh")
process.arguments = [script, "say", "Deploy \"done\"", "--severity", "action", "--tag", "deploy"]
process.environment = ["STACKNUDGE_SOCK": path, "PATH": "/usr/bin:/bin"]
try? process.run()
process.waitUntilExit()
return String(process.terminationStatus)
}
XCTAssertEqual(reply, "0")
XCTAssertEqual(recorder.posted.first?.title, "Deploy \"done\"")
XCTAssertEqual(recorder.posted.first?.severity, .action)
XCTAssertEqual(recorder.posted.first?.tag, "deploy")
}

func test_cli_exitsQuietlyForSayWhenThePanelIsDown() {
let script = Self.cliScript()
let status = offMain {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/sh")
process.arguments = [script, "focus", "on"]
process.environment = ["STACKNUDGE_SOCK": "/tmp/sn-missing.sock", "PATH": "/usr/bin:/bin"]
try? process.run()
process.waitUntilExit()
return String(process.terminationStatus)
}
XCTAssertEqual(status, "0")
}
}
2 changes: 1 addition & 1 deletion Tests/StackNudgePanelCoreTests/CompactGlanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class CompactGlanceTests: XCTestCase {
var exitedCompact = false
nav.actions = SettingsActions(
checkPermissions: {}, openConfig: {}, editPhrases: {},
browseExtensions: {}, openExtensionSources: {}, openExtension: { _ in },
browseExtensions: {}, openExtensionSources: {}, editNotifyTags: {}, openExtension: { _ in },
openReleaseNotes: {}, checkForUpdates: {}, beginUpdate: {}, runUpdate: {},
beginUninstall: {}, runUninstall: {}, runBootstrap: {}, quit: {},
expandFromCompact: { modeWhenExpanded = nav.mode },
Expand Down
Loading
Loading