@@ -3,14 +3,12 @@ import { Test, TestingModule } from '@nestjs/testing';
33import { BadRequestException } from '@nestjs/common' ;
44import { ReaderProcessor } from './reader.processor' ;
55import { ResourcesService } from 'omniboxd/resources/resources.service' ;
6- import { AttachmentsService } from 'omniboxd/attachments/attachments.service' ;
76import { Task } from 'omniboxd/tasks/tasks.entity' ;
87import { Resource } from 'omniboxd/resources/resources.entity' ;
98
109describe ( 'ReaderProcessor' , ( ) => {
1110 let processor : ReaderProcessor ;
1211 let resourcesService : jest . Mocked < ResourcesService > ;
13- let attachmentsService : jest . Mocked < AttachmentsService > ;
1412
1513 const mockResource : Partial < Resource > = {
1614 id : 'test-resource-id' ,
@@ -26,26 +24,18 @@ describe('ReaderProcessor', () => {
2624 update : jest . fn ( ) ,
2725 } ;
2826
29- const mockAttachmentsService = {
30- uploadAttachment : jest . fn ( ) ,
31- } ;
3227
3328 const module : TestingModule = await Test . createTestingModule ( {
3429 providers : [
3530 {
3631 provide : ResourcesService ,
3732 useValue : mockResourcesService ,
3833 } ,
39- {
40- provide : AttachmentsService ,
41- useValue : mockAttachmentsService ,
42- } ,
4334 ] ,
4435 } ) . compile ( ) ;
4536
4637 resourcesService = module . get ( ResourcesService ) ;
47- attachmentsService = module . get ( AttachmentsService ) ;
48- processor = new ReaderProcessor ( resourcesService , attachmentsService ) ;
38+ processor = new ReaderProcessor ( resourcesService ) ;
4939 } ) ;
5040
5141 afterEach ( ( ) => {
@@ -81,7 +71,6 @@ describe('ReaderProcessor', () => {
8171 const result = await processor . process ( task ) ;
8272
8373 expect ( result ) . toEqual ( { } ) ;
84- expect ( attachmentsService . uploadAttachment ) . not . toHaveBeenCalled ( ) ;
8574 expect ( resourcesService . get ) . not . toHaveBeenCalled ( ) ;
8675 } ) ;
8776
@@ -93,7 +82,6 @@ describe('ReaderProcessor', () => {
9382 const result = await processor . process ( task ) ;
9483
9584 expect ( result ) . toEqual ( { } ) ;
96- expect ( attachmentsService . uploadAttachment ) . not . toHaveBeenCalled ( ) ;
9785 } ) ;
9886
9987 it ( 'should process markdown without images and call parent process' , async ( ) => {
@@ -111,16 +99,13 @@ describe('ReaderProcessor', () => {
11199 const result = await processor . process ( task ) ;
112100
113101 expect ( result ) . toEqual ( { resourceId : 'test-resource-id' } ) ;
114- expect ( attachmentsService . uploadAttachment ) . not . toHaveBeenCalled ( ) ;
115102 expect ( resourcesService . get ) . toHaveBeenCalledWith ( 'test-resource-id' ) ;
116103 expect ( resourcesService . update ) . toHaveBeenCalled ( ) ;
117104 } ) ;
118105 } ) ;
119106
120107 describe ( 'image processing' , ( ) => {
121- it ( 'should process images and replace links in markdown' , async ( ) => {
122- const base64Data =
123- 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAHGArEkAAAAAElFTkSuQmCC' ;
108+ it ( 'should process processed images and replace links in markdown' , async ( ) => {
124109 const task = createMockTask ( {
125110 payload : { resource_id : 'test-resource-id' } ,
126111 output : {
@@ -129,57 +114,28 @@ describe('ReaderProcessor', () => {
129114 title : 'Test Document' ,
130115 images : [
131116 {
117+ originalLink : 'temp://image1.png' ,
118+ attachmentId : 'attachment-id-1' ,
132119 name : 'image1.png' ,
133- link : 'temp://image1.png' ,
134- data : base64Data ,
135120 mimetype : 'image/png' ,
136121 } ,
137122 {
138- link : 'temp://image2.jpg' ,
139- data : base64Data ,
123+ originalLink : 'temp://image2.jpg' ,
124+ attachmentId : 'attachment-id-2' ,
140125 mimetype : 'image/jpeg' ,
141126 } ,
142127 ] ,
143128 } ,
144129 } ) ;
145130
146- attachmentsService . uploadAttachment
147- . mockResolvedValueOnce ( 'attachment-id-1' )
148- . mockResolvedValueOnce ( 'attachment-id-2' ) ;
149-
150131 resourcesService . get . mockResolvedValue ( mockResource as Resource ) ;
151132 resourcesService . update . mockResolvedValue ( undefined ) ;
152133
153134 const result = await processor . process ( task ) ;
154135
155- // Verify attachment uploads
156- expect ( attachmentsService . uploadAttachment ) . toHaveBeenCalledTimes ( 2 ) ;
157-
158- // First image upload
159- expect ( attachmentsService . uploadAttachment ) . toHaveBeenNthCalledWith (
160- 1 ,
161- 'test-namespace' ,
162- 'test-resource-id' ,
163- 'test-user' ,
164- 'image1.png' ,
165- expect . any ( Buffer ) ,
166- 'image/png' ,
167- ) ;
168-
169- // Second image upload (should use link as name when name is not provided)
170- expect ( attachmentsService . uploadAttachment ) . toHaveBeenNthCalledWith (
171- 2 ,
172- 'test-namespace' ,
173- 'test-resource-id' ,
174- 'test-user' ,
175- 'temp://image2.jpg' ,
176- expect . any ( Buffer ) ,
177- 'image/jpeg' ,
178- ) ;
179-
180136 // Verify markdown was updated with new image URLs
181137 expect ( task . output ! . markdown ) . toBe (
182- '# Test Document\n\n\n\nSome text.\n\n' ,
138+ '# Test Document\n\n\n\nSome text.\n\n' ,
183139 ) ;
184140
185141 // Verify images array was cleared
@@ -190,32 +146,29 @@ describe('ReaderProcessor', () => {
190146 } ) ;
191147
192148 it ( 'should handle multiple occurrences of the same image link' , async ( ) => {
193- const base64Data =
194- 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAHGArEkAAAAAElFTkSuQmCC' ;
195149 const task = createMockTask ( {
196150 payload : { resource_id : 'test-resource-id' } ,
197151 output : {
198152 markdown :
199153 '\n\nText with  again.' ,
200154 images : [
201155 {
156+ originalLink : 'temp://image.png' ,
157+ attachmentId : 'attachment-id' ,
202158 name : 'image.png' ,
203- link : 'temp://image.png' ,
204- data : base64Data ,
205159 mimetype : 'image/png' ,
206160 } ,
207161 ] ,
208162 } ,
209163 } ) ;
210164
211- attachmentsService . uploadAttachment . mockResolvedValue ( 'attachment-id' ) ;
212165 resourcesService . get . mockResolvedValue ( mockResource as Resource ) ;
213166 resourcesService . update . mockResolvedValue ( undefined ) ;
214167
215168 await processor . process ( task ) ;
216169
217170 expect ( task . output ! . markdown ) . toBe (
218- '\n\nText with  again.' ,
171+ '\n\nText with  again.' ,
219172 ) ;
220173 } ) ;
221174
@@ -226,8 +179,8 @@ describe('ReaderProcessor', () => {
226179 markdown : '' ,
227180 images : [
228181 {
229- link : 'temp://image.png' ,
230- data : 'base64data ' ,
182+ originalLink : 'temp://image.png' ,
183+ attachmentId : 'attachment-id ' ,
231184 mimetype : 'image/png' ,
232185 } ,
233186 ] ,
@@ -243,68 +196,29 @@ describe('ReaderProcessor', () => {
243196 } ) ;
244197
245198 it ( 'should handle payload with resourceId instead of resource_id' , async ( ) => {
246- const base64Data =
247- 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAHGArEkAAAAAElFTkSuQmCC' ;
248199 const task = createMockTask ( {
249200 payload : { resourceId : 'test-resource-id' } ,
250201 output : {
251202 markdown : '' ,
252203 images : [
253204 {
254- link : 'temp://image.png' ,
255- data : base64Data ,
205+ originalLink : 'temp://image.png' ,
206+ attachmentId : 'attachment-id' ,
256207 mimetype : 'image/png' ,
257208 } ,
258209 ] ,
259210 } ,
260211 } ) ;
261212
262- attachmentsService . uploadAttachment . mockResolvedValue ( 'attachment-id' ) ;
263213 resourcesService . get . mockResolvedValue ( mockResource as Resource ) ;
264214 resourcesService . update . mockResolvedValue ( undefined ) ;
265215
266216 const result = await processor . process ( task ) ;
267217
268- expect ( attachmentsService . uploadAttachment ) . toHaveBeenCalledWith (
269- 'test-namespace' ,
270- 'test-resource-id' ,
271- 'test-user' ,
272- 'temp://image.png' ,
273- expect . any ( Buffer ) ,
274- 'image/png' ,
275- ) ;
218+ expect ( task . output ! . markdown ) . toBe ( '' ) ;
276219 expect ( result ) . toEqual ( { resourceId : 'test-resource-id' } ) ;
277220 } ) ;
278221
279- it ( 'should convert base64 data to Buffer correctly' , async ( ) => {
280- const base64Data =
281- 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAHGArEkAAAAAElFTkSuQmCC' ;
282- const task = createMockTask ( {
283- payload : { resource_id : 'test-resource-id' } ,
284- output : {
285- markdown : '' ,
286- images : [
287- {
288- link : 'temp://image.png' ,
289- data : base64Data ,
290- mimetype : 'image/png' ,
291- } ,
292- ] ,
293- } ,
294- } ) ;
295-
296- attachmentsService . uploadAttachment . mockResolvedValue ( 'attachment-id' ) ;
297- resourcesService . get . mockResolvedValue ( mockResource as Resource ) ;
298- resourcesService . update . mockResolvedValue ( undefined ) ;
299-
300- await processor . process ( task ) ;
301-
302- const uploadCall = attachmentsService . uploadAttachment . mock . calls [ 0 ] ;
303- const bufferArg = uploadCall [ 4 ] ;
304-
305- expect ( bufferArg ) . toBeInstanceOf ( Buffer ) ;
306- expect ( bufferArg . toString ( 'base64' ) ) . toBe ( base64Data ) ;
307- } ) ;
308222 } ) ;
309223
310224 describe ( 'inheritance from CollectProcessor' , ( ) => {
@@ -322,7 +236,6 @@ describe('ReaderProcessor', () => {
322236 const result = await processor . process ( task ) ;
323237
324238 // Should not process images when there's an exception
325- expect ( attachmentsService . uploadAttachment ) . not . toHaveBeenCalled ( ) ;
326239
327240 // Should call parent's exception handling
328241 expect ( resourcesService . update ) . toHaveBeenCalledWith (
@@ -357,7 +270,6 @@ describe('ReaderProcessor', () => {
357270
358271 const result = await processor . process ( task ) ;
359272
360- expect ( attachmentsService . uploadAttachment ) . not . toHaveBeenCalled ( ) ;
361273 expect ( task . output ! . images ) . toBeUndefined ( ) ;
362274 expect ( result ) . toEqual ( { resourceId : 'test-resource-id' } ) ;
363275 } ) ;
@@ -369,22 +281,21 @@ describe('ReaderProcessor', () => {
369281 markdown : '# Test Document\n\nNo images here.' ,
370282 images : [
371283 {
372- link : 'temp://image.png' ,
373- data : 'base64data ' ,
284+ originalLink : 'temp://image.png' ,
285+ attachmentId : 'attachment-id ' ,
374286 mimetype : 'image/png' ,
375287 } ,
376288 ] ,
377289 } ,
378290 } ) ;
379291
380- attachmentsService . uploadAttachment . mockResolvedValue ( 'attachment-id' ) ;
381292 resourcesService . get . mockResolvedValue ( mockResource as Resource ) ;
382293 resourcesService . update . mockResolvedValue ( undefined ) ;
383294
384295 await processor . process ( task ) ;
385296
386- // Should still process images even if they're not in markdown
387- expect ( attachmentsService . uploadAttachment ) . toHaveBeenCalled ( ) ;
297+ // Markdown should remain unchanged since no image links found
298+ expect ( task . output ! . markdown ) . toBe ( '# Test Document\n\nNo images here.' ) ;
388299 expect ( task . output ! . images ) . toBeUndefined ( ) ;
389300 } ) ;
390301
0 commit comments