@@ -3,12 +3,14 @@ import { Test, TestingModule } from '@nestjs/testing';
33import { BadRequestException } from '@nestjs/common' ;
44import { CollectProcessor } from './collect.processor' ;
55import { NamespaceResourcesService } from 'omniboxd/namespace-resources/namespace-resources.service' ;
6+ import { TagService } from 'omniboxd/tag/tag.service' ;
67import { Task } from 'omniboxd/tasks/tasks.entity' ;
78import { Resource } from 'omniboxd/resources/entities/resource.entity' ;
89
910describe ( 'CollectProcessor' , ( ) => {
1011 let processor : CollectProcessor ;
1112 let namespaceResourcesService : jest . Mocked < NamespaceResourcesService > ;
13+ let tagService : jest . Mocked < TagService > ;
1214
1315 const mockResource : Partial < Resource > = {
1416 id : 'test-resource-id' ,
@@ -24,17 +26,26 @@ describe('CollectProcessor', () => {
2426 update : jest . fn ( ) ,
2527 } ;
2628
29+ const mockTagService = {
30+ getOrCreateTagsByNames : jest . fn ( ) ,
31+ } ;
32+
2733 const module : TestingModule = await Test . createTestingModule ( {
2834 providers : [
2935 {
3036 provide : NamespaceResourcesService ,
3137 useValue : mockResourcesService ,
3238 } ,
39+ {
40+ provide : TagService ,
41+ useValue : mockTagService ,
42+ } ,
3343 ] ,
3444 } ) . compile ( ) ;
3545
3646 namespaceResourcesService = module . get ( NamespaceResourcesService ) ;
37- processor = new CollectProcessor ( namespaceResourcesService ) ;
47+ tagService = module . get ( TagService ) ;
48+ processor = new CollectProcessor ( namespaceResourcesService , tagService ) ;
3849 } ) ;
3950
4051 afterEach ( ( ) => {
@@ -291,6 +302,90 @@ describe('CollectProcessor', () => {
291302 } ,
292303 ) ;
293304 } ) ;
305+
306+ it ( 'should handle tags in metadata and return tagIds' , async ( ) => {
307+ const task = createMockTask ( {
308+ payload : { resource_id : 'test-resource-id' } ,
309+ output : {
310+ markdown : 'content' ,
311+ title : 'title' ,
312+ metadata : {
313+ tags : [ 'tag1' , 'tag2' , 'tag3' ] ,
314+ otherMeta : 'value' ,
315+ } ,
316+ } ,
317+ } ) ;
318+
319+ namespaceResourcesService . get . mockResolvedValue (
320+ mockResource as Resource ,
321+ ) ;
322+ namespaceResourcesService . update . mockResolvedValue ( undefined ) ;
323+ tagService . getOrCreateTagsByNames . mockResolvedValue ( [ 'id1' , 'id2' , 'id3' ] ) ;
324+
325+ const result = await processor . process ( task ) ;
326+
327+ expect ( tagService . getOrCreateTagsByNames ) . toHaveBeenCalledWith (
328+ 'test-namespace' ,
329+ [ 'tag1' , 'tag2' , 'tag3' ] ,
330+ ) ;
331+ expect ( namespaceResourcesService . update ) . toHaveBeenCalledWith (
332+ 'test-user' ,
333+ 'test-resource-id' ,
334+ {
335+ namespaceId : 'test-namespace' ,
336+ name : 'title' ,
337+ content : 'content' ,
338+ attrs : {
339+ url : 'https://example.com' ,
340+ metadata : {
341+ otherMeta : 'value' ,
342+ } ,
343+ } ,
344+ tag_ids : [ 'id1' , 'id2' , 'id3' ] ,
345+ } ,
346+ ) ;
347+ expect ( result ) . toEqual ( { resourceId : 'test-resource-id' , tagIds : [ 'id1' , 'id2' , 'id3' ] } ) ;
348+ } ) ;
349+
350+ it ( 'should handle images in output and replace links' , async ( ) => {
351+ const task = createMockTask ( {
352+ payload : { resource_id : 'test-resource-id' } ,
353+ output : {
354+ markdown : 'content with ' ,
355+ title : 'title' ,
356+ images : [ {
357+ originalLink : 'http://example.com/image.png' ,
358+ attachmentId : 'attachment-123' ,
359+ } ] ,
360+ } ,
361+ } ) ;
362+
363+ namespaceResourcesService . get . mockResolvedValue (
364+ mockResource as Resource ,
365+ ) ;
366+ namespaceResourcesService . update . mockResolvedValue ( undefined ) ;
367+
368+ const result = await processor . process ( task ) ;
369+
370+ expect ( namespaceResourcesService . update ) . toHaveBeenCalledWith (
371+ 'test-user' ,
372+ 'test-resource-id' ,
373+ {
374+ namespaceId : 'test-namespace' ,
375+ name : 'title' ,
376+ content : 'content with ' ,
377+ attrs : {
378+ url : 'https://example.com' ,
379+ images : [ {
380+ originalLink : 'http://example.com/image.png' ,
381+ attachmentId : 'attachment-123' ,
382+ } ] ,
383+ } ,
384+ tag_ids : undefined ,
385+ } ,
386+ ) ;
387+ expect ( result ) . toEqual ( { resourceId : 'test-resource-id' , tagIds : undefined } ) ;
388+ } ) ;
294389 } ) ;
295390
296391 describe ( 'edge cases' , ( ) => {
@@ -327,13 +422,14 @@ describe('CollectProcessor', () => {
327422 {
328423 namespaceId : 'test-namespace' ,
329424 name : undefined ,
330- content : undefined ,
425+ content : '' ,
331426 attrs : {
332427 url : 'https://example.com' ,
333428 } ,
429+ tag_ids : undefined ,
334430 } ,
335431 ) ;
336- expect ( result ) . toEqual ( { resourceId : 'test-resource-id' } ) ;
432+ expect ( result ) . toEqual ( { resourceId : 'test-resource-id' , tagIds : undefined } ) ;
337433 } ) ;
338434 } ) ;
339435 } ) ;
0 commit comments