Skip to content

refactor(test): use unified TestStaticUtil for setup and teardown. - #53

Open
qyl27 wants to merge 1 commit into
HyperZoneLogin:masterfrom
qyl27:refactor/test-static-util
Open

qyl27 wants to merge 1 commit into
HyperZoneLogin:masterfrom
qyl27:refactor/test-static-util

Conversation

@qyl27

@qyl27 qyl27 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

A test util for accessing static fields in HyperZoneLoginMain. It's a simple refactor.

Summary by Sourcery

Centralize HyperZoneLoginMain test state management and update configuration initialization to support isolated tests.

Enhancements:

  • Centralize static test state setup and cleanup for HyperZoneLoginMain across Velocity tests.
  • Require the core configuration to be initialized explicitly and keep startup configuration local to enablement.

Tests:

  • Standardize test initialization and teardown to improve isolation and remove duplicated static-field handling.

@sourcery-ai

sourcery-ai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Make coreConfig uninitialized until explicitly set and keep the startup configuration local to onEnable, changing how static configuration is accessed.

  • velocity/src/main/kotlin/icu/h2l/login/HyperZoneLoginMain.kt L139, L157

Tests

Centralize test access to HyperZoneLoginMain static fields and update the affected tests to initialize and clear that state consistently.

  • velocity/src/test/kotlin/icu/h2l/login/database/InGameProfileDatabaseTest.kt L45-54 (+3 small edits)
  • velocity/src/test/kotlin/icu/h2l/login/listener/AttachedUserInitialGameProfileListenerTest.kt L37, L130
  • velocity/src/test/kotlin/icu/h2l/login/manager/LoginManagerTest.kt L43-52, L58-75 (+9 small edits)
  • velocity/src/test/kotlin/icu/h2l/login/profile/UserBindingCodeServiceTest.kt L34-55, L63-70, L81-85 (+1 small edits)
  • velocity/src/test/kotlin/icu/h2l/login/vServer/outpre/OutPreVServerAuthTest.kt L31-209 (4 small edits)
  • velocity/src/test/kotlin/util/MockStaticUtil.kt L1-43

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="velocity/src/main/kotlin/icu/h2l/login/HyperZoneLoginMain.kt" line_range="139" />
<code_context>
-        // 访问配置不再抛 UninitializedPropertyAccessException,而是返回安全默认配置。
-        private var coreConfig: CoreConfig = CoreConfig()
-        private var startConfig: StartConfig = StartConfig()
+        private lateinit var coreConfig: CoreConfig

         @JvmStatic
</code_context>
<issue_to_address>
**Configuration access throws**

When a caller accesses configuration before `loadCoreConfig()` assigns it, including after startup exits early or a binding test reaches successful attach, `getCoreConfig()` reads the uninitialized `lateinit` field and throws `UninitializedPropertyAccessException`, so callers fail instead of receiving the previous default configuration.

Initialize `coreConfig` with a safe default configuration rather than declaring it `lateinit`.

Also at `velocity/src/test/kotlin/icu/h2l/login/profile/UserBindingCodeServiceTest.kt:64`.
</issue_to_address>

### Comment 2
<location path="velocity/src/test/kotlin/util/MockStaticUtil.kt" line_range="31-37" />
<code_context>
+    private val instanceField = staticField("instance")
+    private val coreConfigField = staticField("coreConfig")
+
+    fun setCoreConfig(value: CoreConfig) {
+        coreConfigField.set(null, value)
+    }
+
+    fun clear() {
+        instanceField.set(null, null)
+        coreConfigField.set(null, null)
+    }
+
</code_context>
<issue_to_address>
**Parallel tests clear each other’s state**

When the test runner executes these test classes concurrently and one test tears down while another is using the static fields, one test’s `clear()` nulls the shared `HyperZoneLoginMain` fields while another test is using them; the second test then gets an uninitialized-property exception or observes the wrong main instance.

Serialize tests that share this state, or replace the process-wide static test state with isolated state per test.
</issue_to_address>

Sourcery assessment

Approval pending. 2 findings to address first.

Blocking findings: velocity/src/main/kotlin/icu/h2l/login/HyperZoneLoginMain.kt:139, velocity/src/test/kotlin/util/MockStaticUtil.kt:37


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +31 to +37
fun setCoreConfig(value: CoreConfig) {
coreConfigField.set(null, value)
}

fun clear() {
instanceField.set(null, null)
coreConfigField.set(null, null)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Parallel tests clear each other’s state

When the test runner executes these test classes concurrently and one test tears down while another is using the static fields, one test’s clear() nulls the shared HyperZoneLoginMain fields while another test is using them; the second test then gets an uninitialized-property exception or observes the wrong main instance.

Serialize tests that share this state, or replace the process-wide static test state with isolated state per test.

Prompt for AI agents
In `velocity/src/test/kotlin/util/MockStaticUtil.kt` at lines 31-37:

**Parallel tests clear each other’s state**

When the test runner executes these test classes concurrently and one test tears down while another is using the static fields, one test’s `clear()` nulls the shared `HyperZoneLoginMain` fields while another test is using them; the second test then gets an uninitialized-property exception or observes the wrong main instance.

Serialize tests that share this state, or replace the process-wide static test state with isolated state per test.

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.

1 participant