|
| 1 | +import { HighlightsCanonicalPublishedMessage } from '@dailydotdev/schema'; |
| 2 | +import { DataSource } from 'typeorm'; |
| 3 | +import { HighlightSignificance } from '../../src/common/channelHighlight/significance'; |
| 4 | +import { NEW_HIGHLIGHT_CHANNEL } from '../../src/common/highlights'; |
| 5 | +import { HighlightsCanonical } from '../../src/entity/HighlightsCanonical'; |
| 6 | +import { Source, SourceType } from '../../src/entity/Source'; |
| 7 | +import { ArticlePost } from '../../src/entity/posts/ArticlePost'; |
| 8 | +import { PostType } from '../../src/entity/posts/Post'; |
| 9 | +import { redisPubSub } from '../../src/redis'; |
| 10 | +import worker from '../../src/workers/newHighlightRealTime'; |
| 11 | +import { typedWorkers } from '../../src/workers'; |
| 12 | +import createOrGetConnection from '../../src/db'; |
| 13 | +import { expectSuccessfulTypedBackground } from '../helpers'; |
| 14 | + |
| 15 | +let con: DataSource; |
| 16 | + |
| 17 | +beforeAll(async () => { |
| 18 | + con = await createOrGetConnection(); |
| 19 | +}); |
| 20 | + |
| 21 | +describe('newHighlightRealTime worker', () => { |
| 22 | + beforeEach(async () => { |
| 23 | + jest.resetAllMocks(); |
| 24 | + |
| 25 | + await con.getRepository(Source).save({ |
| 26 | + id: 'highlight-source', |
| 27 | + name: 'Highlight Source', |
| 28 | + image: 'https://example.com/highlight.png', |
| 29 | + handle: 'highlight-source', |
| 30 | + type: SourceType.Machine, |
| 31 | + active: true, |
| 32 | + private: false, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + if (!con) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + await con.getRepository(HighlightsCanonical).clear(); |
| 42 | + await con.getRepository(ArticlePost).delete(['highlight-post']); |
| 43 | + await con.getRepository(Source).delete(['highlight-source']); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should be registered', () => { |
| 47 | + const registeredWorker = typedWorkers.find( |
| 48 | + (item) => item.subscription === worker.subscription, |
| 49 | + ); |
| 50 | + |
| 51 | + expect(registeredWorker).toBeDefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should publish full highlights to the broadcast redis channel', async () => { |
| 55 | + await con.getRepository(ArticlePost).save({ |
| 56 | + id: 'highlight-post', |
| 57 | + shortId: 'highlight-post', |
| 58 | + title: 'Highlight post', |
| 59 | + url: 'https://example.com/highlight-post', |
| 60 | + canonicalUrl: 'https://example.com/highlight-post', |
| 61 | + score: 0, |
| 62 | + sourceId: 'highlight-source', |
| 63 | + visible: true, |
| 64 | + deleted: false, |
| 65 | + banned: false, |
| 66 | + showOnFeed: true, |
| 67 | + createdAt: new Date('2026-04-26T10:00:00.000Z'), |
| 68 | + metadataChangedAt: new Date('2026-04-26T10:00:00.000Z'), |
| 69 | + type: PostType.Article, |
| 70 | + }); |
| 71 | + |
| 72 | + const highlight = await con.getRepository(HighlightsCanonical).save({ |
| 73 | + postId: 'highlight-post', |
| 74 | + channels: ['backend', 'javascript', 'vibes'], |
| 75 | + highlightedAt: new Date('2026-04-26T10:05:00.000Z'), |
| 76 | + headline: 'New canonical highlight', |
| 77 | + significance: HighlightSignificance.Major, |
| 78 | + }); |
| 79 | + const publishSpy = jest.spyOn(redisPubSub, 'publish'); |
| 80 | + |
| 81 | + await expectSuccessfulTypedBackground<'api.v1.post-highlighted'>( |
| 82 | + worker, |
| 83 | + new HighlightsCanonicalPublishedMessage({ |
| 84 | + highlightId: highlight.id, |
| 85 | + channels: highlight.channels, |
| 86 | + publishedChannels: ['backend', 'javascript'], |
| 87 | + }), |
| 88 | + ); |
| 89 | + |
| 90 | + expect(publishSpy).toHaveBeenCalledTimes(2); |
| 91 | + for (const channel of ['backend', 'javascript']) { |
| 92 | + expect(publishSpy).toHaveBeenCalledWith( |
| 93 | + NEW_HIGHLIGHT_CHANNEL, |
| 94 | + expect.objectContaining({ |
| 95 | + id: highlight.id, |
| 96 | + postId: 'highlight-post', |
| 97 | + channel, |
| 98 | + channels: ['backend', 'javascript', 'vibes'], |
| 99 | + headline: 'New canonical highlight', |
| 100 | + significance: 'major', |
| 101 | + highlightedAt: highlight.highlightedAt, |
| 102 | + createdAt: highlight.createdAt, |
| 103 | + updatedAt: highlight.updatedAt, |
| 104 | + post: expect.objectContaining({ |
| 105 | + id: 'highlight-post', |
| 106 | + title: 'Highlight post', |
| 107 | + source: expect.objectContaining({ |
| 108 | + id: 'highlight-source', |
| 109 | + handle: 'highlight-source', |
| 110 | + }), |
| 111 | + }), |
| 112 | + }), |
| 113 | + ); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + it('should fallback to canonical channels when published channels are missing', async () => { |
| 118 | + await con.getRepository(ArticlePost).save({ |
| 119 | + id: 'highlight-post', |
| 120 | + shortId: 'highlight-post', |
| 121 | + title: 'Highlight post', |
| 122 | + url: 'https://example.com/highlight-post', |
| 123 | + canonicalUrl: 'https://example.com/highlight-post', |
| 124 | + score: 0, |
| 125 | + sourceId: 'highlight-source', |
| 126 | + visible: true, |
| 127 | + deleted: false, |
| 128 | + banned: false, |
| 129 | + showOnFeed: true, |
| 130 | + createdAt: new Date('2026-04-26T10:00:00.000Z'), |
| 131 | + metadataChangedAt: new Date('2026-04-26T10:00:00.000Z'), |
| 132 | + type: PostType.Article, |
| 133 | + }); |
| 134 | + |
| 135 | + const highlight = await con.getRepository(HighlightsCanonical).save({ |
| 136 | + postId: 'highlight-post', |
| 137 | + channels: ['backend'], |
| 138 | + highlightedAt: new Date('2026-04-26T10:05:00.000Z'), |
| 139 | + headline: 'New canonical highlight', |
| 140 | + significance: HighlightSignificance.Major, |
| 141 | + }); |
| 142 | + const publishSpy = jest.spyOn(redisPubSub, 'publish'); |
| 143 | + |
| 144 | + await expectSuccessfulTypedBackground<'api.v1.post-highlighted'>( |
| 145 | + worker, |
| 146 | + new HighlightsCanonicalPublishedMessage({ |
| 147 | + highlightId: highlight.id, |
| 148 | + channels: ['backend'], |
| 149 | + }), |
| 150 | + ); |
| 151 | + |
| 152 | + expect(publishSpy).toHaveBeenCalledWith( |
| 153 | + NEW_HIGHLIGHT_CHANNEL, |
| 154 | + expect.objectContaining({ |
| 155 | + id: highlight.id, |
| 156 | + channel: 'backend', |
| 157 | + }), |
| 158 | + ); |
| 159 | + }); |
| 160 | +}); |
0 commit comments