From 71b1262e760df5e1a25aea8ac58771bfdeef9012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20OLIVIER?= <35299257+greg-olivier@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:01:51 +0100 Subject: [PATCH 1/2] fix(eligibility): keep the plans when sessionStorage fails Co-Authored-By: Claude --- src/domain/api/get-eligibility.test.ts | 53 +++++++++++++++++---- src/domain/api/get-eligibility.ts | 9 ++-- tests/integration/contract/failures.test.ts | 17 ++++--- 3 files changed, 59 insertions(+), 20 deletions(-) diff --git a/src/domain/api/get-eligibility.test.ts b/src/domain/api/get-eligibility.test.ts index 38d7a256..5ec6999a 100644 --- a/src/domain/api/get-eligibility.test.ts +++ b/src/domain/api/get-eligibility.test.ts @@ -139,32 +139,67 @@ describe('getEligibility', () => { throw securityError } - it('fails with one warning and sends no request when the read throws', async () => { + it('warns once and sends the request when the read throws', async () => { vi.spyOn(Storage.prototype, 'getItem').mockImplementation(throwing) + const setItem = vi.spyOn(Storage.prototype, 'setItem') + vi.mocked(fetchFromApi).mockResolvedValue(mockPlansAllEligible) - expect(await getEligibility(input())).toEqual(FAILED) - expect(fetchFromApi).not.toHaveBeenCalled() + expect(await getEligibility(input())).toEqual({ + status: StatusResponse.SUCCESS, + plans: mockPlansAllEligible, + }) + expect(fetchFromApi).toHaveBeenCalledTimes(1) expect(warn).toHaveBeenCalledTimes(1) - expect(warn).toHaveBeenCalledWith(expect.stringMatching(/^\[Alma widgets\] /), securityError) + expect(warn).toHaveBeenCalledWith(expect.stringContaining('could not be read'), securityError) + // The write is still tried, and works. + expect(setItem).toHaveBeenCalledTimes(1) + expect(sessionStorage.length).toBe(1) + }) + + it('warns about the read and about the write when the whole storage is blocked', async () => { + vi.spyOn(Storage.prototype, 'getItem').mockImplementation(throwing) + vi.spyOn(Storage.prototype, 'setItem').mockImplementation(throwing) + vi.mocked(fetchFromApi).mockResolvedValue(mockPlansAllEligible) + + expect((await getEligibility(input())).status).toBe(StatusResponse.SUCCESS) + expect(warn).toHaveBeenCalledTimes(2) + expect(warn).toHaveBeenNthCalledWith( + 1, + expect.stringContaining('could not be read'), + securityError, + ) + expect(warn).toHaveBeenNthCalledWith( + 2, + expect.stringContaining('could not be cached'), + securityError, + ) }) - it('fails with one warning when the write throws, after one request', async () => { + it('warns once and returns the plans when the write throws', async () => { vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { throw new DOMException('The quota has been exceeded.', 'QuotaExceededError') }) vi.mocked(fetchFromApi).mockResolvedValue(mockPlansAllEligible) - expect(await getEligibility(input())).toEqual(FAILED) + expect(await getEligibility(input())).toEqual({ + status: StatusResponse.SUCCESS, + plans: mockPlansAllEligible, + }) expect(fetchFromApi).toHaveBeenCalledTimes(1) expect(warn).toHaveBeenCalledTimes(1) + expect(warn).toHaveBeenCalledWith( + expect.stringContaining('could not be cached'), + expect.any(DOMException), + ) }) - it('fails with one warning when the delete of an expired entry throws', async () => { + it('warns once and sends the request when the delete of an expired entry throws', async () => { seed({ value: mockPlansAllEligible, timestamp: Date.now() - HOUR }) vi.spyOn(Storage.prototype, 'removeItem').mockImplementation(throwing) + vi.mocked(fetchFromApi).mockResolvedValue(mockPlansAllEligible) - expect(await getEligibility(input())).toEqual(FAILED) - expect(fetchFromApi).not.toHaveBeenCalled() + expect((await getEligibility(input())).status).toBe(StatusResponse.SUCCESS) + expect(fetchFromApi).toHaveBeenCalledTimes(1) expect(warn).toHaveBeenCalledTimes(1) }) }) diff --git a/src/domain/api/get-eligibility.ts b/src/domain/api/get-eligibility.ts index e03a9b2d..489d18b1 100644 --- a/src/domain/api/get-eligibility.ts +++ b/src/domain/api/get-eligibility.ts @@ -42,7 +42,7 @@ const requestEligibility = async (input: EligibilityInput): Promise => { const { purchaseAmount, configPlans } = input // The merchant's options arrive unchecked. @@ -60,11 +60,12 @@ export const getEligibility = async (input: EligibilityInput): Promise { +const expectNoEscape = ({ escaped, warnings }: Outcome, maxWarnings = 1) => { expect(escaped).toEqual([]) - expect(warnings.length).toBeLessThanOrEqual(1) + expect(warnings.length).toBeLessThanOrEqual(maxWarnings) warnings.forEach(([message]) => { expect(String(message).startsWith('[Alma widgets]')).toBe(true) }) @@ -174,7 +174,7 @@ describe('API failures', () => { }) describe('storage failures', () => { - it('shows no plan when the sessionStorage getter throws', async () => { + it('renders the plans and caches nothing when the sessionStorage getter throws', async () => { const outcome = await observe({ before: () => Object.defineProperty(globalThis, 'sessionStorage', { @@ -185,8 +185,11 @@ describe('storage failures', () => { }), }) - expect(outcome.options).toBe(0) - expectNoEscape(outcome) + expect(outcome.options).toBe(3) + expect(outcome.requests).toBe(1) + expect(outcome.keys).toEqual([]) + // The read and the write fail for the same reason, and each logs its own warning. + expectNoEscape(outcome, 2) }) }) From 5426cee45ac43b994af9a3305b9343b725cfe05e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20OLIVIER?= <35299257+greg-olivier@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:04:22 +0100 Subject: [PATCH 2/2] docs(rules): allow one warning per distinct failure in ARCH-13 Co-Authored-By: Claude --- .claude/rules/public-api.md | 2 +- .claude/rules/widgets.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.claude/rules/public-api.md b/.claude/rules/public-api.md index cc25db80..462f27b9 100644 --- a/.claude/rules/public-api.md +++ b/.claude/rules/public-api.md @@ -16,7 +16,7 @@ Generic advice that doesn't apply here: configuring an element through its HTML - **ARCH-17** `add(PaymentPlans)` returns `undefined`. `add(Modal)` returns `{ open, close }`, even when the container doesn't exist. A second `add()` on the same container replaces the widget. Why: integrations rely on these return values and on a second `add()` to refresh a widget. Check: unit tests. - **ARCH-18** Every option keeps its default: `monochrome` is `true`, `locale` is `'en'`, `hideBorder` is `false`, and `transitionDelay` is 5500 ms, turns the animation off when negative, and becomes 1000 ms from 0 to 999 ms. Why: a merchant who sets no option keeps the behaviour they had. Check: unit tests. - **ARCH-19** `onClose` and `onModalClose` receive DOM events with the four members that preact/compat adds: `persist()`, `isPropagationStopped()`, `isDefaultPrevented()` and `nativeEvent`. Why: merchant callbacks may call them. Check: unit tests. -- **ARCH-13** No exception reaches the merchant's page. A misconfiguration logs one console warning prefixed `[Alma widgets]`. Why: an error in the widget must never break the merchant's checkout. Check: unit tests and contract tests. +- **ARCH-13** No exception reaches the merchant's page. Each distinct failure logs one console warning prefixed `[Alma widgets]`, through `warnOnce`, so a message is never logged twice. Why: an error in the widget must never break the merchant's checkout, and the console stays readable. Check: unit tests and contract tests. - **ARCH-25** Merchants change the widget only through the options of `add()`. The widget offers them no other hook: no `part` attribute, no slot, no custom property they can set, and no class or ID that the docs tell them to style. Why: the shadow root keeps merchant CSS out, so the widget's look stays ours to change in any release, and an option is a hook that every release keeps working. Check: review. - **ARCH-08** An option, a callback, the event that a callback receives, or a tag may be added when it is optional and its default keeps the current behaviour. `documentation.md` describes it, and the release is a minor one. Removing or renaming one, or changing its type or default, needs a major release. Why: an integration keeps working across the releases of one major version. Check: review, and the unit tests of ARCH-16 to ARCH-18. diff --git a/.claude/rules/widgets.md b/.claude/rules/widgets.md index 2f564b09..c84d373b 100644 --- a/.claude/rules/widgets.md +++ b/.claude/rules/widgets.md @@ -24,7 +24,7 @@ Generic advice that doesn't apply here: the `@customElement` decorator, `CustomE - **ARCH-25** Merchants change the widget only through the options of `add()`. The widget offers them no other hook: no `part` attribute, no slot, no custom property they can set, and no class or ID that the docs tell them to style. Why: the shadow root keeps merchant CSS out, so the widget's look stays ours to change in any release, and an option is a hook that every release keeps working. Check: review. - **ARCH-08** An option, a callback, the event that a callback receives, or a tag may be added when it is optional and its default keeps the current behaviour. `documentation.md` describes it, and the release is a minor one. Removing or renaming one, or changing its type or default, needs a major release. Why: an integration keeps working across the releases of one major version. Check: review, and the unit tests of ARCH-16 to ARCH-18. - **ARCH-09** No `new CustomEvent` and no `reflect: true`. A widget reaches the page only through the callbacks of its options. Why: an event or a reflected attribute adds to the contract with merchants. Check: review. -- **ARCH-13** No exception reaches the merchant's page. A misconfiguration logs one console warning prefixed `[Alma widgets]`. Why: an error in the widget must never break the merchant's checkout. Check: unit tests and contract tests. +- **ARCH-13** No exception reaches the merchant's page. Each distinct failure logs one console warning prefixed `[Alma widgets]`, through `warnOnce`, so a message is never logged twice. Why: an error in the widget must never break the merchant's checkout, and the console stays readable. Check: unit tests and contract tests. - **ARCH-19** `onClose` and `onModalClose` receive DOM events with the four members that preact/compat adds: `persist()`, `isPropagationStopped()`, `isDefaultPrevented()` and `nativeEvent`. Why: merchant callbacks may call them. Check: unit tests. ## Code