11import { describe , it , expect , vi , beforeEach } from 'vitest' ;
22import { StocksResource } from '../../../../src/modules/core-business/stocks/resource.js' ;
33import { lowStockToolImplementation } from '../../../../src/modules/core-business/stocks/tool.js' ;
4- import { FacturaScriptsClient } from '../../../../src/fs/client.js' ;
54import type { Stock } from '../../../../src/types/facturascripts.js' ;
65
7- // Mock the FacturaScriptsClient
8- vi . mock ( '../../../../src/fs/client.js' ) ;
9-
106describe ( 'StocksResource' , ( ) => {
11- let mockClient : FacturaScriptsClient ;
7+ let mockClient : any ;
128 let stocksResource : StocksResource ;
139
1410 beforeEach ( ( ) => {
15- mockClient = new FacturaScriptsClient ( ) ;
16- stocksResource = new StocksResource ( mockClient ) ;
1711 vi . clearAllMocks ( ) ;
12+ mockClient = {
13+ getWithPagination : vi . fn ( )
14+ } ;
15+ stocksResource = new StocksResource ( mockClient ) ;
1816 } ) ;
1917
2018 describe ( 'matchesUri' , ( ) => {
@@ -72,7 +70,7 @@ describe('StocksResource', () => {
7270 } ;
7371
7472 it ( 'should return stocks data with default pagination' , async ( ) => {
75- vi . mocked ( mockClient . getWithPagination ) . mockResolvedValue ( mockPaginatedResponse ) ;
73+ mockClient . getWithPagination . mockResolvedValue ( mockPaginatedResponse ) ;
7674
7775 const result = await stocksResource . getResource ( 'facturascripts://stocks' ) ;
7876
@@ -92,7 +90,7 @@ describe('StocksResource', () => {
9290 } ) ;
9391
9492 it ( 'should parse and use limit and offset from URI' , async ( ) => {
95- vi . mocked ( mockClient . getWithPagination ) . mockResolvedValue ( {
93+ mockClient . getWithPagination . mockResolvedValue ( {
9694 ...mockPaginatedResponse ,
9795 meta : { ...mockPaginatedResponse . meta , limit : 25 , offset : 10 } ,
9896 } ) ;
@@ -106,7 +104,7 @@ describe('StocksResource', () => {
106104
107105 it ( 'should handle API errors gracefully' , async ( ) => {
108106 const errorMessage = 'Stock API connection failed' ;
109- vi . mocked ( mockClient . getWithPagination ) . mockRejectedValue ( new Error ( errorMessage ) ) ;
107+ mockClient . getWithPagination . mockRejectedValue ( new Error ( errorMessage ) ) ;
110108
111109 const result = await stocksResource . getResource ( 'facturascripts://stocks' ) ;
112110
@@ -118,11 +116,13 @@ describe('StocksResource', () => {
118116} ) ;
119117
120118describe ( 'lowStockToolImplementation' , ( ) => {
121- let mockClient : FacturaScriptsClient ;
119+ let mockClient : any ;
122120
123121 beforeEach ( ( ) => {
124- mockClient = new FacturaScriptsClient ( ) ;
125122 vi . clearAllMocks ( ) ;
123+ mockClient = {
124+ getWithPagination : vi . fn ( )
125+ } ;
126126 } ) ;
127127
128128 const mockStocksWithLowStock : Stock [ ] = [
@@ -169,7 +169,7 @@ describe('lowStockToolImplementation', () => {
169169 describe ( 'successful scenarios' , ( ) => {
170170 it ( 'should return products with stock below or equal to minimum by default' , async ( ) => {
171171 // Mock stocks response
172- vi . mocked ( mockClient . getWithPagination )
172+ mockClient . getWithPagination
173173 . mockResolvedValueOnce ( {
174174 meta : { total : 4 , limit : 1000 , offset : 0 , hasMore : false } ,
175175 data : mockStocksWithLowStock ,
@@ -206,7 +206,7 @@ describe('lowStockToolImplementation', () => {
206206 } ) ;
207207
208208 it ( 'should exclude products with stock equal to minimum when explicitly requested' , async ( ) => {
209- vi . mocked ( mockClient . getWithPagination )
209+ mockClient . getWithPagination
210210 . mockResolvedValueOnce ( {
211211 meta : { total : 4 , limit : 1000 , offset : 0 , hasMore : false } ,
212212 data : mockStocksWithLowStock ,
@@ -232,7 +232,7 @@ describe('lowStockToolImplementation', () => {
232232 } ) ;
233233
234234 it ( 'should include products with stock equal to minimum when explicitly requested' , async ( ) => {
235- vi . mocked ( mockClient . getWithPagination )
235+ mockClient . getWithPagination
236236 . mockResolvedValueOnce ( {
237237 meta : { total : 4 , limit : 1000 , offset : 0 , hasMore : false } ,
238238 data : mockStocksWithLowStock ,
@@ -263,7 +263,7 @@ describe('lowStockToolImplementation', () => {
263263 it ( 'should filter by warehouse when codalmacen is provided' , async ( ) => {
264264 const filteredStocks = mockStocksWithLowStock . filter ( s => s . codalmacen === 'ALM002' ) ;
265265
266- vi . mocked ( mockClient . getWithPagination )
266+ mockClient . getWithPagination
267267 . mockResolvedValueOnce ( {
268268 meta : { total : 1 , limit : 1000 , offset : 0 , hasMore : false } ,
269269 data : filteredStocks ,
@@ -292,7 +292,7 @@ describe('lowStockToolImplementation', () => {
292292 } ) ;
293293
294294 it ( 'should handle pagination correctly - middle page' , async ( ) => {
295- vi . mocked ( mockClient . getWithPagination )
295+ mockClient . getWithPagination
296296 . mockResolvedValueOnce ( {
297297 meta : { total : 4 , limit : 1000 , offset : 0 , hasMore : false } ,
298298 data : mockStocksWithLowStock ,
@@ -322,7 +322,7 @@ describe('lowStockToolImplementation', () => {
322322 } ) ;
323323
324324 it ( 'should handle pagination correctly - first page with more data' , async ( ) => {
325- vi . mocked ( mockClient . getWithPagination )
325+ mockClient . getWithPagination
326326 . mockResolvedValueOnce ( {
327327 meta : { total : 4 , limit : 1000 , offset : 0 , hasMore : false } ,
328328 data : mockStocksWithLowStock ,
@@ -348,7 +348,7 @@ describe('lowStockToolImplementation', () => {
348348 } ) ;
349349
350350 it ( 'should sort results by cantidad ascending' , async ( ) => {
351- vi . mocked ( mockClient . getWithPagination )
351+ mockClient . getWithPagination
352352 . mockResolvedValueOnce ( {
353353 meta : { total : 4 , limit : 1000 , offset : 0 , hasMore : false } ,
354354 data : mockStocksWithLowStock ,
@@ -369,7 +369,7 @@ describe('lowStockToolImplementation', () => {
369369
370370 describe ( 'edge cases and error handling' , ( ) => {
371371 it ( 'should return empty result when no stocks exist' , async ( ) => {
372- vi . mocked ( mockClient . getWithPagination ) . mockResolvedValue ( {
372+ mockClient . getWithPagination . mockResolvedValue ( {
373373 meta : { total : 0 , limit : 1000 , offset : 0 , hasMore : false } ,
374374 data : [ ] ,
375375 } ) ;
@@ -385,7 +385,7 @@ describe('lowStockToolImplementation', () => {
385385 it ( 'should return empty result when no products are below or equal minimum stock' , async ( ) => {
386386 const stocksAboveMin = mockStocksWithLowStock . map ( s => ( { ...s , cantidad : s . stockmin ! + 10 } ) ) ;
387387
388- vi . mocked ( mockClient . getWithPagination ) . mockResolvedValue ( {
388+ mockClient . getWithPagination . mockResolvedValue ( {
389389 meta : { total : 4 , limit : 1000 , offset : 0 , hasMore : false } ,
390390 data : stocksAboveMin ,
391391 } ) ;
@@ -401,7 +401,7 @@ describe('lowStockToolImplementation', () => {
401401 it ( 'should skip items without stockmin defined' , async ( ) => {
402402 const stocksWithoutMin = mockStocksWithLowStock . map ( s => ( { ...s , stockmin : undefined } ) ) ;
403403
404- vi . mocked ( mockClient . getWithPagination ) . mockResolvedValue ( {
404+ mockClient . getWithPagination . mockResolvedValue ( {
405405 meta : { total : 4 , limit : 1000 , offset : 0 , hasMore : false } ,
406406 data : stocksWithoutMin ,
407407 } ) ;
@@ -414,7 +414,7 @@ describe('lowStockToolImplementation', () => {
414414 } ) ;
415415
416416 it ( 'should handle parameter validation' , async ( ) => {
417- vi . mocked ( mockClient . getWithPagination )
417+ mockClient . getWithPagination
418418 . mockResolvedValueOnce ( {
419419 meta : { total : 4 , limit : 1000 , offset : 0 , hasMore : false } ,
420420 data : mockStocksWithLowStock ,
@@ -435,7 +435,7 @@ describe('lowStockToolImplementation', () => {
435435 } ) ;
436436
437437 it ( 'should handle API errors gracefully' , async ( ) => {
438- vi . mocked ( mockClient . getWithPagination ) . mockRejectedValue (
438+ mockClient . getWithPagination . mockRejectedValue (
439439 new Error ( 'API connection failed' )
440440 ) ;
441441
@@ -448,7 +448,7 @@ describe('lowStockToolImplementation', () => {
448448 } ) ;
449449
450450 it ( 'should fallback to stock description when product details fail' , async ( ) => {
451- vi . mocked ( mockClient . getWithPagination )
451+ mockClient . getWithPagination
452452 . mockResolvedValueOnce ( {
453453 meta : { total : 1 , limit : 1000 , offset : 0 , hasMore : false } ,
454454 data : [ mockStocksWithLowStock [ 0 ] ] ,
0 commit comments