@adsim/wordpress-mcp-server 3.1.0 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +564 -176
  2. package/dxt/manifest.json +93 -9
  3. package/index.js +3624 -36
  4. package/package.json +1 -1
  5. package/src/confirmationToken.js +64 -0
  6. package/src/contentAnalyzer.js +476 -0
  7. package/src/htmlParser.js +80 -0
  8. package/src/linkUtils.js +158 -0
  9. package/src/pluginDetector.js +158 -0
  10. package/src/utils/contentCompressor.js +116 -0
  11. package/src/woocommerceClient.js +88 -0
  12. package/tests/unit/contentAnalyzer.test.js +397 -0
  13. package/tests/unit/pluginDetector.test.js +167 -0
  14. package/tests/unit/tools/analyzeEeatSignals.test.js +192 -0
  15. package/tests/unit/tools/approval.test.js +251 -0
  16. package/tests/unit/tools/auditCanonicals.test.js +149 -0
  17. package/tests/unit/tools/auditHeadingStructure.test.js +150 -0
  18. package/tests/unit/tools/auditMediaSeo.test.js +123 -0
  19. package/tests/unit/tools/auditOutboundLinks.test.js +175 -0
  20. package/tests/unit/tools/auditTaxonomies.test.js +173 -0
  21. package/tests/unit/tools/contentCompressor.test.js +320 -0
  22. package/tests/unit/tools/contentIntelligence.test.js +2168 -0
  23. package/tests/unit/tools/destructive.test.js +246 -0
  24. package/tests/unit/tools/findBrokenInternalLinks.test.js +222 -0
  25. package/tests/unit/tools/findKeywordCannibalization.test.js +183 -0
  26. package/tests/unit/tools/findOrphanPages.test.js +145 -0
  27. package/tests/unit/tools/findThinContent.test.js +145 -0
  28. package/tests/unit/tools/internalLinks.test.js +283 -0
  29. package/tests/unit/tools/perTargetControls.test.js +228 -0
  30. package/tests/unit/tools/pluginIntelligence.test.js +864 -0
  31. package/tests/unit/tools/site.test.js +6 -1
  32. package/tests/unit/tools/woocommerce.test.js +344 -0
  33. package/tests/unit/tools/woocommerceIntelligence.test.js +341 -0
  34. package/tests/unit/tools/woocommerceWrite.test.js +323 -0
