Fix CI: extract computeState() for testability; make network init non-fatal

network.init() no longer panics when D-Bus is unavailable (logs warning
instead). This allows go test to run in the nix build sandbox.

checkState() now calls computeState() which returns the target state
without triggering page navigation (which requires OpenGL). Tests call
computeState() directly, avoiding the SIGSEGV from gl.GenVertexArrays.

7 regression tests for state machine, all passing.
This commit is contained in:
NeilClack
2026-07-11 14:42:49 -05:00
parent 3f8144a708
commit 363e6f81f6
3 changed files with 165 additions and 73 deletions
+126 -37
View File
@@ -1,5 +1,3 @@
//go:build linux
package application
import (
@@ -18,14 +16,11 @@ import (
// prerequisites are met but downloads are not complete, the state transitions
// to DOWNLOADING_ASSETS instead of READY.
func TestStateTransitionsToDownloadingAssets(t *testing.T) {
// Set up minimal application state
app := &Application{
state: FIRST_RUN,
DB: setupTestDB(t),
running: true,
state: FIRST_RUN,
DB: setupTestDB(t),
}
// Simulate: network is up, email and license are set
networkIsUp = true
config, err := db.GetConfig(app.DB)
if err != nil {
@@ -35,10 +30,8 @@ func TestStateTransitionsToDownloadingAssets(t *testing.T) {
config.LicenseKey = "test-key-123"
app.DB.Save(config)
// Downloads are NOT complete
app.downloadsComplete = false
// Create a minimal router with channels
downloadDone := make(chan struct{})
app.PageRouter = &page.Router{
Context: page.PageCtx{
@@ -47,12 +40,10 @@ func TestStateTransitionsToDownloadingAssets(t *testing.T) {
},
}
// Run checkState
app.checkState()
result := app.computeState()
// Should be in DOWNLOADING_ASSETS, not READY
if app.state != DOWNLOADING_ASSETS {
t.Errorf("state = %v, want DOWNLOADING_ASSETS (downloads not complete)", app.state)
if result != DOWNLOADING_ASSETS {
t.Errorf("state = %v, want DOWNLOADING_ASSETS (downloads not complete)", result)
}
}
@@ -60,9 +51,8 @@ func TestStateTransitionsToDownloadingAssets(t *testing.T) {
// transitions to READY only after downloads are complete.
func TestStateTransitionsToReadyWhenDownloadsComplete(t *testing.T) {
app := &Application{
state: FIRST_RUN,
DB: setupTestDB(t),
running: true,
state: FIRST_RUN,
DB: setupTestDB(t),
}
networkIsUp = true
@@ -74,7 +64,6 @@ func TestStateTransitionsToReadyWhenDownloadsComplete(t *testing.T) {
config.LicenseKey = "test-key-123"
app.DB.Save(config)
// Downloads ARE complete
app.downloadsComplete = true
downloadDone := make(chan struct{})
@@ -85,10 +74,10 @@ func TestStateTransitionsToReadyWhenDownloadsComplete(t *testing.T) {
},
}
app.checkState()
result := app.computeState()
if app.state != READY {
t.Errorf("state = %v, want READY", app.state)
if result != READY {
t.Errorf("state = %v, want READY", result)
}
}
@@ -96,9 +85,8 @@ func TestStateTransitionsToReadyWhenDownloadsComplete(t *testing.T) {
// If downloads fail, the user sees an error and must restart to retry.
func TestDownloadFailurePreventsReady(t *testing.T) {
app := &Application{
state: FIRST_RUN,
DB: setupTestDB(t),
running: true,
state: FIRST_RUN,
DB: setupTestDB(t),
}
networkIsUp = true
@@ -110,7 +98,6 @@ func TestDownloadFailurePreventsReady(t *testing.T) {
config.LicenseKey = "test-key-123"
app.DB.Save(config)
// Downloads completed but failed
app.downloadsComplete = false
app.downloadFailed = true
@@ -122,21 +109,19 @@ func TestDownloadFailurePreventsReady(t *testing.T) {
},
}
app.checkState()
result := app.computeState()
// Should NOT be READY — download failed
if app.state == READY {
if result == READY {
t.Error("state should not be READY when download has failed")
}
}
// TestStateStaysReadyOnceSet verifies that once the state reaches READY,
// checkState() is a no-op (doesn't reset or re-navigate).
// computeState() returns READY without checking other prerequisites.
func TestStateStaysReadyOnceSet(t *testing.T) {
app := &Application{
state: READY,
DB: setupTestDB(t),
running: true,
state: READY,
DB: setupTestDB(t),
}
downloadDone := make(chan struct{})
@@ -147,14 +132,119 @@ func TestStateStaysReadyOnceSet(t *testing.T) {
},
}
app.checkState()
result := app.computeState()
if app.state != READY {
t.Errorf("state = %v, want READY (should not change once set)", app.state)
if result != READY {
t.Errorf("state = %v, want READY (should not change once set)", result)
}
}
// TestEmailMissingPreventsReady verifies that missing email blocks progress
// even when network is up and downloads are complete.
func TestEmailMissingPreventsReady(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 = "test-key-123"
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", result)
}
}
// TestLicenseMissingPreventsReady verifies that missing license key blocks
// progress even when email and network are present.
func TestLicenseMissingPreventsReady(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)
}
}
// TestDownloadDoneChannelSetsDownloadsComplete verifies that receiving on
// the DownloadDone channel sets downloadsComplete to true.
func TestDownloadDoneChannelSetsDownloadsComplete(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{})
close(downloadDone)
app.PageRouter = &page.Router{
Context: page.PageCtx{
DbPtr: app.DB,
DownloadDone: downloadDone,
},
}
result := app.computeState()
if !app.downloadsComplete {
t.Error("downloadsComplete should be true after DownloadDone channel is closed")
}
if result != READY {
t.Errorf("state = %v, want READY after downloads complete", result)
}
}
// setupTestDB creates a temporary in-memory SQLite database for testing.
func setupTestDB(t *testing.T) *gorm.DB {
t.Helper()
d, err := db.Connect(":memory:")
@@ -164,7 +254,6 @@ func setupTestDB(t *testing.T) *gorm.DB {
if err := db.Migrate(d); err != nil {
t.Fatal(err)
}
// Create default config
cfg := &db.AppConfig{
Model: gorm.Model{ID: 1},
DataDir: t.TempDir(),