feat: add local cdp startup and external association

This commit is contained in:
QiuSW
2026-07-25 15:53:31 +08:00
parent 972c637716
commit 4063b851eb
17 changed files with 845 additions and 158 deletions
+25 -12
View File
@@ -15,6 +15,7 @@ func TestRealChromiumSmoke(t *testing.T) {
ctx := context.Background()
discoverer := NewDiscoverer()
launcher := NewOSLauncher()
cdp := NewCDPInspector()
type running struct {
kind domain.BrowserKind
dir string
@@ -31,7 +32,24 @@ func TestRealChromiumSmoke(t *testing.T) {
t.Fatalf("resolve %s: %v", kind, err)
}
dir := t.TempDir()
handle, err := launcher.Start(ctx, domain.LaunchSpec{Kind: kind, Executable: executable, UserDataDir: dir, TargetURL: "https://example.com", Headless: true, ExtraArgs: []string{"--disable-gpu"}})
var handle ProcessHandle
t.Cleanup(func() {
if handle == nil {
return
}
if err := launcher.StopProfile(ctx, dir, true); err != nil && !errors.Is(err, domain.ErrInstanceNotFound) {
t.Errorf("cleanup force stop %s: %v", kind, err)
return
}
waitCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
_, _ = handle.Wait(waitCtx)
cancel()
})
port, err := cdp.FindAvailableRemoteDebugPort(ctx, domain.DefaultRemoteDebugPort)
if err != nil {
t.Fatalf("allocate %s CDP port: %v", kind, err)
}
handle, err = launcher.Start(ctx, domain.LaunchSpec{Kind: kind, Executable: executable, UserDataDir: dir, RemoteDebugPort: port, TargetURL: "https://example.com", Headless: true, ExtraArgs: []string{"--disable-gpu"}})
if err != nil {
t.Fatalf("start %s: %v", kind, err)
}
@@ -39,6 +57,12 @@ func TestRealChromiumSmoke(t *testing.T) {
if handle.PID() <= 0 {
t.Fatalf("%s returned invalid PID %d", kind, handle.PID())
}
endpointCtx, cancelEndpoint := context.WithTimeout(ctx, 10*time.Second)
endpoint, endpointErr := cdp.WaitForRemoteDebugPort(endpointCtx, kind, port)
cancelEndpoint()
if endpointErr != nil || endpoint.Port != port {
t.Fatalf("%s CDP endpoint = %+v, %v; want port %d", kind, endpoint, endpointErr, port)
}
use, err := launcher.InspectProfile(ctx, dir)
if err != nil || !use.Occupied || use.PID != handle.PID() {
t.Fatalf("%s registry = %+v, %v", kind, use, err)
@@ -62,15 +86,4 @@ func TestRealChromiumSmoke(t *testing.T) {
if processes[0].handle.PID() == processes[1].handle.PID() {
t.Fatalf("Chrome and Edge shared PID %d", processes[0].handle.PID())
}
for _, item := range processes {
if err := launcher.StopProfile(ctx, item.dir, true); err != nil {
t.Fatalf("force stop %s: %v", item.kind, err)
}
waitCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
code, waitErr := item.handle.Wait(waitCtx)
cancel()
if waitErr != nil {
t.Logf("%s force stop returned expected process wait error: code=%d error=%v", item.kind, code, waitErr)
}
}
}