Fix double-close panic on DownloadDone: skip re-navigation between HOME-mapped states

Primary fix: checkState() now detects when both old and new states map
to the HOME page (DOWNLOADING_ASSETS and READY) and skips re-navigation.
This prevents destroying and recreating the Home page, which would
close the already-closed DownloadDone channel.

Defense-in-depth: Home.downloadDone flag prevents close if a new Home
page somehow reaches the close path.

12 application tests, 4 page tests, 18 mixer tests (34 total).
Tests cover: prerequisite priority ordering, state blocking on
downloads, non-blocking channel read, double-close guard, download
failure path, and repeated computeState calls.
This commit is contained in:
NeilClack
2026-07-11 16:43:16 -05:00
parent a93e475899
commit 2ef826fe63
3 changed files with 318 additions and 0 deletions
+200
View File
@@ -2,6 +2,7 @@ package application
import (
"testing"
"time"
"gorm.io/gorm"
"rhemawavetech.com/rhemawave/internal/db"
@@ -245,6 +246,205 @@ func TestDownloadDoneChannelSetsDownloadsComplete(t *testing.T) {
}
}
// TestPrerequisitePriorityEmailBeforeLicense verifies that missing email
// is reported before missing license key, even when both are absent.
func TestPrerequisitePriorityEmailBeforeLicense(t *testing.T) {
app := &Application{
state: FIRST_RUN,
DB: setupTestDB(t),
}
networkIsUp = true
config, err := db.GetConfig(app.DB)
if err != nil {
t.Fatal(err)
}
config.EmailAddress = ""
config.LicenseKey = ""
app.DB.Save(config)
app.downloadsComplete = true
downloadDone := make(chan struct{})
app.PageRouter = &page.Router{
Context: page.PageCtx{
DbPtr: app.DB,
DownloadDone: downloadDone,
},
}
result := app.computeState()
if result != NEEDS_EMAIL_ADDRESS {
t.Errorf("state = %v, want NEEDS_EMAIL_ADDRESS (email should be checked before license)", result)
}
}
// TestPrerequisitePriorityLicenseAfterEmail verifies that when email is
// present but license is missing, the state is NEEDS_LICENSE_KEY.
func TestPrerequisitePriorityLicenseAfterEmail(t *testing.T) {
app := &Application{
state: FIRST_RUN,
DB: setupTestDB(t),
}
networkIsUp = true
config, err := db.GetConfig(app.DB)
if err != nil {
t.Fatal(err)
}
config.EmailAddress = "test@example.com"
config.LicenseKey = ""
app.DB.Save(config)
app.downloadsComplete = true
downloadDone := make(chan struct{})
app.PageRouter = &page.Router{
Context: page.PageCtx{
DbPtr: app.DB,
DownloadDone: downloadDone,
},
}
result := app.computeState()
if result != NEEDS_LICENSE_KEY {
t.Errorf("state = %v, want NEEDS_LICENSE_KEY", result)
}
}
// TestStateBlocksOnDownloadUntilChannelClosed verifies that computeState
// returns DOWNLOADING_ASSETS repeatedly until DownloadDone is closed.
func TestStateBlocksOnDownloadUntilChannelClosed(t *testing.T) {
app := &Application{
state: FIRST_RUN,
DB: setupTestDB(t),
}
networkIsUp = true
config, err := db.GetConfig(app.DB)
if err != nil {
t.Fatal(err)
}
config.EmailAddress = "test@example.com"
config.LicenseKey = "test-key-123"
app.DB.Save(config)
app.downloadsComplete = false
downloadDone := make(chan struct{})
app.PageRouter = &page.Router{
Context: page.PageCtx{
DbPtr: app.DB,
DownloadDone: downloadDone,
},
}
// Should return DOWNLOADING_ASSETS multiple times before channel closes.
for i := 0; i < 5; i++ {
result := app.computeState()
if result != DOWNLOADING_ASSETS {
t.Fatalf("iteration %d: state = %v, want DOWNLOADING_ASSETS", i, result)
}
}
// Now close the channel.
close(downloadDone)
result := app.computeState()
if result != READY {
t.Errorf("after close: state = %v, want READY", result)
}
}
// TestDownloadDoneNonBlocking verifies that computeState does not block
// when DownloadDone channel is open (non-blocking select).
func TestDownloadDoneNonBlocking(t *testing.T) {
app := &Application{
state: FIRST_RUN,
DB: setupTestDB(t),
}
networkIsUp = true
config, err := db.GetConfig(app.DB)
if err != nil {
t.Fatal(err)
}
config.EmailAddress = "test@example.com"
config.LicenseKey = "test-key-123"
app.DB.Save(config)
app.downloadsComplete = false
downloadDone := make(chan struct{})
app.PageRouter = &page.Router{
Context: page.PageCtx{
DbPtr: app.DB,
DownloadDone: downloadDone,
},
}
// computeState must return promptly even though DownloadDone is not closed.
done := make(chan struct{})
go func() {
result := app.computeState()
if result != DOWNLOADING_ASSETS {
t.Errorf("state = %v, want DOWNLOADING_ASSETS", result)
}
close(done)
}()
select {
case <-done:
// Good: returned promptly.
case <-time.After(2 * time.Second):
t.Fatal("computeState blocked on DownloadDone channel (should be non-blocking)")
}
}
// TestMultipleComputeStateCallsAfterDownloadComplete verifies that calling
// computeState repeatedly after downloads finish does not panic or change state.
func TestMultipleComputeStateCallsAfterDownloadComplete(t *testing.T) {
app := &Application{
state: FIRST_RUN,
DB: setupTestDB(t),
}
networkIsUp = true
config, err := db.GetConfig(app.DB)
if err != nil {
t.Fatal(err)
}
config.EmailAddress = "test@example.com"
config.LicenseKey = "test-key-123"
app.DB.Save(config)
downloadDone := make(chan struct{})
close(downloadDone)
app.PageRouter = &page.Router{
Context: page.PageCtx{
DbPtr: app.DB,
DownloadDone: downloadDone,
},
}
// First call transitions to READY.
result := app.computeState()
if result != READY {
t.Fatalf("first call: state = %v, want READY", result)
}
app.state = result
// Subsequent calls should return READY without panic.
for i := 0; i < 10; i++ {
result = app.computeState()
if result != READY {
t.Fatalf("iteration %d: state = %v, want READY", i, result)
}
}
}
func setupTestDB(t *testing.T) *gorm.DB {
t.Helper()
d, err := db.Connect(":memory:")