smartfuzzy/test/test.smartfuzzy.ts

68 lines
2.2 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.getChangeScoreForString('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) than other entries
expect(result['Apple Inc.']).toBeLessThan(result['Sony']);
});
tap.test('should get closest match for a string', async () => {
const result = testSmartfuzzy.getClosestMatchForString('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.getClosestMatchForString('');
// For empty strings, behavior should be defined (either null or a specific result)
expect(result).toBeNullOrUndefined();
});
export default tap.start();