package creds import ( "context" "errors" "io/fs" "slices" "testing" ) // Task 9 follow-up, round 3, #1: a watch settled at a restart is // acknowledged (Keychain.Acked) only once its record is durably checked // or removed, so the proof of its end it used may go. One whose end is // not proven, or whose settling could not list the Keychain, is not. func TestSettledWatchAcknowledgedOnceDurable(t *testing.T) { ctx := context.Background() f := &fakeKeychain{} k := newWatch(t, f) _ = k.Before(ctx, "signin-1", configDir) f.add(own()) restarted := &Keychain{Dir: k.Dir, Login: login, User: "paulequipo", List: f.list, Delete: f.delete} var acked []string restarted.Acked = func(w Watch) { rec, err := restarted.load(w.Marker) if !errors.Is(err, fs.ErrNotExist) && (err != nil || !rec.Checked) { t.Errorf("acknowledged %s before its record was settled: %+v %v", w.Marker, rec, err) } acked = append(acked, w.Marker) } _ = restarted.Settle(ctx, true, func(Watch) bool { return false }) f.broken = true _ = restarted.Settle(ctx, true, func(Watch) bool { return true }) f.broken = false // The second look lists, but the recheck's listing fails: the record // is never saved as checked. n := 0 restarted.List = func(ctx context.Context, kc string) ([]Item, error) { if n++; n == 2 { return nil, errors.New("listing failed") } return f.list(ctx, kc) } _ = restarted.Settle(ctx, true, func(Watch) bool { return true }) restarted.List = f.list if len(acked) != 0 { t.Fatalf("acknowledged without a settled record: %v", acked) } if err := restarted.Settle(ctx, true, func(Watch) bool { return true }); err != nil || !slices.Equal(acked, []string{"signin-1"}) { t.Fatalf("settled watch: err %v, acknowledged %v", err, acked) } }