ai cooking
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

put dedupe elsewhere (#532)

Co-authored-by: paul miller <paul.miller>

authored by

Paul Miller
paul miller
and committed by
GitHub
c950dc68 9218802b

+104 -14
+2 -6
internal/recipes/server_test.go
··· 632 632 } 633 633 634 634 captured := generator.LastParams() 635 - if captured == nil { 636 - t.Fatal("expected captured params") 637 - } 635 + require.NotNil(t, captured) 638 636 if got, want := captured.LastRecipes, []string{"Cooked Recently"}; !slices.Equal(got, want) { 639 637 t.Fatalf("expected only recently cooked recipes in avoid list, got %v", got) 640 638 } ··· 1610 1608 } 1611 1609 1612 1610 captured := generator.LastParams() 1613 - if captured == nil { 1614 - t.Fatal("expected captured params") 1615 - } 1611 + require.NotNil(t, captured) 1616 1612 if got, want := captured.PriorSavedHashes, []string{alreadySaved.ComputeHash()}; !slices.Equal(got, want) { 1617 1613 t.Fatalf("expected prior saved hashes %v, got %v", want, got) 1618 1614 }
+19 -8
internal/recipes/staples.go
··· 35 35 staplesProvider 36 36 } 37 37 38 - // sends to the right backend but also dedupes product ids and errors on empty ones. 39 38 type routingStaplesProvider struct { 40 39 backends []backendStaplesProvider 40 + } 41 + 42 + type dedupingStaplesProvider struct { 43 + provider staplesProvider 41 44 } 42 45 43 46 func NewStaplesProvider(cfg *config.Config) (staplesProvider, error) { ··· 46 49 return nil, err 47 50 } 48 51 49 - return routingStaplesProvider{ 52 + return dedupingStaplesProvider{provider: routingStaplesProvider{ 50 53 backends: backends, 51 - }, nil 54 + }}, nil 52 55 } 53 56 54 57 func (p routingStaplesProvider) FetchStaples(ctx context.Context, locationID string) ([]ai.InputIngredient, error) { ··· 56 59 if err != nil { 57 60 return nil, err 58 61 } 59 - ingredients, err := provider.FetchStaples(ctx, locationID) 62 + return provider.FetchStaples(ctx, locationID) 63 + } 64 + 65 + func (p routingStaplesProvider) GetIngredients(ctx context.Context, locationID string, searchTerm string, skip int) ([]ai.InputIngredient, error) { 66 + provider, err := p.providerForLocation(locationID) 60 67 if err != nil { 61 68 return nil, err 62 69 } 63 - return dedupeInputIngredients(ingredients) 70 + return provider.GetIngredients(ctx, locationID, searchTerm, skip) 64 71 } 65 72 66 - func (p routingStaplesProvider) GetIngredients(ctx context.Context, locationID string, searchTerm string, skip int) ([]ai.InputIngredient, error) { 67 - provider, err := p.providerForLocation(locationID) 73 + func (p dedupingStaplesProvider) FetchStaples(ctx context.Context, locationID string) ([]ai.InputIngredient, error) { 74 + ingredients, err := p.provider.FetchStaples(ctx, locationID) 68 75 if err != nil { 69 76 return nil, err 70 77 } 71 - ingredients, err := provider.GetIngredients(ctx, locationID, searchTerm, skip) 78 + return dedupeInputIngredients(ingredients) 79 + } 80 + 81 + func (p dedupingStaplesProvider) GetIngredients(ctx context.Context, locationID string, searchTerm string, skip int) ([]ai.InputIngredient, error) { 82 + ingredients, err := p.provider.GetIngredients(ctx, locationID, searchTerm, skip) 72 83 if err != nil { 73 84 return nil, err 74 85 }
+83
internal/recipes/staples_test.go
··· 148 148 } 149 149 } 150 150 151 + func TestRoutingStaplesProvider_DoesNotDedupeIngredients(t *testing.T) { 152 + backend := &stubStaplesProvider{ 153 + ids: map[string]bool{"70100023": true}, 154 + ingredients: []ai.InputIngredient{ 155 + {ProductID: "apple-1", Description: "Honeycrisp Apple"}, 156 + {ProductID: "apple-1", Description: "Honeycrisp Apple"}, 157 + }, 158 + } 159 + provider := routingStaplesProvider{ 160 + backends: []backendStaplesProvider{backend}, 161 + } 162 + 163 + got, err := provider.FetchStaples(t.Context(), "70100023") 164 + if err != nil { 165 + t.Fatalf("FetchStaples returned error: %v", err) 166 + } 167 + if len(got) != 2 { 168 + t.Fatalf("expected routing provider to preserve backend ingredients, got %+v", got) 169 + } 170 + } 171 + 172 + func TestDedupingStaplesProvider_FetchStaplesDedupesProductIDs(t *testing.T) { 173 + provider := dedupingStaplesProvider{ 174 + provider: &stubRoutingStaplesProvider{ 175 + ingredients: []ai.InputIngredient{ 176 + {ProductID: "apple-1", Description: "Honeycrisp Apple"}, 177 + {ProductID: "apple-1", Description: "Honeycrisp Apple"}, 178 + {ProductID: "spinach-1", Description: "Baby Spinach"}, 179 + }, 180 + }, 181 + } 182 + 183 + got, err := provider.FetchStaples(t.Context(), "70100023") 184 + if err != nil { 185 + t.Fatalf("FetchStaples returned error: %v", err) 186 + } 187 + if len(got) != 2 { 188 + t.Fatalf("expected deduped ingredients, got %+v", got) 189 + } 190 + if got[0].ProductID != "apple-1" || got[1].ProductID != "spinach-1" { 191 + t.Fatalf("expected first occurrence of each product id in order, got %+v", got) 192 + } 193 + } 194 + 195 + func TestDedupingStaplesProvider_GetIngredientsDedupesProductIDs(t *testing.T) { 196 + provider := dedupingStaplesProvider{ 197 + provider: &stubRoutingStaplesProvider{ 198 + ingredients: []ai.InputIngredient{ 199 + {ProductID: "wine-1", Description: "Pinot Noir"}, 200 + {ProductID: "wine-1", Description: "Pinot Noir"}, 201 + {ProductID: "wine-2", Description: "Cabernet Sauvignon"}, 202 + }, 203 + }, 204 + } 205 + 206 + got, err := provider.GetIngredients(t.Context(), "70100023", "pinot noir", 0) 207 + if err != nil { 208 + t.Fatalf("GetIngredients returned error: %v", err) 209 + } 210 + if len(got) != 2 { 211 + t.Fatalf("expected deduped ingredients, got %+v", got) 212 + } 213 + if got[0].ProductID != "wine-1" || got[1].ProductID != "wine-2" { 214 + t.Fatalf("expected first occurrence of each product id in order, got %+v", got) 215 + } 216 + } 217 + 218 + func TestDedupingStaplesProvider_RejectsBlankProductID(t *testing.T) { 219 + provider := dedupingStaplesProvider{ 220 + provider: &stubRoutingStaplesProvider{ 221 + ingredients: []ai.InputIngredient{{Description: "Mystery Ingredient"}}, 222 + }, 223 + } 224 + 225 + _, err := provider.FetchStaples(t.Context(), "70100023") 226 + if err == nil { 227 + t.Fatal("expected blank product id error") 228 + } 229 + if !strings.Contains(err.Error(), "blank product id for ingredient") { 230 + t.Fatalf("unexpected error: %v", err) 231 + } 232 + } 233 + 151 234 func TestStaplesSignatureForLocation_UsesAlbertsonsIdentityProvider(t *testing.T) { 152 235 t.Parallel() 153 236