1- // browser_helper_test.go
21package browser_test
32
43import (
@@ -10,7 +9,6 @@ import (
109 "testing"
1110 "time"
1211
13- "github.com/stretchr/testify/require"
1412 "go.uber.org/zap"
1513
1614 "github.com/xkilldash9x/scalpel-cli/internal/browser"
@@ -20,37 +18,63 @@ import (
2018)
2119
2220var (
23- sharedManager * browser.Manager
24- testLogger * zap.Logger
21+ testLogger * zap.Logger
22+ testManager * browser.Manager
23+ testConfig * config.Config
2524)
2625
27- // testFixture holds all the necessary components for a single, isolated browser test.
26+ // testFixture holds components for a single, isolated browser test.
2827type testFixture struct {
2928 Session * browser.AnalysisContext
3029 Config * config.Config
3130}
3231
33- // TestMain sets up the shared browser manager for all tests in the package
34- // and guarantees its shutdown after all tests have completed.
32+ // TestMain sets up a SINGLE, shared browser manager for all tests (Principle 1).
3533func TestMain (m * testing.M ) {
3634 var err error
3735 testLogger = getTestLogger ()
38- // Use a background context as the manager's lifecycle is for the whole package.
39- sharedManager , err = browser .NewManager (context .Background (), testLogger , 4 )
36+
37+ const testConcurrency = 4
38+
39+ // Configuration for the tests, matching the updated config.BrowserConfig.
40+ browserCfg := config.BrowserConfig {
41+ Headless : true ,
42+ DisableCache : true ,
43+ Concurrency : testConcurrency ,
44+ Humanoid : humanoid .DefaultConfig (),
45+ Debug : false , // Set to true for verbose CDP logs (Principle 5).
46+ }
47+
48+ testConfig = & config.Config {
49+ Browser : browserCfg ,
50+ Network : config.NetworkConfig {
51+ CaptureResponseBodies : true ,
52+ NavigationTimeout : 30 * time .Second , // Matching the updated config.NetworkConfig.
53+ // PostLoadWait is deprecated in the refactored code (Principle 2).
54+ },
55+ }
56+
57+ // Create the manager once. Use context.Background() as the initCtx for the test suite lifetime.
58+ // Principle 3: Timeout for initialization.
59+ initCtx , cancel := context .WithTimeout (context .Background (), 45 * time .Second )
60+ defer cancel ()
61+
62+ // Call the updated NewManager function signature.
63+ testManager , err = browser .NewManager (initCtx , testLogger , browserCfg )
4064 if err != nil {
41- testLogger .Fatal ("Failed to create shared browser manager for tests " , zap .Error (err ))
65+ testLogger .Fatal ("Failed to initialize browser manager for test suite " , zap .Error (err ))
4266 }
4367
44- // m. Run() executes all tests in the package .
68+ // Run all the tests .
4569 code := m .Run ()
4670
47- // This block runs AFTER all tests are finished, including parallel ones.
48- testLogger .Info ("Shutting down shared browser manager after tests." )
49- shutdownCtx , cancel := context .WithTimeout (context .Background (), 15 * time .Second )
50- defer cancel ()
51- if err := sharedManager .Shutdown (shutdownCtx ); err != nil {
52- testLogger .Error ("Error during shared manager shutdown" , zap .Error (err ))
71+ // Shut down the manager (Principle 4).
72+ shutdownCtx , cancelShutdown := context .WithTimeout (context .Background (), 15 * time .Second )
73+ defer cancelShutdown ()
74+ if err := testManager .Shutdown (shutdownCtx ); err != nil {
75+ testLogger .Error ("Error during test manager shutdown" , zap .Error (err ))
5376 }
77+
5478 os .Exit (code )
5579}
5680
@@ -63,49 +87,51 @@ func getTestLogger() *zap.Logger {
6387 return logger
6488}
6589
66- // newTestFixture creates a fully initialized AnalysisContext for a test .
90+ // newTestFixture acquires a new session from the shared manager .
6791func newTestFixture (t * testing.T ) (* testFixture , func ()) {
6892 t .Helper ()
6993
70- cfg := & config.Config {
71- Browser : config.BrowserConfig {
72- Headless : true ,
73- DisableCache : true ,
74- Humanoid : humanoid .DefaultConfig (),
75- },
76- Network : config.NetworkConfig {
77- CaptureResponseBodies : true ,
78- PostLoadWait : 250 * time .Millisecond ,
79- },
80- }
81- persona := stealth .DefaultPersona
82- testCtx , cancelTest := context .WithTimeout (context .Background (), 30 * time .Second )
94+ // Principle 3: Timeout for acquiring and initializing the session.
95+ sessionCtx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
96+
97+ session , err := testManager .NewAnalysisContext (
98+ sessionCtx ,
99+ testConfig ,
100+ stealth .DefaultPersona ,
101+ "" , // No taint template
102+ "" , // No taint config
103+ )
83104
84- session , err := sharedManager .NewAnalysisContext (testCtx , cfg , persona , "" , "" )
85- require .NoError (t , err , "Failed to initialize session from manager for test fixture" )
105+ if err != nil {
106+ cancel ()
107+ t .Fatalf ("Failed to create new analysis session: %v" , err )
108+ }
86109
87110 fixture := & testFixture {
88111 Session : session ,
89- Config : cfg ,
112+ Config : testConfig ,
90113 }
91114
92115 cleanup := func () {
93- session .Close (context .Background ())
94- cancelTest ()
116+ // Use a new context for cleanup in case the sessionCtx was cancelled (Principle 4).
117+ closeCtx , closeCancel := context .WithTimeout (context .Background (), 10 * time .Second )
118+ defer closeCancel ()
119+ session .Close (closeCtx )
120+ cancel ()
95121 }
96122
97123 return fixture , cleanup
98124}
99125
100- // createTestServer creates an httptest.Server for dynamic content tests .
126+ // createTestServer creates an httptest.Server.
101127func createTestServer (t * testing.T , handler http.Handler ) * httptest.Server {
102128 t .Helper ()
103129 server := httptest .NewServer (handler )
104130 t .Cleanup (server .Close )
105131 return server
106132}
107133
108- // createStaticTestServer creates an httptest.Server for serving simple, static HTML.
134+ // createStaticTestServer creates an httptest.Server for static HTML.
109135func createStaticTestServer (t * testing.T , htmlContent string ) * httptest.Server {
110136 t .Helper ()
111137 return createTestServer (t , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
0 commit comments