Browse Source

fix: update regex construction for filter conditions to use RegExp.escape

Yuki Takei 1 week ago
parent
commit
0c0883137e
1 changed files with 12 additions and 4 deletions
  1. 12 4
      packages/remark-lsx/src/server/routes/list-pages/index.spec.ts

+ 12 - 4
packages/remark-lsx/src/server/routes/list-pages/index.spec.ts

@@ -186,7 +186,9 @@ describe('listPages', () => {
       // setup
       // setup
       const pagePath = '/parent';
       const pagePath = '/parent';
       const optionsFilter = '^child';
       const optionsFilter = '^child';
-      const expectedRegex = /^\/parent\/child/;
+      const expectedRegex = new RegExp(
+        `^${RegExp.escape('/parent/')}${RegExp.escape('child')}`,
+      );
 
 
       // when
       // when
       addFilterCondition(queryMock, pagePath, optionsFilter);
       addFilterCondition(queryMock, pagePath, optionsFilter);
@@ -199,7 +201,9 @@ describe('listPages', () => {
       // setup
       // setup
       const pagePath = '/parent';
       const pagePath = '/parent';
       const optionsFilter = 'child';
       const optionsFilter = 'child';
-      const expectedRegex = /^\/parent\/.*child/;
+      const expectedRegex = new RegExp(
+        `^${RegExp.escape('/parent/')}.*${RegExp.escape('child')}`,
+      );
 
 
       // when
       // when
       addFilterCondition(queryMock, pagePath, optionsFilter);
       addFilterCondition(queryMock, pagePath, optionsFilter);
@@ -225,7 +229,9 @@ describe('listPages', () => {
       // setup
       // setup
       const pagePath = '/parent';
       const pagePath = '/parent';
       const optionsFilter = 'child';
       const optionsFilter = 'child';
-      const expectedRegex = /^\/parent\/.*child/;
+      const expectedRegex = new RegExp(
+        `^${RegExp.escape('/parent/')}.*${RegExp.escape('child')}`,
+      );
 
 
       // when
       // when
       addFilterCondition(queryMock, pagePath, optionsFilter, true);
       addFilterCondition(queryMock, pagePath, optionsFilter, true);
@@ -313,7 +319,9 @@ describe('when excludedPaths is handled', () => {
     await handler(reqMock, resMock);
     await handler(reqMock, resMock);
 
 
     // check if the logic generates the correct regex: ^\/(user|tmp)(\/|$)
     // check if the logic generates the correct regex: ^\/(user|tmp)(\/|$)
-    const expectedRegex = /^\/(user|tmp)(\/|$)/;
+    const expectedRegex = new RegExp(
+      `^\\/(${RegExp.escape('user')}|${RegExp.escape('tmp')})(\\/|$)`,
+    );
     expect(queryMock.and).toHaveBeenCalledWith([
     expect(queryMock.and).toHaveBeenCalledWith([
       {
       {
         path: { $not: expectedRegex },
         path: { $not: expectedRegex },