@@ -14,6 +14,7 @@ import { updateUserConfig } from '@app/store/modules/config';
1414import { FileLoadStatus } from '@app/store/types' ;
1515
1616import { ApiKeyService } from './api-key.service' ;
17+ import { environment } from '@app/environment' ;
1718
1819// Mock the store and its modules
1920vi . mock ( '@app/store' , ( ) => ( {
@@ -84,6 +85,7 @@ describe('ApiKeyService', () => {
8485 } ;
8586
8687 beforeEach ( async ( ) => {
88+ environment . IS_MAIN_PROCESS = true ;
8789 vi . resetAllMocks ( ) ;
8890
8991 // Create mock logger methods
@@ -159,7 +161,7 @@ describe('ApiKeyService', () => {
159161 const { key, id, description, roles } = mockApiKeyWithSecret ;
160162 const name = 'Test API Key' ;
161163
162- const result = await apiKeyService . create ( name , description ?? '' , roles ) ;
164+ const result = await apiKeyService . create ( { name, description : description ?? '' , roles } ) ;
163165
164166 expect ( result ) . toMatchObject ( {
165167 id,
@@ -176,17 +178,23 @@ describe('ApiKeyService', () => {
176178 it ( 'should validate input parameters' , async ( ) => {
177179 const saveSpy = vi . spyOn ( apiKeyService , 'saveApiKey' ) ;
178180
179- await expect ( apiKeyService . create ( '' , 'desc' , [ Role . GUEST ] ) ) . rejects . toThrow (
181+ await expect (
182+ apiKeyService . create ( { name : '' , description : 'desc' , roles : [ Role . GUEST ] } )
183+ ) . rejects . toThrow (
180184 'API key name must contain only letters, numbers, and spaces (Unicode letters are supported)'
181185 ) ;
182186
183- await expect ( apiKeyService . create ( 'name' , 'desc' , [ ] ) ) . rejects . toThrow (
184- 'At least one role must be specified'
185- ) ;
187+ await expect (
188+ apiKeyService . create ( { name : 'name' , description : 'desc' , roles : [ ] } )
189+ ) . rejects . toThrow ( 'At least one role must be specified' ) ;
186190
187- await expect ( apiKeyService . create ( 'name' , 'desc' , [ 'invalid_role' as Role ] ) ) . rejects . toThrow (
188- 'Invalid role specified'
189- ) ;
191+ await expect (
192+ apiKeyService . create ( {
193+ name : 'name' ,
194+ description : 'desc' ,
195+ roles : [ 'invalid_role' as Role ] ,
196+ } )
197+ ) . rejects . toThrow ( 'Invalid role specified' ) ;
190198
191199 expect ( saveSpy ) . not . toHaveBeenCalled ( ) ;
192200 } ) ;
@@ -248,12 +256,12 @@ describe('ApiKeyService', () => {
248256
249257 await apiKeyService [ 'createLocalApiKeyForConnectIfNecessary' ] ( ) ;
250258
251- expect ( apiKeyService . create ) . toHaveBeenCalledWith (
252- 'Connect' ,
253- 'API key for Connect user' ,
254- [ Role . CONNECT ] ,
255- true
256- ) ;
259+ expect ( apiKeyService . create ) . toHaveBeenCalledWith ( {
260+ name : 'Connect' ,
261+ description : 'API key for Connect user' ,
262+ roles : [ Role . CONNECT ] ,
263+ overwrite : true ,
264+ } ) ;
257265 expect ( store . dispatch ) . toHaveBeenCalledWith (
258266 updateUserConfig ( {
259267 remote : {
@@ -263,15 +271,12 @@ describe('ApiKeyService', () => {
263271 ) ;
264272 } ) ;
265273
266- it ( 'should throw error if key creation fails' , async ( ) => {
274+ it ( 'should log an error if key creation fails' , async ( ) => {
267275 vi . spyOn ( apiKeyService , 'findByField' ) . mockReturnValue ( null ) ;
268- vi . spyOn ( apiKeyService , 'create' ) . mockResolvedValue ( {
269- ...mockApiKeyWithSecret ,
270- key : '' , // Empty string instead of undefined/null
271- } as ApiKeyWithSecret ) ;
276+ vi . spyOn ( apiKeyService , 'createLocalConnectApiKey' ) . mockResolvedValue ( null ) ;
272277
273- await expect ( apiKeyService [ 'createLocalApiKeyForConnectIfNecessary' ] ( ) ) . rejects . toThrow (
274- 'Failed to create local API key'
278+ await expect ( apiKeyService [ 'createLocalApiKeyForConnectIfNecessary' ] ( ) ) . resolves . toBe (
279+ undefined
275280 ) ;
276281 expect ( mockLogger . error ) . toHaveBeenCalledWith (
277282 'Failed to create local API key - no key returned'
@@ -431,60 +436,6 @@ describe('ApiKeyService', () => {
431436 } ) ;
432437 } ) ;
433438
434- describe ( 'findOneByKey' , ( ) => {
435- it ( 'should return UserAccount when API key exists' , async ( ) => {
436- const findByKeySpy = vi
437- . spyOn ( apiKeyService , 'findByKey' )
438- . mockResolvedValue ( mockApiKeyWithSecret ) ;
439- const result = await apiKeyService . findOneByKey ( 'test-api-key' ) ;
440-
441- expect ( result ) . toEqual ( {
442- id : mockApiKeyWithSecret . id ,
443- name : mockApiKeyWithSecret . name ,
444- description : mockApiKeyWithSecret . description ,
445- roles : mockApiKeyWithSecret . roles ,
446- } ) ;
447- expect ( findByKeySpy ) . toHaveBeenCalledWith ( 'test-api-key' ) ;
448- } ) ;
449-
450- it ( 'should use default description when none provided' , async ( ) => {
451- const keyWithoutDesc = { ...mockApiKeyWithSecret , description : null } ;
452- vi . spyOn ( apiKeyService , 'findByKey' ) . mockResolvedValue ( keyWithoutDesc ) ;
453- const result = await apiKeyService . findOneByKey ( 'test-api-key' ) ;
454-
455- expect ( result ) . toEqual ( {
456- id : keyWithoutDesc . id ,
457- name : keyWithoutDesc . name ,
458- description : `API Key ${ keyWithoutDesc . name } ` ,
459- roles : keyWithoutDesc . roles ,
460- } ) ;
461- } ) ;
462-
463- it ( 'should return null when API key not found' , async ( ) => {
464- vi . spyOn ( apiKeyService , 'findByKey' ) . mockResolvedValue ( null ) ;
465-
466- await expect ( apiKeyService . findOneByKey ( 'non-existent-key' ) ) . rejects . toThrow (
467- 'API key not found'
468- ) ;
469- } ) ;
470-
471- it ( 'should throw error when API key not found' , async ( ) => {
472- vi . spyOn ( apiKeyService , 'findByKey' ) . mockResolvedValue ( null ) ;
473-
474- await expect ( apiKeyService . findOneByKey ( 'non-existent-key' ) ) . rejects . toThrow (
475- 'API key not found'
476- ) ;
477- } ) ;
478-
479- it ( 'should throw error when unexpected error occurs' , async ( ) => {
480- vi . spyOn ( apiKeyService , 'findByKey' ) . mockRejectedValue ( new Error ( 'Test error' ) ) ;
481-
482- await expect ( apiKeyService . findOneByKey ( 'test-api-key' ) ) . rejects . toThrow (
483- 'Failed to retrieve user account'
484- ) ;
485- } ) ;
486- } ) ;
487-
488439 describe ( 'saveApiKey' , ( ) => {
489440 it ( 'should save API key to file' , async ( ) => {
490441 vi . mocked ( ApiKeyWithSecretSchema ) . mockReturnValue ( {
0 commit comments