|
| 1 | +/* |
| 2 | +Copyright 2025 Adobe. All rights reserved. |
| 3 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +
|
| 7 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | +governing permissions and limitations under the License. |
| 11 | +*/ |
| 12 | + |
| 13 | +const { AdminAPI } = require('../actions/check-product-changes/lib/aem'); |
| 14 | +const { request } = require('../actions/utils'); |
| 15 | + |
| 16 | +jest.mock('../actions/utils', () => ({ |
| 17 | + request: jest.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +describe('AdminAPI Optimized Tests', () => { |
| 21 | + let adminAPI; |
| 22 | + const context = { logger: { info: jest.fn(), error: jest.fn() } }; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + adminAPI = new AdminAPI( |
| 26 | + { org: 'testOrg', site: 'testSite' }, |
| 27 | + context, |
| 28 | + { requestPerSecond: 5, publishBatchSize: 100, authToken: 'testToken' } |
| 29 | + ); |
| 30 | + jest.useFakeTimers(); |
| 31 | + jest.spyOn(global, 'setInterval'); |
| 32 | + jest.spyOn(global, 'clearInterval'); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + jest.clearAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + test('should initialize with correct parameters', () => { |
| 40 | + expect(adminAPI.org).toBe('testOrg'); |
| 41 | + expect(adminAPI.site).toBe('testSite'); |
| 42 | + expect(adminAPI.requestPerSecond).toBe(5); |
| 43 | + expect(adminAPI.publishBatchSize).toBe(100); |
| 44 | + expect(adminAPI.authToken).toBe('testToken'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('should add record to previewQueue on previewAndPublish', async () => { |
| 48 | + const record = { path: '/test' }; |
| 49 | + adminAPI.previewAndPublish(record); |
| 50 | + expect(adminAPI.previewQueue).toHaveLength(1); |
| 51 | + await Promise.resolve(); |
| 52 | + }); |
| 53 | + |
| 54 | + test('should add record to unpublishQueue on unpublishAndDelete', async () => { |
| 55 | + const record = { path: '/test' }; |
| 56 | + adminAPI.unpublishAndDelete(record); |
| 57 | + expect(adminAPI.unpublishQueue).toHaveLength(1); |
| 58 | + await Promise.resolve(); |
| 59 | + }); |
| 60 | + |
| 61 | + test('should start processing queues', async () => { |
| 62 | + await adminAPI.startProcessing(); |
| 63 | + expect(global.setInterval).toHaveBeenCalled(); |
| 64 | + jest.runOnlyPendingTimers(); |
| 65 | + }); |
| 66 | + |
| 67 | + test('should stop processing queues', async () => { |
| 68 | + await adminAPI.startProcessing(); |
| 69 | + jest.runOnlyPendingTimers(); |
| 70 | + const stopPromise = adminAPI.stopProcessing(); |
| 71 | + jest.runOnlyPendingTimers(); |
| 72 | + await stopPromise; |
| 73 | + expect(global.clearInterval).toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('should execute admin request', async () => { |
| 77 | + await adminAPI.execAdminRequest('POST', 'preview', '/test', { data: 'test' }); |
| 78 | + expect(request).toHaveBeenCalledWith('preview', 'https://admin.hlx.page/preview/testOrg/testSite/main/test', { |
| 79 | + method: 'POST', |
| 80 | + headers: { |
| 81 | + 'content-type': 'application/json', |
| 82 | + 'x-auth-token': 'testToken', |
| 83 | + }, |
| 84 | + body: JSON.stringify({ data: 'test' }), |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + test('should process preview queue', async () => { |
| 89 | + const record = { path: '/test' }; |
| 90 | + adminAPI.previewQueue.push({ record, resolve: jest.fn() }); |
| 91 | + adminAPI.processQueues(); |
| 92 | + expect(context.logger.info).toHaveBeenCalledWith('Queues: preview=1, publish=0, unpublish=0, inflight=0'); |
| 93 | + }); |
| 94 | + |
| 95 | + test('should process publish queue', async () => { |
| 96 | + const record = { path: '/test' }; |
| 97 | + adminAPI.publishQueue.push({ record, resolve: jest.fn() }); |
| 98 | + adminAPI.processQueues(); |
| 99 | + expect(context.logger.info).toHaveBeenCalledWith('Queues: preview=0, publish=1, unpublish=0, inflight=0'); |
| 100 | + }); |
| 101 | + |
| 102 | + test('should process unpublish queue', async () => { |
| 103 | + const record = { path: '/test' }; |
| 104 | + adminAPI.unpublishQueue.push({ record, resolve: jest.fn() }); |
| 105 | + adminAPI.processQueues(); |
| 106 | + expect(context.logger.info).toHaveBeenCalledWith('Queues: preview=0, publish=0, unpublish=1, inflight=0'); |
| 107 | + }); |
| 108 | +}); |
0 commit comments