@shopbite-de/storefront 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/.env.example +13 -1
  2. package/CLAUDE.md +5 -3
  3. package/app/components/Home/Categories.vue +2 -2
  4. package/app/components/Navigation/MobileTop.vue +1 -1
  5. package/app/composables/useAddressAutocomplete.ts +3 -11
  6. package/app/composables/useCheckoutMethodGuard.ts +1 -1
  7. package/app/composables/useShippingMethodChoice.ts +2 -2
  8. package/app/composables/useTrackEvent.ts +4 -3
  9. package/app/pages/account/recover/password/index.vue +10 -0
  10. package/app/pages/registrierung/bestaetigen.vue +4 -0
  11. package/app/utils/openingHours.ts +1 -1
  12. package/app/utils/schema.ts +2 -2
  13. package/app/utils/trackingUrl.ts +39 -0
  14. package/content/index.yml +16 -16
  15. package/content/zahlung-und-versand.md +3 -3
  16. package/docs/notes/2026-07-04-issue-239-hydration-mismatches.md +1 -1
  17. package/docs/notes/2026-09-08-issue-244-category-url-trailing-slash.md +2 -2
  18. package/docs/notes/2026-09-10-issue-251-shop-data-in-base-layer.md +4 -4
  19. package/docs/notes/2026-09-10-issue-272-restaurant-schema.md +3 -3
  20. package/docs/notes/2026-09-10-issue-273-performance-media.md +4 -4
  21. package/docs/notes/2026-09-10-issue-274-seo-basics.md +6 -6
  22. package/docs/notes/2026-09-10-issue-275-accessibility.md +3 -3
  23. package/docs/notes/2026-09-10-issue-290-footer-nap.md +5 -5
  24. package/docs/notes/2026-09-10-issue-294-matomo-without-config.md +1 -1
  25. package/docs/notes/2026-09-10-issue-296-track-direct-search.md +1 -1
  26. package/docs/notes/2026-09-10-issue-312-store-api-criteria-post.md +3 -3
  27. package/docs/notes/2026-09-10-issue-313-wishlist-scoped-tailwind.md +1 -1
  28. package/docs/notes/2026-09-10-issue-314-mobile-js-cost.md +1 -1
  29. package/docs/notes/2026-09-12-issue-319-stylesheet-cost.md +1 -1
  30. package/docs/notes/2026-09-12-issue-325-product-card.md +2 -2
  31. package/docs/notes/2026-09-12-issue-327-home-hero.md +2 -2
  32. package/docs/notes/2026-09-13-issue-332-home-info-cards.md +1 -1
  33. package/docs/notes/2026-09-13-quick-view-flicker.md +1 -1
  34. package/docs/notes/2026-09-14-demo-noindex.md +2 -2
  35. package/docs/notes/2026-09-17-quick-view-compact-options.md +1 -1
  36. package/docs/notes/2026-09-21-issue-273-cache-and-demo-video.md +1 -1
  37. package/docs/notes/2026-09-21-issue-363-non-fatal-404.md +2 -2
  38. package/docs/notes/2026-09-21-issue-366-wishlist-404.md +1 -1
  39. package/docs/notes/2026-09-21-issue-378-dev-modules.md +1 -1
  40. package/docs/notes/2026-10-05-issue-289-product-deep-links.md +1 -1
  41. package/docs/notes/2026-10-05-issue-400-menu-item-ingredients-diet.md +1 -1
  42. package/docs/notes/2026-10-05-issue-401-restaurant-schema-ordering.md +1 -1
  43. package/docs/notes/2026-10-05-issue-402-llms-txt.md +1 -1
  44. package/docs/notes/2026-10-05-issue-403-ai-crawlers.md +1 -1
  45. package/docs/notes/2026-10-05-issue-404-faq.md +1 -1
  46. package/docs/notes/2026-10-06-old-seo-url-redirect.md +5 -5
  47. package/docs/notes/2026-10-07-issue-434-property-group-projection.md +1 -1
  48. package/nuxt.config.ts +56 -8
  49. package/package.json +2 -1
  50. package/server/api/address/autocomplete.get.ts +79 -14
  51. package/server/api/listing/[categoryId].get.ts +5 -28
  52. package/server/api/search.get.ts +4 -18
  53. package/server/plugins/bounded-cache.ts +28 -0
  54. package/server/plugins/security-headers.ts +54 -0
  55. package/server/routes/llms.txt.ts +1 -1
  56. package/server/utils/addressAutocomplete.ts +181 -0
  57. package/server/utils/listingParams.ts +117 -0
  58. package/server/utils/securityHeaders.ts +165 -0
  59. package/test/layer/nuxt.config.ts +2 -2
  60. package/test/nuxt/AccountPagesPreset.test.ts +10 -10
  61. package/test/nuxt/AuthPagesPreset.test.ts +3 -0
  62. package/test/nuxt/CheckoutFormsPreset.test.ts +5 -5
  63. package/test/nuxt/CheckoutSummary.test.ts +1 -1
  64. package/test/nuxt/HeaderFooterPreset.test.ts +10 -10
  65. package/test/nuxt/HomeFaq.test.ts +1 -1
  66. package/test/nuxt/NavigationMobileTop.test.ts +1 -1
  67. package/test/nuxt/OrderPagesPreset.test.ts +2 -2
  68. package/test/nuxt/RegisterConfirmPage.test.ts +12 -0
  69. package/test/nuxt/useAddressAutocomplete.test.ts +7 -22
  70. package/test/nuxt/useCheckoutMethodGuard.test.ts +1 -1
  71. package/test/nuxt/useShippingMethodChoice.test.ts +1 -1
  72. package/test/nuxt/useTrackEvent.test.ts +16 -0
  73. package/test/unit/addressAutocomplete.spec.ts +247 -0
  74. package/test/unit/boundedCache.spec.ts +231 -0
  75. package/test/unit/highlight.spec.ts +2 -2
  76. package/test/unit/llmsTxt.spec.ts +28 -28
  77. package/test/unit/openingHours.spec.ts +3 -3
  78. package/test/unit/schema.spec.ts +29 -32
  79. package/test/unit/securityHeaders.spec.ts +254 -0
  80. package/test/unit/seo.spec.ts +23 -29
  81. package/test/unit/sitemap.spec.ts +1 -1
  82. package/test/unit/trackingUrl.spec.ts +48 -0
@@ -69,10 +69,10 @@ describe("registration form with a preset (#443)", () => {
69
69
  await wrapper.get("#firstName").setValue("Giulia");
70
70
  await wrapper.get("#lastName").setValue("Rossi");
71
71
  await wrapper.get("#email").setValue("giulia@example.com");
72
- await wrapper.get("#billingAddress-street").setValue("Kantstraße 12");
73
- await wrapper.get("#billingAddress-zipcode").setValue("63179");
74
- await wrapper.get("#billingAddress-city").setValue("Obertshausen");
75
- await wrapper.get("#billingAddress-phoneNumber").setValue("06104 12345");
72
+ await wrapper.get("#billingAddress-street").setValue("Musterstraße 12");
73
+ await wrapper.get("#billingAddress-zipcode").setValue("12345");
74
+ await wrapper.get("#billingAddress-city").setValue("Musterstadt");
75
+ await wrapper.get("#billingAddress-phoneNumber").setValue("0123 4567890");
76
76
  await wrapper.get("#acceptedDataProtection").trigger("click");
77
77
  await wrapper.find("form").trigger("submit");
78
78
  await flushPromises();
@@ -84,7 +84,7 @@ describe("registration form with a preset (#443)", () => {
84
84
  email: "giulia@example.com",
85
85
  billingAddress: expect.objectContaining({
86
86
  firstName: "Giulia",
87
- street: "Kantstraße 12",
87
+ street: "Musterstraße 12",
88
88
  }),
89
89
  }),
90
90
  );
@@ -134,7 +134,7 @@ describe("Checkout Summary order button", () => {
134
134
  expect(button.attributes("disabled")).toBeUndefined();
135
135
  });
136
136
 
137
- // La Fattoria: "Lieferung" needs a shipping city in the delivery area
137
+ // e.g. "Lieferung" needs a shipping city in the delivery area
138
138
  it("says delivery is not possible once the address is known", async () => {
139
139
  state.isLoading = false;
140
140
  state.validTime = true;
@@ -73,12 +73,12 @@ describe("header and footer with a preset (#455)", () => {
73
73
  state.wishlistCount = 0;
74
74
  state.loggedIn = false;
75
75
  Object.assign(useRuntimeConfig().public.site, {
76
- name: "La Fattoria",
77
- telephone: "06104 71427",
76
+ name: "Esempio",
77
+ telephone: "0123 456789",
78
78
  address: {
79
- street: "Kantstraße 6",
80
- postalCode: "63179",
81
- city: "Obertshausen",
79
+ street: "Musterstraße 1",
80
+ postalCode: "12345",
81
+ city: "Musterstadt",
82
82
  country: "DE",
83
83
  },
84
84
  googleBusinessProfileUrl: "",
@@ -88,11 +88,11 @@ describe("header and footer with a preset (#455)", () => {
88
88
  it("names the logo link and the icon-only controls", async () => {
89
89
  const wrapper = await mountSuspended(Header);
90
90
  expect(
91
- wrapper.find('a[aria-label="La Fattoria, zur Startseite"]').exists(),
91
+ wrapper.find('a[aria-label="Esempio, zur Startseite"]').exists(),
92
92
  ).toBe(true);
93
93
  expect(
94
- wrapper.find('a[href="tel:0610471427"]').attributes("aria-label"),
95
- ).toBe("Anrufen: 06104 71427");
94
+ wrapper.find('a[href="tel:0123456789"]').attributes("aria-label"),
95
+ ).toBe("Anrufen: 0123 456789");
96
96
  expect(wrapper.find('a[href="/anmelden"]').attributes("aria-label")).toBe(
97
97
  "Anmelden",
98
98
  );
@@ -128,8 +128,8 @@ describe("header and footer with a preset (#455)", () => {
128
128
 
129
129
  it("shows contact data and the footer columns without a colour switch", async () => {
130
130
  const wrapper = await mountSuspended(Footer);
131
- expect(wrapper.find("address").text()).toContain("Kantstraße 6");
132
- expect(wrapper.find("address").text()).toContain("63179 Obertshausen");
131
+ expect(wrapper.find("address").text()).toContain("Musterstraße 1");
132
+ expect(wrapper.find("address").text()).toContain("12345 Musterstadt");
133
133
  const navs = wrapper.findAll("nav");
134
134
  expect(navs.map((nav) => nav.attributes("aria-label"))).toEqual([
135
135
  "Rechtliches",
@@ -3,7 +3,7 @@ import { mountSuspended } from "@nuxt/test-utils/runtime";
3
3
  import HomeFaq from "~/components/Home/Faq.vue";
4
4
 
5
5
  const items = [
6
- { question: "Wohin liefern Sie?", answer: "Nach Obertshausen." },
6
+ { question: "Wohin liefern Sie?", answer: "Nach Musterstadt." },
7
7
  { question: "Wie kann ich bezahlen?", answer: "Bar oder mit EC-Karte." },
8
8
  ];
9
9
 
@@ -4,7 +4,7 @@ import type { VueWrapper } from "@vue/test-utils";
4
4
  import { ref } from "vue";
5
5
  import MobileTop from "~/components/Navigation/MobileTop.vue";
6
6
 
7
- // La Fattoria's menu: Fleischgerichte has sub-categories, Pizza has none.
7
+ // A menu where Fleischgerichte has sub-categories, Pizza has none.
8
8
  vi.mock("~/composables/useNavigation", () => ({
9
9
  useNavigation: () => ({
10
10
  menuCardNavigation: ref([
@@ -62,7 +62,7 @@ describe("order pages with a preset (#445)", () => {
62
62
  vi.clearAllMocks();
63
63
  order.value = undefined;
64
64
  Object.assign(useRuntimeConfig().public.site, {
65
- telephone: "06104 71427",
65
+ telephone: "0123 456789",
66
66
  });
67
67
  });
68
68
 
@@ -95,7 +95,7 @@ describe("order pages with a preset (#445)", () => {
95
95
 
96
96
  const call = wrapper.find('a[href^="tel:"]');
97
97
  expect(call.text()).toContain("Anrufen:");
98
- expect(call.text()).toContain("06104 71427");
98
+ expect(call.text()).toContain("0123 456789");
99
99
  });
100
100
 
101
101
  it("titles the order page with the order number", async () => {
@@ -63,6 +63,18 @@ describe("registrierung/bestaetigen", () => {
63
63
  expect(navigateToMock).toHaveBeenCalledWith({ path: "/konto" });
64
64
  });
65
65
 
66
+ it("removes the confirmation hashes from the address bar", async () => {
67
+ invokeMock.mockResolvedValueOnce({ data: {} });
68
+ const replaceMock = vi.spyOn(useRouter(), "replace");
69
+
70
+ await mountPage();
71
+
72
+ expect(replaceMock).toHaveBeenCalledWith({ query: {} });
73
+ expect(replaceMock.mock.invocationCallOrder[0]).toBeLessThan(
74
+ invokeMock.mock.invocationCallOrder[0]!,
75
+ );
76
+ });
77
+
66
78
  it("shows an error and stays on the page when the link is invalid", async () => {
67
79
  invokeMock.mockRejectedValueOnce(
68
80
  new ApiClientErrorMock({
@@ -16,16 +16,6 @@ mockNuxtImport("useRuntimeConfig", () => () => ({
16
16
  geoapifyApiKey: "test-api-key",
17
17
  }));
18
18
 
19
- // Mock useAddressSuggestionArea
20
- const { mockBoundingBoxCoordinates } = vi.hoisted(() => ({
21
- mockBoundingBoxCoordinates: {
22
- value: "8.822251,50.055026,8.899077,50.104327",
23
- },
24
- }));
25
- mockNuxtImport("useAddressSuggestionArea", () => () => ({
26
- boundingBoxCoordinates: mockBoundingBoxCoordinates,
27
- }));
28
-
29
19
  describe("useAddressAutocomplete", () => {
30
20
  beforeEach(() => {
31
21
  vi.clearAllMocks();
@@ -45,9 +35,9 @@ describe("useAddressAutocomplete", () => {
45
35
  properties: {
46
36
  street: "Main St",
47
37
  housenumber: "1",
48
- city: "Obertshausen",
49
- postcode: "63179",
50
- formatted: "Main St 1, 63179 Obertshausen, Germany",
38
+ city: "Musterstadt",
39
+ postcode: "12345",
40
+ formatted: "Main St 1, 12345 Musterstadt, Germany",
51
41
  },
52
42
  },
53
43
  ],
@@ -67,7 +57,6 @@ describe("useAddressAutocomplete", () => {
67
57
  text: "Main St 1",
68
58
  lang: "de",
69
59
  limit: 5,
70
- filter: "rect:8.822251,50.055026,8.899077,50.104327",
71
60
  }),
72
61
  }),
73
62
  expect.any(String),
@@ -75,9 +64,9 @@ describe("useAddressAutocomplete", () => {
75
64
  expect(suggestions).toHaveLength(1);
76
65
  expect(suggestions[0]).toEqual({
77
66
  street: "Main St 1",
78
- city: "Obertshausen",
79
- zipcode: "63179",
80
- label: "Main St 1, 63179 Obertshausen, Germany",
67
+ city: "Musterstadt",
68
+ zipcode: "12345",
69
+ label: "Main St 1, 12345 Musterstadt, Germany",
81
70
  });
82
71
  });
83
72
 
@@ -102,8 +91,7 @@ describe("useAddressAutocomplete", () => {
102
91
  expect(consoleSpy).toHaveBeenCalled();
103
92
  consoleSpy.mockRestore();
104
93
  });
105
- it("should not include filter when boundingBoxCoordinates is empty", async () => {
106
- mockBoundingBoxCoordinates.value = "";
94
+ it("leaves the area filter to the server route", async () => {
107
95
  mockUseFetch.mockResolvedValue({
108
96
  data: ref({ features: [] }),
109
97
  });
@@ -120,8 +108,5 @@ describe("useAddressAutocomplete", () => {
120
108
  }),
121
109
  expect.any(String),
122
110
  );
123
-
124
- // Reset for other tests
125
- mockBoundingBoxCoordinates.value = "8.822251,50.055026,8.899077,50.104327";
126
111
  });
127
112
  });
@@ -155,7 +155,7 @@ describe("useCheckoutMethodGuard", () => {
155
155
  });
156
156
 
157
157
  it("never switches a blocked shipping method, it only reports it", async () => {
158
- // La Fattoria: "Lieferung" is blocked until the guest has entered an
158
+ // e.g. "Lieferung" is blocked until the guest has entered an
159
159
  // address in the delivery area; switching made every order a pickup
160
160
  cart.value = cartWith({ [shippingBlocked.key]: shippingBlocked });
161
161
 
@@ -21,7 +21,7 @@ describe("useShippingMethodChoice", () => {
21
21
  });
22
22
 
23
23
  it("loads all active methods, also those whose rule needs an address", async () => {
24
- // "Lieferung" at La Fattoria only matches with a shipping city in the
24
+ // "Lieferung" in a shop with a delivery-area rule only matches with a shipping city in the
25
25
  // delivery area; onlyAvailable hid it from guests without an address
26
26
  const { methods, load } = useShippingMethodChoice();
27
27
  await load({ forceReload: true });
@@ -96,6 +96,22 @@ describe("useTrackEvent", () => {
96
96
  ]);
97
97
  });
98
98
 
99
+ it("tracks no query parameters that may carry secrets", () => {
100
+ setMatomoConfig("https://analytics.example.com/", 3);
101
+ document.title = "Neues Passwort";
102
+
103
+ useTrackEvent().trackPageView("/account/recover/password?hash=abc#x");
104
+ useTrackEvent().trackShare("copy", "P1", "/?produkt=P1&em=x");
105
+
106
+ expect(matomoWindow._paq).toEqual([
107
+ ["setCustomUrl", "/account/recover/password"],
108
+ ["setDocumentTitle", "Neues Passwort"],
109
+ ["trackPageView"],
110
+ ["setCustomUrl", "/?produkt=P1"],
111
+ ["trackEvent", "Product", "CopyLink", "P1"],
112
+ ]);
113
+ });
114
+
99
115
  it("tracks an add from the cart suggestions as its own action", () => {
100
116
  setMatomoConfig("https://analytics.example.com/", 3);
101
117
 
@@ -0,0 +1,247 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { stubNitro, type TestEvent } from "./nitroStubs";
3
+
4
+ const API_KEY = "secret-geoapify-key";
5
+ const BOX = "8.822251,50.055026,8.899077,50.104327";
6
+
7
+ type Handler = (event: TestEvent & { ip?: string }) => Promise<unknown>;
8
+
9
+ const feature = {
10
+ type: "Feature",
11
+ geometry: { type: "Point", coordinates: [8.86, 50.07] },
12
+ properties: {
13
+ street: "Hauptstraße",
14
+ housenumber: "1",
15
+ city: "Musterstadt",
16
+ postcode: "12345",
17
+ formatted: "Hauptstraße 1, 12345 Musterstadt, Deutschland",
18
+ place_id: "abc",
19
+ datasource: { sourcename: "openstreetmap" },
20
+ },
21
+ };
22
+
23
+ let fetchMock: ReturnType<typeof vi.fn>;
24
+
25
+ async function loadHandler(
26
+ boundingBox = BOX,
27
+ apiKey = API_KEY,
28
+ ): Promise<Handler> {
29
+ vi.resetModules();
30
+ const utils = await import("../../server/utils/addressAutocomplete");
31
+ fetchMock = vi.fn().mockResolvedValue({ features: [feature] });
32
+ stubNitro({
33
+ ...utils,
34
+ $fetch: fetchMock,
35
+ getRequestIP: (event: { ip?: string }) => event.ip ?? "203.0.113.1",
36
+ setResponseHeader: vi.fn(),
37
+ useRuntimeConfig: () => ({
38
+ geoapifyApiKey: apiKey,
39
+ addressAutocomplete: {
40
+ rateLimit: { capacity: 3, refillPerMinute: 6 },
41
+ },
42
+ public: { shopBite: { addressAutocomplete: { boundingBox } } },
43
+ }),
44
+ });
45
+ return (await import("../../server/api/address/autocomplete.get"))
46
+ .default as unknown as Handler;
47
+ }
48
+
49
+ const requestedUrl = (call = 0) => new URL(fetchMock.mock.calls[call]![0]);
50
+
51
+ beforeEach(() => {
52
+ vi.useFakeTimers();
53
+ });
54
+
55
+ afterEach(() => {
56
+ vi.useRealTimers();
57
+ vi.unstubAllGlobals();
58
+ vi.restoreAllMocks();
59
+ });
60
+
61
+ describe("/api/address/autocomplete", () => {
62
+ it("asks Geoapify with the allowed parameters only", async () => {
63
+ const handler = await loadHandler();
64
+ await handler({
65
+ query: { text: "Hauptstraße 1", lang: "de", limit: "5", apiKey: "x" },
66
+ });
67
+
68
+ const url = requestedUrl();
69
+ expect(url.origin + url.pathname).toBe(
70
+ "https://api.geoapify.com/v1/geocode/autocomplete",
71
+ );
72
+ expect(Object.fromEntries(url.searchParams)).toEqual({
73
+ apiKey: API_KEY,
74
+ text: "Hauptstraße 1",
75
+ lang: "de",
76
+ limit: "5",
77
+ filter: `rect:${BOX}`,
78
+ });
79
+ });
80
+
81
+ it("takes the area from the runtime config, never from the query", async () => {
82
+ const handler = await loadHandler("");
83
+ await handler({
84
+ query: { text: "Hauptstraße 1", filter: "rect:0,0,180,90" },
85
+ });
86
+
87
+ expect(requestedUrl().searchParams.has("filter")).toBe(false);
88
+ });
89
+
90
+ it("ignores a malformed area in the runtime config", async () => {
91
+ const handler = await loadHandler("8.8,50.0,nope");
92
+ await handler({ query: { text: "Hauptstraße 1" } });
93
+
94
+ expect(requestedUrl().searchParams.has("filter")).toBe(false);
95
+ });
96
+
97
+ it("returns no suggestions for text shorter than three characters", async () => {
98
+ const handler = await loadHandler();
99
+
100
+ expect(await handler({ query: { text: "ab" } })).toEqual({ features: [] });
101
+ expect(await handler({ query: {} })).toEqual({ features: [] });
102
+ expect(fetchMock).not.toHaveBeenCalled();
103
+ });
104
+
105
+ it.each([
106
+ [{ text: "a".repeat(201) }],
107
+ [{ text: ["Hauptstraße", "Nebenstraße"] }],
108
+ [{ text: "Hauptstraße 1", limit: "0" }],
109
+ [{ text: "Hauptstraße 1", limit: "11" }],
110
+ [{ text: "Hauptstraße 1", limit: "2.5" }],
111
+ [{ text: "Hauptstraße 1", limit: "5abc" }],
112
+ [{ text: "Hauptstraße 1", lang: "deu" }],
113
+ [{ text: "Hauptstraße 1", lang: "de&apiKey=x" }],
114
+ ])("rejects %o with 400 without asking Geoapify", async (query) => {
115
+ const handler = await loadHandler();
116
+
117
+ await expect(handler({ query })).rejects.toMatchObject({
118
+ statusCode: 400,
119
+ });
120
+ expect(fetchMock).not.toHaveBeenCalled();
121
+ });
122
+
123
+ it("returns only the address fields of the suggestions", async () => {
124
+ const handler = await loadHandler();
125
+
126
+ expect(await handler({ query: { text: "Hauptstraße 1" } })).toEqual({
127
+ features: [
128
+ {
129
+ properties: {
130
+ street: "Hauptstraße",
131
+ housenumber: "1",
132
+ city: "Musterstadt",
133
+ postcode: "12345",
134
+ formatted: "Hauptstraße 1, 12345 Musterstadt, Deutschland",
135
+ },
136
+ },
137
+ ],
138
+ });
139
+ });
140
+
141
+ it("never passes the upstream error text on and logs the URL redacted", async () => {
142
+ const handler = await loadHandler();
143
+ const upstream = `[GET] "https://api.geoapify.com/v1/geocode/autocomplete?apiKey=${API_KEY}&text=x": 401 Unauthorized`;
144
+ fetchMock.mockRejectedValue(
145
+ Object.assign(new Error(upstream), { response: { status: 401 } }),
146
+ );
147
+ const log = vi.spyOn(console, "error").mockImplementation(() => {});
148
+
149
+ const error = await handler({ query: { text: "Hauptstraße 1" } }).catch(
150
+ (e: unknown) => e as Error & { statusCode: number },
151
+ );
152
+
153
+ expect(error).toMatchObject({ statusCode: 502 });
154
+ expect(JSON.stringify(error)).not.toContain(API_KEY);
155
+ expect((error as Error).message).not.toContain(API_KEY);
156
+ expect((error as Error).message).not.toContain("geoapify.com");
157
+ expect(log).toHaveBeenCalled();
158
+ const logged = JSON.stringify(log.mock.calls);
159
+ expect(logged).not.toContain(API_KEY);
160
+ expect(logged).toContain("apiKey=***");
161
+ });
162
+
163
+ it("answers an identical lookup from a short-lived cache", async () => {
164
+ const handler = await loadHandler();
165
+ await handler({ query: { text: "Hauptstraße 1" } });
166
+ await handler({ query: { text: "Hauptstraße 1" }, ip: "x" });
167
+
168
+ expect(fetchMock).toHaveBeenCalledTimes(1);
169
+
170
+ vi.advanceTimersByTime(10 * 60 * 1000);
171
+ await handler({ query: { text: "Hauptstraße 1" }, ip: "y" });
172
+ expect(fetchMock).toHaveBeenCalledTimes(2);
173
+ });
174
+
175
+ it("does not cache a failed lookup", async () => {
176
+ const handler = await loadHandler();
177
+ vi.spyOn(console, "error").mockImplementation(() => {});
178
+ fetchMock.mockRejectedValueOnce(new Error("fetch failed"));
179
+
180
+ await expect(
181
+ handler({ query: { text: "Hauptstraße 1" } }),
182
+ ).rejects.toBeTruthy();
183
+ await handler({ query: { text: "Hauptstraße 1" } });
184
+
185
+ expect(fetchMock).toHaveBeenCalledTimes(2);
186
+ });
187
+
188
+ it("limits the lookups per client IP", async () => {
189
+ const handler = await loadHandler();
190
+ for (const n of [1, 2, 3]) {
191
+ await handler({ query: { text: `Hauptstraße ${n}` }, ip: "a" });
192
+ }
193
+
194
+ await expect(
195
+ handler({ query: { text: "Hauptstraße 4" }, ip: "a" }),
196
+ ).rejects.toMatchObject({ statusCode: 429 });
197
+ // another client is not affected
198
+ await handler({ query: { text: "Hauptstraße 4" }, ip: "b" });
199
+ expect(fetchMock).toHaveBeenCalledTimes(4);
200
+
201
+ // capacity 3, 6 per minute: one token after 10 s
202
+ vi.advanceTimersByTime(10_000);
203
+ await handler({ query: { text: "Hauptstraße 5" }, ip: "a" });
204
+ expect(fetchMock).toHaveBeenCalledTimes(5);
205
+ });
206
+
207
+ it("returns no suggestions without an API key", async () => {
208
+ const handler = await loadHandler(BOX, "");
209
+
210
+ expect(await handler({ query: { text: "Hauptstraße 1" } })).toEqual({
211
+ features: [],
212
+ });
213
+ expect(fetchMock).not.toHaveBeenCalled();
214
+ });
215
+ });
216
+
217
+ describe("createTokenBucketLimiter", () => {
218
+ it("keeps at most maxKeys clients", async () => {
219
+ const { createTokenBucketLimiter } =
220
+ await import("../../server/utils/addressAutocomplete");
221
+ const limiter = createTokenBucketLimiter({
222
+ capacity: 1,
223
+ refillPerMinute: 1,
224
+ maxKeys: 2,
225
+ });
226
+
227
+ expect(limiter.take("a")).toBe(true);
228
+ expect(limiter.take("b")).toBe(true);
229
+ expect(limiter.take("c")).toBe(true);
230
+ expect(limiter.size()).toBeLessThanOrEqual(2);
231
+ });
232
+ });
233
+
234
+ describe("createTtlCache", () => {
235
+ it("keeps at most maxEntries entries", async () => {
236
+ const { createTtlCache } =
237
+ await import("../../server/utils/addressAutocomplete");
238
+ const cache = createTtlCache<number>({ ttlMs: 60_000, maxEntries: 2 });
239
+ cache.set("a", 1);
240
+ cache.set("b", 2);
241
+ cache.set("c", 3);
242
+
243
+ expect(cache.get("a")).toBeUndefined();
244
+ expect(cache.get("b")).toBe(2);
245
+ expect(cache.get("c")).toBe(3);
246
+ });
247
+ });