Make getAiDisclosureSettings survive blocked localStorage, and add tests

Open Beginner friendly
#53 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
94/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
next.js, typescript
Domain
frontend

Research direction

Start with frontend/src/lib/ai-disclosure.ts, comparing the unguarded reads in getAiDisclosureSettings with the guarded writes in saveAiDisclosureSettings and the existing SSR defaults. Add frontend/src/lib/ai-disclosure.test.ts with // @vitest-environment jsdom, then run cd frontend && npx vitest run src/lib/ai-disclosure.test.ts. The change is done when blocked storage returns DEFAULT_AI_DISCLOSURE without throwing, all listed fallback cases pass, and a changelog fragment is added under changelog.d/.

Written by the indexing model from the issue text.

Description

engine good first issue level: 1

What is wrong / what to do
saveAiDisclosureSettings wraps its localStorage writes in try/catch, but getAiDisclosureSettings reads localStorage with no guard. When storage is blocked (some private modes, sandboxed iframes, or blocked site data), reading window.localStorage throws a SecurityError. That error goes through getAiDisclosure(), which both export paths call when they build provenance metadata, so the whole export fails. Wrap the reads so that a throw falls back to the same defaults the SSR branch already returns (voice_script, empty custom text, enabled), as the comment at line 83 says it should. Add ai-disclosure.test.ts to cover this.

Where in the code

  • frontend/src/lib/ai-disclosure.ts:64-73 — unguarded localStorage.getItem at lines 68, 70, 71
  • frontend/src/lib/ai-disclosure.ts:76-85 — the write path, which is guarded
  • frontend/src/lib/ai-disclosure.ts:91-99 — getAiDisclosure
  • frontend/src/engine/render-pipeline.ts:350 and :692, frontend/src/engine/cartoon/cartoon-renderer.ts:215 — export-time callers

How to reproduce, or the test command

# put `// @vitest-environment jsdom` on the first line of the test file
cd frontend && npx vitest run src/lib/ai-disclosure.test.ts

Repro in a test: Object.defineProperty(window, 'localStorage', { get() { throw new DOMException('denied', 'SecurityError') }, configurable: true }), then call getAiDisclosure(). Today it throws SecurityError: denied.

Done when

  • With a throwing localStorage, getAiDisclosure() returns DEFAULT_AI_DISCLOSURE and does not throw
  • Tests cover: nothing stored returns DEFAULT_AI_DISCLOSURE; enabled = 'false' returns ''; preset custom with blank text falls back to the default; preset custom with text returns the trimmed text; an unknown stored preset id falls back to voice_script
  • Tests added or updated, and the command above passes
  • Changelog fragment in changelog.d/
Dominant language
TypeScript
Stars
0
Forks
3
Avg merge
1d 4h
Merged PRs (30d)
5

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from BeyondITL/contentine-create

All issues in BeyondITL/contentine-create

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.