diff --git a/.github/workflows/test-persistence.yml b/.github/workflows/test-persistence.yml index e8514efed..582df4fe2 100644 --- a/.github/workflows/test-persistence.yml +++ b/.github/workflows/test-persistence.yml @@ -11,7 +11,8 @@ on: jobs: # Checks for changes to the modules/persistence directory changes: - runs-on: ubuntu-latest + # locking to ubuntu 22.04 to allow mongodb-memory-server to run + runs-on: ubuntu-22.04 outputs: persistence: ${{ steps.filter.outputs.persistence }} steps: @@ -29,7 +30,8 @@ jobs: defaults: run: working-directory: 'modules/persistence' - runs-on: ubuntu-latest + # locking to ubuntu 22.04 to allow mongodb-memory-server to run + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v1 diff --git a/modules/persistence/src/services/pages/index.ts b/modules/persistence/src/services/pages/index.ts index 0b81529ec..a1db8de6e 100644 --- a/modules/persistence/src/services/pages/index.ts +++ b/modules/persistence/src/services/pages/index.ts @@ -17,12 +17,20 @@ interface PageAst { [key: string]: any; } +interface Facet { + category: string; + value: string; + display_name: string; + sub_facets?: { [key: string]: any }[]; +} + export interface Page { page_id: string; filename: string; ast: PageAst; static_assets: UpdatedAsset[]; github_username: string; + facets?: Facet[]; } export interface UpdatedPage extends Page { @@ -35,6 +43,7 @@ interface PreviousPageMapping { [key: string]: { ast: PageAst; static_assets: StaticAsset[]; + facets?: Facet[]; }; } @@ -75,6 +84,7 @@ const findPrevPageDocs = async (pageIdPrefix: string, collection: string, github page_id: 1, ast: 1, static_assets: 1, + facets: 1, }; try { @@ -96,6 +106,7 @@ const createPageAstMapping = async (docsCursor: FindCursor) => { mapping[doc.page_id] = { ast: doc.ast, static_assets: doc.static_assets, + facets: doc.facets, }; pageIds.add(doc.page_id); } @@ -148,7 +159,7 @@ class UpdatedPagesManager { // Update the document if page's current AST is different from previous build's. // New pages should always count as having a "different" AST - if (!isEqual(page.ast, prevPageData?.ast)) { + if (!isEqual(page.ast, prevPageData?.ast) || !isEqual(page.facets, prevPageData?.facets)) { const operation = { updateOne: { filter: { page_id: currentPageId, github_username: page.github_username }, @@ -162,6 +173,7 @@ class UpdatedPagesManager { deleted: false, // Track the last build ID to update the content build_id: this.buildId, + facets: page.facets, }, $setOnInsert: { created_at: this.updateTime, diff --git a/modules/persistence/tests/services/pages.test.ts b/modules/persistence/tests/services/pages.test.ts index 03b059f2f..2bf6ca20e 100644 --- a/modules/persistence/tests/services/pages.test.ts +++ b/modules/persistence/tests/services/pages.test.ts @@ -127,6 +127,49 @@ describe('pages module', () => { expect(res[0].updated_at !== res[1].updated_at).toBeTruthy(); }); + it('should update pages with modified facets', async () => { + const pagePrefix = generatePagePrefix(); + const pages = generatePages(pagePrefix); + await _updatePages(pages, collection, GH_USER, new ObjectID()); + // Applying facet updates to both pages + const updatedPages = generatePages(pagePrefix); + updatedPages[0].facets = [ + { + category: 'target_product', + value: 'atlas', + display_name: 'Atlas', + }, + ]; + updatedPages[1].facets = [ + { + category: 'target_product', + value: 'atlas', + display_name: 'Atlas', + sub_facets: [{ category: 'sub_product', value: 'kubernetes-operator', display_name: 'Kubernetes Operator' }], + }, + ]; + + const findQuery = { + page_id: { $regex: new RegExp(`^${pagePrefix}`) }, + }; + await _updatePages(updatedPages, collection, GH_USER, new ObjectID()); + const res = await mockDb.collection(collection).find(findQuery).toArray(); + expect(res).toHaveLength(2); + expect(res.every(({ facets }) => facets && facets.length)).toBeTruthy(); + expect(res[1]).toHaveProperty('facets[0].sub_facets[0].value'); + + // removal of facet also propagates to update + const removedFacetUpdates = generatePages(pagePrefix).slice(0, 1); + delete removedFacetUpdates[0].facets; + + await _updatePages(removedFacetUpdates, collection, GH_USER, new ObjectID()); + const findRemovalQuery = { + page_id: { $regex: new RegExp(`^${pagePrefix}/page0.txt`) }, + }; + const removeFacetRes = await mockDb.collection(collection).find(findRemovalQuery).toArray(); + expect(removeFacetRes[0].facets).toBeNull(); + }); + it('should mark pages for deletion', async () => { const pagePrefix = generatePagePrefix(); const pages = generatePages(pagePrefix);