| 1 |
// Net-new helper in the rebuild — mirrors DOMHighlighter's eligibility |
| 2 |
// gates so the Ask AI pill only surfaces on blocks the agent's selector |
| 3 |
// tool would have accepted. The existing |
| 4 |
// src/QuickEdit/lib/__tests__/hover-bar.test.js (misnamed; it actually |
| 5 |
// covers this module) pins the happy path; this file fills the gaps |
| 6 |
// (null safety, the three TAGGED_INNER_SEL selectors, post-* prefix). |
| 7 |
|
| 8 |
import { isAgentEligibleForTarget } from '@quick-edit/lib/agent-gate'; |
| 9 |
|
| 10 |
const makeTarget = (el, overrides = {}) => ({ |
| 11 |
el, |
| 12 |
blockType: 'core/group', |
| 13 |
source: { kind: 'post', id: 1 }, |
| 14 |
...overrides, |
| 15 |
}); |
| 16 |
|
| 17 |
describe('isAgentEligibleForTarget — null safety', () => { |
| 18 |
it('returns false for a null target', () => { |
| 19 |
expect(isAgentEligibleForTarget(null)).toBe(false); |
| 20 |
}); |
| 21 |
|
| 22 |
it('returns false for an undefined target', () => { |
| 23 |
expect(isAgentEligibleForTarget(undefined)).toBe(false); |
| 24 |
}); |
| 25 |
|
| 26 |
it('returns false when target.el is missing', () => { |
| 27 |
expect(isAgentEligibleForTarget({ blockType: 'core/paragraph' })).toBe( |
| 28 |
false, |
| 29 |
); |
| 30 |
}); |
| 31 |
|
| 32 |
it('returns false when target.el has no classList', () => { |
| 33 |
expect(isAgentEligibleForTarget(makeTarget({}))).toBe(false); |
| 34 |
}); |
| 35 |
}); |
| 36 |
|
| 37 |
describe('isAgentEligibleForTarget — IGNORED_CLASS_RX', () => { |
| 38 |
it('rejects any wp-block-post-* class (the prefix is wildcarded)', () => { |
| 39 |
for (const cls of [ |
| 40 |
'wp-block-post-title', |
| 41 |
'wp-block-post-author', |
| 42 |
'wp-block-post-date', |
| 43 |
'wp-block-post-terms', |
| 44 |
'wp-block-post-excerpt', |
| 45 |
'wp-block-post-featured-image', |
| 46 |
]) { |
| 47 |
const el = document.createElement('div'); |
| 48 |
el.classList.add(cls); |
| 49 |
expect(isAgentEligibleForTarget(makeTarget(el))).toBe(false); |
| 50 |
} |
| 51 |
}); |
| 52 |
|
| 53 |
it('rejects on any one matching class even when others would pass', () => { |
| 54 |
const el = document.createElement('div'); |
| 55 |
el.classList.add('wp-block-group'); |
| 56 |
el.classList.add('wp-block-spacer'); |
| 57 |
expect(isAgentEligibleForTarget(makeTarget(el))).toBe(false); |
| 58 |
}); |
| 59 |
}); |
| 60 |
|
| 61 |
describe('isAgentEligibleForTarget — inner-block counting', () => { |
| 62 |
const addInner = (parent, attr) => { |
| 63 |
const child = document.createElement('div'); |
| 64 |
child.setAttribute(attr, '1'); |
| 65 |
parent.appendChild(child); |
| 66 |
}; |
| 67 |
|
| 68 |
it('counts data-extendify-part-block-id descendants', () => { |
| 69 |
const el = document.createElement('div'); |
| 70 |
el.classList.add('wp-block-group'); |
| 71 |
for (let i = 0; i < 51; i++) addInner(el, 'data-extendify-part-block-id'); |
| 72 |
expect(isAgentEligibleForTarget(makeTarget(el))).toBe(false); |
| 73 |
}); |
| 74 |
|
| 75 |
it('counts .wp-block-navigation descendants', () => { |
| 76 |
const el = document.createElement('div'); |
| 77 |
el.classList.add('wp-block-group'); |
| 78 |
for (let i = 0; i < 51; i++) { |
| 79 |
const nav = document.createElement('nav'); |
| 80 |
nav.classList.add('wp-block-navigation'); |
| 81 |
el.appendChild(nav); |
| 82 |
} |
| 83 |
expect(isAgentEligibleForTarget(makeTarget(el))).toBe(false); |
| 84 |
}); |
| 85 |
|
| 86 |
it('sums across the three tagged-inner selectors', () => { |
| 87 |
const el = document.createElement('div'); |
| 88 |
el.classList.add('wp-block-group'); |
| 89 |
for (let i = 0; i < 20; i++) addInner(el, 'data-extendify-agent-block-id'); |
| 90 |
for (let i = 0; i < 20; i++) addInner(el, 'data-extendify-part-block-id'); |
| 91 |
for (let i = 0; i < 11; i++) { |
| 92 |
const nav = document.createElement('nav'); |
| 93 |
nav.classList.add('wp-block-navigation'); |
| 94 |
el.appendChild(nav); |
| 95 |
} |
| 96 |
expect(isAgentEligibleForTarget(makeTarget(el))).toBe(false); |
| 97 |
}); |
| 98 |
|
| 99 |
it('accepts at exactly 50 inner tagged blocks (boundary)', () => { |
| 100 |
const el = document.createElement('div'); |
| 101 |
el.classList.add('wp-block-group'); |
| 102 |
for (let i = 0; i < 50; i++) addInner(el, 'data-extendify-agent-block-id'); |
| 103 |
expect(isAgentEligibleForTarget(makeTarget(el))).toBe(true); |
| 104 |
}); |
| 105 |
}); |
| 106 |
|