@@ -247,6 +247,70 @@ func TestCheckPresetDrift_OperatorEditedTemplates_NoDrift(t *testing.T) {
247247 }
248248}
249249
250+ // TestCheckPresetDrift_MalformedLock_ErrorsGracefully pins the failure mode
251+ // for a corrupted .talm-preset.lock: unparseable YAML must surface as an
252+ // error with drift=false — never a panic, never a spurious drift verdict.
253+ // Callers (evaluatePresetDrift → decideDrift) downgrade the error to a
254+ // non-fatal warning, so a mangled lock never blocks a command.
255+ func TestCheckPresetDrift_MalformedLock_ErrorsGracefully (t * testing.T ) {
256+ root := t .TempDir ()
257+ if err := os .WriteFile (filepath .Join (root , ".talm-preset.lock" ), []byte ("preset: [unclosed\n " ), 0o644 ); err != nil {
258+ t .Fatalf ("write lock: %v" , err )
259+ }
260+
261+ drift , _ , err := CheckPresetDrift (root , "0.30.0" )
262+ if err == nil {
263+ t .Error ("expected an error for an unparseable lock, got nil" )
264+ }
265+
266+ if drift {
267+ t .Error ("must not report drift when the lock cannot be parsed" )
268+ }
269+ }
270+
271+ // TestCheckPresetDrift_LockMissingFields_ErrorsGracefully pins that a lock
272+ // which parses as YAML but lacks preset or presetHash (hand-edited, or
273+ // truncated by a bad merge) is an error with drift=false. Treating it as
274+ // "no drift" would silently disable the check; inventing drift would nag on
275+ // a baseline that was never pinned.
276+ func TestCheckPresetDrift_LockMissingFields_ErrorsGracefully (t * testing.T ) {
277+ root := t .TempDir ()
278+ if err := os .WriteFile (filepath .Join (root , ".talm-preset.lock" ), []byte ("preset: cozystack\n " ), 0o644 ); err != nil {
279+ t .Fatalf ("write lock: %v" , err )
280+ }
281+
282+ drift , _ , err := CheckPresetDrift (root , "0.30.0" )
283+ if err == nil {
284+ t .Error ("expected an error for a lock without presetHash, got nil" )
285+ }
286+
287+ if drift {
288+ t .Error ("must not report drift for a lock with no pinned baseline" )
289+ }
290+ }
291+
292+ // TestCheckPresetDrift_LockUnknownPreset_ErrorsGracefully pins the
293+ // cross-binary shape: a well-formed lock naming a preset this binary does
294+ // not ship (pinned by a different talm build) is an error with drift=false,
295+ // so the caller's warning names the unknown preset instead of fabricating a
296+ // drift verdict against a baseline that cannot be recomputed.
297+ func TestCheckPresetDrift_LockUnknownPreset_ErrorsGracefully (t * testing.T ) {
298+ root := t .TempDir ()
299+ lock := "preset: does-not-exist\n presetHash: 0000000000000000000000000000000000000000000000000000000000000000\n "
300+ if err := os .WriteFile (filepath .Join (root , ".talm-preset.lock" ), []byte (lock ), 0o644 ); err != nil {
301+ t .Fatalf ("write lock: %v" , err )
302+ }
303+
304+ drift , _ , err := CheckPresetDrift (root , "0.30.0" )
305+ if err == nil {
306+ t .Error ("expected an error for a lock naming an unknown preset, got nil" )
307+ }
308+
309+ if drift {
310+ t .Error ("must not report drift when the baseline preset is not embedded" )
311+ }
312+ }
313+
250314// TestWritePresetLock_UnknownPreset_Errors pins that pinning a preset the
251315// binary does not ship fails loudly rather than writing an empty baseline.
252316func TestWritePresetLock_UnknownPreset_Errors (t * testing.T ) {
0 commit comments