diff --git a/crates/domain/src/lib.rs b/crates/domain/src/lib.rs index c67bae2..289bb49 100644 --- a/crates/domain/src/lib.rs +++ b/crates/domain/src/lib.rs @@ -22,7 +22,7 @@ pub use monitor::MonitorInfo; pub use new_focus::NewFocus; pub use notification::{ all_sources, FocusesOverCapSource, NotificationSettings, NotificationSource, - TaskTimerExpiredSource, TasksOverCapSource, TimerExpiredSource, + NotificationSourceDescriptor, TaskTimerExpiredSource, TasksOverCapSource, TimerExpiredSource, }; pub use parse::{parse_focus_md, ParseError}; pub use pig_rect::{PigRect, RectUpdater}; diff --git a/crates/domain/src/notification.rs b/crates/domain/src/notification.rs index f292af4..d5e97a2 100644 --- a/crates/domain/src/notification.rs +++ b/crates/domain/src/notification.rs @@ -7,6 +7,14 @@ pub trait NotificationSource { fn label(&self) -> &'static str; } +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[cfg_attr(feature = "export-ts", derive(ts_rs::TS))] +#[cfg_attr(feature = "export-ts", ts(export))] +pub struct NotificationSourceDescriptor { + pub key: String, + pub label: String, +} + #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] #[cfg_attr(feature = "export-ts", derive(ts_rs::TS))] #[cfg_attr(feature = "export-ts", ts(export))] diff --git a/issues/README.md b/issues/README.md index 0728c61..f2312db 100644 --- a/issues/README.md +++ b/issues/README.md @@ -4,11 +4,11 @@ Each issue is a self-contained vertical slice that one person or coding agent ca ## Priority queue -- [031](031-notification-source-settings.md) — Notification source settings registry (GH #31) +No feature issues queued. ### Architecture queue (deepening) -These are not required before 031. Pick one when we choose to spend a slice on internal seams or future-proofing. +Pick one when we choose to spend a slice on internal seams or future-proofing. - [056](056-task-timer-binding.md) — Task Timers drift when Tasks are hand-edited - [051](051-ranch-animal-vocabulary-seam.md) — Animal vocabulary for the movement layer (render contract shipped; `Pig*` names remain) diff --git a/issues/031-notification-source-settings.md b/issues/done/031-notification-source-settings.md similarity index 63% rename from issues/031-notification-source-settings.md rename to issues/done/031-notification-source-settings.md index 08b6fb5..985025b 100644 --- a/issues/031-notification-source-settings.md +++ b/issues/done/031-notification-source-settings.md @@ -41,14 +41,22 @@ Notification toggles in Preferences are driven by the Rust `NotificationSource` ## Acceptance criteria -- [ ] A command/API returns notification source descriptors from `all_sources()` -- [ ] `SettingsWindow` no longer contains a hardcoded `NOTIFICATION_SOURCES` list -- [ ] "Timer expired", "Too many focuses", and "Too many tasks in a focus" toggles still render with current settings values -- [ ] Toggling any source updates `settings.yaml` and takes effect immediately (no restart needed) -- [ ] Adding a second or later `NotificationSource` requires no changes to `SettingsWindow` -- [ ] Existing Settings sections (General, Widget, Displays, Debug) are unaffected -- [ ] `task check` green +- [x] A command/API returns notification source descriptors from `all_sources()` +- [x] `SettingsWindow` no longer contains a hardcoded `NOTIFICATION_SOURCES` list +- [x] "Timer expired", "Too many focuses", and "Too many tasks in a focus" toggles still render with current settings values +- [x] Toggling any source updates `settings.yaml` and takes effect immediately (no restart needed) +- [x] Adding a second or later `NotificationSource` requires no changes to `SettingsWindow` +- [x] Existing Settings sections (General, Widget, Displays, Debug) are unaffected +- [x] `task check` green ## Blocked by 050 — settings update workflow and runtime freshness. 029 and 032 are done. + +## Validation + +Automated acceptance passed: the registry projection, API/hook wiring, current labels and values, and arbitrary-descriptor rendering/toggling are covered by focused tests. Existing settings workflow, flat YAML round-trip, and latest-settings notification tests passed, covering persistence and immediate runtime application without changing the settings schema. + +`task check` and `task check:windows` passed. Implementation review returned PASS; the rules review was clean. + +Native Preferences interaction, actual settings-file changes from native toggles, and OS notification delivery were not observed. These checks establish automated behavior, not native GUI verification. diff --git a/src-tauri/src/app/mod.rs b/src-tauri/src/app/mod.rs index 5ade4b3..c0d834c 100644 --- a/src-tauri/src/app/mod.rs +++ b/src-tauri/src/app/mod.rs @@ -82,6 +82,7 @@ pub fn run() { ui_bridge::update_pig_rects, ui_bridge::set_pig_drag_active, ui_bridge::get_settings, + ui_bridge::list_notification_sources, ui_bridge::update_settings, ui_bridge::edit_saved_timer_minutes, ui_bridge::get_monitors, diff --git a/src-tauri/src/ui_bridge/mod.rs b/src-tauri/src/ui_bridge/mod.rs index c89445d..1e2e125 100644 --- a/src-tauri/src/ui_bridge/mod.rs +++ b/src-tauri/src/ui_bridge/mod.rs @@ -7,7 +7,10 @@ use adhd_ranch_commands::{ }; use adhd_ranch_domain::agents::hooks::{HookFiring, HookWiring}; use adhd_ranch_domain::session::AgentSession; -use adhd_ranch_domain::{Focus, Settings, TimerOwner, TimerPreset, TimerPresetEdit, TimerPresets}; +use adhd_ranch_domain::{ + all_sources, Focus, NotificationSourceDescriptor, Settings, TimerOwner, TimerPreset, + TimerPresetEdit, TimerPresets, +}; use tauri::{AppHandle, Emitter, Manager, State, Wry}; @@ -199,6 +202,17 @@ pub fn set_pig_drag_active(active: bool, state: State<'_, DragLockState>) { state.0.store(active, Ordering::Relaxed); } +#[tauri::command] +pub fn list_notification_sources() -> Vec { + all_sources() + .iter() + .map(|source| NotificationSourceDescriptor { + key: source.key().to_string(), + label: source.label().to_string(), + }) + .collect() +} + #[tauri::command] pub fn get_settings(state: State<'_, SettingsState>) -> Settings { state.0.lock().map(|s| s.clone()).unwrap_or_default() @@ -283,3 +297,18 @@ pub fn get_devtools_open(app: AppHandle) -> bool { pub fn get_devtools_open() -> bool { false } + +#[cfg(test)] +mod tests { + #[test] + fn notification_descriptors_follow_the_domain_registry() { + let descriptors = super::list_notification_sources(); + let sources = adhd_ranch_domain::all_sources(); + + assert_eq!(descriptors.len(), sources.len()); + for (descriptor, source) in descriptors.iter().zip(sources) { + assert_eq!(descriptor.key, source.key()); + assert_eq!(descriptor.label, source.label()); + } + } +} diff --git a/src/api/settings.test.ts b/src/api/settings.test.ts index a9e6d47..dcfa146 100644 --- a/src/api/settings.test.ts +++ b/src/api/settings.test.ts @@ -1,7 +1,7 @@ import { invoke } from "@tauri-apps/api/core"; import { beforeEach, describe, expect, it, vi } from "vitest"; import type { TimerPresetEdit } from "../types/generated/TimerPresetEdit"; -import { editSavedTimerMinutes } from "./settings"; +import { editSavedTimerMinutes, listNotificationSources } from "./settings"; vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() })); @@ -30,3 +30,20 @@ describe("editSavedTimerMinutes", () => { await expect(editSavedTimerMinutes({ Add: 25 })).rejects.toBe(error); }); }); + +describe("listNotificationSources", () => { + it("returns registry descriptors from IPC without filtering future keys", async () => { + const sources = [{ key: "future_source", label: "A future notification" }]; + vi.mocked(invoke).mockResolvedValue(sources); + + expect(await listNotificationSources()).toEqual(sources); + expect(invoke).toHaveBeenCalledWith("list_notification_sources"); + }); + + it("propagates registry read failures", async () => { + const error = new Error("unavailable"); + vi.mocked(invoke).mockRejectedValue(error); + + await expect(listNotificationSources()).rejects.toBe(error); + }); +}); diff --git a/src/api/settings.ts b/src/api/settings.ts index d973a7c..8395486 100644 --- a/src/api/settings.ts +++ b/src/api/settings.ts @@ -1,8 +1,13 @@ import { invoke } from "@tauri-apps/api/core"; +import type { NotificationSourceDescriptor } from "../types/generated/NotificationSourceDescriptor"; import type { TimerPresetEdit } from "../types/generated/TimerPresetEdit"; import type { TimerPresets } from "../types/generated/TimerPresets"; import type { Settings } from "../types/settings"; +export async function listNotificationSources(): Promise { + return invoke("list_notification_sources"); +} + export async function getSettings(): Promise { return invoke("get_settings"); } diff --git a/src/components/SettingsWindow.test.tsx b/src/components/SettingsWindow.test.tsx index fc27daa..a4a3d29 100644 --- a/src/components/SettingsWindow.test.tsx +++ b/src/components/SettingsWindow.test.tsx @@ -15,10 +15,22 @@ const SETTINGS: Settings = { timers: { saved_minutes: [15] }, }; -function renderSettings(onUpdate: (next: Settings) => void) { +const SOURCES = [ + { key: "timer_expired", label: "Timer expired" }, + { key: "task_timer_expired", label: "Task timer expired" }, + { key: "focuses_over_cap", label: "Too many focuses" }, + { key: "tasks_over_cap", label: "Too many tasks in a focus" }, +]; + +function renderSettings( + onUpdate: (next: Settings) => void, + settings = SETTINGS, + notificationSources = SOURCES, +) { render( { }); }); }); + +describe("SettingsWindow notification registry", () => { + it("renders registered labels with explicit values and enabled defaults", () => { + renderSettings(vi.fn(), { + ...SETTINGS, + notifications: { sources: { timer_expired: false, focuses_over_cap: true } }, + }); + + expect(screen.getByLabelText("Timer expired")).not.toBeChecked(); + expect(screen.getByLabelText("Task timer expired")).toBeChecked(); + expect(screen.getByLabelText("Too many focuses")).toBeChecked(); + expect(screen.getByLabelText("Too many tasks in a focus")).toBeChecked(); + }); + + it.each([true, false])( + "toggles an injected future source from %s and preserves settings", + async (enabled) => { + const onUpdate = vi.fn(); + const settings = { + ...SETTINGS, + notifications: { sources: { timer_expired: false, future_source: enabled } }, + }; + renderSettings(onUpdate, settings, [ + ...SOURCES, + { key: "future_source", label: "A future notification" }, + ]); + + await userEvent.click(screen.getByLabelText("A future notification")); + + expect(onUpdate).toHaveBeenCalledWith({ + ...settings, + notifications: { sources: { timer_expired: false, future_source: !enabled } }, + }); + }, + ); + + it("does not invent notification rows before the registry loads", () => { + renderSettings(vi.fn(), SETTINGS, []); + + expect(screen.queryByLabelText("Timer expired")).not.toBeInTheDocument(); + expect(screen.getByLabelText("Max focuses")).toHaveValue(5); + expect(screen.getByLabelText("Always on Top")).not.toBeChecked(); + expect(screen.getByLabelText("Max pen size")).toHaveValue(320); + expect(screen.getByLabelText("Debug Overlay")).not.toBeChecked(); + }); +}); diff --git a/src/components/SettingsWindow.tsx b/src/components/SettingsWindow.tsx index e3590fa..a5a32a8 100644 --- a/src/components/SettingsWindow.tsx +++ b/src/components/SettingsWindow.tsx @@ -1,20 +1,15 @@ import type React from "react"; import { useState } from "react"; +import type { NotificationSourceDescriptor } from "../types/generated/NotificationSourceDescriptor"; import type { MonitorInfo } from "../types/monitor"; import type { Settings } from "../types/settings"; const PEN_SIZE_RANGE = { min: 160, max: 960 } as const; -const NOTIFICATION_SOURCES = [ - { key: "timer_expired", label: "Timer expired" }, - { key: "task_timer_expired", label: "Task timer expired" }, - { key: "focuses_over_cap", label: "Too many focuses" }, - { key: "tasks_over_cap", label: "Too many tasks in a focus" }, -] as const; - interface SettingsWindowProps { readonly settings: Settings | null; readonly monitors: MonitorInfo[]; + readonly notificationSources: readonly NotificationSourceDescriptor[]; readonly debugOverlay: boolean; readonly devtoolsOpen: boolean; readonly onUpdate: (next: Settings) => void; @@ -94,6 +89,7 @@ function toggleDisplayIndex(indices: readonly number[], idx: number): number[] { export function SettingsWindow({ settings, monitors, + notificationSources, debugOverlay, devtoolsOpen, onUpdate, @@ -182,7 +178,7 @@ export function SettingsWindow({

