// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {FuzzyFinder, fuzzyMatch} from './fuzzy';

describe('FuzzyFinder', () => {
  const items = ['aaa', 'aba', 'zzz', 'c z d z e', 'CAPS', 'ababc'];
  const finder = new FuzzyFinder(items, (x) => x);

  it('finds all for empty search term', () => {
    const result = finder.find('');
    // Expect all results are returned in original order.
    expect(result).toEqual([
      {item: 'aaa', segments: [{matching: false, value: 'aaa'}]},
      {item: 'aba', segments: [{matching: false, value: 'aba'}]},
      {item: 'zzz', segments: [{matching: false, value: 'zzz'}]},
      {item: 'c z d z e', segments: [{matching: false, value: 'c z d z e'}]},
      {item: 'CAPS', segments: [{matching: false, value: 'CAPS'}]},
      {item: 'ababc', segments: [{matching: false, value: 'ababc'}]},
    ]);
  });

  it('finds exact match', () => {
    const result = finder.find('aaa');
    expect(result).toEqual(
      expect.arrayContaining([
        {item: 'aaa', segments: [{matching: true, value: 'aaa'}]},
      ]),
    );
  });

  it('finds approx matches', () => {
    const result = finder.find('aa');
    // Allow finding results in any order.
    expect(result).toEqual(
      expect.arrayContaining([
        {
          item: 'aaa',
          // Either |aa|a or a|aa| is valid.
          segments: expect.arrayContaining([
            {matching: true, value: 'aa'},
            {matching: false, value: 'a'},
          ]),
        },
        {
          item: 'aba',
          segments: [
            {matching: true, value: 'a'},
            {matching: false, value: 'b'},
            {matching: true, value: 'a'},
          ],
        },
      ]),
    );
  });

  it('does not find completely unrelated items', () => {
    // |zzz| looks nothing like |aa| and should not be returned.
    const result = finder.find('aa');
    expect(result).not.toEqual(
      expect.arrayContaining([expect.objectContaining({item: 'zzz'})]),
    );
  });

  it('finds caps match when search term is in lower case', () => {
    const result = finder.find('caps');
    expect(result).toEqual(
      expect.arrayContaining([
        {item: 'CAPS', segments: [{matching: true, value: 'CAPS'}]},
      ]),
    );
  });
});

describe('FuzzyFinder camelCase tokenization', () => {
  const items = [
    'dev.perfetto.LiveMemory',
    'dev.perfetto.RecordTraceV2',
    'com.android.XMLParser',
  ];
  const finder = new FuzzyFinder(items, (x) => x);

  it('finds camelCase sub-word', () => {
    const result = finder.find('memory');
    expect(result).toEqual(
      expect.arrayContaining([
        expect.objectContaining({item: 'dev.perfetto.LiveMemory'}),
      ]),
    );
  });

  it('finds dotted segment', () => {
    const result = finder.find('perfetto');
    expect(result.length).toBeGreaterThanOrEqual(2);
  });

  it('finds uppercase acronym split', () => {
    const result = finder.find('parser');
    expect(result).toEqual(
      expect.arrayContaining([
        expect.objectContaining({item: 'com.android.XMLParser'}),
      ]),
    );
  });
});

test('fuzzyMatch', () => {
  expect(fuzzyMatch('foo bar baz', 'foo')).toEqual({
    matches: true,
    segments: [
      {matching: true, value: 'foo'},
      {matching: false, value: ' bar baz'},
    ],
  });

  expect(fuzzyMatch('foo bar baz', 'qux')).toEqual({
    matches: false,
    segments: [],
  });

  expect(fuzzyMatch('bar baz', 'foo', 'bar')).toEqual({
    matches: true,
    segments: [
      {matching: true, value: 'bar'},
      {matching: false, value: ' baz'},
    ],
  });
});