@@ -0,0 +1,320 @@
1
+ import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
2
+
3
+ vi.mock('node-fetch', () => ({ default: vi.fn() }));
4
+
5
+ import fetch from 'node-fetch';
6
+ import { handleToolCall, _testSetTarget } from '../../../index.js';
7
+ import { mockSuccess, mockError, getAuditLogs, makeRequest, parseResult } from '../../helpers/mockWpRequest.js';
8
+ import { stripHtml, extractLinksOnly, truncateContent, summarizePost, applyContentFormat } from '../../../src/utils/contentCompressor.js';
9
+
10
+ function call(name, args = {}) {
11
+ return handleToolCall(makeRequest(name, args));
12
+ }
13
+
14
+ let consoleSpy;
15
+ beforeEach(() => { fetch.mockReset(); consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); });
16
+ afterEach(() => { consoleSpy.mockRestore(); _testSetTarget(null); });
17
+
18
+ // =========================================================================
19
+ // Helpers
20
+ // =========================================================================
21
+
22
+ function makeWpPost(id, content = '<p>Content</p>', extras = {}) {
23
+ return {
24
+ id,
25
+ title: { rendered: `Post ${id}` },
26
+ content: { rendered: content },
27
+ excerpt: { rendered: `Excerpt ${id}` },
28
+ status: 'publish',
29
+ date: '2025-06-01',
30
+ modified: '2025-06-02',
31
+ link: `https://test.example.com/post-${id}`,
32
+ slug: `post-${id}`,
33
+ categories: [1],
34
+ tags: [2],
35
+ author: 1,
36
+ featured_media: 0,
37
+ comment_status: 'open',
38
+ meta: {},
39
+ ...extras
40
+ };
41
+ }
42
+
43
+ // =========================================================================
44
+ // Unit tests — contentCompressor.js
45
+ // =========================================================================
46
+
47
+ describe('contentCompressor — stripHtml', () => {
48
+ it('strips tags and decodes entities', () => {
49
+ const html = '<p>Hello &amp; <strong>world</strong></p>';
50
+ expect(stripHtml(html)).toBe('Hello & world');
51
+ });
52
+
53
+ it('removes script and style blocks', () => {
54
+ const html = '<p>text</p><script>alert("x")</script><style>.a{}</style><p>more</p>';
55
+ expect(stripHtml(html)).not.toContain('alert');
56
+ expect(stripHtml(html)).not.toContain('.a{}');
57
+ expect(stripHtml(html)).toContain('text');
58
+ expect(stripHtml(html)).toContain('more');
59
+ });
60
+
61
+ it('returns empty string for falsy input', () => {
62
+ expect(stripHtml('')).toBe('');
63
+ expect(stripHtml(null)).toBe('');
64
+ expect(stripHtml(undefined)).toBe('');
65
+ });
66
+ });
67
+
68
+ describe('contentCompressor — extractLinksOnly', () => {
69
+ const siteUrl = 'https://mysite.example.com';
70
+
71
+ it('extracts internal links only', () => {
72
+ const html = `<p>
73
+ <a href="https://mysite.example.com/about/">About</a>
74
+ <a href="https://external.com/page">External</a>
75
+ </p>`;
76
+ const links = extractLinksOnly(html, siteUrl);
77
+ expect(links).toHaveLength(1);
78
+ expect(links[0].text).toBe('About');
79
+ expect(links[0].href).toBe('https://mysite.example.com/about/');
80
+ });
81
+
82
+ it('handles relative links', () => {
83
+ const html = '<a href="/contact/">Contact</a>';
84
+ const links = extractLinksOnly(html, siteUrl);
85
+ expect(links).toHaveLength(1);
86
+ expect(links[0].href).toBe('https://mysite.example.com/contact/');
87
+ });
88
+
89
+ it('uses [no anchor text] for empty anchors', () => {
90
+ const html = '<a href="https://mysite.example.com/page/"></a>';
91
+ const links = extractLinksOnly(html, siteUrl);
92
+ expect(links[0].text).toBe('[no anchor text]');
93
+ });
94
+
95
+ it('returns empty array for no html', () => {
96
+ expect(extractLinksOnly('', siteUrl)).toEqual([]);
97
+ expect(extractLinksOnly(null, siteUrl)).toEqual([]);
98
+ });
99
+ });
100
+
101
+ describe('contentCompressor — truncateContent', () => {
102
+ it('does not truncate short content', () => {
103
+ expect(truncateContent('short text', 100)).toBe('short text');
104
+ });
105
+
106
+ it('truncates at word boundary', () => {
107
+ const text = 'word1 word2 word3 word4 word5';
108
+ const result = truncateContent(text, 15);
109
+ expect(result).toContain('[truncated:');
110
+ expect(result).not.toContain('word4');
111
+ });
112
+
113
+ it('returns empty string for falsy input', () => {
114
+ expect(truncateContent('', 100)).toBe('');
115
+ expect(truncateContent(null, 100)).toBe('');
116
+ });
117
+
118
+ it('no limit when maxChars is 0', () => {
119
+ const longText = 'a'.repeat(50000);
120
+ expect(truncateContent(longText, 0)).toBe(longText);
121
+ });
122
+ });
123
+
124
+ describe('contentCompressor — summarizePost', () => {
125
+ it('filters to requested fields only', () => {
126
+ const post = { id: 1, title: 'T', content: 'C', slug: 's', status: 'publish', date: '2025-01-01' };
127
+ const result = summarizePost(post, ['id', 'title']);
128
+ expect(Object.keys(result)).toEqual(['id', 'title']);
129
+ expect(result.id).toBe(1);
130
+ });
131
+
132
+ it('returns full post when fields is empty or null', () => {
133
+ const post = { id: 1, title: 'T', content: 'C' };
134
+ expect(summarizePost(post, [])).toBe(post);
135
+ expect(summarizePost(post, null)).toBe(post);
136
+ });
137
+
138
+ it('ignores non-existent fields gracefully', () => {
139
+ const post = { id: 1, title: 'T' };
140
+ const result = summarizePost(post, ['id', 'nonexistent']);
141
+ expect(result).toEqual({ id: 1 });
142
+ });
143
+ });
144
+
145
+ describe('contentCompressor — applyContentFormat', () => {
146
+ const siteUrl = 'https://mysite.example.com';
147
+
148
+ it('html mode — truncates long content', () => {
149
+ const post = { id: 1, content: 'a'.repeat(20000) };
150
+ const result = applyContentFormat(post, 'html', siteUrl, 15000);
151
+ expect(result._content_format).toBe('html');
152
+ expect(result.content.length).toBeLessThan(20000);
153
+ expect(result.content).toContain('[truncated:');
154
+ });
155
+
156
+ it('text mode — strips HTML and truncates', () => {
157
+ const post = { id: 1, content: '<p>Hello <strong>world</strong></p>' };
158
+ const result = applyContentFormat(post, 'text', siteUrl, 15000);
159
+ expect(result._content_format).toBe('text');
160
+ expect(result.content).toContain('Hello world');
161
+ expect(result.content).not.toContain('<p>');
162
+ });
163
+
164
+ it('links_only mode — extracts internal links', () => {
165
+ const html = '<a href="https://mysite.example.com/page/">Page</a><a href="https://ext.com/x">Ext</a>';
166
+ const post = { id: 1, content: html };
167
+ const result = applyContentFormat(post, 'links_only', siteUrl);
168
+ expect(result._content_format).toBe('links_only');
169
+ expect(result.content).toBeNull();
170
+ expect(result.internal_links).toHaveLength(1);
171
+ expect(result.internal_links[0].text).toBe('Page');
172
+ });
173
+
174
+ it('returns post unchanged if no content', () => {
175
+ const post = { id: 1 };
176
+ expect(applyContentFormat(post, 'html', siteUrl)).toBe(post);
177
+ });
178
+ });
179
+
180
+ // =========================================================================
181
+ // Integration tests — wp_get_post with content_format & fields
182
+ // =========================================================================
183
+
184
+ describe('wp_get_post — content_format', () => {
185
+ it('DEFAULT — returns truncated HTML (html mode)', async () => {
186
+ _testSetTarget('test', { url: 'https://test.example.com', username: 'u', password: 'p' });
187
+ const longContent = '<p>' + 'word '.repeat(5000) + '</p>';
188
+ mockSuccess(makeWpPost(1, longContent));
189
+
190
+ const res = await call('wp_get_post', { id: 1 });
191
+ const data = parseResult(res);
192
+
193
+ expect(data._content_format).toBe('html');
194
+ expect(data.id).toBe(1);
195
+ expect(data.title).toBe('Post 1');
196
+ });
197
+
198
+ it('TEXT — strips HTML tags', async () => {
199
+ _testSetTarget('test', { url: 'https://test.example.com', username: 'u', password: 'p' });
200
+ mockSuccess(makeWpPost(1, '<p>Hello <strong>world</strong></p>'));
201
+
202
+ const res = await call('wp_get_post', { id: 1, content_format: 'text' });
203
+ const data = parseResult(res);
204
+
205
+ expect(data._content_format).toBe('text');
206
+ expect(data.content).toContain('Hello world');
207
+ expect(data.content).not.toContain('<p>');
208
+ });
209
+
210
+ it('LINKS_ONLY — extracts internal links', async () => {
211
+ _testSetTarget('test', { url: 'https://test.example.com', username: 'u', password: 'p' });
212
+ const html = '<p>See <a href="https://test.example.com/other/">other</a> and <a href="https://ext.com/x">ext</a></p>';
213
+ mockSuccess(makeWpPost(1, html));
214
+
215
+ const res = await call('wp_get_post', { id: 1, content_format: 'links_only' });
216
+ const data = parseResult(res);
217
+
218
+ expect(data._content_format).toBe('links_only');
219
+ expect(data.content).toBeNull();
220
+ expect(data.internal_links).toHaveLength(1);
221
+ expect(data.internal_links[0].text).toBe('other');
222
+ expect(data.internal_links[0].href).toBe('https://test.example.com/other/');
223
+ });
224
+ });
225
+
226
+ describe('wp_get_post — fields filter', () => {
227
+ it('returns only requested fields', async () => {
228
+ _testSetTarget('test', { url: 'https://test.example.com', username: 'u', password: 'p' });
229
+ mockSuccess(makeWpPost(1));
230
+
231
+ const res = await call('wp_get_post', { id: 1, fields: ['id', 'title', 'slug'] });
232
+ const data = parseResult(res);
233
+
234
+ expect(Object.keys(data).sort()).toEqual(['id', 'slug', 'title']);
235
+ expect(data.id).toBe(1);
236
+ expect(data.title).toBe('Post 1');
237
+ expect(data.slug).toBe('post-1');
238
+ });
239
+
240
+ it('fields + content_format work together', async () => {
241
+ _testSetTarget('test', { url: 'https://test.example.com', username: 'u', password: 'p' });
242
+ mockSuccess(makeWpPost(1, '<p>Hello <b>world</b></p>'));
243
+
244
+ const res = await call('wp_get_post', { id: 1, fields: ['id', 'content'], content_format: 'text' });
245
+ const data = parseResult(res);
246
+
247
+ expect(Object.keys(data).sort()).toEqual(['content', 'id']);
248
+ expect(data.content).toContain('Hello world');
249
+ expect(data.content).not.toContain('<p>');
250
+ });
251
+ });
252
+
253
+ // =========================================================================
254
+ // Integration tests — wp_list_posts with mode
255
+ // =========================================================================
256
+
257
+ describe('wp_list_posts — mode', () => {
258
+ function makeWpListPost(id) {
259
+ return {
260
+ id,
261
+ title: { rendered: `Post ${id}` },
262
+ status: 'publish',
263
+ date: '2025-06-01',
264
+ modified: '2025-06-02',
265
+ link: `https://test.example.com/post-${id}`,
266
+ slug: `post-${id}`,
267
+ author: 1,
268
+ categories: [1],
269
+ tags: [],
270
+ excerpt: { rendered: `Excerpt for post ${id}` }
271
+ };
272
+ }
273
+
274
+ it('FULL — returns all fields (default)', async () => {
275
+ _testSetTarget('test', { url: 'https://test.example.com', username: 'u', password: 'p' });
276
+ mockSuccess([makeWpListPost(1), makeWpListPost(2)]);
277
+
278
+ const res = await call('wp_list_posts');
279
+ const data = parseResult(res);
280
+
281
+ expect(data.total).toBe(2);
282
+ expect(data.posts[0].excerpt).toBeDefined();
283
+ expect(data.posts[0].author).toBeDefined();
284
+ });
285
+
286
+ it('IDS_ONLY — returns flat array of IDs', async () => {
287
+ _testSetTarget('test', { url: 'https://test.example.com', username: 'u', password: 'p' });
288
+ mockSuccess([makeWpListPost(1), makeWpListPost(2), makeWpListPost(3)]);
289
+
290
+ const res = await call('wp_list_posts', { mode: 'ids_only' });
291
+ const data = parseResult(res);
292
+
293
+ expect(data.mode).toBe('ids_only');
294
+ expect(data.ids).toEqual([1, 2, 3]);
295
+ expect(data.posts).toBeUndefined();
296
+ });
297
+
298
+ it('SUMMARY — returns id/title/slug/date/status/link only', async () => {
299
+ _testSetTarget('test', { url: 'https://test.example.com', username: 'u', password: 'p' });
300
+ mockSuccess([makeWpListPost(1)]);
301
+
302
+ const res = await call('wp_list_posts', { mode: 'summary' });
303
+ const data = parseResult(res);
304
+
305
+ expect(data.mode).toBe('summary');
306
+ expect(data.posts).toHaveLength(1);
307
+ const p = data.posts[0];
308
+ expect(Object.keys(p).sort()).toEqual(['date', 'id', 'link', 'slug', 'status', 'title']);
309
+ expect(p.excerpt).toBeUndefined();
310
+ expect(p.author).toBeUndefined();
311
+ expect(p.categories).toBeUndefined();
312
+ });
313
+
314
+ it('VALIDATION — rejects invalid mode', async () => {
315
+ _testSetTarget('test', { url: 'https://test.example.com', username: 'u', password: 'p' });
316
+
317
+ const res = await call('wp_list_posts', { mode: 'invalid' });
318
+ expect(res.isError).toBe(true);
319
+ });
320
+ });