From 2e6d53f137720f54404e5a4fe097ea8dfd3a8dc1 Mon Sep 17 00:00:00 2001 From: robertjamesprior <83608739+robertjamesprior@users.noreply.github.com> Date: Fri, 6 Nov 2026 04:23:20 +0000 Subject: [PATCH 1/2] Cover window promotion across chromium restarts --- server/cmd/api/api/window_promotion_test.go | 45 ++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/server/cmd/api/api/window_promotion_test.go b/server/cmd/api/api/window_promotion_test.go index 188d552e..13c7f580 100644 --- a/server/cmd/api/api/window_promotion_test.go +++ b/server/cmd/api/api/window_promotion_test.go @@ -132,7 +132,13 @@ func newPromotionService(t *testing.T, url string) *ApiService { logPath := filepath.Join(t.TempDir(), "chromium.log") line := "2026-11-05T00:00:00 chromium DevTools listening on " + url + "\n" require.NoError(t, os.WriteFile(logPath, []byte(line), 0o644)) + return newPromotionServiceForLog(t, logPath, url) +} +// newPromotionServiceForLog is newPromotionService for a caller that keeps +// appending to the log, which is how a chromium restart announces itself. +func newPromotionServiceForLog(t *testing.T, logPath, wantURL string) *ApiService { + t.Helper() um := devtoolsproxy.NewUpstreamManager(logPath, slog.Default()) ctx, cancel := context.WithCancel(context.Background()) um.Start(ctx) @@ -141,11 +147,48 @@ func newPromotionService(t *testing.T, url string) *ApiService { _, err := um.WaitForInitial(10 * time.Second) require.NoError(t, err, "upstream manager never discovered the fake url") - require.Equal(t, url, um.Current()) + require.Equal(t, wantURL, um.Current()) return &ApiService{lifecycleCtx: ctx, upstreamMgr: um} } +// maximizedCalls reports how many times the fake has been asked to maximize. +func (f *fakeDevTools) maximizedCalls() int { + f.mu.Lock() + defer f.mu.Unlock() + return f.setMaximized +} + +// A chromium restart is a new devtools URL, and chromium opens a --window-size +// window in normal state, so each start needs its own promotion rather than one +// at boot. This is the path a profile load and a crash both take. +func TestPromoteWindowsLoopPromotesOnEveryChromiumStart(t *testing.T) { + first := &fakeDevTools{pageTargetID: "target-1", windowState: "normal"} + firstURL := first.start(t) + second := &fakeDevTools{pageTargetID: "target-2", windowState: "normal"} + secondURL := second.start(t) + + logPath := filepath.Join(t.TempDir(), "chromium.log") + require.NoError(t, os.WriteFile(logPath, []byte("2026-11-05T00:00:00 chromium DevTools listening on "+firstURL+"\n"), 0o644)) + s := newPromotionServiceForLog(t, logPath, firstURL) + + ch, unsubscribe := s.upstreamMgr.Subscribe() + go s.promoteWindowsLoop(ch, unsubscribe) + + // The URL that is already current when the loop starts is promoted without + // waiting for a notification. + require.Eventually(t, func() bool { return first.maximizedCalls() == 1 }, 5*time.Second, 10*time.Millisecond) + + f, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY, 0o644) + require.NoError(t, err) + _, err = f.WriteString("2026-11-05T00:05:00 chromium DevTools listening on " + secondURL + "\n") + require.NoError(t, err) + require.NoError(t, f.Close()) + + require.Eventually(t, func() bool { return second.maximizedCalls() == 1 }, 5*time.Second, 10*time.Millisecond) + assert.Equal(t, 1, second.maximizedCalls(), "the restarted browser is promoted once") +} + func TestPromoteWindowMaximizedHappyPath(t *testing.T) { f := &fakeDevTools{pageTargetID: "target-1", windowState: "normal"} url := f.start(t) From 25364f6ad4c6deb1306617f7c7ca3b89811f8650 Mon Sep 17 00:00:00 2001 From: robertjamesprior <83608739+robertjamesprior@users.noreply.github.com> Date: Fri, 6 Nov 2026 17:31:22 +0000 Subject: [PATCH 2/2] Assert the promotion count settles at one per start The trailing Equal ran immediately after the Eventually that already waited for exactly one call, so it could only catch a duplicate that landed in the gap between them. Never gives it a real settle window, and covers the first url not being promoted again after it is replaced. Co-Authored-By: Claude Opus 5 --- server/cmd/api/api/window_promotion_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/cmd/api/api/window_promotion_test.go b/server/cmd/api/api/window_promotion_test.go index 13c7f580..7b8494fb 100644 --- a/server/cmd/api/api/window_promotion_test.go +++ b/server/cmd/api/api/window_promotion_test.go @@ -186,7 +186,7 @@ func TestPromoteWindowsLoopPromotesOnEveryChromiumStart(t *testing.T) { require.NoError(t, f.Close()) require.Eventually(t, func() bool { return second.maximizedCalls() == 1 }, 5*time.Second, 10*time.Millisecond) - assert.Equal(t, 1, second.maximizedCalls(), "the restarted browser is promoted once") + assert.Never(t, func() bool { return first.maximizedCalls() != 1 || second.maximizedCalls() != 1 }, 250*time.Millisecond, 10*time.Millisecond, "each start is promoted once and the replaced url is not promoted again") } func TestPromoteWindowMaximizedHappyPath(t *testing.T) {