70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { expect, tap } from '@push.rocks/tapbundle';
|
|
import * as smartfuzzy from '../ts/index.js';
|
|
|
|
let testSmartfuzzy: smartfuzzy.Smartfuzzy;
|
|
const testDictionary = [
|
|
'Sony',
|
|
'Deutsche Bahn',
|
|
'Apple Inc.',
|
|
"Trader Joe's",
|
|
];
|
|
|
|
tap.test('should create an instance of Smartfuzzy', async () => {
|
|
testSmartfuzzy = new smartfuzzy.Smartfuzzy(testDictionary);
|
|
expect(testSmartfuzzy).toBeInstanceOf(smartfuzzy.Smartfuzzy);
|
|
expect(testSmartfuzzy.dictionary).toEqual(testDictionary);
|
|
});
|
|
|
|
tap.test('should compute a score for a string against the dictionary', async () => {
|
|
const result = testSmartfuzzy.calculateScores('Apple');
|
|
|
|
// Check that we got a dictionary map back
|
|
expect(result).toBeTypeOf('object');
|
|
|
|
// Check that every dictionary entry has a score
|
|
for (const word of testDictionary) {
|
|
expect(result).toHaveProperty(word);
|
|
expect(result[word]).toBeTypeofNumber();
|
|
}
|
|
|
|
// Check that 'Apple Inc.' has a lower score (better match) for 'Apple' than other entries
|
|
// The leven distance for 'Apple Inc.' from 'Apple' should be less than that of other entries
|
|
// We can't predict exact values but we can compare them
|
|
expect(result['Apple Inc.']).toBeLessThanOrEqual(result['Sony']);
|
|
});
|
|
|
|
tap.test('should get closest match for a string', async () => {
|
|
const result = testSmartfuzzy.findClosestMatch('Apple');
|
|
|
|
// Should return closest match as string
|
|
expect(result).toBeTypeofString();
|
|
|
|
// Should match the expected closest entry
|
|
expect(result).toEqual('Apple Inc.');
|
|
});
|
|
|
|
tap.test('should add words to dictionary', async () => {
|
|
const initialLength = testSmartfuzzy.dictionary.length;
|
|
|
|
// Add a single word
|
|
testSmartfuzzy.addToDictionary('Microsoft');
|
|
expect(testSmartfuzzy.dictionary.length).toEqual(initialLength + 1);
|
|
expect(testSmartfuzzy.dictionary).toContain('Microsoft');
|
|
|
|
// Add multiple words
|
|
const additionalWords = ['Google', 'Amazon', 'Facebook'];
|
|
testSmartfuzzy.addToDictionary(additionalWords);
|
|
expect(testSmartfuzzy.dictionary.length).toEqual(initialLength + 4);
|
|
for (const word of additionalWords) {
|
|
expect(testSmartfuzzy.dictionary).toContain(word);
|
|
}
|
|
});
|
|
|
|
tap.test('should handle empty query string', async () => {
|
|
const result = testSmartfuzzy.findClosestMatch('');
|
|
// For empty strings, behavior should be defined (either null or a specific result)
|
|
expect(result).toBeNullOrUndefined();
|
|
});
|
|
|
|
export default tap.start();
|