Skip to content

Refactor WebKitClient into protocol, target, command, script, and event modules #706

@shaun0927

Description

@shaun0927

Tracked by #713.

Background

src/webkit/client.ts is a central 1,316-line module that mixes protocol transport, target multiplexing, browser commands, DOM script templates, event adapters, and errors. This is maintainability risk for a core automation path.

Objective

Split WebKitClient internals into focused modules while preserving the public BrowserBackend-facing facade.

Scope

  • Extract protocol transport without changing public methods.
  • Extract DOM script builders used by browser commands.
  • Extract typed event adapters.
  • Keep WebKitClient as compatibility facade during migration.

Target files and paths

  • src/webkit/client.ts
  • new src/webkit/protocol-transport.ts
  • new src/webkit/target-session.ts
  • new src/webkit/browser-commands.ts
  • new src/webkit/dom-scripts.ts
  • new src/webkit/events.ts
  • new src/webkit/errors.ts
  • tests/unit/webkit*.test.ts

Implementation steps

  1. Create error module and update imports.
  2. Extract pure DOM script builders with tests.
  3. Extract protocol send/pending-request handling behind an interface.
  4. Move target lifecycle state into a target session module.
  5. Keep facade methods and run existing tests after each extraction.

Acceptance criteria

  • Public WebKitClient constructor and BrowserBackend methods remain compatible.
  • Extracted modules have focused tests or existing tests covering their behavior.
  • No CDP or Playwright abstractions are introduced.
  • File size of client.ts is materially reduced without a behavior rewrite.

Verification

  • npm run lint -- --quiet
  • Relevant WebKit unit tests
  • npm run build:cli
  • Manual smoke: navigate, screenshot, javascript, inspect against Safari simulator.

Dependencies / recommended order

Run after WebKit hot-path tests exist. Split into multiple PRs; do not attempt the full extraction in one PR.

Risks and cautions

  • This is a refactor; behavior changes need separate issues.
  • Avoid broad renames that make review difficult.
  • Keep WebKit protocol constraints from CLAUDE.md.

Non-goals

  • Do not change tool names or schemas.
  • Do not migrate to Playwright/CDP.
  • Do not combine with HTTP security work.

Ambiguity reducers

  • “Facade” means external imports can still use WebKitClient from the same path until a deprecation plan exists.
  • Each extraction PR must have a small, reviewable diff.
  • Pure script builders should be testable without simulator.

Validity review

Proceed after higher-priority safety and speed issues. The work is valid because WebKitClient is a core change surface.

Implementation detail refinements

  • Use adapter interfaces to avoid circular dependencies.
  • Move tests with the code where practical.
  • Track line-count reduction as a secondary metric, not the success criterion.

Verification checklist reinforcement

  • Existing WebKit tests pass after each extraction.
  • At least one pure module has direct unit coverage.
  • Manual Safari smoke passes at the end of the final PR.
  • No public API break is introduced.

Metadata

Metadata

Assignees

No one assigned

    Labels

    architectureArchitecture changes and improvementsautomation-roadmapOpenSafari automation roadmap work itemstech-debtTechnical debt and maintainability

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions