@@ -133,6 +133,77 @@ public void ClearingManagedSettingClearsLegacyAndSidecar()
133133 Assert . Empty ( service . LoadStoredRegexes ( ) ) ;
134134 }
135135
136+ [ Fact ]
137+ public void Constructor_FileBackedModeReflectsSettingsValueSetBeforeConstruction ( )
138+ {
139+ // EnableFileBackedManagedSettings must be read AFTER any migration so the
140+ // persisted user preference is honoured for the current session.
141+ Settings settings = new ( )
142+ {
143+ FirstRun = false ,
144+ EnableFileBackedManagedSettings = true
145+ } ;
146+
147+ SettingsService service = CreateService ( settings ) ;
148+
149+ Assert . True ( service . IsFileBackedManagedSettingsEnabled ) ;
150+ }
151+
152+ [ Fact ]
153+ public void Constructor_FileBackedModeDefaultsToFalseWhenNotSet ( )
154+ {
155+ Settings settings = new ( )
156+ {
157+ FirstRun = false ,
158+ EnableFileBackedManagedSettings = false
159+ } ;
160+
161+ SettingsService service = CreateService ( settings ) ;
162+
163+ Assert . False ( service . IsFileBackedManagedSettingsEnabled ) ;
164+ }
165+
166+ [ Fact ]
167+ public void Constructor_UnpackagedUpgradePathDoesNotThrowWhenNoPreviousVersion ( )
168+ {
169+ // When saveClassicSettingsChanges is false (test mode) the Upgrade() code path is
170+ // skipped, so this simply verifies that the constructor completes successfully when
171+ // FirstRun is true and localSettings is null.
172+ Settings settings = new ( )
173+ {
174+ FirstRun = true ,
175+ EnableFileBackedManagedSettings = false
176+ } ;
177+
178+ SettingsService service = CreateService ( settings ) ;
179+
180+ // Service initialises without throwing; FileBackedMode reflects the setting.
181+ Assert . False ( service . IsFileBackedManagedSettingsEnabled ) ;
182+ }
183+
184+ [ Fact ]
185+ public void LoadStoredRegexes_SidecarSurvivesSimulatedPackageUpgrade ( )
186+ {
187+ // Simulate a package upgrade: sidecar file already exists (from the previous
188+ // version) but ClassicSettings.RegexList is empty (reset by the upgrade).
189+ // The service must load from the sidecar and backfill ClassicSettings.
190+ Settings settings = new ( )
191+ {
192+ FirstRun = false ,
193+ EnableFileBackedManagedSettings = false ,
194+ RegexList = string . Empty
195+ } ;
196+ string regexFilePath = Path . Combine ( _tempFolder , "RegexList.json" ) ;
197+ File . WriteAllText ( regexFilePath , SerializeRegexes ( "survived-upgrade" ) ) ;
198+
199+ SettingsService service = CreateService ( settings ) ;
200+
201+ StoredRegex loaded = Assert . Single ( service . LoadStoredRegexes ( ) ) ;
202+ Assert . Equal ( "survived-upgrade" , loaded . Id ) ;
203+ // Verify backfill into ClassicSettings so the next migration has something to copy.
204+ Assert . Contains ( "survived-upgrade" , settings . RegexList ) ;
205+ }
206+
136207 private SettingsService CreateService ( Settings settings ) =>
137208 new (
138209 settings ,
0 commit comments