Web Development Pro | Lesson 059 | Capstone Hardening: Resilience and Evidence
Web Development Pro · Lesson 59 of 60
Module 6: Delivery and the capstone
In this lesson
Prerequisites and learning outcomes
Complete lesson 58 first. This lesson builds on persistence, recovery, and integrated checks. By the end, you can verify error recovery, harmless saved preferences, and essential user journeys. Plan 10–15 minutes for the core explanation and additional time for the worked practice.
Harden the boundaries that can fail
The enhanced hub already has search, URL state, and a readable fallback. This stage adds optional saved spacing and tests how the application behaves when data, timing, or storage is unreliable. Resilience means the visitor can still understand what happened and recover, not that every failure is silently ignored.
Keep the essential task separate from convenience features. Reading the schedule should not depend on a stored preference. A corrupted spacing value should not block event loading. A failed enhancement request should leave static information available. These boundaries make the small application easier to reason about under partial failure.
Persist only the harmless preference
The preference key is community-learning-hub:density:v1. Its JSON value contains version:1 and density set to comfortable or compact. The code saves no query text, event selection history, attendee details, or credentials. The Access information page explains this behavior in visitor-facing language.
{"version": 1, "density": "compact"}
A version lets the reader recognize the expected shape. Unknown versions or values fall back to comfortable rather than being interpreted optimistically. This example does not migrate older formats; a future migration would need a deliberate transformation and tests. Do not guess that an unfamiliar saved object means the same thing.
Catch access as well as parsing failures
Accessing window.localStorage itself can fail in some contexts. app.mjs therefore obtains it inside try/catch and passes either the storage object or null into helpers. The helpers also catch getItem, JSON parsing, setItem, and removeItem failures. Handling only malformed JSON would miss unavailable storage entirely.
readDensity returns a safe default when storage is missing, malformed, or incompatible. saveDensity returns a boolean so the caller can tell whether persistence succeeded. The current page still changes spacing when saving fails, but its message says the change is for this page only. Changing or forgetting spacing preserves unsubmitted query and category drafts; only explicit filter actions or history restoration synchronize those controls. That wording matches the actual result.
Clear one key and report the outcome honestly
Forget saved spacing calls removeItem for the application key only. It restores comfortable spacing on the current page regardless, then reports whether the saved value could be removed. It does not call localStorage.clear, which would affect unrelated values on the origin.
If removal fails, the message explains that the page reset succeeded but saved spacing could not be removed. A later reload might still read the old preference. Hiding that distinction would create a confusing recurrence. The visitor can still use the directory, and the limitation is visible.
The test fixture keeps an unrelated key named unrelated with value keep. After forgetting the preference, that key must remain. This small assertion checks the scope of the operation instead of merely checking that the application's own value disappeared.
Test asynchronous ordering deliberately
The controller's request generation is incremented for each load. Starting a new load aborts the old controller. Before any fulfilled or rejected result changes state, the code checks that it belongs to the current generation. An obsolete result therefore cannot overwrite a newer success or error.
Tests use manually controlled promises to resolve requests in the wrong order. One case fulfills the newer request first, then the old request; another rejects the old request after a newer success. These cases are deterministic and do not need an actual slow network to reproduce the timing relationship.
Preserve recovery information during failures
Loading and error states keep the static schedule visible. Retry starts another load through the same validated path. A successful retry enables controls and replaces fallback with the enhanced view; a malformed response remains an error. The visitor never sees raw exception text containing potentially sensitive details.
An empty valid array and a failed load remain distinct. The former is a ready state with no matching events; the latter leaves fallback and recovery guidance. That distinction matters to trust: a data outage should not tell visitors that no workshops exist.
The enhanced result list is hidden when not ready. Its old DOM children may remain internally, but hidden stale cards are not presented as current results. A later successful render replaces them. This is a deliberate implementation detail that should be reviewed if loading-state design changes.
Combine tests across the owned layers
Pure tests establish filter and validation contracts. Controller tests establish controlled transitions and URL side effects. Renderer recorder tests establish selected element operations. Preference tests establish scoped read/write/delete behavior with allowed, corrupted, unknown-version, and throwing storage doubles.
An additional integrated recorder journey can connect a real controller to the renderer with controlled data, then load, filter, restore, and reset. That checks cooperation between owned modules while still avoiding a claim of browser equivalence. The fake objects do not implement native events or CSS, and their limitations must remain visible in the report.
Finish with a focused manual stress review
In an available browser, test actual storage blocking, corrupt the application key only in a disposable practice origin, reload, and verify recovery. Check whether Forget communicates accurately. Inspect keyboard focus around Retry and after enhancement returns; the recorder cannot establish whether a disappearing control leaves focus in a useful place.
Also test repeated Apply, Reset after no matches, copied URLs, Back/Forward, narrow layout, zoom, and actual status announcements. No browser could run in the authoring environment, so these remain untested. The local package is hardened with code-level evidence, while release readiness still depends on completing the manual and delivery gates.
Worked practice
Worked exercise 1: Handle a throwing getter
Task: Reading window.localStorage throws before getItem runs. Worked answer: wrap acquisition of the storage object itself, then pass a nullable dependency to the helpers. Catching JSON.parse alone cannot recover from an earlier access failure.
Worked exercise 2: Report a partial reset
Task: Forget restores comfortable spacing, but removeItem throws. Worked answer: say that the current page reset succeeded while the saved preference could not be removed. Do not promise that the old setting cannot return on reload.
Worked exercise 3: Reproduce a stale error
Task: An old request fails after a new request succeeds and replaces the success message. Worked answer: add a controlled-promise test for that order and apply the same generation guard to rejection as to fulfillment. Guarding only successful results leaves the stale-error path open.
Knowledge check
Question 1
What is saved in this snapshot? A) The current query and every earlier search. B) A versioned comfortable-or-compact spacing preference. C) The last selected event plus its entire record. D) A privileged service token.
Check question 1: answer and explanation
Answer: B. Persistence is deliberately limited to one harmless display choice.
Question 2
Why catch access to localStorage itself? A) The property access can fail before a storage method is called. B) A successful acquisition guarantees every later write will succeed. C) Catching malformed JSON also catches failures before getItem is called. D) A version field prevents browser storage access restrictions.
Check question 2: answer and explanation
Answer: A. Failure can occur at more than one boundary.
Question 3
What should Forget remove? A) Every key on the origin. B) Every key sharing the same JSON version number. C) Only the documented application preference key. D) All keys before writing the default density back.
Check question 3: answer and explanation
Answer: C. A narrow reset should preserve unrelated state.
Question 4
Which stale result needs guarding? A) Only successful JSON. B) Only responses with nonempty arrays. C) Only the first load. D) Both older fulfillment and older rejection.
Check question 4: answer and explanation
Answer: D. Either can wrongly overwrite the latest state.
Question 5
What does a controller-plus-recorder journey establish? A) Actual keyboard focus in the browser. B) Cooperation of the tested modules within the recorder's limited model. C) Live hosting correctness. D) Complete accessibility conformance.
Check question 5: answer and explanation
Answer: B. Integration evidence still inherits the substitute's limitations.