Skip to content

Conversation

@seanmcguire12
Copy link
Member

@seanmcguire12 seanmcguire12 commented Dec 19, 2025

why

  • we need coverage for utilities that are used when parsing the response from DOM.getDocument

what changed

  • exported helpers in domTree.ts for testing
  • added tests for shouldExpandNode(), mergeDomNodes(), collectDomTraversalTargets(), and findNodeByBackendId()

test plan

  • this is it

Summary by cubic

Add unit tests for synchronous DOM tree helpers used when parsing DOM.getDocument. Supports STG-1094 by exporting helpers and adding tests for shouldExpandNode, mergeDomNodes, collectDomTraversalTargets, and findNodeByBackendId.

Written for commit 078e98c. Summary will update automatically on new commits.

@changeset-bot
Copy link

changeset-bot bot commented Dec 19, 2025

⚠️ No Changeset found

Latest commit: 078e98c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 19, 2025

Greptile Summary

Adds unit test coverage for four synchronous DOM node helper functions used when parsing DOM.getDocument responses. The changes export existing helper functions (shouldExpandNode, mergeDomNodes, collectDomTraversalTargets, findNodeByBackendId) for testing without modifying their implementation.

Test Coverage:

  • shouldExpandNode(): Validates logic for detecting truncated children in DOM nodes
  • mergeDomNodes(): Tests merging expanded node data and preserving original structures when source omits fields
  • collectDomTraversalTargets(): Verifies collection of children, shadow roots, and content documents
  • findNodeByBackendId(): Tests stack-based traversal through children and shadow roots

Minor Observation:
The test for findNodeByBackendId() correctly reflects the implementation which only traverses children and shadowRoots, not contentDocument nodes (unlike collectDomTraversalTargets() which includes all three).

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • Simple export changes and comprehensive unit tests for existing utilities with no behavioral changes to production code
  • No files require special attention

Important Files Changed

Filename Overview
packages/core/lib/v3/understudy/a11y/snapshot/domTree.ts Exported 4 helper functions for testing without changing implementation
packages/core/tests/snapshot-dom-tree-utils.test.ts Comprehensive test coverage for DOM helper functions with good edge case handling

Sequence Diagram

sequenceDiagram
    participant Test as Test Suite
    participant Helper as DOM Helper Functions
    participant Node as Protocol.DOM.Node

    Note over Test,Node: shouldExpandNode Testing
    Test->>Helper: shouldExpandNode(node)
    Helper->>Node: Check childNodeCount
    Helper->>Node: Check children.length
    Helper-->>Test: true/false (needs expansion?)

    Note over Test,Node: mergeDomNodes Testing
    Test->>Helper: mergeDomNodes(target, source)
    Helper->>Node: Update childNodeCount
    Helper->>Node: Update children array
    Helper->>Node: Update shadowRoots
    Helper->>Node: Update contentDocument
    Helper-->>Test: void (target mutated)

    Note over Test,Node: collectDomTraversalTargets Testing
    Test->>Helper: collectDomTraversalTargets(node)
    Helper->>Node: Collect children
    Helper->>Node: Collect shadowRoots
    Helper->>Node: Collect contentDocument
    Helper-->>Test: Array of target nodes

    Note over Test,Node: findNodeByBackendId Testing
    Test->>Helper: findNodeByBackendId(root, backendId)
    Helper->>Node: Stack-based traversal
    Helper->>Node: Check children
    Helper->>Node: Check shadowRoots
    Helper-->>Test: Found node or undefined
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +28 to +45
describe("shouldExpandNode", () => {
it("returns true when declared children exceed realized children", () => {
const node = makeNode({
childNodeCount: 2,
children: [makeNode()],
});
expect(shouldExpandNode(node)).toBe(true);
});

it("returns false when all declared children are realized", () => {
const child = makeNode();
const node = makeNode({
childNodeCount: 1,
children: [child],
});
expect(shouldExpandNode(node)).toBe(false);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider edge case: node with childNodeCount: 0 and no children array (should return false)

Suggested change
describe("shouldExpandNode", () => {
it("returns true when declared children exceed realized children", () => {
const node = makeNode({
childNodeCount: 2,
children: [makeNode()],
});
expect(shouldExpandNode(node)).toBe(true);
});
it("returns false when all declared children are realized", () => {
const child = makeNode();
const node = makeNode({
childNodeCount: 1,
children: [child],
});
expect(shouldExpandNode(node)).toBe(false);
});
});
describe("shouldExpandNode", () => {
it("returns true when declared children exceed realized children", () => {
const node = makeNode({
childNodeCount: 2,
children: [makeNode()],
});
expect(shouldExpandNode(node)).toBe(true);
});
it("returns false when all declared children are realized", () => {
const child = makeNode();
const node = makeNode({
childNodeCount: 1,
children: [child],
});
expect(shouldExpandNode(node)).toBe(false);
});
it("returns false when node has no children", () => {
const node = makeNode({
childNodeCount: 0,
});
expect(shouldExpandNode(node)).toBe(false);
});
});
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/core/tests/snapshot-dom-tree-utils.test.ts
Line: 28:45

Comment:
**style:** Consider edge case: node with `childNodeCount: 0` and no `children` array (should return false)

```suggestion
describe("shouldExpandNode", () => {
  it("returns true when declared children exceed realized children", () => {
    const node = makeNode({
      childNodeCount: 2,
      children: [makeNode()],
    });
    expect(shouldExpandNode(node)).toBe(true);
  });

  it("returns false when all declared children are realized", () => {
    const child = makeNode();
    const node = makeNode({
      childNodeCount: 1,
      children: [child],
    });
    expect(shouldExpandNode(node)).toBe(false);
  });

  it("returns false when node has no children", () => {
    const node = makeNode({
      childNodeCount: 0,
    });
    expect(shouldExpandNode(node)).toBe(false);
  });
});
```

How can I resolve this? If you propose a fix, please make it concise.

@seanmcguire12 seanmcguire12 merged commit 962b1fa into main Dec 19, 2025
33 of 34 checks passed
@seanmcguire12 seanmcguire12 changed the title [chore]: add tests for synchronous DOM node helpers [test]: add tests for synchronous DOM node helpers Dec 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants