Skip to content

Conversation

@Snider
Copy link
Owner

@Snider Snider commented Nov 13, 2025

Previously, the NewWithFactories function in runtime_pkg.go iterated over an empty slice, which prevented any services from being created from the provided factories. This commit fixes the loop to correctly iterate over the keys of the factories map, ensuring that services are properly instantiated and registered.

feat(testing): Add Good, Bad, and Ugly tests for NewWithFactories

To improve test quality and ensure the reliability of the NewWithFactories function, this commit replaces the existing basic test with a comprehensive test suite following the Good, Bad, Ugly methodology:

  • TestNewWithFactories_Good: Verifies the happy path, ensuring a service is created and registered successfully.
  • TestNewWithFactories_Bad: Checks that the function correctly returns an error when a service factory fails.
  • TestNewWithFactories_Ugly: Confirms that the function panics when a nil factory is provided, as this represents an unrecoverable programmer error.

Previously, the `NewWithFactories` function in `runtime_pkg.go` iterated over an empty slice, which prevented any services from being created from the provided factories. This commit fixes the loop to correctly iterate over the keys of the `factories` map, ensuring that services are properly instantiated and registered.

feat(testing): Add Good, Bad, and Ugly tests for NewWithFactories

To improve test quality and ensure the reliability of the `NewWithFactories` function, this commit replaces the existing basic test with a comprehensive test suite following the Good, Bad, Ugly methodology:

- `TestNewWithFactories_Good`: Verifies the happy path, ensuring a service is created and registered successfully.
- `TestNewWithFactories_Bad`: Checks that the function correctly returns an error when a service factory fails.
- `TestNewWithFactories_Ugly`: Confirms that the function panics when a `nil` factory is provided, as this represents an unrecoverable programmer error.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 13, 2025

📝 Walkthrough

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Service factories now properly initialise and create all configured services in a deterministic order.
  • Tests

    • Enhanced test coverage for service factory operations, including error handling and edge case validation.

Walkthrough

The PR fixes NewWithFactories to deterministically create services from factories by sorting factory names before iteration, replacing previous non-functional behaviour. Test coverage is enhanced with three test cases covering success, error, and panic scenarios.

Changes

Cohort / File(s) Summary
Runtime service factory initialization
runtime_pkg.go
Added "sort" import and modified NewWithFactories to retrieve and iterate over sorted factory names, creating services in deterministic order with proper error handling
Service factory tests
runtime_pkg_test.go
Enhanced TestNewWithFactories_Good with service existence and type assertions; added TestNewWithFactories_Bad for error handling validation; added TestNewWithFactories_Ugly for nil factory entry panic verification

Possibly related PRs

  • Feature add tdd core tests #22: Modifies the same NewWithFactories function and runtime test behaviour, introducing factory-based initialization patterns and validation.

Suggested labels

enhancement

Poem

🐰 A sorted tale of factories fine,
Where services bloom in ordered line,
No more chaos, no random dance—
Determinism gets its chance!
Bad paths caught, nil panics too,
Tests now strong and shiny new.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the primary fix in the changeset: correcting the NewWithFactories service creation loop in runtime_pkg.go.
Description check ✅ Passed The description is directly related to the changeset, explaining both the runtime bug fix and the test improvements with clear detail.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-good-bad-ugly-tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@codecov
Copy link

codecov bot commented Nov 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@coderabbitai coderabbitai bot added the enhancement New feature or request label Nov 13, 2025
@Snider Snider merged commit 1013d49 into dev Nov 13, 2025
3 of 4 checks passed
@Snider Snider deleted the feat-good-bad-ugly-tests branch November 13, 2025 21:24
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
runtime_pkg.go (1)

6-43: Deterministic factory order and closure capture look correct; consider removing write-only services map

Sorting the factory names and iterating over names gives predictable service initialisation order, and using svcCopy in the closure avoids the common loop‑variable capture bug — this wiring looks sound.

Currently, the local services map is only written to and never read inside NewWithFactories or passed on, so it is effectively redundant. If nothing else in this constructor relies on it, you could simplify by dropping it:

-func NewWithFactories(app *application.App, factories map[string]ServiceFactory) (*Runtime, error) {
-	services := make(map[string]any)
-	coreOpts := []Option{
+func NewWithFactories(app *application.App, factories map[string]ServiceFactory) (*Runtime, error) {
+	coreOpts := []Option{
 		WithWails(app),
 	}
@@
-	for _, name := range names {
-		factory := factories[name]
-		svc, err := factory()
+	for _, name := range names {
+		factory := factories[name]
+		svc, err := factory()
 		if err != nil {
 			return nil, fmt.Errorf("failed to create service %s: %w", name, err)
 		}
-		services[name] = svc
 		svcCopy := svc
 		coreOpts = append(coreOpts, WithName(name, func(c *Core) (any, error) { return svcCopy, nil }))
 	}

If you intend to use services later (e.g. for diagnostics), keeping it is fine.

runtime_pkg_test.go (1)

87-93: Nil‑factory panic is explicitly codified; confirm this is the intended contract

TestNewWithFactories_Ugly locks in the behaviour that providing a nil factory results in a panic. That matches the current implementation (calling the nil factory) but also constrains future changes: if you later decide a nil factory should be treated as a normal error case, this test will need to change.

If the long‑term API contract is “nil factory is programmer error and should panic”, this test is perfect. If you might want to soften that in future, consider documenting the intended behaviour here or adjusting the test to assert the specific failure mode you want to guarantee.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2898ddf and b4a57f0.

📒 Files selected for processing (2)
  • runtime_pkg.go (2 hunks)
  • runtime_pkg_test.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
runtime_pkg_test.go (3)
interfaces.go (1)
  • Core (36-48)
core_test.go (1)
  • MockService (19-21)
runtime_pkg.go (2)
  • ServiceFactory (19-19)
  • NewWithFactories (22-58)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: CodeQL
🔇 Additional comments (2)
runtime_pkg_test.go (2)

60-74: Good‑path test now validates actual service wiring

Asserting that rt.Core.Service("test") exists, has the correct concrete type, and carries the expected name gives strong coverage that NewWithFactories both invokes the factory and correctly registers the service. This is a solid upgrade over just checking rt for non‑nil.


76-85: Error‑path coverage for failing factory is appropriate

This test cleanly exercises the case where a factory returns an error and confirms that NewWithFactories surfaces that error to the caller, including preserving the original error for assert.ErrorIs. The scenario and assertions look well‑chosen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants