|
| 1 | +import type { ForestAdminServerInterface } from '../src/types'; |
| 2 | + |
| 3 | +import * as factories from './__factories__'; |
| 4 | +import buildApplicationServices from '../src/build-application-services'; |
| 5 | +import ForestHttpApi from '../src/permissions/forest-http-api'; |
| 6 | + |
| 7 | +jest.mock('../src/permissions/forest-http-api'); |
| 8 | + |
| 9 | +describe('buildApplicationServices', () => { |
| 10 | + let mockForestHttpApi: jest.Mocked<ForestAdminServerInterface>; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + |
| 15 | + mockForestHttpApi = { |
| 16 | + getRenderingPermissions: jest.fn(), |
| 17 | + getEnvironmentPermissions: jest.fn(), |
| 18 | + getUsers: jest.fn(), |
| 19 | + getModelCustomizations: jest.fn(), |
| 20 | + getMcpServerConfigs: jest.fn(), |
| 21 | + makeAuthService: jest.fn().mockReturnValue({ |
| 22 | + init: jest.fn(), |
| 23 | + getUserInfo: jest.fn(), |
| 24 | + generateAuthorizationUrl: jest.fn(), |
| 25 | + generateTokens: jest.fn(), |
| 26 | + }), |
| 27 | + getSchema: jest.fn(), |
| 28 | + postSchema: jest.fn(), |
| 29 | + checkSchemaHash: jest.fn(), |
| 30 | + getIpWhitelistRules: jest.fn(), |
| 31 | + createActivityLog: jest.fn(), |
| 32 | + updateActivityLogStatus: jest.fn(), |
| 33 | + }; |
| 34 | + |
| 35 | + (ForestHttpApi as jest.Mock).mockImplementation(() => mockForestHttpApi); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('withDefaultImplementation (fallback mechanism)', () => { |
| 39 | + it('should use custom implementation when method is provided', async () => { |
| 40 | + const customGetUsers = jest.fn().mockResolvedValue([{ id: 1, name: 'Custom User' }]); |
| 41 | + const partialInterface: Partial<ForestAdminServerInterface> = { |
| 42 | + getUsers: customGetUsers, |
| 43 | + }; |
| 44 | + |
| 45 | + const options = factories.forestAdminClientOptions.build(); |
| 46 | + buildApplicationServices(partialInterface, options); |
| 47 | + |
| 48 | + // The custom method should be called, not the default |
| 49 | + await customGetUsers(); |
| 50 | + expect(customGetUsers).toHaveBeenCalled(); |
| 51 | + expect(mockForestHttpApi.getUsers).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should fallback to ForestHttpApi when method is not provided', () => { |
| 55 | + const partialInterface: Partial<ForestAdminServerInterface> = { |
| 56 | + // Only provide getUsers, not makeAuthService |
| 57 | + getUsers: jest.fn(), |
| 58 | + }; |
| 59 | + |
| 60 | + const options = factories.forestAdminClientOptions.build(); |
| 61 | + buildApplicationServices(partialInterface, options); |
| 62 | + |
| 63 | + // makeAuthService should have been called from ForestHttpApi (the fallback) |
| 64 | + expect(mockForestHttpApi.makeAuthService).toHaveBeenCalledWith( |
| 65 | + expect.objectContaining({ |
| 66 | + envSecret: options.envSecret, |
| 67 | + }), |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should work with empty partial interface (all methods fallback)', () => { |
| 72 | + const partialInterface: Partial<ForestAdminServerInterface> = {}; |
| 73 | + |
| 74 | + const options = factories.forestAdminClientOptions.build(); |
| 75 | + const result = buildApplicationServices(partialInterface, options); |
| 76 | + |
| 77 | + // All services should be created successfully using ForestHttpApi fallbacks |
| 78 | + expect(result.auth).toBeDefined(); |
| 79 | + expect(result.schema).toBeDefined(); |
| 80 | + expect(result.activityLogs).toBeDefined(); |
| 81 | + expect(mockForestHttpApi.makeAuthService).toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should allow partial override of methods', async () => { |
| 85 | + const customCheckSchemaHash = jest.fn().mockResolvedValue({ sendSchema: false }); |
| 86 | + const partialInterface: Partial<ForestAdminServerInterface> = { |
| 87 | + checkSchemaHash: customCheckSchemaHash, |
| 88 | + // postSchema not provided - should fallback |
| 89 | + }; |
| 90 | + |
| 91 | + const options = factories.forestAdminClientOptions.build(); |
| 92 | + const { schema } = buildApplicationServices(partialInterface, options); |
| 93 | + |
| 94 | + // Mock the postSchema to not actually call the server |
| 95 | + mockForestHttpApi.postSchema.mockResolvedValue(undefined); |
| 96 | + |
| 97 | + await schema.postSchema({ |
| 98 | + collections: [], |
| 99 | + meta: { |
| 100 | + liana: 'test', |
| 101 | + liana_version: '1.0.0', |
| 102 | + liana_features: null, |
| 103 | + stack: { engine: 'nodejs', engine_version: '16.0.0' }, |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + // Custom checkSchemaHash should be used |
| 108 | + expect(customCheckSchemaHash).toHaveBeenCalled(); |
| 109 | + // postSchema should fallback to ForestHttpApi (but not called since sendSchema: false) |
| 110 | + expect(mockForestHttpApi.postSchema).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should correctly bind this context for custom methods', async () => { |
| 114 | + const customState = { called: false }; |
| 115 | + const customGetUsers = jest |
| 116 | + .fn() |
| 117 | + .mockImplementation(function setCalledFlag(this: typeof customState) { |
| 118 | + this.called = true; |
| 119 | + |
| 120 | + return Promise.resolve([]); |
| 121 | + }) |
| 122 | + .bind(customState); |
| 123 | + |
| 124 | + const customInterface: Partial<ForestAdminServerInterface> = { |
| 125 | + getUsers: customGetUsers, |
| 126 | + }; |
| 127 | + |
| 128 | + const options = factories.forestAdminClientOptions.build(); |
| 129 | + buildApplicationServices(customInterface, options); |
| 130 | + |
| 131 | + if (customInterface.getUsers) { |
| 132 | + await customInterface.getUsers(options); |
| 133 | + } |
| 134 | + |
| 135 | + expect(customState.called).toBe(true); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should correctly bind this context for fallback methods', () => { |
| 139 | + const partialInterface: Partial<ForestAdminServerInterface> = {}; |
| 140 | + |
| 141 | + const options = factories.forestAdminClientOptions.build(); |
| 142 | + buildApplicationServices(partialInterface, options); |
| 143 | + |
| 144 | + // makeAuthService is called during buildApplicationServices |
| 145 | + // It should work correctly with proper this binding |
| 146 | + expect(mockForestHttpApi.makeAuthService).toHaveBeenCalled(); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments