backend-manager 5.9.7 → 5.9.8

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 (209) hide show
  1. package/package.json +5 -3
  2. package/templates/_.env +42 -0
  3. package/templates/_.gitignore +60 -0
  4. package/templates/backend-manager-config.json +249 -0
  5. package/templates/database.rules.json +83 -0
  6. package/templates/firebase.json +66 -0
  7. package/templates/firestore.indexes.json +4 -0
  8. package/templates/firestore.rules +112 -0
  9. package/templates/public/404.html +26 -0
  10. package/templates/public/index.html +24 -0
  11. package/templates/remoteconfig.template.json +1 -0
  12. package/templates/storage-lifecycle-config-1-day.json +9 -0
  13. package/templates/storage-lifecycle-config-30-days.json +9 -0
  14. package/templates/storage.rules +8 -0
  15. package/test/_init/accounts-validation.js +58 -0
  16. package/test/_legacy/ai/index.js +231 -0
  17. package/test/_legacy/ai/test.jpg +0 -0
  18. package/test/_legacy/ai/test.pdf +0 -0
  19. package/test/_legacy/index.js +31 -0
  20. package/test/_legacy/payment-resolver/chargebee/orders/refunded.json +0 -0
  21. package/test/_legacy/payment-resolver/chargebee/orders/unpaid.json +98 -0
  22. package/test/_legacy/payment-resolver/chargebee/subscriptions/active.json +578 -0
  23. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-in-trial.json +38 -0
  24. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-skipped-to-active.json +135 -0
  25. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-active-to-cancelled.json +42 -0
  26. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-active.json +44 -0
  27. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-cancelled.json +39 -0
  28. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-refund.json +143 -0
  29. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-suspended.json +48 -0
  30. package/test/_legacy/payment-resolver/coinbase/orders/regular.json +204 -0
  31. package/test/_legacy/payment-resolver/coinbase/subscriptions/cancelled.json +204 -0
  32. package/test/_legacy/payment-resolver/coinbase/subscriptions/paid-2.json +125 -0
  33. package/test/_legacy/payment-resolver/index.js +1558 -0
  34. package/test/_legacy/payment-resolver/paypal/orders/refunded.json +0 -0
  35. package/test/_legacy/payment-resolver/paypal/orders/regular.json +120 -0
  36. package/test/_legacy/payment-resolver/paypal/subscriptions/active-refund-previous-stmnt.json +323 -0
  37. package/test/_legacy/payment-resolver/paypal/subscriptions/active.json +192 -0
  38. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-in-trial.json +125 -0
  39. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-payment-not-complete.json +76 -0
  40. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-payment-overdue-2.json +114 -0
  41. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-payment-overdue.json +114 -0
  42. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-active-to-cancelled.json +111 -0
  43. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-active.json +132 -0
  44. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-cancelled.json +107 -0
  45. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-expired.json +91 -0
  46. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-refund.json +147 -0
  47. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-suspended.json +120 -0
  48. package/test/_legacy/payment-resolver/stripe/orders/refunded.json +0 -0
  49. package/test/_legacy/payment-resolver/stripe/orders/regular.json +91 -0
  50. package/test/_legacy/payment-resolver/stripe/subscriptions/active.json +421 -0
  51. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-in-trial.json +349 -0
  52. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-active-failed-authorization.json +430 -0
  53. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-active.json +319 -0
  54. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-cancelled.json +319 -0
  55. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-refund.json +414 -0
  56. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-suspended.json +319 -0
  57. package/test/_legacy/payment-resolver/stripe/subscriptions/unsure.json +339 -0
  58. package/test/_legacy/test-new +1178 -0
  59. package/test/_legacy/test.md +89 -0
  60. package/test/_legacy/usage.js +175 -0
  61. package/test/_legacy/user.js +31 -0
  62. package/test/ai/tools-live.js +170 -0
  63. package/test/boot/emulator-boots.js +37 -0
  64. package/test/content/blog-generate.js +160 -0
  65. package/test/email/campaign-send.js +45 -0
  66. package/test/email/consent-lifecycle.js +257 -0
  67. package/test/email/feedback-and-plain-send.js +52 -0
  68. package/test/email/fixtures/clean.json +30 -0
  69. package/test/email/fixtures/editorial.json +30 -0
  70. package/test/email/fixtures/field-report.json +53 -0
  71. package/test/email/marketing-lifecycle.js +132 -0
  72. package/test/email/newsletter-generate.js +819 -0
  73. package/test/email/newsletter-templates.js +491 -0
  74. package/test/email/templates.js +207 -0
  75. package/test/email/transactional-send.js +34 -0
  76. package/test/email/transactional.js +560 -0
  77. package/test/email/validation.js +710 -0
  78. package/test/events/auth-delete-race.js +201 -0
  79. package/test/events/payments/journey-payments-cancel-endpoint.js +100 -0
  80. package/test/events/payments/journey-payments-cancel.js +182 -0
  81. package/test/events/payments/journey-payments-discount.js +89 -0
  82. package/test/events/payments/journey-payments-failure.js +146 -0
  83. package/test/events/payments/journey-payments-legacy-product.js +149 -0
  84. package/test/events/payments/journey-payments-one-time-failure.js +111 -0
  85. package/test/events/payments/journey-payments-one-time.js +136 -0
  86. package/test/events/payments/journey-payments-plan-change.js +144 -0
  87. package/test/events/payments/journey-payments-refund-webhook.js +180 -0
  88. package/test/events/payments/journey-payments-suspend.js +181 -0
  89. package/test/events/payments/journey-payments-trial-cancel.js +130 -0
  90. package/test/events/payments/journey-payments-trial.js +171 -0
  91. package/test/events/payments/journey-payments-uid-resolution.js +132 -0
  92. package/test/events/payments/journey-payments-upgrade.js +135 -0
  93. package/test/fixtures/chargebee/invoice-one-time.json +27 -0
  94. package/test/fixtures/chargebee/subscription-active.json +44 -0
  95. package/test/fixtures/chargebee/subscription-cancelled.json +42 -0
  96. package/test/fixtures/chargebee/subscription-in-trial.json +41 -0
  97. package/test/fixtures/chargebee/subscription-legacy-plan.json +41 -0
  98. package/test/fixtures/chargebee/subscription-non-renewing.json +41 -0
  99. package/test/fixtures/chargebee/subscription-paused.json +42 -0
  100. package/test/fixtures/chargebee/webhook-payment-failed.json +51 -0
  101. package/test/fixtures/chargebee/webhook-subscription-created.json +47 -0
  102. package/test/fixtures/paypal/order-approved.json +62 -0
  103. package/test/fixtures/paypal/order-completed.json +110 -0
  104. package/test/fixtures/paypal/subscription-active.json +76 -0
  105. package/test/fixtures/paypal/subscription-cancelled.json +50 -0
  106. package/test/fixtures/paypal/subscription-suspended.json +65 -0
  107. package/test/fixtures/stripe/checkout-session-completed.json +130 -0
  108. package/test/fixtures/stripe/invoice-payment-failed.json +148 -0
  109. package/test/fixtures/stripe/invoice-subscription-payment-failed.json +28 -0
  110. package/test/fixtures/stripe/subscription-active.json +161 -0
  111. package/test/fixtures/stripe/subscription-canceled.json +161 -0
  112. package/test/fixtures/stripe/subscription-trialing.json +161 -0
  113. package/test/functions/admin/database-read.js +134 -0
  114. package/test/functions/admin/database-write.js +159 -0
  115. package/test/functions/admin/edit-post.js +366 -0
  116. package/test/functions/admin/firestore-query.js +207 -0
  117. package/test/functions/admin/firestore-read.js +129 -0
  118. package/test/functions/admin/firestore-write.js +105 -0
  119. package/test/functions/admin/get-stats.js +115 -0
  120. package/test/functions/admin/send-email.js +119 -0
  121. package/test/functions/admin/send-notification.js +208 -0
  122. package/test/functions/admin/write-repo-content.js +223 -0
  123. package/test/functions/general/add-marketing-contact.js +335 -0
  124. package/test/functions/general/fetch-post.js +54 -0
  125. package/test/functions/general/generate-uuid.js +114 -0
  126. package/test/functions/test/authenticate.js +64 -0
  127. package/test/functions/user/create-custom-token.js +73 -0
  128. package/test/functions/user/delete.js +135 -0
  129. package/test/functions/user/get-active-sessions.js +111 -0
  130. package/test/functions/user/get-subscription-info.js +99 -0
  131. package/test/functions/user/regenerate-api-keys.js +156 -0
  132. package/test/functions/user/resolve.js +52 -0
  133. package/test/functions/user/sign-out-all-sessions.js +94 -0
  134. package/test/functions/user/sign-up.js +252 -0
  135. package/test/functions/user/submit-feedback.js +104 -0
  136. package/test/functions/user/validate-settings.js +82 -0
  137. package/test/helpers/ai-request-payload.js +300 -0
  138. package/test/helpers/ai-test-provider.js +202 -0
  139. package/test/helpers/ai-tools-format.js +383 -0
  140. package/test/helpers/content/blog-auto-publisher.js +484 -0
  141. package/test/helpers/content/feed-parser.js +528 -0
  142. package/test/helpers/content/ghostii-blocks.js +134 -0
  143. package/test/helpers/content/ghostii-feed-integration.js +404 -0
  144. package/test/helpers/content/ghostii-write-article.js +243 -0
  145. package/test/helpers/environment.js +230 -0
  146. package/test/helpers/infer-contact.js +113 -0
  147. package/test/helpers/merge-line-files.js +271 -0
  148. package/test/helpers/payment/chargebee/parse-webhook.js +413 -0
  149. package/test/helpers/payment/chargebee/to-unified-one-time.js +147 -0
  150. package/test/helpers/payment/chargebee/to-unified-subscription.js +648 -0
  151. package/test/helpers/payment/paypal/parse-webhook.js +539 -0
  152. package/test/helpers/payment/paypal/to-unified-one-time.js +382 -0
  153. package/test/helpers/payment/paypal/to-unified-subscription.js +820 -0
  154. package/test/helpers/payment/stripe/parse-webhook.js +447 -0
  155. package/test/helpers/payment/stripe/to-unified-one-time.js +306 -0
  156. package/test/helpers/payment/stripe/to-unified-subscription.js +708 -0
  157. package/test/helpers/sanitize.js +222 -0
  158. package/test/helpers/slugify.js +394 -0
  159. package/test/helpers/storage.js +194 -0
  160. package/test/helpers/user.js +755 -0
  161. package/test/helpers/webhook-forward.js +418 -0
  162. package/test/mcp/discovery.js +53 -0
  163. package/test/mcp/oauth.js +161 -0
  164. package/test/mcp/protocol.js +268 -0
  165. package/test/mcp/roles.js +188 -0
  166. package/test/mcp/utils.js +245 -0
  167. package/test/routes/admin/create-post.js +364 -0
  168. package/test/routes/admin/database.js +131 -0
  169. package/test/routes/admin/deduplicate-image-alts.js +190 -0
  170. package/test/routes/admin/email.js +114 -0
  171. package/test/routes/admin/firestore-query.js +204 -0
  172. package/test/routes/admin/firestore.js +127 -0
  173. package/test/routes/admin/infer-contact.js +218 -0
  174. package/test/routes/admin/notification.js +198 -0
  175. package/test/routes/admin/post-resize-image.js +184 -0
  176. package/test/routes/admin/post.js +368 -0
  177. package/test/routes/admin/repo-content.js +223 -0
  178. package/test/routes/admin/stats.js +112 -0
  179. package/test/routes/content/post.js +54 -0
  180. package/test/routes/general/uuid.js +115 -0
  181. package/test/routes/marketing/campaign.js +125 -0
  182. package/test/routes/marketing/contact.js +370 -0
  183. package/test/routes/marketing/email-preferences.js +292 -0
  184. package/test/routes/marketing/push-send.js +32 -0
  185. package/test/routes/marketing/webhook-forward.js +61 -0
  186. package/test/routes/marketing/webhook.js +639 -0
  187. package/test/routes/payments/cancel.js +163 -0
  188. package/test/routes/payments/discount.js +80 -0
  189. package/test/routes/payments/dispute-alert.js +320 -0
  190. package/test/routes/payments/intent.js +336 -0
  191. package/test/routes/payments/portal.js +92 -0
  192. package/test/routes/payments/refund.js +179 -0
  193. package/test/routes/payments/trial-eligibility.js +71 -0
  194. package/test/routes/payments/webhook.js +107 -0
  195. package/test/routes/test/authenticate.js +59 -0
  196. package/test/routes/test/schema.js +554 -0
  197. package/test/routes/test/usage.js +342 -0
  198. package/test/routes/user/api-keys.js +156 -0
  199. package/test/routes/user/delete.js +136 -0
  200. package/test/routes/user/feedback.js +104 -0
  201. package/test/routes/user/sessions.js +199 -0
  202. package/test/routes/user/settings-validate.js +82 -0
  203. package/test/routes/user/signup.js +542 -0
  204. package/test/routes/user/subscription.js +99 -0
  205. package/test/routes/user/token.js +73 -0
  206. package/test/routes/user/user.js +157 -0
  207. package/test/rules/notifications.js +410 -0
  208. package/test/rules/payments-carts.js +371 -0
  209. package/test/rules/user.js +396 -0
