refactor(services): WindowPort + TrayPort traits to expose tray/window to test build#32
Merged
Merged
Conversation
…cfg(not(test)) gate Phase 1 of test/coverage-to-90 sub-branch (#8.5 of v0.7.0 epic). PR #31 left tray.rs (~537 lines) and window.rs (~162 lines) gated behind `#[cfg(not(test))]` in `services/mod.rs:10-13`, which kept them out of the test-build coverage pool. The reported 86.68% backend coverage was therefore measured against a smaller denominator than production reality. This commit introduces `WindowPort` and `TrayPort` traits that abstract the Tauri APIs each service depends on: - `WindowPort` (6 methods): list_monitors, primary_monitor, webview_exists, create_tip_window, close_window, list_window_labels - `TrayPort` (8 methods): install_tray, set_tooltip, set_menu_text, emit_to_window, unminimize_window, show_window, focus_window, quit_app Production adapters `TauriWindowPort` and `TauriTrayPort` (in `runtime` sub-modules) stay behind `#[cfg(not(test))]` because they bind directly to Tauri runtime types. The orchestration code in `WindowService` and `TrayService` no longer touches `AppHandle` and now compiles unconditionally. `services/mod.rs` loses the cfg gate on `tray` and `window`; the test build exercises both modules through `FakeWindowPort` / `FakeTrayPort` recording fakes (Vec<Call> shape, per dispatch instructions). Coverage results: | File | Lines (before) | Lines (after) | |-----------------------|----------------|----------------| | services/window.rs | excluded | 100.00% | | services/tray.rs | excluded | 93.25% | | TOTAL (backend) | 86.68% | 87.95% | The denominator grew by ~700 logic lines (tray + window now in pool) yet the total ratio went UP because the new tests cover both files well above the existing average. Test count: 227 -> 259 (+32 new orchestration tests). Phase 2 (frontend gap fill) and Phase 3 (gate bump to 90/85) are explicit follow-up dispatches, NOT in this PR. The CI gate stays at `--fail-under-lines 80` (Rust) and `lines: 80, functions: 70` (vitest). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 23, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 1 of
test/coverage-to-90sub-branch (#8.5 of v0.7.0 epic). PR #31 lefttray.rs(~537 lines) andwindow.rs(~162 lines) gated behind#[cfg(not(test))]inservices/mod.rs:10-13, which kept them out of thetest-build coverage pool. The reported 86.68% backend coverage was therefore
measured against a smaller denominator than production reality.
This PR introduces
WindowPortandTrayPorttraits so both services compilein the test build, drives them with recording fakes, and pulls the modules out
from behind the cfg gate.
Trait designs
WindowPort(6 methods, one Tauri API each):list_monitors() -> Result<Vec<MonitorInfo>>app.available_monitors()primary_monitor() -> Option<MonitorInfo>app.primary_monitor()webview_exists(label) -> boolapp.get_webview_window(label).is_some()create_tip_window(spec) -> Result<()>WebviewWindowBuilder+ fullscreen-failure-retry fallbackclose_window(label) -> Result<()>WebviewWindow::closelist_window_labels() -> Vec<String>app.webview_windows().keys()TrayPort(8 methods):install_tray(labels) -> Result<()>TrayIconBuilderone-shot setupset_tooltip(text) -> Result<()>tray.set_tooltipset_menu_text(item, text) -> Result<()>MenuItem::set_textemit_to_window(window, event, payload) -> Result<()>WebviewWindow::emitunminimize_window(label) -> Result<()>WebviewWindow::unminimizeshow_window(label) -> Result<()>WebviewWindow::showfocus_window(label) -> Result<()>WebviewWindow::set_focusquit_app()AppHandle::exit(0)Production adapters
TauriWindowPortandTauriTrayPort(inruntimesub-modules) stay behind
#[cfg(not(test))]because they bind directly toTauri runtime types. The orchestration code in
WindowServiceandTrayServiceno longer touchesAppHandleand now compiles unconditionally.The Tauri-specific tray-panel positioning + click-callback wiring still lives
in
runtime(production only) since it's pure Tauri integration with notestable orchestration left.
services/mod.rs:10-13lose the#[cfg(not(test))]lines onmod tray;andmod window;.Coverage delta
services/window.rsservices/tray.rsThe denominator grew by ~700 logic lines (tray + window now in the pool) yet
the total ratio went UP because the new tests cover both files well above the
pre-existing average.
Files modified
src-tauri/src/services/window.rs— full rewrite (WindowPorttrait,TauriWindowPortadapter, 11 orchestration tests)src-tauri/src/services/tray.rs— full rewrite (TrayPorttrait,TauriTrayPortadapter, 21 orchestration tests)src-tauri/src/services/mod.rs— drop#[cfg(not(test))]onmod tray/mod windowsrc-tauri/src/services/context.rs— adapt EffectSink to callservices.window.show_tip_windows()/services.tray.update_tooltip_best_effort(...)(no longer pass&AppHandle)src-tauri/src/lib.rs— wireTauriWindowPort+TauriTrayPortadapters into service constructionTests
services::window::tests: 11 cases (happy path, multi-monitor primary selection, no-primary fallback, no-monitors guard, monitor-enumeration failure, label-already-alive skip, create-error continues, close-error continues, hide filters only tip-window labels, lifecycle no-op)services::tray::tests: 21 cases (menu-event dispatch including unknown id, initial labels, tooltip happy path + error propagation, pause/resume label flip + error propagation, locale change re-texts every item + tolerates partial failure, show-main-window full sequence + emit/show/focus error short-circuits + unminimize tolerance, quit routes to port, start-with-initial-config installs tray and pushes tooltip + install-error propagation, shutdown without watch, init/start lifecycle no-op without runtime context)Verification
npm run ci8 steps all green:--all-targets -- -D warningsAlso verified:
cargo test --no-default-features— 259 passedcargo llvm-cov --fail-under-lines 80— passes at 87.95%Out of scope (NOT in this PR)
lines: 80 / functions: 70to90 / 85(separate dispatch)The CI gate stays at
--fail-under-lines 80(Rust CI) andlines: 80, functions: 70(vitest config) on purpose; that gets bumped in a dedicatedPhase 3 PR after Phase 2 has lifted the frontend numbers.
Test plan
npm run cipasses locallycargo test --no-default-featuresjob greencargo-coveragejob clears--fail-under-lines 80🤖 Generated with Claude Code