|
| 1 | +import { getVoidLogger } from '@backstage/backend-common'; |
| 2 | +import { mockServices } from '@backstage/backend-test-utils'; |
| 3 | +import { catalogServiceMock } from '@backstage/plugin-catalog-node/testUtils'; |
| 4 | +import { Entity } from '@backstage/catalog-model'; |
| 5 | +import { ConfigReader } from '@backstage/config'; |
| 6 | +import { TechDocsMetadata } from '@backstage/plugin-techdocs-backend'; |
| 7 | +import { rest } from 'msw'; |
| 8 | +import { setupServer } from 'msw/node'; |
| 9 | +import { GleanIndexClient } from './GleanIndexClient'; |
| 10 | +import { htmlFixture } from './fixtures/staticTechDocsHtml'; |
| 11 | +import { GleanDocument } from './types'; |
| 12 | + |
| 13 | +describe('GleanIndexClient', () => { |
| 14 | + let gleanIndexClient: GleanIndexClient; |
| 15 | + const server = setupServer(); |
| 16 | + const discoveryApi = { getBaseUrl: jest.fn() }; |
| 17 | + const gleanApiIndexUrl = |
| 18 | + 'https://customer-be.glean.com/api/index/v1/bulkindexdocuments'; |
| 19 | + const auth = mockServices.auth(); |
| 20 | + |
| 21 | + const config = new ConfigReader({ |
| 22 | + backend: { |
| 23 | + baseUrl: 'http://localhost', |
| 24 | + listen: { port: 7000 }, |
| 25 | + }, |
| 26 | + app: { |
| 27 | + baseUrl: 'http://localhost', |
| 28 | + listen: { port: 3000 }, |
| 29 | + }, |
| 30 | + glean: { |
| 31 | + apiIndexUrl: gleanApiIndexUrl, |
| 32 | + token: 'I-am-a-token', |
| 33 | + datasource: 'I-am-a-datasource', |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + const entityWithUrlRef: Entity = { |
| 38 | + apiVersion: 'backstage.io/v1alpha1', |
| 39 | + kind: 'Component', |
| 40 | + metadata: { |
| 41 | + name: 'some-handbook-with-url-ref', |
| 42 | + namespace: 'default', |
| 43 | + annotations: { |
| 44 | + 'backstage.io/techdocs-ref': 'url:some_url', |
| 45 | + }, |
| 46 | + spec: {}, |
| 47 | + }, |
| 48 | + }; |
| 49 | + const entityWithDirRef: Entity = { |
| 50 | + apiVersion: 'backstage.io/v1alpha1', |
| 51 | + kind: 'Component', |
| 52 | + metadata: { |
| 53 | + name: 'some-handbook-with-dir-ref', |
| 54 | + namespace: 'default', |
| 55 | + annotations: { |
| 56 | + 'backstage.io/techdocs-ref': 'dir:.', |
| 57 | + }, |
| 58 | + spec: {}, |
| 59 | + }, |
| 60 | + }; |
| 61 | + const entities = [entityWithUrlRef, entityWithDirRef]; |
| 62 | + const catalogApi = catalogServiceMock({ entities }); |
| 63 | + |
| 64 | + beforeAll(() => server.listen()); |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + gleanIndexClient = GleanIndexClient.create({ |
| 68 | + auth, |
| 69 | + catalogApi, |
| 70 | + config, |
| 71 | + discoveryApi, |
| 72 | + logger: getVoidLogger(), |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + jest.resetAllMocks(); |
| 78 | + server.resetHandlers(); |
| 79 | + }); |
| 80 | + |
| 81 | + afterAll(() => server.close()); |
| 82 | + |
| 83 | + describe('create', () => { |
| 84 | + it('returns a new instance of GleanIndexClient', () => { |
| 85 | + expect( |
| 86 | + GleanIndexClient.create({ |
| 87 | + auth, |
| 88 | + catalogApi, |
| 89 | + config, |
| 90 | + discoveryApi, |
| 91 | + logger: getVoidLogger(), |
| 92 | + }), |
| 93 | + ).toBeInstanceOf(GleanIndexClient); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('parseMainContent', () => { |
| 98 | + it('removes all nav elements from HTML', () => { |
| 99 | + expect(htmlFixture).toEqual(expect.stringContaining('<nav')); |
| 100 | + // eslint-disable-next-line dot-notation |
| 101 | + expect(gleanIndexClient['parseMainContent'](htmlFixture)).toEqual( |
| 102 | + expect.not.stringContaining('<nav'), |
| 103 | + ); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('buildDocument', () => { |
| 108 | + beforeEach(() => { |
| 109 | + // eslint-disable-next-line dot-notation |
| 110 | + gleanIndexClient['techDocsClient'].getTechDocsStaticFile = jest |
| 111 | + .fn() |
| 112 | + .mockResolvedValue(htmlFixture); |
| 113 | + }); |
| 114 | + |
| 115 | + it('returns a document object', async () => { |
| 116 | + expect( |
| 117 | + await gleanIndexClient.buildDocument( |
| 118 | + entityWithUrlRef, |
| 119 | + 'foo/index.html', |
| 120 | + ), |
| 121 | + ).toEqual({ |
| 122 | + id: 'default/component/some-handbook-with-url-ref/foo/index.html', |
| 123 | + title: 'Engineering Handbook', |
| 124 | + container: 'some-handbook-with-url-ref', |
| 125 | + datasource: 'I-am-a-datasource', |
| 126 | + viewURL: |
| 127 | + 'http://localhost/docs/default/component/some-handbook-with-url-ref/foo', |
| 128 | + body: { |
| 129 | + mimeType: 'HTML', |
| 130 | + textContent: expect.stringContaining( |
| 131 | + "Welcome to Company's Engineering Handbook!", |
| 132 | + ), |
| 133 | + }, |
| 134 | + updatedAt: Math.floor(new Date('April 6, 2022').getTime() / 1000), |
| 135 | + permissions: { allowAnonymousAccess: true }, |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('batchIndexTechDocs', () => { |
| 141 | + const mockDocument: GleanDocument = { |
| 142 | + id: 'document-1', |
| 143 | + title: 'I am a document', |
| 144 | + container: 'some-handbook', |
| 145 | + datasource: 'I-am-a-datasource', |
| 146 | + viewURL: 'http://backstage.w10e.com', |
| 147 | + body: { |
| 148 | + mimeType: 'HTML', |
| 149 | + textContent: 'I am some text content', |
| 150 | + }, |
| 151 | + updatedAt: 1652818028, |
| 152 | + permissions: { allowAnonymousAccess: true }, |
| 153 | + }; |
| 154 | + |
| 155 | + const mockTechDocsMetadata: TechDocsMetadata = { |
| 156 | + site_name: 'some-handbook', |
| 157 | + site_description: 'Company',s Engineering Handbook', |
| 158 | + etag: '38cf6ed97f8c501426a0e311b76d67c69fc46df3', |
| 159 | + build_timestamp: 1652796973948, |
| 160 | + files: ['index.html', 'interviewing/index.html', 'onboarding.html'], |
| 161 | + }; |
| 162 | + |
| 163 | + beforeEach(() => { |
| 164 | + jest |
| 165 | + .spyOn(gleanIndexClient, 'buildDocument') |
| 166 | + .mockResolvedValue(mockDocument); |
| 167 | + jest |
| 168 | + .spyOn(gleanIndexClient, 'indexDocuments') |
| 169 | + .mockResolvedValue('response'); |
| 170 | + |
| 171 | + // eslint-disable-next-line dot-notation |
| 172 | + gleanIndexClient['techDocsClient'].getTechDocsMetadata = jest |
| 173 | + .fn() |
| 174 | + .mockResolvedValue(mockTechDocsMetadata); |
| 175 | + |
| 176 | + server.use( |
| 177 | + rest.post(`${gleanApiIndexUrl}`, (_req, res, ctx) => { |
| 178 | + return res(ctx.status(200)); |
| 179 | + }), |
| 180 | + ); |
| 181 | + }); |
| 182 | + |
| 183 | + it('uploads the Glean documents', async () => { |
| 184 | + const indexTechDocs = await gleanIndexClient.batchIndexDocuments( |
| 185 | + 'upload-', |
| 186 | + [mockDocument], |
| 187 | + ); |
| 188 | + expect(gleanIndexClient.indexDocuments).toHaveBeenCalledTimes(1); |
| 189 | + expect(indexTechDocs).toEqual(1); |
| 190 | + }); |
| 191 | + |
| 192 | + it('builds and uploads the Glean documents for all entities', async () => { |
| 193 | + const batchIndexTechDocs = await gleanIndexClient.batchIndexTechDocs( |
| 194 | + entities, |
| 195 | + ); |
| 196 | + expect(batchIndexTechDocs.uploadId).toContain('upload-'); |
| 197 | + expect(batchIndexTechDocs.batchCount).toEqual(1); |
| 198 | + }); |
| 199 | + |
| 200 | + describe('when there are no files to index', () => { |
| 201 | + beforeEach(() => { |
| 202 | + // eslint-disable-next-line dot-notation |
| 203 | + gleanIndexClient['techDocsClient'].getTechDocsMetadata = jest |
| 204 | + .fn() |
| 205 | + .mockResolvedValue({ ...mockTechDocsMetadata, files: [] }); |
| 206 | + }); |
| 207 | + |
| 208 | + it('does not index tech docs with Glean', async () => { |
| 209 | + const batchIndexTechDocs = await gleanIndexClient.batchIndexTechDocs( |
| 210 | + [], |
| 211 | + ); |
| 212 | + expect(gleanIndexClient.buildDocument).not.toHaveBeenCalled(); |
| 213 | + expect(batchIndexTechDocs.uploadId).toContain('upload-'); |
| 214 | + expect(batchIndexTechDocs.batchCount).toEqual(0); |
| 215 | + }); |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + describe('batchIndex', () => { |
| 220 | + beforeEach(() => { |
| 221 | + jest.spyOn(gleanIndexClient, 'batchIndexTechDocs').mockResolvedValue({ |
| 222 | + uploadId: 'upload-7bbf4c41-b73a-4ca2-8245-a23a0c4f37e7', |
| 223 | + batchCount: 1, |
| 224 | + }); |
| 225 | + }); |
| 226 | + |
| 227 | + it('indexes the TechDocs entities', async () => { |
| 228 | + await gleanIndexClient.batchIndex(entities); |
| 229 | + expect(gleanIndexClient.batchIndexTechDocs).toHaveBeenCalledTimes(1); |
| 230 | + }); |
| 231 | + }); |
| 232 | +}); |
0 commit comments