@@ -0,0 +1,528 @@
1
+ /**
2
+ * Test: content/feed-parser
3
+ * Unit tests for the RSS 2.0, Atom 1.0, and JSON Feed parser + article content extractor.
4
+ *
5
+ * Run: npx mgr test helpers/content/feed-parser
6
+ *
7
+ * Pure function tests (parseFeed, stripHtml, extractElement) — required
8
+ * directly and called with plain inputs. NOT a mock.
9
+ */
10
+ const { parseFeed, stripHtml, extractTextFromHtml } = require('../../../src/manager/libraries/content/feed-parser.js');
11
+
12
+ // --- Sample feeds for testing ---
13
+
14
+ const RSS_FULL = `<?xml version="1.0" encoding="UTF-8"?>
15
+ <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
16
+ <channel>
17
+ <title>Tech News</title>
18
+ <link>https://example.com</link>
19
+ <description>Latest tech news</description>
20
+ <item>
21
+ <guid>https://example.com/article-1</guid>
22
+ <title>First Article Title</title>
23
+ <link>https://example.com/article-1</link>
24
+ <description>Short description of the first article.</description>
25
+ <content:encoded><![CDATA[<p>Full content of the first article with <strong>HTML</strong> formatting.</p>]]></content:encoded>
26
+ <pubDate>Mon, 16 Jun 2025 10:00:00 GMT</pubDate>
27
+ <dc:creator>Jane Doe</dc:creator>
28
+ </item>
29
+ <item>
30
+ <guid isPermaLink="false">article-2-id</guid>
31
+ <title>Second Article Title</title>
32
+ <link>https://example.com/article-2</link>
33
+ <description>Short description of the second article.</description>
34
+ <pubDate>Tue, 17 Jun 2025 12:00:00 GMT</pubDate>
35
+ </item>
36
+ </channel>
37
+ </rss>`;
38
+
39
+ const RSS_MINIMAL = `<?xml version="1.0"?>
40
+ <rss version="2.0">
41
+ <channel>
42
+ <title>Minimal</title>
43
+ <item>
44
+ <title>Only Title</title>
45
+ <link>https://example.com/only</link>
46
+ </item>
47
+ </channel>
48
+ </rss>`;
49
+
50
+ const RSS_EMPTY_CHANNEL = `<?xml version="1.0"?>
51
+ <rss version="2.0">
52
+ <channel>
53
+ <title>Empty</title>
54
+ </channel>
55
+ </rss>`;
56
+
57
+ const ATOM_FULL = `<?xml version="1.0" encoding="utf-8"?>
58
+ <feed xmlns="http://www.w3.org/2005/Atom">
59
+ <title>Atom Feed</title>
60
+ <entry>
61
+ <id>urn:uuid:entry-1</id>
62
+ <title>Atom Entry One</title>
63
+ <link rel="alternate" href="https://example.com/atom-1"/>
64
+ <link rel="enclosure" href="https://example.com/atom-1.mp3"/>
65
+ <summary>Summary of entry one.</summary>
66
+ <content>Full content of entry one.</content>
67
+ <published>2025-06-16T10:00:00Z</published>
68
+ </entry>
69
+ <entry>
70
+ <id>urn:uuid:entry-2</id>
71
+ <title type="html">Atom Entry &amp; Two</title>
72
+ <link href="https://example.com/atom-2"/>
73
+ <updated>2025-06-17T12:00:00Z</updated>
74
+ </entry>
75
+ </feed>`;
76
+
77
+ const ATOM_EMPTY = `<?xml version="1.0"?>
78
+ <feed xmlns="http://www.w3.org/2005/Atom">
79
+ <title>Empty Atom</title>
80
+ </feed>`;
81
+
82
+ const JSON_FEED = JSON.stringify({
83
+ version: 'https://jsonfeed.org/version/1.1',
84
+ title: 'JSON Feed',
85
+ items: [
86
+ {
87
+ id: 'json-1',
88
+ title: 'JSON Article One',
89
+ url: 'https://example.com/json-1',
90
+ summary: 'Short summary.',
91
+ content_html: '<p>Full HTML content.</p>',
92
+ content_text: 'Full text content.',
93
+ date_published: '2025-06-16T10:00:00Z',
94
+ },
95
+ {
96
+ id: 'json-2',
97
+ title: 'JSON Article Two',
98
+ url: 'https://example.com/json-2',
99
+ content_text: 'Second article text.',
100
+ date_modified: '2025-06-17T12:00:00Z',
101
+ },
102
+ ],
103
+ });
104
+
105
+ const RSSAPP_FORMAT = JSON.stringify({
106
+ items: [
107
+ {
108
+ title: 'RSS.app Item',
109
+ url: 'https://example.com/rssapp-1',
110
+ content_text: 'Content from RSS.app feed.',
111
+ },
112
+ ],
113
+ });
114
+
115
+ module.exports = {
116
+ description: 'content/feed-parser',
117
+ type: 'group',
118
+
119
+ tests: [
120
+ // ============================
121
+ // RSS 2.0 PARSING
122
+ // ============================
123
+ {
124
+ name: 'rss-parses-standard-items',
125
+ async run({ assert }) {
126
+ const { items } = parseFeed(RSS_FULL);
127
+ assert.equal(items.length, 2, 'should parse 2 items');
128
+ assert.equal(items[0].title, 'First Article Title');
129
+ assert.equal(items[1].title, 'Second Article Title');
130
+ },
131
+ },
132
+
133
+ {
134
+ name: 'rss-extracts-guid-as-id',
135
+ async run({ assert }) {
136
+ const { items } = parseFeed(RSS_FULL);
137
+ assert.equal(items[0].id, 'https://example.com/article-1', 'text guid');
138
+ assert.equal(items[1].id, 'article-2-id', 'guid with isPermaLink attr');
139
+ },
140
+ },
141
+
142
+ {
143
+ name: 'rss-extracts-link-as-url',
144
+ async run({ assert }) {
145
+ const { items } = parseFeed(RSS_FULL);
146
+ assert.equal(items[0].url, 'https://example.com/article-1');
147
+ assert.equal(items[1].url, 'https://example.com/article-2');
148
+ },
149
+ },
150
+
151
+ {
152
+ name: 'rss-extracts-description-as-summary',
153
+ async run({ assert }) {
154
+ const { items } = parseFeed(RSS_FULL);
155
+ assert.equal(items[0].summary, 'Short description of the first article.');
156
+ },
157
+ },
158
+
159
+ {
160
+ name: 'rss-extracts-content-encoded-as-content',
161
+ async run({ assert }) {
162
+ const { items } = parseFeed(RSS_FULL);
163
+ assert.ok(items[0].content.includes('Full content of the first article'), 'has content:encoded text');
164
+ assert.ok(!items[0].content.includes('<p>'), 'HTML stripped from content');
165
+ },
166
+ },
167
+
168
+ {
169
+ name: 'rss-falls-back-to-description-when-no-content-encoded',
170
+ async run({ assert }) {
171
+ const { items } = parseFeed(RSS_FULL);
172
+ assert.equal(items[1].content, 'Short description of the second article.');
173
+ },
174
+ },
175
+
176
+ {
177
+ name: 'rss-extracts-pubdate',
178
+ async run({ assert }) {
179
+ const { items } = parseFeed(RSS_FULL);
180
+ assert.equal(items[0].publishedAt, 'Mon, 16 Jun 2025 10:00:00 GMT');
181
+ },
182
+ },
183
+
184
+ {
185
+ name: 'rss-handles-missing-optional-fields',
186
+ async run({ assert }) {
187
+ const { items } = parseFeed(RSS_MINIMAL);
188
+ assert.equal(items.length, 1);
189
+ assert.equal(items[0].title, 'Only Title');
190
+ assert.equal(items[0].url, 'https://example.com/only');
191
+ assert.equal(items[0].summary, '', 'no description');
192
+ assert.equal(items[0].publishedAt, '', 'no pubDate');
193
+ },
194
+ },
195
+
196
+ {
197
+ name: 'rss-preserves-item-order',
198
+ async run({ assert }) {
199
+ const { items } = parseFeed(RSS_FULL);
200
+ assert.equal(items[0].title, 'First Article Title', 'first item is first');
201
+ assert.equal(items[1].title, 'Second Article Title', 'second item is second');
202
+ },
203
+ },
204
+
205
+ {
206
+ name: 'rss-empty-channel-returns-empty-items',
207
+ async run({ assert }) {
208
+ const { items } = parseFeed(RSS_EMPTY_CHANNEL);
209
+ assert.equal(items.length, 0);
210
+ },
211
+ },
212
+
213
+ // ============================
214
+ // ATOM 1.0 PARSING
215
+ // ============================
216
+ {
217
+ name: 'atom-parses-standard-entries',
218
+ async run({ assert }) {
219
+ const { items } = parseFeed(ATOM_FULL);
220
+ assert.equal(items.length, 2, 'should parse 2 entries');
221
+ assert.equal(items[0].title, 'Atom Entry One');
222
+ },
223
+ },
224
+
225
+ {
226
+ name: 'atom-extracts-id',
227
+ async run({ assert }) {
228
+ const { items } = parseFeed(ATOM_FULL);
229
+ assert.equal(items[0].id, 'urn:uuid:entry-1');
230
+ assert.equal(items[1].id, 'urn:uuid:entry-2');
231
+ },
232
+ },
233
+
234
+ {
235
+ name: 'atom-picks-alternate-link-href',
236
+ async run({ assert }) {
237
+ const { items } = parseFeed(ATOM_FULL);
238
+ assert.equal(items[0].url, 'https://example.com/atom-1', 'picks rel=alternate over enclosure');
239
+ },
240
+ },
241
+
242
+ {
243
+ name: 'atom-falls-back-to-first-link-when-no-rel',
244
+ async run({ assert }) {
245
+ const { items } = parseFeed(ATOM_FULL);
246
+ assert.equal(items[1].url, 'https://example.com/atom-2', 'link without rel');
247
+ },
248
+ },
249
+
250
+ {
251
+ name: 'atom-extracts-summary-and-content',
252
+ async run({ assert }) {
253
+ const { items } = parseFeed(ATOM_FULL);
254
+ assert.equal(items[0].summary, 'Summary of entry one.');
255
+ assert.equal(items[0].content, 'Full content of entry one.');
256
+ },
257
+ },
258
+
259
+ {
260
+ name: 'atom-extracts-published-or-updated',
261
+ async run({ assert }) {
262
+ const { items } = parseFeed(ATOM_FULL);
263
+ assert.equal(items[0].publishedAt, '2025-06-16T10:00:00Z', 'published');
264
+ assert.equal(items[1].publishedAt, '2025-06-17T12:00:00Z', 'updated fallback');
265
+ },
266
+ },
267
+
268
+ {
269
+ name: 'atom-empty-feed-returns-empty-items',
270
+ async run({ assert }) {
271
+ const { items } = parseFeed(ATOM_EMPTY);
272
+ assert.equal(items.length, 0);
273
+ },
274
+ },
275
+
276
+ // ============================
277
+ // JSON FEED PARSING
278
+ // ============================
279
+ {
280
+ name: 'json-parses-standard-items',
281
+ async run({ assert }) {
282
+ const { items } = parseFeed(JSON_FEED);
283
+ assert.equal(items.length, 2);
284
+ assert.equal(items[0].title, 'JSON Article One');
285
+ assert.equal(items[1].title, 'JSON Article Two');
286
+ },
287
+ },
288
+
289
+ {
290
+ name: 'json-extracts-id-and-url',
291
+ async run({ assert }) {
292
+ const { items } = parseFeed(JSON_FEED);
293
+ assert.equal(items[0].id, 'json-1');
294
+ assert.equal(items[0].url, 'https://example.com/json-1');
295
+ },
296
+ },
297
+
298
+ {
299
+ name: 'json-prefers-content-html-over-content-text',
300
+ async run({ assert }) {
301
+ const { items } = parseFeed(JSON_FEED);
302
+ assert.equal(items[0].content, '<p>Full HTML content.</p>');
303
+ },
304
+ },
305
+
306
+ {
307
+ name: 'json-falls-back-to-content-text',
308
+ async run({ assert }) {
309
+ const { items } = parseFeed(JSON_FEED);
310
+ assert.equal(items[1].content, 'Second article text.');
311
+ },
312
+ },
313
+
314
+ {
315
+ name: 'json-extracts-dates',
316
+ async run({ assert }) {
317
+ const { items } = parseFeed(JSON_FEED);
318
+ assert.equal(items[0].publishedAt, '2025-06-16T10:00:00Z', 'date_published');
319
+ assert.equal(items[1].publishedAt, '2025-06-17T12:00:00Z', 'date_modified fallback');
320
+ },
321
+ },
322
+
323
+ {
324
+ name: 'json-handles-rssapp-format',
325
+ async run({ assert }) {
326
+ const { items } = parseFeed(RSSAPP_FORMAT);
327
+ assert.equal(items.length, 1);
328
+ assert.equal(items[0].title, 'RSS.app Item');
329
+ assert.equal(items[0].content, 'Content from RSS.app feed.');
330
+ },
331
+ },
332
+
333
+ {
334
+ name: 'json-empty-items-returns-empty',
335
+ async run({ assert }) {
336
+ const { items } = parseFeed(JSON.stringify({ items: [] }));
337
+ assert.equal(items.length, 0);
338
+ },
339
+ },
340
+
341
+ // ============================
342
+ // EDGE CASES
343
+ // ============================
344
+ {
345
+ name: 'returns-empty-for-null-input',
346
+ async run({ assert }) {
347
+ assert.deepEqual(parseFeed(null), { items: [] });
348
+ },
349
+ },
350
+
351
+ {
352
+ name: 'returns-empty-for-undefined-input',
353
+ async run({ assert }) {
354
+ assert.deepEqual(parseFeed(undefined), { items: [] });
355
+ },
356
+ },
357
+
358
+ {
359
+ name: 'returns-empty-for-empty-string',
360
+ async run({ assert }) {
361
+ assert.deepEqual(parseFeed(''), { items: [] });
362
+ },
363
+ },
364
+
365
+ {
366
+ name: 'returns-empty-for-invalid-xml',
367
+ async run({ assert }) {
368
+ const { items } = parseFeed('<not<valid>xml');
369
+ assert.equal(items.length, 0);
370
+ },
371
+ },
372
+
373
+ {
374
+ name: 'returns-empty-for-non-feed-json',
375
+ async run({ assert }) {
376
+ const { items } = parseFeed(JSON.stringify({ data: [1, 2, 3] }));
377
+ assert.equal(items.length, 0);
378
+ },
379
+ },
380
+
381
+ {
382
+ name: 'returns-empty-for-plain-text',
383
+ async run({ assert }) {
384
+ const { items } = parseFeed('just some plain text content');
385
+ assert.equal(items.length, 0);
386
+ },
387
+ },
388
+
389
+ {
390
+ name: 'handles-bom-prefix',
391
+ async run({ assert }) {
392
+ const withBom = '' + RSS_MINIMAL;
393
+ const { items } = parseFeed(withBom);
394
+ assert.equal(items.length, 1, 'BOM does not break parsing');
395
+ assert.equal(items[0].title, 'Only Title');
396
+ },
397
+ },
398
+
399
+ {
400
+ name: 'handles-namespace-prefixed-elements',
401
+ async run({ assert }) {
402
+ const { items } = parseFeed(RSS_FULL);
403
+ assert.ok(items[0].content.includes('Full content'), 'content:encoded parsed despite namespace');
404
+ },
405
+ },
406
+
407
+ {
408
+ name: 'truncates-long-content',
409
+ async run({ assert }) {
410
+ const longContent = 'x'.repeat(20000);
411
+ const feed = JSON.stringify({
412
+ items: [{ id: '1', title: 'Long', url: 'https://example.com', content_text: longContent }],
413
+ });
414
+ const { items } = parseFeed(feed);
415
+ assert.ok(items[0].content.length <= 1024 * 14, 'content truncated to 14KB');
416
+ },
417
+ },
418
+
419
+ {
420
+ name: 'truncates-long-summary',
421
+ async run({ assert }) {
422
+ const longSummary = 'y'.repeat(1000);
423
+ const feed = JSON.stringify({
424
+ items: [{ id: '1', title: 'Long Summary', url: 'https://example.com', summary: longSummary }],
425
+ });
426
+ const { items } = parseFeed(feed);
427
+ assert.ok(items[0].summary.length <= 500, 'summary truncated to 500 chars');
428
+ },
429
+ },
430
+
431
+ // ============================
432
+ // STRIP HTML
433
+ // ============================
434
+ {
435
+ name: 'stripHtml-removes-tags',
436
+ async run({ assert }) {
437
+ assert.equal(stripHtml('<p>Hello <strong>world</strong></p>'), 'Hello world');
438
+ },
439
+ },
440
+
441
+ {
442
+ name: 'stripHtml-normalizes-whitespace',
443
+ async run({ assert }) {
444
+ assert.equal(stripHtml('<p>Hello</p> <p>World</p>'), 'Hello World');
445
+ },
446
+ },
447
+
448
+ {
449
+ name: 'stripHtml-handles-empty-string',
450
+ async run({ assert }) {
451
+ assert.equal(stripHtml(''), '');
452
+ },
453
+ },
454
+
455
+ // ============================
456
+ // EXTRACT TEXT FROM HTML (Cheerio)
457
+ // ============================
458
+ {
459
+ name: 'extractTextFromHtml-extracts-from-article-tag',
460
+ async run({ assert }) {
461
+ const html = '<html><body><nav>Menu</nav><article class="post"><p>Article content</p></article><footer>Footer</footer></body></html>';
462
+ const text = extractTextFromHtml(html);
463
+ assert.ok(text.includes('Article content'), 'extracts article text');
464
+ assert.ok(!text.includes('Menu'), 'nav removed');
465
+ assert.ok(!text.includes('Footer'), 'footer removed');
466
+ },
467
+ },
468
+
469
+ {
470
+ name: 'extractTextFromHtml-falls-back-to-main',
471
+ async run({ assert }) {
472
+ const html = '<html><body><main><p>Main content</p></main><aside>Sidebar</aside></body></html>';
473
+ const text = extractTextFromHtml(html);
474
+ assert.ok(text.includes('Main content'), 'extracts main text');
475
+ assert.ok(!text.includes('Sidebar'), 'aside removed');
476
+ },
477
+ },
478
+
479
+ {
480
+ name: 'extractTextFromHtml-falls-back-to-body',
481
+ async run({ assert }) {
482
+ const html = '<html><body><p>Body content here</p></body></html>';
483
+ const text = extractTextFromHtml(html);
484
+ assert.ok(text.includes('Body content here'), 'extracts body text');
485
+ },
486
+ },
487
+
488
+ {
489
+ name: 'extractTextFromHtml-strips-scripts-and-styles',
490
+ async run({ assert }) {
491
+ const html = '<html><body><article><script>alert("xss")</script><style>.x{color:red}</style><p>Clean text</p></article></body></html>';
492
+ const text = extractTextFromHtml(html);
493
+ assert.ok(text.includes('Clean text'), 'keeps article text');
494
+ assert.ok(!text.includes('alert'), 'script removed');
495
+ assert.ok(!text.includes('color'), 'style removed');
496
+ },
497
+ },
498
+
499
+ {
500
+ name: 'extractTextFromHtml-strips-forms-and-buttons',
501
+ async run({ assert }) {
502
+ const html = '<html><body><article><p>Article text</p><form><input placeholder="Email"><button>Subscribe</button></form></article></body></html>';
503
+ const text = extractTextFromHtml(html);
504
+ assert.ok(text.includes('Article text'), 'keeps article text');
505
+ assert.ok(!text.includes('Subscribe'), 'button removed');
506
+ },
507
+ },
508
+
509
+ {
510
+ name: 'extractTextFromHtml-strips-ad-classes',
511
+ async run({ assert }) {
512
+ const html = '<html><body><article><p>Real content</p><div class="advertisement">Buy stuff</div><div class="social-share">Share this</div></article></body></html>';
513
+ const text = extractTextFromHtml(html);
514
+ assert.ok(text.includes('Real content'), 'keeps article text');
515
+ assert.ok(!text.includes('Buy stuff'), 'ad removed');
516
+ assert.ok(!text.includes('Share this'), 'social share removed');
517
+ },
518
+ },
519
+
520
+ {
521
+ name: 'extractTextFromHtml-returns-empty-for-empty-html',
522
+ async run({ assert }) {
523
+ assert.equal(extractTextFromHtml(''), '');
524
+ assert.equal(extractTextFromHtml('<html><body></body></html>'), '');
525
+ },
526
+ },
527
+ ],
528
+ };
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Test: content/ghostii.blocksToPost()
3
+ * Unit tests for the Ghostii-JSON → BEM-post transform.
4
+ *
5
+ * Run: npx mgr test helpers/content/ghostii-blocks
6
+ *
7
+ * Ghostii is unopinionated about BEM: its /write/article response is a generic
8
+ * block array ([{ name, content }], name ∈ heading-1..6/image/paragraph/blockquote/list).
9
+ * blocksToPost() is the SSOT that turns those blocks into what admin/post wants —
10
+ * title + header image as SEPARATE fields, body = content only.
11
+ *
12
+ * Pure function (no I/O) → required directly and called with plain inputs. NOT a mock.
13
+ *
14
+ * Contract:
15
+ * - title ← first heading-1 block, leading markdown `#` stripped
16
+ * - headerImageUrl ← first image block's URL (from `![alt](url)`)
17
+ * - body ← every remaining block, joined with blank lines, trimmed
18
+ * - the title block and the header-image block are EXCLUDED from body
19
+ * - section images (non-first image blocks) STAY in the body
20
+ * - non-array / empty input → all empty strings
21
+ */
22
+ const { blocksToPost } = require('../../../src/manager/libraries/content/ghostii.js');
23
+
24
+ module.exports = {
25
+ description: 'content/ghostii.blocksToPost()',
26
+ type: 'group',
27
+
28
+ tests: [
29
+ {
30
+ name: 'extracts-title-from-heading-1-stripping-marker',
31
+ async run({ assert }) {
32
+ const result = blocksToPost([
33
+ { name: 'heading-1', content: '# My Great Article' },
34
+ { name: 'paragraph', content: 'Body.' },
35
+ ]);
36
+ assert.equal(result.title, 'My Great Article');
37
+ },
38
+ },
39
+
40
+ {
41
+ name: 'extracts-header-image-url-from-first-image-block',
42
+ async run({ assert }) {
43
+ const result = blocksToPost([
44
+ { name: 'heading-1', content: '# Title' },
45
+ { name: 'image', content: '![Title](https://images.unsplash.com/photo-abc?w=1080)' },
46
+ { name: 'paragraph', content: 'Body.' },
47
+ ]);
48
+ assert.equal(result.headerImageUrl, 'https://images.unsplash.com/photo-abc?w=1080');
49
+ },
50
+ },
51
+
52
+ {
53
+ name: 'body-excludes-title-and-header-image',
54
+ async run({ assert }) {
55
+ const result = blocksToPost([
56
+ { name: 'heading-1', content: '# Title' },
57
+ { name: 'image', content: '![Title](https://img/header.jpg)' },
58
+ { name: 'heading-2', content: '## First Section' },
59
+ { name: 'paragraph', content: 'First paragraph.' },
60
+ ]);
61
+ assert.equal(result.body, '## First Section\n\nFirst paragraph.', 'body is content only');
62
+ assert.notMatch(result.body, /^#\s/m, 'no H1 title in body');
63
+ assert.notMatch(result.body, /^!\[Title\]\(https:\/\/img\/header\.jpg\)/m, 'no header image in body');
64
+ },
65
+ },
66
+
67
+ {
68
+ name: 'keeps-section-images-in-body',
69
+ async run({ assert }) {
70
+ const result = blocksToPost([
71
+ { name: 'heading-1', content: '# Title' },
72
+ { name: 'image', content: '![Title](https://img/header.jpg)' },
73
+ { name: 'heading-2', content: '## Section' },
74
+ { name: 'paragraph', content: 'Text.' },
75
+ { name: 'image', content: '![Section](https://img/section.jpg)' },
76
+ { name: 'paragraph', content: 'More text.' },
77
+ ]);
78
+ // Only the FIRST image is the header; later images belong to the body.
79
+ assert.ok(result.body.includes('![Section](https://img/section.jpg)'), 'section image stays in body');
80
+ assert.equal((result.body.match(/!\[/g) || []).length, 1, 'exactly one (section) image in body');
81
+ },
82
+ },
83
+
84
+ {
85
+ name: 'preserves-blockquotes-and-lists-in-body',
86
+ async run({ assert }) {
87
+ const result = blocksToPost([
88
+ { name: 'heading-1', content: '# Title' },
89
+ { name: 'heading-2', content: '## Section' },
90
+ { name: 'blockquote', content: '> A pithy quote.' },
91
+ { name: 'list', content: '- one\n- two' },
92
+ ]);
93
+ assert.ok(result.body.includes('> A pithy quote.'), 'blockquote kept');
94
+ assert.ok(result.body.includes('- one\n- two'), 'list kept');
95
+ },
96
+ },
97
+
98
+ {
99
+ name: 'handles-missing-heading-1-and-image-gracefully',
100
+ async run({ assert }) {
101
+ const result = blocksToPost([
102
+ { name: 'heading-2', content: '## Only Section' },
103
+ { name: 'paragraph', content: 'Text.' },
104
+ ]);
105
+ assert.equal(result.title, '', 'no title when no heading-1');
106
+ assert.equal(result.headerImageUrl, '', 'no header image when no image block');
107
+ assert.equal(result.body, '## Only Section\n\nText.', 'body is all blocks when nothing excluded');
108
+ },
109
+ },
110
+
111
+ {
112
+ name: 'uses-only-the-first-image-as-header',
113
+ async run({ assert }) {
114
+ const result = blocksToPost([
115
+ { name: 'image', content: '![first](https://img/first.jpg)' },
116
+ { name: 'paragraph', content: 'Text.' },
117
+ { name: 'image', content: '![second](https://img/second.jpg)' },
118
+ ]);
119
+ assert.equal(result.headerImageUrl, 'https://img/first.jpg', 'first image is the header');
120
+ assert.ok(result.body.includes('https://img/second.jpg'), 'second image stays in body');
121
+ assert.ok(!result.body.includes('https://img/first.jpg'), 'first image not in body');
122
+ },
123
+ },
124
+
125
+ {
126
+ name: 'non-array-or-empty-input-returns-empty-strings',
127
+ async run({ assert }) {
128
+ assert.deepEqual(blocksToPost([]), { title: '', headerImageUrl: '', body: '' }, 'empty array');
129
+ assert.deepEqual(blocksToPost(null), { title: '', headerImageUrl: '', body: '' }, 'null');
130
+ assert.deepEqual(blocksToPost(undefined), { title: '', headerImageUrl: '', body: '' }, 'undefined');
131
+ },
132
+ },
133
+ ],
134
+ };