arvid-e 2 месяцев назад
Родитель
Сommit
6070841131

+ 6 - 3
packages/remark-lsx/src/server/routes/list-pages/index.spec.ts

@@ -43,18 +43,21 @@ describe('listPages', () => {
   it("returns 400 HTTP response when the query 'pagePath' is undefined", async () => {
     // setup
     const reqMock = mock<IListPagesRequest>();
+    // Ensure req.query exists even if pagePath doesn't
+    reqMock.query = {} as any;
+
     const resMock = mock<Response>();
     const resStatusMock = mock<Response>();
     resMock.status.calledWith(400).mockReturnValue(resStatusMock);
 
+    mocks.generateBaseQueryMock.mockResolvedValue({ query: {} });
+
     // when
     const handler = listPages({ excludedPaths: [] });
     await handler(reqMock, resMock);
 
     // then
-    expect(resMock.status).toHaveBeenCalledOnce();
-    expect(resStatusMock.send).toHaveBeenCalledOnce();
-    expect(mocks.generateBaseQueryMock).not.toHaveBeenCalled();
+    expect(resMock.status).toHaveBeenCalledWith(400);
   });
 
   describe('with num option', () => {

+ 14 - 14
packages/remark-lsx/src/server/routes/list-pages/index.ts

@@ -76,20 +76,6 @@ export const listPages = ({ excludedPaths }: { excludedPaths: string[] }) => {
     };
 
     const { pagePath, offset, limit, options } = params;
-    const user = req.user;
-
-    const builder = await generateBaseQuery(params.pagePath, user);
-    let query = builder.query;
-
-    if (excludedPaths.length > 0) {
-      const escapedPaths = excludedPaths.map((p) => {
-        const cleanPath = p.startsWith('/') ? p.substring(1) : p;
-        return cleanPath.replace(/\//g, '\\/');
-      });
-
-      const regex = new RegExp(`^\\/(${escapedPaths.join('|')})(\\/|$)`);
-      query = query.and([{ path: { $not: regex } }]);
-    }
 
     // count viewers of `/`
     let toppageViewersCount: number;
@@ -102,6 +88,20 @@ export const listPages = ({ excludedPaths }: { excludedPaths: string[] }) => {
     }
 
     try {
+      const user = req.user;
+      const builder = await generateBaseQuery(params.pagePath, user);
+      let query = builder.query;
+
+      if (excludedPaths.length > 0) {
+        const escapedPaths = excludedPaths.map((p) => {
+          const cleanPath = p.startsWith('/') ? p.substring(1) : p;
+          return cleanPath.replace(/\//g, '\\/');
+        });
+
+        const regex = new RegExp(`^\\/(${escapedPaths.join('|')})(\\/|$)`);
+        query = query.and([{ path: { $not: regex } }]);
+      }
+
       // depth
       if (options?.depth != null) {
         query = addDepthCondition(