Skip to content

refactor(studio): extract settings persistence to src/prefs.js (step 4) - #19

Merged
byrongamatos merged 2 commits into
mainfrom
feat/es-module-split-prefs
Jul 8, 2026
Merged

refactor(studio): extract settings persistence to src/prefs.js (step 4)#19
byrongamatos merged 2 commits into
mainfrom
feat/es-module-split-prefs

Conversation

@byrongamatos

@byrongamatos byrongamatos commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

What

Step 4. Moves _loadSettings / _saveSettings (localStorage-backed user name + selected input device, now reading/writing the S container) into src/prefs.js, imported by the same names. main.js unchanged at the call sites.

Move-only, no behaviour change.

Tests

New tests/prefs.test.mjs (in-memory localStorage stub): round-trip through S, no-op when empty, corrupt-JSON tolerance, and the deviceId !== undefined apply rule. node --test 11/11, pytest 25/25. Codex preflight: 0.

Summary by CodeRabbit

  • Chores
    • Refactored settings persistence into a dedicated module and updated the app to use it.
    • Updated the changelog for the ES-module migration step.
  • Tests
    • Added automated coverage for saving/loading preferences, including round-trip, empty storage, corrupt data handling, and partial/empty stored values.

_loadSettings/_saveSettings (localStorage user name + input device, via the S
container) move to src/prefs.js, imported by the same names. New
tests/prefs.test.mjs (in-memory localStorage stub). Move-only.

node 11/11, python 25/25.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 8, 2026 19:30
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2d67d40e-35f4-4fe8-81da-db587a793f0d

📥 Commits

Reviewing files that changed from the base of the PR and between ec01c21 and ff6eb7c.

📒 Files selected for processing (1)
  • tests/prefs.test.mjs
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/prefs.test.mjs

📝 Walkthrough

Walkthrough

Settings persistence logic is moved from src/main.js into src/prefs.js, and main.js now imports the helpers from that module. New tests cover persistence behavior, and the changelog records the extraction.

Changes

Settings persistence extraction

Layer / File(s) Summary
New prefs.js module implementing load/save settings
src/prefs.js
Adds STORAGE_KEY, _loadSettings(), and _saveSettings() for localStorage-backed settings persistence.
main.js wiring to use prefs.js
src/main.js
Imports _loadSettings and _saveSettings from ./prefs.js and removes the inline persistence implementation.
Tests and changelog for prefs extraction
tests/prefs.test.mjs, CHANGELOG.md
Adds tests for round-trip, empty storage, corrupt JSON, and partial state handling; updates the changelog entry for the extraction.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: extracting settings persistence into src/prefs.js as step 4.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/es-module-split-prefs

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors studio settings persistence by extracting the localStorage-backed _loadSettings / _saveSettings logic into a dedicated src/prefs.js module while keeping main.js call sites unchanged, and adds targeted Node test coverage for the persistence behavior.

Changes:

  • Added src/prefs.js to own settings load/save logic via the shared S state container.
  • Updated src/main.js to import _loadSettings / _saveSettings from src/prefs.js and removed the inline implementations.
  • Added tests/prefs.test.mjs and updated CHANGELOG.md to document the migration step.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/prefs.test.mjs Adds unit tests for settings persistence via S using an in-memory localStorage stub.
src/prefs.js New module implementing _loadSettings / _saveSettings using S and localStorage.
src/main.js Switches to importing settings persistence functions from src/prefs.js.
CHANGELOG.md Documents ES-module migration step 4 and the new prefs tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/prefs.test.mjs Outdated
Comment thread tests/prefs.test.mjs

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/prefs.test.mjs (1)

45-51: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Strengthen "userName left as-is" assertion.

Unlike deviceId, S.userName isn't seeded with a stale sentinel before calling _loadSettings() (it's already '' from beforeEach), so line 50 passes trivially and doesn't actually verify the "left as-is" behavior.

✅ Proposed fix
 test('_loadSettings applies an empty deviceId (deviceId !== undefined) but skips missing userName', () => {
     store.set('slopsmith_studio', JSON.stringify({ deviceId: '' }));
     S.selectedDeviceId = 'stale';
+    S.userName = 'stale';
     _loadSettings();
     assert.equal(S.selectedDeviceId, '');   // '' is applied (!== undefined)
-    assert.equal(S.userName, '');           // absent userName left as-is
+    assert.equal(S.userName, 'stale');      // absent userName left as-is
 });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/prefs.test.mjs` around lines 45 - 51, The _loadSettings test currently
does not really verify that a missing userName is left unchanged because
S.userName starts as an empty string from beforeEach, so the assertion passes
trivially. Update the test in prefs.test.mjs by seeding S.userName with a
non-empty stale sentinel before calling _loadSettings(), then assert it remains
unchanged when userName is absent from the stored settings, alongside the
existing S.selectedDeviceId check.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/prefs.test.mjs`:
- Around line 45-51: The _loadSettings test currently does not really verify
that a missing userName is left unchanged because S.userName starts as an empty
string from beforeEach, so the assertion passes trivially. Update the test in
prefs.test.mjs by seeding S.userName with a non-empty stale sentinel before
calling _loadSettings(), then assert it remains unchanged when userName is
absent from the stored settings, alongside the existing S.selectedDeviceId
check.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 5321a1a8-e50e-4c5c-8c93-86b9caefbc51

📥 Commits

Reviewing files that changed from the base of the PR and between 9e660b9 and ec01c21.

📒 Files selected for processing (4)
  • CHANGELOG.md
  • src/main.js
  • src/prefs.js
  • tests/prefs.test.mjs

…rtion meaningful

Address Copilot on #19: capture/restore globalThis.localStorage via an after()
hook; set S.userName to a non-empty value before loading a blob without userName
so the 'left as-is' assertion actually exercises the skip path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@byrongamatos
byrongamatos merged commit 13b4036 into main Jul 8, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants