Skip to content

Commit 7095afc

Browse files
committed
refactor(controller): separate storeInitialFanState and storeCurrentFanState
Update storeCurrentFanState to unconditionally capture and overwrite the current fan state snapshot. Introduce storeInitialFanState to preserve the initial snapshot on subsequent calls, and clearInitialFanState to reset it. Add unit tests for storeInitialFanState, storeCurrentFanState, and clearInitialFanState.
1 parent 00e9a4a commit 7095afc

2 files changed

Lines changed: 83 additions & 6 deletions

File tree

internal/controller/controller.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,9 @@ func (f *DefaultFanController) prepareController() (err error) {
188188
return err
189189
}
190190

191+
// storeCurrentFanState captures the current fan control mode and PWM value,
192+
// overwriting any previously saved snapshot in originalFanState.
191193
func (f *DefaultFanController) storeCurrentFanState() error {
192-
if f.originalFanState != nil {
193-
return nil
194-
}
195-
196194
fan := f.fan
197195
// store original pwm value
198196
pwm, err := f.getPwm()
@@ -215,6 +213,20 @@ func (f *DefaultFanController) storeCurrentFanState() error {
215213
return nil
216214
}
217215

216+
// storeInitialFanState stores the initial fan state snapshot if it hasn't been captured yet.
217+
// Subsequent calls are no-ops to preserve the state before any controller actions.
218+
func (f *DefaultFanController) storeInitialFanState() error {
219+
if f.originalFanState != nil {
220+
return nil
221+
}
222+
return f.storeCurrentFanState()
223+
}
224+
225+
// clearInitialFanState resets the captured initial fan state snapshot.
226+
func (f *DefaultFanController) clearInitialFanState() {
227+
f.originalFanState = nil
228+
}
229+
218230
func (f *DefaultFanController) Run(ctx context.Context) error {
219231
// prepare the controller by initializing persistence and checking the fan
220232
err := f.prepareController()
@@ -223,7 +235,7 @@ func (f *DefaultFanController) Run(ctx context.Context) error {
223235
}
224236

225237
// store the current fan state to restore it when stopping the controller
226-
err = f.storeCurrentFanState()
238+
err = f.storeInitialFanState()
227239
if err != nil {
228240
return err
229241
}
@@ -372,7 +384,7 @@ func (f *DefaultFanController) runInitializationIfNeeded(ctx context.Context) (m
372384
}
373385

374386
func (f *DefaultFanController) RunInitialization(ctx context.Context) (map[int]float64, error) {
375-
err := f.storeCurrentFanState()
387+
err := f.storeInitialFanState()
376388
if err != nil {
377389
return nil, err
378390
}

internal/controller/controller_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,3 +2350,68 @@ func TestGetPwmSetDelay_FallsBackToGlobal(t *testing.T) {
23502350
// THEN
23512351
assert.Equal(t, globalDelay, result)
23522352
}
2353+
2354+
func TestStoreInitialFanState_OnlyCapturesOnce(t *testing.T) {
2355+
fan := &mockFanForRestore{
2356+
MockFan: MockFan{
2357+
ID: "fan",
2358+
PWM: 100,
2359+
ControlMode: fans.ControlModePWM,
2360+
},
2361+
supportsControlMode: true,
2362+
}
2363+
controller := DefaultFanController{
2364+
fan: fan,
2365+
}
2366+
2367+
err := controller.storeInitialFanState()
2368+
assert.NoError(t, err)
2369+
assert.NotNil(t, controller.originalFanState)
2370+
assert.Equal(t, 100, controller.originalFanState.PwmValue)
2371+
2372+
// Change fan PWM to 200
2373+
fan.PWM = 200
2374+
2375+
// Subsequent storeInitialFanState call should not overwrite existing snapshot
2376+
err = controller.storeInitialFanState()
2377+
assert.NoError(t, err)
2378+
assert.Equal(t, 100, controller.originalFanState.PwmValue)
2379+
}
2380+
2381+
func TestStoreCurrentFanState_OverwritesSnapshot(t *testing.T) {
2382+
fan := &mockFanForRestore{
2383+
MockFan: MockFan{
2384+
ID: "fan",
2385+
PWM: 100,
2386+
ControlMode: fans.ControlModePWM,
2387+
},
2388+
supportsControlMode: true,
2389+
}
2390+
controller := DefaultFanController{
2391+
fan: fan,
2392+
}
2393+
2394+
err := controller.storeInitialFanState()
2395+
assert.NoError(t, err)
2396+
assert.Equal(t, 100, controller.originalFanState.PwmValue)
2397+
2398+
// Change fan PWM to 200
2399+
fan.PWM = 200
2400+
2401+
// storeCurrentFanState should overwrite snapshot with new value
2402+
err = controller.storeCurrentFanState()
2403+
assert.NoError(t, err)
2404+
assert.Equal(t, 200, controller.originalFanState.PwmValue)
2405+
}
2406+
2407+
func TestClearInitialFanState_ResetsSnapshot(t *testing.T) {
2408+
controller := DefaultFanController{
2409+
originalFanState: &FanStateSnapshot{
2410+
PwmValue: 100,
2411+
ControlMode: fans.ControlModePWM,
2412+
},
2413+
}
2414+
2415+
controller.clearInitialFanState()
2416+
assert.Nil(t, controller.originalFanState)
2417+
}

0 commit comments

Comments
 (0)