|
| 1 | +//go:build e2e && unix |
| 2 | +// +build e2e,unix |
| 3 | + |
| 4 | +// Copyright (c) 2025-2026 Netresearch DTT GmbH |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +package e2e |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "net" |
| 13 | + "os" |
| 14 | + "os/exec" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/chromedp/chromedp" |
| 19 | +) |
| 20 | + |
| 21 | +// uiRefreshInterval mirrors the `setInterval(refresh, 5000)` in |
| 22 | +// static/ui/index.html. The test has to outlast one full cycle to prove the |
| 23 | +// refresh does not clobber user state, so it is stated once here rather than |
| 24 | +// buried as a magic number in the waits below. |
| 25 | +const uiRefreshInterval = 5 * time.Second |
| 26 | + |
| 27 | +// noKeyMarker is what the expand step reports when the expanded <details> |
| 28 | +// carries no data-key — i.e. a build without the fix. It has to be |
| 29 | +// distinguishable from "no element at all", because those two mean very |
| 30 | +// different things about why the test could not find its row afterwards. |
| 31 | +const noKeyMarker = "NO_KEY" |
| 32 | + |
| 33 | +// TestE2E_WebUI_ExpandedOutputSurvivesRefresh drives the real web UI in a real |
| 34 | +// browser and pins the fix for https://github.com/netresearch/ofelia/issues/764. |
| 35 | +// |
| 36 | +// The UI polls the history endpoint every 5s and re-renders the table by |
| 37 | +// replacing the tbody's innerHTML. Before the fix the `<details>` element that |
| 38 | +// wraps a run's output was re-created without its `open` attribute, so an |
| 39 | +// output the user had expanded collapsed on its own within five seconds. |
| 40 | +// |
| 41 | +// This is deliberately a browser test and not an assertion on the served HTML: |
| 42 | +// the bug lives in what the refresh does to DOM state after the user has |
| 43 | +// interacted with it, which no amount of markup inspection can observe. |
| 44 | +func TestE2E_WebUI_ExpandedOutputSurvivesRefresh(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + browserPath := chromeExecutable() |
| 48 | + if browserPath == "" { |
| 49 | + t.Skip("no Chrome/Chromium executable found; skipping browser-driven UI test") |
| 50 | + } |
| 51 | + |
| 52 | + addr := reserveLoopbackAddr(t) |
| 53 | + |
| 54 | + // Cadence is a deliberate trade-off against history eviction. A job keeps |
| 55 | + // the last `HistoryLimit` runs (default 10) and drops the OLDEST first, so |
| 56 | + // at @every 1s an entry is evicted 10s after it appears - faster than this |
| 57 | + // test's wait on a loaded runner, which would make a legitimately dropped |
| 58 | + // row look like the collapse bug. At 2s the entry we expand needs ~20s to |
| 59 | + // reach the cut, while new runs still arrive during the wait so the |
| 60 | + // re-render is genuinely exercised. |
| 61 | + configBody := `[global] |
| 62 | + log-level = info |
| 63 | +
|
| 64 | +[job-local "e2e-web-output"] |
| 65 | + schedule = @every 2s |
| 66 | + command = sh -c "echo OFELIA_E2E_WEB_OUTPUT" |
| 67 | +` |
| 68 | + configPath := writeConfig(t, configBody) |
| 69 | + daemon := startDaemon(t, configPath, "--enable-web", "--web-address="+addr) |
| 70 | + t.Cleanup(func() { daemon.shutdown(t, 15*time.Second) }) |
| 71 | + |
| 72 | + // Wait until the job has actually run, otherwise the history table renders |
| 73 | + // the "No history yet." placeholder and there is nothing to expand. |
| 74 | + if err := daemon.waitForLog(`Job \"e2e-web-output\"`, 15*time.Second); err != nil { |
| 75 | + t.Fatalf("job did not run before the UI check: %v\nstdout=%s", err, daemon.stdout.String()) |
| 76 | + } |
| 77 | + |
| 78 | + allocCtx, cancelAlloc := chromedp.NewExecAllocator(context.Background(), |
| 79 | + append(chromedp.DefaultExecAllocatorOptions[:], |
| 80 | + chromedp.ExecPath(browserPath), |
| 81 | + )...) |
| 82 | + t.Cleanup(cancelAlloc) |
| 83 | + |
| 84 | + // The budget covers browser startup plus the refresh cycle the test waits |
| 85 | + // out; without it a wedged browser would hang until the package timeout. |
| 86 | + browserCtx, cancelBrowser := chromedp.NewContext(allocCtx) |
| 87 | + t.Cleanup(cancelBrowser) |
| 88 | + ctx, cancelTimeout := context.WithTimeout(browserCtx, 90*time.Second) |
| 89 | + t.Cleanup(cancelTimeout) |
| 90 | + |
| 91 | + const ( |
| 92 | + jobRow = `#jobs tbody tr` |
| 93 | + historyDetails = `#history tbody details` |
| 94 | + historySummary = `#history tbody details summary` |
| 95 | + ) |
| 96 | + |
| 97 | + // The history table renders oldest run first, so the LAST output is the |
| 98 | + // newest one - and the newest is the furthest from eviction. Expanding the |
| 99 | + // first would pick the entry that is about to be dropped. |
| 100 | + // |
| 101 | + // "Last" is resolved in JS over the full node list rather than with a CSS |
| 102 | + // positional selector: each <details> sits alone in its own <td>, so |
| 103 | + // `details:last-of-type` matches every one of them and querySelector then |
| 104 | + // returns the FIRST. That mismatch - click the newest row, inspect the |
| 105 | + // oldest - is exactly what broke this test once already. |
| 106 | + // |
| 107 | + // Locating and clicking happen in one expression so no refresh can slip |
| 108 | + // between them and re-render the node under us. The trade-off is a |
| 109 | + // synthetic click instead of a CDP input event; on a <summary> that still |
| 110 | + // runs the browser's native toggle, which is the behavior under test. |
| 111 | + const expandNewest = `(() => { |
| 112 | + const all = document.querySelectorAll('` + historyDetails + `'); |
| 113 | + const d = all[all.length - 1]; |
| 114 | + if (!d) return ''; |
| 115 | + d.querySelector('summary').click(); |
| 116 | + return d.dataset.key || '` + noKeyMarker + `'; |
| 117 | + })()` |
| 118 | + const newestIsOpen = `(() => { |
| 119 | + const all = document.querySelectorAll('` + historyDetails + `'); |
| 120 | + const d = all[all.length - 1]; |
| 121 | + return d ? d.open === true : false; |
| 122 | + })()` |
| 123 | + |
| 124 | + var openAfterClick, openAfterRefresh, stillPresent bool |
| 125 | + var keyAfterClick, keyAfterRefresh string |
| 126 | + |
| 127 | + err := chromedp.Run(ctx, |
| 128 | + chromedp.Navigate("http://"+addr+"/"), |
| 129 | + |
| 130 | + // Selecting the job opens the history panel and loads its runs. |
| 131 | + chromedp.WaitVisible(jobRow, chromedp.ByQuery), |
| 132 | + chromedp.Click(jobRow, chromedp.ByQuery), |
| 133 | + chromedp.WaitVisible(historySummary, chromedp.ByQuery), |
| 134 | + |
| 135 | + // Wait for several runs before interacting. A single-row history would |
| 136 | + // make "oldest" and "newest" the same element and hide selector bugs |
| 137 | + // locally that only surface on a slower runner. |
| 138 | + chromedp.Poll(`document.querySelectorAll('`+historyDetails+`').length >= 3`, |
| 139 | + nil, chromedp.WithPollingTimeout(30*time.Second)), |
| 140 | + |
| 141 | + // Expand the newest run's output and note which execution it is, so we |
| 142 | + // can find the same one after the refresh. |
| 143 | + chromedp.Evaluate(expandNewest, &keyAfterClick), |
| 144 | + chromedp.Evaluate(newestIsOpen, &openAfterClick), |
| 145 | + ) |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("driving the web UI failed: %v", err) |
| 148 | + } |
| 149 | + if keyAfterClick == "" { |
| 150 | + t.Fatalf("no run with output found in the history table; nothing to expand") |
| 151 | + } |
| 152 | + if !openAfterClick { |
| 153 | + t.Fatalf("output did not expand on click; the test cannot observe the refresh behavior") |
| 154 | + } |
| 155 | + |
| 156 | + // Outlast a full refresh cycle. The daemon keeps firing the job, so this |
| 157 | + // also covers the harder case: new runs arrive and the expanded row is no |
| 158 | + // longer the newest one. |
| 159 | + // |
| 160 | + // Presence is read separately from openness. Without that split, a row that |
| 161 | + // legitimately aged out of the capped history is indistinguishable from one |
| 162 | + // the refresh collapsed - the two need different fixes, and conflating them |
| 163 | + // once already sent this test chasing the wrong bug. |
| 164 | + err = chromedp.Run(ctx, |
| 165 | + chromedp.Sleep(uiRefreshInterval+2*time.Second), |
| 166 | + chromedp.Evaluate(selectorForKey(keyAfterClick, historyDetails)+` !== null`, &stillPresent), |
| 167 | + chromedp.Evaluate(selectorForKey(keyAfterClick, historyDetails)+`?.open === true`, &openAfterRefresh), |
| 168 | + chromedp.Evaluate(`(() => { |
| 169 | + const all = document.querySelectorAll('`+historyDetails+`'); |
| 170 | + const d = all[all.length - 1]; |
| 171 | + return d ? (d.dataset.key || '') : ''; |
| 172 | + })()`, &keyAfterRefresh), |
| 173 | + ) |
| 174 | + if err != nil { |
| 175 | + t.Fatalf("re-reading the expanded output after a refresh failed: %v", err) |
| 176 | + } |
| 177 | + |
| 178 | + if !stillPresent { |
| 179 | + t.Fatalf("execution %q dropped out of the history table within %s, so the test could not "+ |
| 180 | + "observe the refresh behavior — the job cadence is outrunning the history limit, "+ |
| 181 | + "not a regression of issue #764", keyAfterClick, uiRefreshInterval+2*time.Second) |
| 182 | + } |
| 183 | + |
| 184 | + if !openAfterRefresh { |
| 185 | + t.Fatalf("expanded job output collapsed on its own across the %s refresh cycle "+ |
| 186 | + "(execution %q is still listed, just no longer open); regression of issue #764", |
| 187 | + uiRefreshInterval, keyAfterClick) |
| 188 | + } |
| 189 | + |
| 190 | + // Not an assertion, just context: whether the row we kept open was still |
| 191 | + // the newest one tells us which variant was exercised. |
| 192 | + if keyAfterRefresh != keyAfterClick { |
| 193 | + t.Logf("history advanced during the test (newest run is now %q, kept %q open) — "+ |
| 194 | + "the expanded output survived a re-render that also reordered rows", |
| 195 | + keyAfterRefresh, keyAfterClick) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +// selectorForKey builds a JS expression that finds the <details> belonging to |
| 200 | +// one specific execution. |
| 201 | +// |
| 202 | +// A build without the fix emits no data-key at all (noKeyMarker), so there is |
| 203 | +// nothing to look the row up by. It falls back to the last output — the same |
| 204 | +// one that was expanded — so the failure is reported as "collapsed" rather |
| 205 | +// than as a row that vanished from the history. |
| 206 | +func selectorForKey(key, fallbackSelector string) string { |
| 207 | + if key == noKeyMarker { |
| 208 | + return `(() => { |
| 209 | + const all = document.querySelectorAll('` + fallbackSelector + `'); |
| 210 | + return all[all.length - 1] || null; |
| 211 | + })()` |
| 212 | + } |
| 213 | + return fmt.Sprintf(`document.querySelector('#history tbody details[data-key="%s"]')`, key) |
| 214 | +} |
| 215 | + |
| 216 | +// reserveLoopbackAddr asks the kernel for a free loopback port and returns it |
| 217 | +// as host:port. The listener is closed before returning, so there is a small |
| 218 | +// race window — acceptable here, and far better than a hardcoded port that |
| 219 | +// would make parallel e2e runs collide. |
| 220 | +func reserveLoopbackAddr(t *testing.T) string { |
| 221 | + t.Helper() |
| 222 | + ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 223 | + if err != nil { |
| 224 | + t.Fatalf("reserve loopback port: %v", err) |
| 225 | + } |
| 226 | + addr := ln.Addr().String() |
| 227 | + if err := ln.Close(); err != nil { |
| 228 | + t.Fatalf("close port reservation: %v", err) |
| 229 | + } |
| 230 | + return addr |
| 231 | +} |
| 232 | + |
| 233 | +// chromeExecutable returns the path to a usable Chrome/Chromium binary, or "" |
| 234 | +// when none is installed. Mirrors dockerAvailable's skip-cleanly contract: a |
| 235 | +// developer machine without a browser should not fail the suite, while CI |
| 236 | +// runners (ubuntu-latest ships Chrome) exercise the test for real. |
| 237 | +// |
| 238 | +// OFELIA_E2E_CHROME overrides the search for developers whose browser is not on |
| 239 | +// PATH — e.g. one managed by a browser-automation toolchain in a cache dir. |
| 240 | +func chromeExecutable() string { |
| 241 | + if fromEnv := os.Getenv("OFELIA_E2E_CHROME"); fromEnv != "" { |
| 242 | + if _, err := os.Stat(fromEnv); err == nil { |
| 243 | + return fromEnv |
| 244 | + } |
| 245 | + } |
| 246 | + for _, candidate := range []string{ |
| 247 | + "google-chrome", |
| 248 | + "google-chrome-stable", |
| 249 | + "chromium", |
| 250 | + "chromium-browser", |
| 251 | + } { |
| 252 | + if path, err := exec.LookPath(candidate); err == nil { |
| 253 | + return path |
| 254 | + } |
| 255 | + } |
| 256 | + return "" |
| 257 | +} |
0 commit comments