backend-manager 5.7.5 → 5.8.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.
@@ -0,0 +1,342 @@
1
+ /**
2
+ * Test: content/ghostii-auto-publisher
3
+ * Tests for the upgraded ghostii-auto-publisher cron: source type detection,
4
+ * feed processing, Firestore tracking, hash determinism, and override pass-through.
5
+ *
6
+ * Run: npx mgr test helpers/content/ghostii-auto-publisher
7
+ *
8
+ * Pure-function tests for exported utilities (feedItemHash, isURL). Feed
9
+ * processing and Firestore tracking tests run against the real emulator.
10
+ */
11
+ const path = require('path');
12
+ const publisherPath = path.resolve(__dirname, '../../../src/manager/events/cron/daily/ghostii-auto-publisher.js');
13
+ const { feedItemHash, isURL } = require(publisherPath);
14
+
15
+ // --- Sample feed XML for testing ---
16
+ const SAMPLE_RSS = `<?xml version="1.0"?>
17
+ <rss version="2.0">
18
+ <channel>
19
+ <title>Test Feed</title>
20
+ <item>
21
+ <guid>feed-item-1</guid>
22
+ <title>Feed Article One</title>
23
+ <link>https://example.com/article-1</link>
24
+ <description>Description of article one.</description>
25
+ </item>
26
+ <item>
27
+ <guid>feed-item-2</guid>
28
+ <title>Feed Article Two</title>
29
+ <link>https://example.com/article-2</link>
30
+ <description>Description of article two.</description>
31
+ </item>
32
+ <item>
33
+ <guid>feed-item-3</guid>
34
+ <title>Feed Article Three</title>
35
+ <link>https://example.com/article-3</link>
36
+ <description>Description of article three.</description>
37
+ </item>
38
+ </channel>
39
+ </rss>`;
40
+
41
+ module.exports = {
42
+ description: 'content/ghostii-auto-publisher',
43
+ type: 'group',
44
+
45
+ tests: [
46
+ // ============================
47
+ // SOURCE TYPE DETECTION (isURL)
48
+ // ============================
49
+ {
50
+ name: 'isURL-detects-http-url',
51
+ async run({ assert }) {
52
+ assert.equal(isURL('https://example.com/page'), true);
53
+ assert.equal(isURL('http://example.com'), true);
54
+ },
55
+ },
56
+
57
+ {
58
+ name: 'isURL-rejects-non-urls',
59
+ async run({ assert }) {
60
+ assert.equal(isURL('$app'), false);
61
+ assert.equal(isURL('Write about AI'), false);
62
+ assert.equal(isURL('$feed:https://example.com/feed'), false);
63
+ },
64
+ },
65
+
66
+ {
67
+ name: 'isURL-rejects-empty-and-null',
68
+ async run({ assert }) {
69
+ assert.equal(isURL(''), false);
70
+ assert.equal(isURL(null), false);
71
+ assert.equal(isURL(undefined), false);
72
+ },
73
+ },
74
+
75
+ // ============================
76
+ // $feed: PREFIX DETECTION
77
+ // ============================
78
+ {
79
+ name: 'feed-prefix-detected-correctly',
80
+ async run({ assert }) {
81
+ const source = '$feed:https://techcrunch.com/feed/';
82
+ assert.equal(source.startsWith('$feed:'), true, '$feed: prefix detected');
83
+
84
+ const feedUrl = source.slice('$feed:'.length);
85
+ assert.equal(feedUrl, 'https://techcrunch.com/feed/', 'URL extracted after prefix');
86
+ },
87
+ },
88
+
89
+ {
90
+ name: 'feed-prefix-not-confused-with-app',
91
+ async run({ assert }) {
92
+ assert.equal('$app'.startsWith('$feed:'), false);
93
+ },
94
+ },
95
+
96
+ {
97
+ name: 'feed-prefix-not-confused-with-plain-url',
98
+ async run({ assert }) {
99
+ assert.equal('https://example.com'.startsWith('$feed:'), false);
100
+ },
101
+ },
102
+
103
+ {
104
+ name: 'feed-prefix-not-confused-with-text',
105
+ async run({ assert }) {
106
+ assert.equal('Write about technology'.startsWith('$feed:'), false);
107
+ },
108
+ },
109
+
110
+ // ============================
111
+ // FEED ITEM HASH
112
+ // ============================
113
+ {
114
+ name: 'feedItemHash-is-deterministic',
115
+ async run({ assert }) {
116
+ const hash1 = feedItemHash('https://example.com/feed', 'item-123');
117
+ const hash2 = feedItemHash('https://example.com/feed', 'item-123');
118
+ assert.equal(hash1, hash2, 'same input produces same hash');
119
+ },
120
+ },
121
+
122
+ {
123
+ name: 'feedItemHash-different-for-different-items',
124
+ async run({ assert }) {
125
+ const hash1 = feedItemHash('https://example.com/feed', 'item-1');
126
+ const hash2 = feedItemHash('https://example.com/feed', 'item-2');
127
+ assert.notEqual(hash1, hash2, 'different items produce different hashes');
128
+ },
129
+ },
130
+
131
+ {
132
+ name: 'feedItemHash-different-for-different-feeds',
133
+ async run({ assert }) {
134
+ const hash1 = feedItemHash('https://example.com/feed-a', 'item-1');
135
+ const hash2 = feedItemHash('https://example.com/feed-b', 'item-1');
136
+ assert.notEqual(hash1, hash2, 'same item ID in different feeds produces different hash');
137
+ },
138
+ },
139
+
140
+ {
141
+ name: 'feedItemHash-is-20-chars',
142
+ async run({ assert }) {
143
+ const hash = feedItemHash('https://example.com/feed', 'item-123');
144
+ assert.equal(hash.length, 20, 'hash is 20 hex chars');
145
+ },
146
+ },
147
+
148
+ {
149
+ name: 'feedItemHash-is-hex-only',
150
+ async run({ assert }) {
151
+ const hash = feedItemHash('https://example.com/feed', 'item-123');
152
+ assert.ok(/^[0-9a-f]+$/.test(hash), 'hash contains only hex characters');
153
+ },
154
+ },
155
+
156
+ // ============================
157
+ // FIRESTORE TRACKING (emulator)
158
+ // ============================
159
+ {
160
+ name: 'trackFeedItem-writes-correct-schema',
161
+ async run({ assert, admin }) {
162
+ if (!admin) {
163
+ return assert.ok(true, 'skipped: no emulator');
164
+ }
165
+
166
+ const { trackFeedItem } = require(publisherPath);
167
+ const feedUrl = 'https://test-feed.com/rss';
168
+ const item = { id: 'test-item-1', url: 'https://test-feed.com/article-1', title: 'Test Article' };
169
+ const docId = feedItemHash(feedUrl, item.id);
170
+
171
+ await trackFeedItem(admin, {
172
+ feedUrl,
173
+ item,
174
+ brandId: 'test-brand',
175
+ postUrl: 'https://test-brand.com/blog/test-article',
176
+ postSlug: 'test-article',
177
+ });
178
+
179
+ const doc = await admin.firestore().doc(`ghostii-feed-items/${docId}`).get();
180
+ assert.ok(doc.exists, 'tracking doc created');
181
+
182
+ const data = doc.data();
183
+ assert.equal(data.feedUrl, feedUrl);
184
+ assert.equal(data.itemId, 'test-item-1');
185
+ assert.equal(data.itemUrl, 'https://test-feed.com/article-1');
186
+ assert.equal(data.itemTitle, 'Test Article');
187
+ assert.equal(data.brandId, 'test-brand');
188
+ assert.equal(data.postUrl, 'https://test-brand.com/blog/test-article');
189
+ assert.equal(data.postSlug, 'test-article');
190
+ assert.ok(data.metadata, 'has metadata object');
191
+ },
192
+ },
193
+
194
+ {
195
+ name: 'getProcessedItemIds-returns-tracked-ids',
196
+ async run({ assert, admin }) {
197
+ if (!admin) {
198
+ return assert.ok(true, 'skipped: no emulator');
199
+ }
200
+
201
+ const { getProcessedItemIds, trackFeedItem } = require(publisherPath);
202
+ const feedUrl = 'https://processed-test.com/rss';
203
+
204
+ // Track two items
205
+ await trackFeedItem(admin, {
206
+ feedUrl,
207
+ item: { id: 'proc-1', url: 'https://processed-test.com/a1', title: 'Article 1' },
208
+ brandId: 'test-brand',
209
+ postUrl: null,
210
+ postSlug: null,
211
+ });
212
+ await trackFeedItem(admin, {
213
+ feedUrl,
214
+ item: { id: 'proc-2', url: 'https://processed-test.com/a2', title: 'Article 2' },
215
+ brandId: 'test-brand',
216
+ postUrl: null,
217
+ postSlug: null,
218
+ });
219
+
220
+ const ids = await getProcessedItemIds(admin, feedUrl);
221
+ assert.ok(ids.has('proc-1'), 'first item tracked');
222
+ assert.ok(ids.has('proc-2'), 'second item tracked');
223
+ assert.ok(!ids.has('proc-3'), 'untracked item not present');
224
+ },
225
+ },
226
+
227
+ {
228
+ name: 'getProcessedItemIds-scoped-to-feed-url',
229
+ async run({ assert, admin }) {
230
+ if (!admin) {
231
+ return assert.ok(true, 'skipped: no emulator');
232
+ }
233
+
234
+ const { getProcessedItemIds, trackFeedItem } = require(publisherPath);
235
+
236
+ // Track item in feed A
237
+ await trackFeedItem(admin, {
238
+ feedUrl: 'https://feed-a.com/rss',
239
+ item: { id: 'scoped-1', url: 'https://feed-a.com/article', title: 'Feed A Article' },
240
+ brandId: 'test-brand',
241
+ postUrl: null,
242
+ postSlug: null,
243
+ });
244
+
245
+ // Query feed B — should NOT see feed A's items
246
+ const ids = await getProcessedItemIds(admin, 'https://feed-b.com/rss');
247
+ assert.ok(!ids.has('scoped-1'), 'feed A item not visible in feed B query');
248
+ },
249
+ },
250
+
251
+ {
252
+ name: 'getProcessedItemIds-returns-empty-set-without-admin',
253
+ async run({ assert }) {
254
+ const { getProcessedItemIds } = require(publisherPath);
255
+ const ids = await getProcessedItemIds(null, 'https://example.com/feed');
256
+ assert.equal(ids.size, 0, 'returns empty Set when admin is null');
257
+ },
258
+ },
259
+
260
+ // ============================
261
+ // SOURCE RESOLUTION (resolveSource)
262
+ // ============================
263
+ {
264
+ name: 'resolveSource-app-returns-description-no-sourceContent',
265
+ async run({ assert }) {
266
+ const { resolveSource } = require(publisherPath);
267
+ const mockAssistant = { log() {}, error() {} };
268
+ const settings = {
269
+ brand: { brand: { name: 'Test', description: 'A test brand', id: 'test' } },
270
+ prompt: 'Focus on AI',
271
+ };
272
+
273
+ const result = await resolveSource(mockAssistant, '$app', settings, null);
274
+ assert.ok(result.description.includes('Test'), 'description includes brand name');
275
+ assert.ok(result.description.includes('Focus on AI'), 'description includes custom prompt');
276
+ assert.equal(result.sourceContent, '', 'no sourceContent for $app');
277
+ assert.equal(result.feedItem, undefined, 'no feedItem for $app');
278
+ },
279
+ },
280
+
281
+ {
282
+ name: 'resolveSource-text-returns-suggestion-in-description',
283
+ async run({ assert }) {
284
+ const { resolveSource } = require(publisherPath);
285
+ const mockAssistant = { log() {}, error() {} };
286
+ const settings = {
287
+ brand: { brand: { name: 'Test', description: 'A test brand', id: 'test' } },
288
+ prompt: '',
289
+ };
290
+
291
+ const result = await resolveSource(mockAssistant, 'Write about blockchain technology', settings, null);
292
+ assert.ok(result.description.includes('blockchain technology'), 'text source in description');
293
+ assert.equal(result.sourceContent, '', 'no sourceContent for text');
294
+ },
295
+ },
296
+
297
+ {
298
+ name: 'resolveSource-feed-falls-back-to-app-without-admin',
299
+ async run({ assert }) {
300
+ const { resolveSource } = require(publisherPath);
301
+ const mockAssistant = { log() {}, error() {} };
302
+ const settings = {
303
+ brand: { brand: { name: 'Test', description: 'A test brand', id: 'test' } },
304
+ prompt: '',
305
+ };
306
+
307
+ const result = await resolveSource(mockAssistant, '$feed:https://nonexistent.example.com/feed', settings, null);
308
+ assert.ok(result.description, 'falls back to $app and returns description');
309
+ assert.equal(result.sourceContent, '', 'no sourceContent on fallback');
310
+ },
311
+ },
312
+
313
+ // ============================
314
+ // MIXED SOURCE DETECTION
315
+ // ============================
316
+ {
317
+ name: 'source-type-detection-covers-all-types',
318
+ async run({ assert }) {
319
+ const sources = [
320
+ '$app',
321
+ '$feed:https://example.com/feed',
322
+ 'https://example.com/page',
323
+ 'Write about technology trends',
324
+ ];
325
+
326
+ // $app
327
+ assert.equal(sources[0], '$app');
328
+ assert.equal(sources[0].startsWith('$feed:'), false);
329
+
330
+ // $feed:
331
+ assert.equal(sources[1].startsWith('$feed:'), true);
332
+
333
+ // URL
334
+ try { new URL(sources[2]); assert.ok(true, 'URL is valid'); } catch (e) { assert.fail('URL should be valid'); }
335
+
336
+ // text (not $app, not $feed:, not URL)
337
+ assert.ok(!sources[3].startsWith('$feed:'), 'text is not feed');
338
+ try { new URL(sources[3]); assert.fail('text should not be URL'); } catch (e) { assert.ok(true, 'text is not URL'); }
339
+ },
340
+ },
341
+ ],
342
+ };