// ***************************************************************************** // Copyright (C) 2026 Safi Seid-Ahmad, K2view and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom'; let disableJSDOM = enableJSDOM(); import { ChatModel, ChatRequestModel, ChatResponseContent, CodeChatResponseContentImpl, ErrorChatResponseContentImpl, InformationalChatResponseContentImpl, MarkdownChatResponseContentImpl, TextChatResponseContentImpl, ThinkingChatResponseContentImpl, ToolCallChatResponseContentImpl } from '@theia/ai-chat/lib/common'; import { expect } from 'chai'; import { ChatFindMatcher, ChatFindOptions } from './chat-find-matcher.js'; disableJSDOM(); const plain: ChatFindOptions = { matchCase: false, wholeWord: false, useRegex: false }; function request(id: string, text: string, content: ChatResponseContent[]): ChatRequestModel { return { id, message: { parts: [{ kind: 'text', text }] }, response: { id: `${id}-response`, response: { content } } } as unknown as ChatRequestModel; } function model(...requests: ChatRequestModel[]): ChatModel { return { getRequests: () => requests } as unknown as ChatModel; } describe('ChatFindMatcher', () => { const matcher = new ChatFindMatcher(); const count = (chat: ChatModel, query: string, options: ChatFindOptions = plain): number => matcher.findMatches(chat, matcher.createRegExp(query, options)!).length; before(() => disableJSDOM = enableJSDOM()); after(() => disableJSDOM()); describe('createRegExp', () => { it('returns undefined for an empty query', () => { expect(matcher.createRegExp('', plain)).to.be.undefined; }); it('escapes regex characters unless useRegex is set', () => { expect(matcher.createRegExp('a.b', plain)!.test('axb')).to.be.false; expect(matcher.createRegExp('a.b', { ...plain, useRegex: true })!.test('axb')).to.be.true; }); it('is case-insensitive unless matchCase is set', () => { expect(matcher.createRegExp('abc', plain)!.test('ABC')).to.be.true; expect(matcher.createRegExp('abc', { ...plain, matchCase: true })!.test('ABC')).to.be.false; }); it('matches whole words only for wholeWord', () => { const whole = { ...plain, wholeWord: true }; const find = (query: string, text: string) => matcher.findInText(text, matcher.createRegExp(query, whole)!); expect(find('cat', 'a cat sat')).to.deep.equal([{ start: 2, end: 5 }]); expect(find('cat', 'concatenate')).to.deep.equal([]); expect(find('cat', 'cat')).to.deep.equal([{ start: 0, end: 3 }]); expect(find('cat', 'cat.')).to.deep.equal([{ start: 0, end: 3 }]); }); it('matches whole words that start or end with a separator, unlike \\b', () => { const whole = { ...plain, wholeWord: true }; const find = (query: string, text: string) => matcher.findInText(text, matcher.createRegExp(query, whole)!); expect(find('C++', 'I like C++ a lot')).to.deep.equal([{ start: 7, end: 10 }]); expect(find('foo()', 'call foo() now')).to.deep.equal([{ start: 5, end: 10 }]); expect(find('C++', 'xC++')).to.deep.equal([]); }); it('returns undefined for an invalid regular expression', () => { expect(matcher.createRegExp('(', { ...plain, useRegex: true })).to.be.undefined; }); }); describe('findInText', () => { it('returns all occurrences in order', () => { const ranges = matcher.findInText('foo bar foo', matcher.createRegExp('foo', plain)!); expect(ranges).to.deep.equal([{ start: 0, end: 3 }, { start: 8, end: 11 }]); }); it('skips zero-length matches without looping forever', () => { const ranges = matcher.findInText('aab', matcher.createRegExp('a*', { ...plain, useRegex: true })!); expect(ranges).to.deep.equal([{ start: 0, end: 2 }]); }); }); describe('findMatches', () => { it('searches request text and response parts in tree order', () => { const chat = model( request('r1', 'find me', [new TextChatResponseContentImpl('nothing'), new MarkdownChatResponseContentImpl('find **me** and find me')]), request('r2', 'other', [new CodeChatResponseContentImpl('const findMe = 1;', 'ts')]) ); const matches = matcher.findMatches(chat, matcher.createRegExp('find', plain)!); // Offsets are into the rendered text: the markdown renders as 'find me and find me'. expect(matches).to.deep.equal([ { nodeId: 'r1', kind: 'request', contentIndex: undefined, occurrence: 0, start: 0, end: 4 }, { nodeId: 'r1-response', kind: 'markdownContent', contentIndex: 1, occurrence: 0, start: 0, end: 4 }, { nodeId: 'r1-response', kind: 'markdownContent', contentIndex: 1, occurrence: 1, start: 12, end: 16 }, { nodeId: 'r2-response', kind: 'code', contentIndex: 0, occurrence: 0, start: 6, end: 10 } ]); }); it('searches markdown as rendered: link targets and markup are not searched, text across markup is', () => { const chat = model(request('r1', '', [ new MarkdownChatResponseContentImpl('See the [docs](https://example.org/docs/page) for **important note** details.') ])); expect(count(chat, 'docs')).to.equal(1); expect(count(chat, 'for important')).to.equal(1); expect(count(chat, '**')).to.equal(0); }); it('follows streamed markdown', () => { const content = new MarkdownChatResponseContentImpl('first needle'); const chat = model(request('r1', '', [content])); expect(count(chat, 'needle')).to.equal(1); content.merge(new MarkdownChatResponseContentImpl(' and a second needle')); expect(count(chat, 'needle')).to.equal(2); }); it('searches request text as rendered markdown', () => { expect(count(model(request('r1', 'use `npm` **now**', [])), 'npm now')).to.equal(1); }); it('searches the visible error headline only and skips informational parts, which are not rendered', () => { const error = new ErrorChatResponseContentImpl(new Error('401 {"error":{"message":"needle in headline","type":"needle_type"}}')); const chat = model(request('r1', '', [new InformationalChatResponseContentImpl('needle info'), error])); const matches = matcher.findMatches(chat, matcher.createRegExp('needle', plain)!); expect(matches.map(m => [m.contentIndex, m.kind, m.occurrence])).to.deep.equal([[1, 'error', 0]]); }); it('ignores a mermaid block, which is rendered as a diagram rather than as its source', () => { const chart = new CodeChatResponseContentImpl('flowchart TD\n A[needle] --> B[other]', 'mermaid'); const chat = model(request('r1', '', [chart, new TextChatResponseContentImpl('needle in text')])); const matches = matcher.findMatches(chat, matcher.createRegExp('needle', plain)!); expect(matches.map(m => m.contentIndex)).to.deep.equal([1]); }); it('still searches a plain code block', () => { const chat = model(request('r1', '', [new CodeChatResponseContentImpl('const needle = 1;', 'ts')])); expect(matcher.findMatches(chat, matcher.createRegExp('needle', plain)!)).to.have.length(1); }); it('ignores tool calls and reasoning', () => { const chat = model(request('r1', '', [ new ToolCallChatResponseContentImpl('id', 'needle_tool', '{"q":"needle"}', true), new ThinkingChatResponseContentImpl('thinking about needle', 'sig') ])); expect(matcher.findMatches(chat, matcher.createRegExp('needle', plain)!)).to.deep.equal([]); }); it('joins request parts, e.g. agent mentions, into one text', () => { const req = { id: 'r1', message: { parts: [{ kind: 'agent', text: '@Coder' }, { kind: 'text', text: ' please fix' }] }, response: { id: 'r1-response', response: { content: [] } } } as unknown as ChatRequestModel; const matches = matcher.findMatches(model(req), matcher.createRegExp('coder please', plain)!); expect(matches).to.deep.equal([{ nodeId: 'r1', kind: 'request', contentIndex: undefined, occurrence: 0, start: 1, end: 13 }]); }); }); });