@@ -2,6 +2,19 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
22import { db } from '../src/lib/db' ;
33import { MediaService } from '../src/modules/media/service' ;
44
5+ // Helper to create a chainable select mock that resolves to `data`
6+ // Works for both db.select().from().where() and db.select().from().where().orderBy().limit().offset()
7+ function makeSelectChain ( data : unknown [ ] ) {
8+ const promise = Promise . resolve ( data ) ;
9+ const chain : Record < string , unknown > = {
10+ then : promise . then . bind ( promise ) ,
11+ catch : promise . catch . bind ( promise ) ,
12+ } ;
13+ const fns = [ 'from' , 'where' , 'orderBy' , 'limit' , 'offset' ] ;
14+ fns . forEach ( fn => { chain [ fn ] = vi . fn ( ) . mockReturnValue ( chain ) ; } ) ;
15+ return chain ;
16+ }
17+
518vi . mock ( '../src/lib/db' , ( ) => ( {
619 db : {
720 query : {
@@ -29,7 +42,7 @@ describe('MediaService', () => {
2942 vi . clearAllMocks ( ) ;
3043 } ) ;
3144
32- describe ( 'searchMedia ' , ( ) => {
45+ describe ( 'search ' , ( ) => {
3346 it ( 'should return media matching search query' , async ( ) => {
3447 const mockMedia = [
3548 {
@@ -40,45 +53,28 @@ describe('MediaService', () => {
4053 name : 'Epic Sunset' ,
4154 mediaType : 'image' ,
4255 status : 'active' ,
56+ creatorUserId : null ,
4357 } ,
4458 ] ;
4559
46- vi . mocked ( db . query . mediaMetadata . findMany ) . mockResolvedValue ( mockMedia as any ) ;
60+ vi . mocked ( db . select ) . mockReturnValue ( makeSelectChain ( mockMedia ) as any ) ;
4761
48- const result = await mediaService . searchMedia ( {
49- search : 'sunset' ,
50- limit : 20 ,
51- offset : 0 ,
52- } ) ;
62+ const result = await mediaService . search ( 'sunset' , 20 , 0 ) ;
5363
54- expect ( result . media ) . toHaveLength ( 1 ) ;
55- expect ( result . media [ 0 ] . name ) . toBe ( 'Epic Sunset' ) ;
64+ expect ( result ) . toHaveLength ( 1 ) ;
65+ expect ( result [ 0 ] . name ) . toBe ( 'Epic Sunset' ) ;
5666 } ) ;
5767
58- it ( 'should filter by media type' , async ( ) => {
59- const mockMedia = [
60- {
61- id : 'media-1' ,
62- ticker : 'VID' ,
63- name : 'Cool Video' ,
64- mediaType : 'video' ,
65- } ,
66- ] ;
67-
68- vi . mocked ( db . query . mediaMetadata . findMany ) . mockResolvedValue ( mockMedia as any ) ;
68+ it ( 'should return empty array when no results' , async ( ) => {
69+ vi . mocked ( db . select ) . mockReturnValue ( makeSelectChain ( [ ] ) as any ) ;
6970
70- const result = await mediaService . searchMedia ( {
71- mediaType : 'video' ,
72- limit : 20 ,
73- offset : 0 ,
74- } ) ;
71+ const result = await mediaService . search ( 'nonexistent' , 20 , 0 ) ;
7572
76- expect ( result . media ) . toHaveLength ( 1 ) ;
77- expect ( result . media [ 0 ] . mediaType ) . toBe ( 'video' ) ;
73+ expect ( result ) . toHaveLength ( 0 ) ;
7874 } ) ;
7975 } ) ;
8076
81- describe ( 'getMediaById ' , ( ) => {
77+ describe ( 'getById ' , ( ) => {
8278 it ( 'should return media by ID' , async ( ) => {
8379 const mockMedia = {
8480 id : 'media-1' ,
@@ -87,45 +83,52 @@ describe('MediaService', () => {
8783 ticker : 'PIC' ,
8884 name : 'Epic Sunset' ,
8985 status : 'active' ,
86+ creatorUserId : null ,
9087 } ;
9188
92- vi . mocked ( db . query . mediaMetadata . findFirst ) . mockResolvedValue ( mockMedia as any ) ;
89+ // getById uses db.select().from().where() which resolves array, then destructures [0]
90+ const chain = makeSelectChain ( [ mockMedia ] ) ;
91+ vi . mocked ( db . select ) . mockReturnValue ( chain as any ) ;
9392
94- const result = await mediaService . getMediaById ( 'media_001' ) ;
93+ const result = await mediaService . getById ( 'media_001' ) ;
9594
9695 expect ( result ) . toBeDefined ( ) ;
9796 expect ( result ?. name ) . toBe ( 'Epic Sunset' ) ;
9897 } ) ;
9998
10099 it ( 'should return null for non-existent media' , async ( ) => {
101- vi . mocked ( db . query . mediaMetadata . findFirst ) . mockResolvedValue ( null ) ;
100+ const chain = makeSelectChain ( [ ] ) ;
101+ vi . mocked ( db . select ) . mockReturnValue ( chain as any ) ;
102102
103- const result = await mediaService . getMediaById ( 'nonexistent' ) ;
103+ const result = await mediaService . getById ( 'nonexistent' ) ;
104104
105105 expect ( result ) . toBeNull ( ) ;
106106 } ) ;
107107 } ) ;
108108
109- describe ( 'getMediaByOwner ' , ( ) => {
109+ describe ( 'getByOwner ' , ( ) => {
110110 it ( 'should return all media owned by address' , async ( ) => {
111111 const mockMedia = [
112112 {
113113 id : 'media-1' ,
114114 ownerAddress : '0x1234567890123456789012345678901234567890' ,
115115 ticker : 'PIC1' ,
116+ creatorUserId : null ,
116117 } ,
117118 {
118119 id : 'media-2' ,
119120 ownerAddress : '0x1234567890123456789012345678901234567890' ,
120121 ticker : 'PIC2' ,
122+ creatorUserId : null ,
121123 } ,
122124 ] ;
123125
124- vi . mocked ( db . query . mediaMetadata . findMany ) . mockResolvedValue ( mockMedia as any ) ;
126+ vi . mocked ( db . select ) . mockReturnValue ( makeSelectChain ( mockMedia ) as any ) ;
125127
126- const result = await mediaService . getMediaByOwner ( '0x1234567890123456789012345678901234567890' ) ;
128+ const result = await mediaService . getByOwner ( '0x1234567890123456789012345678901234567890' ) ;
127129
128130 expect ( result ) . toHaveLength ( 2 ) ;
129131 } ) ;
130132 } ) ;
131133} ) ;
134+
0 commit comments