@@ -4673,6 +4673,182 @@ static bool app_test_explicit_index(cbm_daemon_runtime_application_callbacks_t *
46734673 return app_test_explicit_index_with_options (callbacks , session , root , name , NULL );
46744674}
46754675
4676+ TEST (daemon_application_watch_roots_match_windows_separators_only ) {
4677+ ASSERT_TRUE (
4678+ cbm_daemon_application_watch_roots_equal_for_test ("C:\\repos\\cbm" , "C:/repos/cbm" , true));
4679+ ASSERT_TRUE (cbm_daemon_application_watch_roots_equal_for_test ("\\\\server\\share\\cbm" ,
4680+ "//server/share/cbm" , true));
4681+ ASSERT_FALSE (
4682+ cbm_daemon_application_watch_roots_equal_for_test ("C:\\repos\\cbm" , "C:/repos/cbm2" , true));
4683+ ASSERT_FALSE (cbm_daemon_application_watch_roots_equal_for_test ("C:\\repos\\cbm" ,
4684+ "C:/repos/cbm/sub" , true));
4685+ ASSERT_FALSE (
4686+ cbm_daemon_application_watch_roots_equal_for_test ("/repos/a\\b" , "/repos/a/b" , false));
4687+ ASSERT_TRUE (
4688+ cbm_daemon_application_watch_roots_equal_for_test ("/repos/a\\b" , "/repos/a\\b" , false));
4689+ PASS ();
4690+ }
4691+
4692+ /* A held async worker completes after the global switch changes. No subsequent
4693+ * request may hide target-only cleanup by refreshing the session root. */
4694+ TEST (daemon_application_async_completion_disables_all_watches ) {
4695+ app_watch_race_fixture_t fixture ;
4696+ bool fixture_ready = app_watch_race_fixture_init_mcp (& fixture , 9320 , true);
4697+ char target [APP_TEST_PATH_CAP ];
4698+ (void )snprintf (target , sizeof (target ), "%s/cbm-app-disable-async-XXXXXX" , fixture .root );
4699+ bool target_ready = fixture_ready && cbm_mkdtemp (target ) != NULL ;
4700+ char db_path [APP_TEST_PATH_CAP ];
4701+ (void )snprintf (db_path , sizeof (db_path ), "%s/disable-target.db" , fixture .cache );
4702+ bool db_ready = target_ready && app_test_create_empty_file (db_path );
4703+ atomic_store (& fixture .fake .allow_completion , true);
4704+ bool indexed = db_ready && app_test_explicit_index (& fixture .callbacks , fixture .session , target ,
4705+ "disable-target" );
4706+ int watches_before = fixture .watcher ? cbm_watcher_watch_count (fixture .watcher ) : -1 ;
4707+ atomic_store (& fixture .fake .allow_completion , false);
4708+ uint8_t * request = NULL ;
4709+ uint32_t request_length = 0 ;
4710+ uint8_t * response = NULL ;
4711+ uint32_t response_length = 0 ;
4712+ bool encoded =
4713+ indexed && app_test_mcp_index_request (target , "disable-target" , ",\"async\":true" , & request ,
4714+ & request_length );
4715+ cbm_daemon_runtime_application_status_t status =
4716+ encoded ? app_test_request (& fixture .callbacks , fixture .session , request , request_length ,
4717+ & response , & response_length )
4718+ : CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR ;
4719+ bool started = status == CBM_DAEMON_RUNTIME_APPLICATION_OK && response &&
4720+ strstr ((char * )response , "\"async\":true" ) &&
4721+ app_wait_for_atomic_int (& fixture .fake .starts , 2 );
4722+ bool disabled = started && cbm_config_set (fixture .config , CBM_CONFIG_AUTO_WATCH , "false" ) == 0 ;
4723+ atomic_store (& fixture .fake .allow_completion , true);
4724+ bool drained = started && app_wait_for_active_jobs (fixture .application , 0 );
4725+ int watches_after = fixture .watcher ? cbm_watcher_watch_count (fixture .watcher ) : -1 ;
4726+ bool cleaned = app_watch_race_fixture_finish (& fixture );
4727+ free (request );
4728+ free (response );
4729+
4730+ ASSERT_TRUE (fixture_ready );
4731+ ASSERT_TRUE (target_ready );
4732+ ASSERT_TRUE (db_ready );
4733+ ASSERT_TRUE (indexed );
4734+ ASSERT_EQ (watches_before , 2 );
4735+ ASSERT_TRUE (started );
4736+ ASSERT_TRUE (disabled );
4737+ ASSERT_TRUE (drained );
4738+ ASSERT_EQ (watches_after , 0 );
4739+ ASSERT_TRUE (cleaned );
4740+ PASS ();
4741+ }
4742+
4743+ /* The callback can be the first observer of a config change, without a
4744+ * client request. It must drain every target before admitting refresh work. */
4745+ TEST (daemon_application_watcher_callback_disables_all_watches ) {
4746+ app_watch_race_fixture_t fixture ;
4747+ bool fixture_ready = app_watch_race_fixture_init_mcp (& fixture , 9322 , true);
4748+ char target [APP_TEST_PATH_CAP ];
4749+ (void )snprintf (target , sizeof (target ), "%s/cbm-app-disable-callback-XXXXXX" , fixture .root );
4750+ bool target_ready = fixture_ready && cbm_mkdtemp (target ) != NULL ;
4751+ char db_path [APP_TEST_PATH_CAP ];
4752+ (void )snprintf (db_path , sizeof (db_path ), "%s/callback-target.db" , fixture .cache );
4753+ bool db_ready = target_ready && app_test_create_empty_file (db_path );
4754+ atomic_store (& fixture .fake .allow_completion , true);
4755+ bool indexed = db_ready && app_test_explicit_index (& fixture .callbacks , fixture .session , target ,
4756+ "callback-target" );
4757+ int watches_before = fixture .watcher ? cbm_watcher_watch_count (fixture .watcher ) : -1 ;
4758+ bool disabled = indexed && cbm_config_set (fixture .config , CBM_CONFIG_AUTO_WATCH , "false" ) == 0 ;
4759+ int refreshed = disabled ? cbm_daemon_application_watcher_index ("callback-target" , target ,
4760+ fixture .application )
4761+ : -1 ;
4762+ int watches_after = fixture .watcher ? cbm_watcher_watch_count (fixture .watcher ) : -1 ;
4763+ int starts = atomic_load (& fixture .fake .starts );
4764+ bool cleaned = app_watch_race_fixture_finish (& fixture );
4765+
4766+ ASSERT_TRUE (fixture_ready );
4767+ ASSERT_TRUE (target_ready );
4768+ ASSERT_TRUE (db_ready );
4769+ ASSERT_TRUE (indexed );
4770+ ASSERT_EQ (watches_before , 2 );
4771+ ASSERT_TRUE (disabled );
4772+ ASSERT_EQ (refreshed , 1 );
4773+ ASSERT_EQ (watches_after , 0 );
4774+ ASSERT_EQ (starts , 1 );
4775+ ASSERT_TRUE (cleaned );
4776+ PASS ();
4777+ }
4778+
4779+ static int app_watch_preserves_doc_candidates (int explicit_value ) {
4780+ app_watch_race_fixture_t fixture ;
4781+ bool fixture_ready = app_watch_race_fixture_init_mcp (& fixture , 9321 , true);
4782+ char target [APP_TEST_PATH_CAP ];
4783+ (void )snprintf (target , sizeof (target ), "%s/cbm-app-watch-options-XXXXXX" , fixture .root );
4784+ bool target_ready = fixture_ready && cbm_mkdtemp (target ) != NULL ;
4785+ char db_path [APP_TEST_PATH_CAP ];
4786+ (void )snprintf (db_path , sizeof (db_path ), "%s/options-target.db" , fixture .cache );
4787+ bool db_ready = target_ready && app_test_create_empty_file (db_path );
4788+ /* The opposite global setting must be replaced, not duplicated in JSON. */
4789+ bool configured = db_ready && cbm_config_set (fixture .config , CBM_CONFIG_DOC_CANDIDATES ,
4790+ explicit_value < 0 ? "auto"
4791+ : explicit_value ? "false"
4792+ : "true" ) == 0 ;
4793+ atomic_store (& fixture .fake .allow_completion , true);
4794+ bool indexed =
4795+ configured &&
4796+ app_test_explicit_index_with_options (
4797+ & fixture .callbacks , fixture .session , target , "options-target" ,
4798+ explicit_value < 0 ? ",\"mode\":\"fast\",\"persistence\":true"
4799+ : explicit_value ? ",\"mode\":\"fast\",\"persistence\":true,\"doc_candidates\":true"
4800+ : ",\"mode\":\"fast\",\"persistence\":true,\"doc_candidates\":false" );
4801+ bool default_updated =
4802+ explicit_value >= 0 ||
4803+ (indexed && cbm_config_set (fixture .config , CBM_CONFIG_DOC_CANDIDATES , "true" ) == 0 );
4804+ int refreshed =
4805+ indexed && default_updated
4806+ ? cbm_daemon_application_watcher_index ("options-target" , target , fixture .application )
4807+ : -1 ;
4808+ const char * args = fixture .fake .args_json [1 ];
4809+ yyjson_doc * doc = refreshed == 0 ? yyjson_read (args , strlen (args ), 0 ) : NULL ;
4810+ yyjson_val * root = doc ? yyjson_doc_get_root (doc ) : NULL ;
4811+ yyjson_val * choice = yyjson_obj_get (root , "doc_candidates" );
4812+ int forwarded = !choice ? -1 : !yyjson_is_bool (choice ) ? -2 : yyjson_get_bool (choice ) ? 1 : 0 ;
4813+ bool mode_preserved = yyjson_equals_str (yyjson_obj_get (root , "mode" ), "fast" );
4814+ bool persistence_preserved = yyjson_get_bool (yyjson_obj_get (root , "persistence" ));
4815+ size_t choices = 0 ;
4816+ size_t idx , max ;
4817+ yyjson_val * key , * value ;
4818+ yyjson_obj_foreach (root , idx , max , key , value ) {
4819+ if (yyjson_equals_str (key , "doc_candidates" )) {
4820+ choices ++ ;
4821+ }
4822+ }
4823+ yyjson_doc_free (doc );
4824+ bool cleaned = app_watch_race_fixture_finish (& fixture );
4825+ ASSERT_TRUE (fixture_ready );
4826+ ASSERT_TRUE (target_ready );
4827+ ASSERT_TRUE (db_ready );
4828+ ASSERT_TRUE (configured );
4829+ ASSERT_TRUE (indexed );
4830+ ASSERT_TRUE (default_updated );
4831+ ASSERT_EQ (refreshed , 0 );
4832+ ASSERT_TRUE (cleaned );
4833+ ASSERT_TRUE (mode_preserved );
4834+ ASSERT_TRUE (persistence_preserved );
4835+ ASSERT_EQ (choices , 1 );
4836+ ASSERT_EQ (forwarded , explicit_value != 0 );
4837+ PASS ();
4838+ }
4839+
4840+ TEST (daemon_application_watch_refresh_uses_current_doc_candidates_default ) {
4841+ return app_watch_preserves_doc_candidates (-1 );
4842+ }
4843+
4844+ TEST (daemon_application_watch_refresh_preserves_doc_candidates_true ) {
4845+ return app_watch_preserves_doc_candidates (true);
4846+ }
4847+
4848+ TEST (daemon_application_watch_refresh_preserves_doc_candidates_false ) {
4849+ return app_watch_preserves_doc_candidates (false);
4850+ }
4851+
46764852TEST (daemon_application_explicit_index_registers_target_watch_for_live_session ) {
46774853 app_watch_race_fixture_t fixture ;
46784854 bool fixture_ready = app_watch_race_fixture_init_mcp (& fixture , 9301 , true);
@@ -7467,6 +7643,12 @@ SUITE(daemon_application) {
74677643 RUN_TEST (daemon_application_final_cancel_drains_admitted_watcher_job );
74687644 RUN_TEST (daemon_application_watcher_job_follows_exact_live_watch_owners );
74697645 RUN_TEST (daemon_application_late_watcher_session_owns_active_watcher_job );
7646+ RUN_TEST (daemon_application_watch_roots_match_windows_separators_only );
7647+ RUN_TEST (daemon_application_async_completion_disables_all_watches );
7648+ RUN_TEST (daemon_application_watcher_callback_disables_all_watches );
7649+ RUN_TEST (daemon_application_watch_refresh_uses_current_doc_candidates_default );
7650+ RUN_TEST (daemon_application_watch_refresh_preserves_doc_candidates_true );
7651+ RUN_TEST (daemon_application_watch_refresh_preserves_doc_candidates_false );
74707652 RUN_TEST (daemon_application_explicit_index_registers_target_watch_for_live_session );
74717653 RUN_TEST (daemon_application_failed_explicit_index_does_not_register_target_watch );
74727654 RUN_TEST (daemon_application_auto_watch_false_suppresses_explicit_target_watch );
0 commit comments