Skip to content

fix: clamp imported 0-minute sessions to the minimum, not the 30-minute default - #22

Open
bfs2021 wants to merge 3 commits into
LetsCook-Organization:mainfrom
bfs2021:fix/import-duration-zero
Open

bfs2021 wants to merge 3 commits into
LetsCook-Organization:mainfrom
bfs2021:fix/import-duration-zero

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

🌌 Pull Request Summary

Fixes #10

Problem

When a backup JSON is imported, importJSON cleaned durations with Math.max(1, parseInt(a.duration, 10) || 30). Because 0 is falsy, a 0-minute session silently became 30 minutes, inflating logged hours, the productivity score and the Century Club achievement, while -10 correctly clamped to 1.

Root cause

The || 30 fallback fires for any falsy parsed value, not just missing or non-numeric ones. parseInt("0", 10) is 0, so a valid 0 was replaced by the 30-minute default instead of being clamped to the 1-minute minimum like other out-of-range values.

Fix

Fall back to 30 only when the parsed value is NaN (missing or non-numeric); otherwise clamp with Math.max(1, …) as before:

const duration = parseInt(a.duration, 10);
a.duration = Math.max(1, Number.isNaN(duration) ? 30 : duration);

Imported durations now behave exactly as specified in the issue: -10 → 1, 0 → 1, 'abc'/missing → 30.

Type of Change

  • Bug fix (non-breaking change resolving an unexpected behavior or error)

How Has This Been Tested?

Extended the existing clamps progress to 0–100 and keeps durations positive test in tests/storage.test.js to assert the exact values from the issue's table (-10 → 1, 0 → 1, 'x' → 30, missing → 30). The new assertions failed against the old code (0 became 30) and pass with the fix.

  • npm test: 52/52 pass in 4 timezones
  • npm run lint: clean
  • npm run format:check: clean

(The change is in import sanitization only, verified through the unit tests; no UI behavior outside the documented table is affected.)

Contributor Checklist

  • My code adheres to the project's vanilla standards (HTML5, CSS3, ES6+ modules; zero heavy frontend frameworks or build steps).
  • I have read and followed the Contributing Guidelines.
  • I have performed a self-review of my own code.
  • I have preserved all existing unrelated code, docstrings, and comments.
  • Any user-facing HTML injection is sanitized with escapeHTML() to prevent XSS. (n/a — no user-facing HTML changed)
  • I have updated corresponding documentation if my changes modify existing features or settings. (added a line under Unreleased → Fixed in CHANGELOG.md)
  • npm run check passes locally.
  • No design-choice or call-graph changes, so docs/DECISIONS.md and docs/FLOW.md need no updates.

…te default

parseInt(duration) || 30 treated 0 as falsy, so a 0-minute session in an
imported backup became 30 minutes, inflating study hours, the productivity
score and the Century Club achievement. Only fall back to the 30-minute
default when the value is not a number; zero and negative values clamp to
the 1-minute minimum like any other out-of-range input.
…port date filter

Upstream 344b1fc made importJSON drop activities whose date is not a
valid ISO date, so the four dateless activities in this fixture were all
filtered out on the merge commit and CI saw [] instead of [1, 30, 1, 30].
The duration expectations are unchanged.
@bfs2021

bfs2021 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

The earlier red CI run came from the merge with main, not from the fix itself: upstream 344b1fc made importJSON drop activities whose date is not a valid ISO date, so the four dateless activities in this PR's test fixture were all filtered out on the merge commit and the assertion saw [] instead of [1, 30, 1, 30].

Merged current main into the branch and gave the fixture activities valid dates (24b35b5); the duration expectations are unchanged. All checks are green again (63 tests x 4 timezones, lint, format).

This branch has not been deployed

No deployments
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.

Restoring a backup turns a 0-minute session into 30 minutes

1 participant