Skip to content

fix: fix parse url issue #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
moduleNameMapper: {
'^monaco-editor$': '<rootDir>/node_modules/monaco-editor/esm/vs/editor/editor.api.js',
},
};
5 changes: 4 additions & 1 deletion src/common/monaco/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import { monacoEnvironment } from './environment.ts';
import { searchCompletionProvider } from './completion.ts';

self.MonacoEnvironment = monacoEnvironment;
// Only assign MonacoEnvironment if 'self' is defined (browser or web worker)
if (typeof self !== 'undefined') {
self.MonacoEnvironment = monacoEnvironment;

Check warning on line 9 in src/common/monaco/index.ts

View check run for this annotation

Codecov / codecov/patch

src/common/monaco/index.ts#L9

Added line #L9 was not covered by tests
}

monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true);
monaco.languages.register({ id: search.id });
Expand Down
3 changes: 2 additions & 1 deletion src/common/monaco/tokenlizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ const replaceTripleQuotes = (value: string) =>
value
.replace(/'''(.*?)'''/gs, (_, match) => jsonify.stringify(match))
.replace(/"""(.*?)"""/gs, (_, match) => jsonify.stringify(match));
const replaceComments = (value: string) => value.replace(/\/\/.*/g, '').trim();
const replaceComments = (value: string) =>
value.replace(/((['"]).*?\2)|\/\/.*$/gm, (match, quoted) => (quoted ? match : '')).trim();

export const transformQDSL = ({ path, qdsl }: Pick<SearchAction, 'path' | 'qdsl'>) => {
try {
Expand Down
122 changes: 122 additions & 0 deletions tests/common/monaco/tokenlizer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { transformQDSL } from '../../../src/common/monaco';

jest.mock('monaco-editor', () => ({
self: { MonacoEnvironment: {} },
editor: {
IModel: {},
ITextModel: {},
getLineCount: () => 1,
getLineContent: () => '',
},
Range: {},
Position: {},
languages: {
typescript: {
typescriptDefaults: {
setEagerModelSync: jest.fn(),
},
},
register: jest.fn(),
setMonarchTokensProvider: jest.fn(),
setLanguageConfiguration: jest.fn(),
registerCompletionItemProvider: jest.fn(),
CodeLens: {},
},
}));

describe('Unit test for transformQDSL', () => {
it('should transform QDSL string to JSON when given qdsl is valid', () => {
const input = {
path: '/test',
qdsl: '{ "query": { "match_all": {} } }',
};
const result = transformQDSL(input);
expect(result).toBe(JSON.stringify(JSON.parse(input.qdsl), null, 2));
});

it('should handle _bulk path with multiple lines when a valid bulk qdsl provided', () => {
const input = {
path: '/_bulk',
qdsl: '{ "index": {} }\n{ "field": "value" }',
};
const result = transformQDSL(input);
const expected =
JSON.stringify(JSON.parse('{ "index": {} }')) +
'\n' +
JSON.stringify(JSON.parse('{ "field": "value" }')) +
'\n';
expect(result).toBe(expected);
});

it('should remove comments and triple quotes', () => {
const input = {
path: '/test',
qdsl: '{ "query": { "match_all": {} } } // comment',
};
const result = transformQDSL(input);
expect(result).toBe(JSON.stringify({ query: { match_all: {} } }, null, 2));
});

it('should throw CustomError on invalid JSON', () => {
const input = {
path: '/test',
qdsl: '{ invalid json }',
};
expect(() => transformQDSL(input)).toThrow();
});

it('should not remove // inside qoutes', () => {
const input = {
path: '/test',
qdsl: '{ "query": { "match": { "field": "http://example.com//foo" } } } // comment',
};
const result = transformQDSL(input);
// The comment should be removed, but the // inside the string should remain
expect(result).toBe(
JSON.stringify(
{
query: {
match: {
field: 'http://example.com//foo',
},
},
},
null,
2,
),
);
});

it('should handle URLs with // inside JSON and not treat them as comments', () => {
const input = {
path: '/detail/_search',
qdsl: `{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"terms": {
"url": ["https://baidu.com/"]
}
}
]
}
}
}`,
};
const result = transformQDSL(input);
expect(result).toBe(
JSON.stringify(
{
from: 0,
size: 10,
query: { bool: { must: [{ terms: { url: ['https://baidu.com/'] } }] } },
},
null,
2,
),
);
});
});