11package webui
22
33import (
4+ "html/template"
45 "net/http"
56 "net/http/httptest"
67 "os"
@@ -10,30 +11,48 @@ import (
1011 "github.com/recinq/wave/internal/onboarding"
1112)
1213
14+ // minimalBridgeTemplates returns a template map with a stub bridge.html so
15+ // handleBridge can be called without the full template set.
16+ func minimalBridgeTemplates () map [string ]* template.Template {
17+ m := map [string ]* template.Template {}
18+ m ["templates/bridge.html" ] = template .Must (
19+ template .New ("templates/bridge.html" ).Parse (`<!doctype html><html><body><p>Bridge</p></body></html>` ),
20+ )
21+ return m
22+ }
23+
1324// TestHandleRoot drives Server.handleRoot directly via httptest, asserting
1425// that GET / branches on the presence of .agents/.onboarding-done under the
15- // configured repoDir.
26+ // configured repoDir. When sentinel is present, renders bridge (200).
27+ // When sentinel is missing, redirects to /onboard.
1628func TestHandleRoot (t * testing.T ) {
1729 tests := []struct {
18- name string
19- writeSentinel bool
20- wantLocation string
21- repoDirOverlay func (t * testing.T , dir string ) string
30+ name string
31+ writeSentinel bool
32+ wantCode int
33+ wantLocation string
34+ repoDir string
35+ // repoDirOverlay, when non-nil, runs before each test to override the
36+ // repoDir. Returns the repoDir to use (empty = use tmp dir directly).
37+ repoDirOverlay func (t * testing.T , tmp string ) string
2238 }{
2339 {
24- name : "sentinel present redirects to /work " ,
40+ name : "sentinel present renders bridge " ,
2541 writeSentinel : true ,
26- wantLocation : "/work" ,
42+ wantCode : http .StatusOK ,
43+ wantLocation : "" ,
2744 },
2845 {
2946 name : "sentinel missing redirects to /onboard" ,
3047 writeSentinel : false ,
31- wantLocation : "/onboard" ,
48+ wantCode : http .StatusFound ,
49+ wantLocation : "/onboard" ,
3250 },
3351 {
3452 name : "empty repoDir treated as cwd and missing sentinel" ,
3553 writeSentinel : false ,
36- wantLocation : "/onboard" ,
54+ wantCode : http .StatusFound ,
55+ wantLocation : "/onboard" ,
3756 repoDirOverlay : func (t * testing.T , _ string ) string {
3857 t .Helper ()
3958 cwd := t .TempDir ()
@@ -69,20 +88,20 @@ func TestHandleRoot(t *testing.T) {
6988
7089 srv := & Server {
7190 runtime : serverRuntime {repoDir : repoDir },
91+ assets : serverAssets {templates : minimalBridgeTemplates ()},
7292 }
7393
7494 req := httptest .NewRequest ("GET" , "/" , nil )
7595 rec := httptest .NewRecorder ()
7696 srv .handleRoot (rec , req )
7797
78- if rec .Code != http . StatusFound {
79- t .Fatalf ("expected 302 , got %d" , rec .Code )
98+ if rec .Code != tc . wantCode {
99+ t .Fatalf ("expected %d , got %d" , tc . wantCode , rec .Code )
80100 }
81- if got := rec .Header ().Get ("Location" ); got != tc .wantLocation {
82- t .Fatalf ("Location: want %q, got %q" , tc .wantLocation , got )
83- }
84- if rec .Header ().Get ("Location" ) == "/runs" {
85- t .Fatalf ("legacy /runs redirect leaked through handleRoot" )
101+ if tc .wantLocation != "" {
102+ if got := rec .Header ().Get ("Location" ); got != tc .wantLocation {
103+ t .Fatalf ("Location: want %q, got %q" , tc .wantLocation , got )
104+ }
86105 }
87106 })
88107 }
0 commit comments