Skip to content

refactor: improve code quality and robustness#5643

Open
Ashutoshx7 wants to merge 7 commits intosugarlabs:masterfrom
Ashutoshx7:refactor/code-quality-improvements
Open

refactor: improve code quality and robustness#5643
Ashutoshx7 wants to merge 7 commits intosugarlabs:masterfrom
Ashutoshx7:refactor/code-quality-improvements

Conversation

@Ashutoshx7
Copy link
Contributor

@Ashutoshx7 Ashutoshx7 commented Feb 9, 2026

Overview

This PR improves code quality and robustness in two core areas of the codebase.


1. Dependency Injection in ModeWidget (js/widgets/modewidget.js)

Problem

ModeWidget relied on deeply nested property chains throughout the class:

  • this.activity.logo.*
  • this.activity.turtles.*
  • this.activity.blocks.*

This created tight coupling to the activity global, making the code harder to test and maintain.

Solution

Refactored the constructor to resolve dependencies upfront from the activity object into local instance properties:

  • this.logo, this.turtles, this.blocks, this.storage
  • this.hideMsgs, this.textMsg, this.errorMsg, this.refreshCanvas

An optional deps parameter allows explicit dependency injection in tests, while the activity reference is preserved for backward compatibility.

Result

  • Cleaner method bodies (e.g., this.logo.synth.trigger(...) instead of this.activity.logo.synth.trigger(...))
  • Easier unit testing without mocking deep object hierarchies

2. Global Export for Painter (js/turtle-painter.js)

Problem

The Painter class was exported only via CommonJS (module.exports), but the RequireJS shim in the browser expected it on window.

Solution

Added a conditional window.Painter = Painter export so the class is:

  • Available globally when loaded in the browser
  • Remaining compatible with Node.js/Jest environments

Testing

  • ✅ All 8 ModeWidget tests pass (modewidget.test.js + __tests__/modewidget.test.js)
  • ModeWidget opens, plays modes, saves, and rotates correctly in the browser
  • Painter loads correctly via RequireJS once again

- index.html: implement retry mechanism for doSearch to prevent initialization errors
- js/turtle-painter.js: export Painter class to global window scope for loader compatibility
- js/widgets/modewidget.js: implement dependency injection pattern to reduce global state usage
@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2026

❌ Some Jest tests failed. Please check the logs and fix the issues before merging.

Failed Tests:

modewidget.test.js

@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2026

❌ Some Jest tests failed. Please check the logs and fix the issues before merging.

Failed Tests:

modewidget.test.js

@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2026

✅ All Jest tests passed! This PR is ready to merge.

@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@walterbender
Copy link
Member

do search was removed from index.html so that change (and large diff) is not needed here.

doSearch was removed from index.html upstream, so the tryDoSearch
retry logic and related changes are no longer needed. This reverts
index.html to match upstream master.
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@Ashutoshx7
Copy link
Contributor Author

do search was removed from index.html so that change (and large diff) is not needed here.

ready for review

@walterbender
Copy link
Member

I am a bit confused as to what I am looking at. The changes only vaguely resemble the description in your PR

@github-actions
Copy link
Contributor

❌ Some Jest tests failed. Please check the logs and fix the issues before merging.

Failed Tests:

palette.test.js
logo.test.js

@Ashutoshx7
Copy link
Contributor Author

Ashutoshx7 commented Feb 22, 2026

I am a bit confused as to what I am looking at. The changes only vaguely resemble the description in your PR

updated the pr description and resolved merge conflict
jest are failing on palette.test.js,logo.test.js but its not related to my pr
should i fix those and recommit them in my pr ? @walterbender

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