Notifications

- {NOTIFICATION_SOURCES.map(({ key, label }) => ( + {notificationSources.map(({ key, label }) => ( ({ + getSettings: vi.fn(), + listNotificationSources: vi.fn(), + updateSettings: vi.fn(), +})); +vi.mock("../api/monitors", () => ({ getMonitors: vi.fn() })); +vi.mock("../api/debug", () => ({ + getDebugOverlay: vi.fn(), + getDevtoolsOpen: vi.fn(), + setDebugOverlay: vi.fn(), + toggleDevtools: vi.fn(), +})); + +const SETTINGS: Settings = { + caps: { max_focuses: 5, max_tasks_per_focus: 7 }, + notifications: { sources: { timer_expired: false } }, + widget: { always_on_top: false, confirm_delete: true }, + displays: { enabled_indices: [0] }, + agents: { enabled: false }, + pens: { max_size: 320 }, + timers: { saved_minutes: [15] }, +}; + +describe("useSettingsWindow notification registry", () => { + beforeEach(() => { + vi.clearAllMocks(); + vi.mocked(getSettings).mockResolvedValue(SETTINGS); + vi.mocked(getMonitors).mockResolvedValue([{ idx: 0, label: "Primary" }]); + vi.mocked(getDebugOverlay).mockResolvedValue(true); + vi.mocked(getDevtoolsOpen).mockResolvedValue(false); + vi.mocked(updateSettings).mockResolvedValue(); + }); + + it("loads arbitrary descriptors and sends source edits through the existing settings workflow", async () => { + const sources = [{ key: "future_source", label: "A future notification" }]; + vi.mocked(listNotificationSources).mockResolvedValue(sources); + const { result } = renderHook(() => useSettingsWindow()); + await waitFor(() => expect(result.current.settings).toEqual(SETTINGS)); + + expect(result.current.notificationSources).toEqual(sources); + expect(result.current.monitors).toEqual([{ idx: 0, label: "Primary" }]); + expect(result.current.debugOverlay).toBe(true); + const next = { + ...SETTINGS, + notifications: { sources: { ...SETTINGS.notifications.sources, future_source: false } }, + }; + + act(() => result.current.update(next)); + + expect(updateSettings).toHaveBeenCalledWith(next); + expect(result.current.settings).toEqual(next); + }); + + it("keeps the other settings available when the registry read fails", async () => { + const error = new Error("unavailable"); + vi.mocked(listNotificationSources).mockRejectedValue(error); + const report = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + const { result } = renderHook(() => useSettingsWindow()); + + await waitFor(() => expect(result.current.settings).toEqual(SETTINGS)); + + expect(result.current.notificationSources).toEqual([]); + } finally { + report.mockRestore(); + } + }); +}); diff --git a/src/hooks/useSettingsWindow.ts b/src/hooks/useSettingsWindow.ts index 67e624f..f90db5e 100644 --- a/src/hooks/useSettingsWindow.ts +++ b/src/hooks/useSettingsWindow.ts @@ -1,13 +1,15 @@ import { useCallback, useEffect, useState } from "react"; import { getDebugOverlay, getDevtoolsOpen, setDebugOverlay, toggleDevtools } from "../api/debug"; import { getMonitors } from "../api/monitors"; -import { getSettings, updateSettings } from "../api/settings"; +import { getSettings, listNotificationSources, updateSettings } from "../api/settings"; +import type { NotificationSourceDescriptor } from "../types/generated/NotificationSourceDescriptor"; import type { MonitorInfo } from "../types/monitor"; import type { Settings } from "../types/settings"; export interface SettingsWindowState { settings: Settings | null; monitors: MonitorInfo[]; + notificationSources: NotificationSourceDescriptor[]; debugOverlay: boolean; devtoolsOpen: boolean; update: (next: Settings) => void; @@ -17,12 +19,16 @@ export interface SettingsWindowState { export function useSettingsWindow(): SettingsWindowState { const [settings, setSettings] = useState(null); + const [notificationSources, setNotificationSources] = useState( + [], + ); const [monitors, setMonitors] = useState([]); const [debugOverlay, setDebugOverlayState] = useState(false); // real state loaded from backend on mount const [devtoolsOpen, setDevtoolsOpen] = useState(false); useEffect(() => { getSettings().then(setSettings).catch(console.error); + listNotificationSources().then(setNotificationSources).catch(console.error); getMonitors().then(setMonitors).catch(console.error); getDebugOverlay().then(setDebugOverlayState).catch(console.error); getDevtoolsOpen().then(setDevtoolsOpen).catch(console.error); @@ -46,6 +52,7 @@ export function useSettingsWindow(): SettingsWindowState { return { settings, monitors, + notificationSources, debugOverlay, devtoolsOpen, update, diff --git a/src/settings.tsx b/src/settings.tsx index e31b6b8..35ae3f5 100644 --- a/src/settings.tsx +++ b/src/settings.tsx @@ -10,6 +10,7 @@ function SettingsApp() { const { settings, monitors, + notificationSources, debugOverlay, devtoolsOpen, update, @@ -34,6 +35,7 @@ function SettingsApp() { containerRef={containerRef} settings={settings} monitors={monitors} + notificationSources={notificationSources} debugOverlay={debugOverlay} devtoolsOpen={devtoolsOpen} onUpdate={update} diff --git a/src/types/generated/NotificationSourceDescriptor.ts b/src/types/generated/NotificationSourceDescriptor.ts new file mode 100644 index 0000000..07457eb --- /dev/null +++ b/src/types/generated/NotificationSourceDescriptor.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type NotificationSourceDescriptor = { key: string, label: string, };