Skip to content

Commit 9d70d88

Browse files
committed
setup mock tests on problematic code segments
1 parent 6f9829d commit 9d70d88

5 files changed

Lines changed: 455 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createPingCommand } from './command.js';
2+
3+
describe('ping command', () => {
4+
test('should return a command builder instance', () => {
5+
const result = createPingCommand();
6+
expect(result).toBeDefined();
7+
expect(typeof result).toBe('object');
8+
});
9+
10+
test('should create a command with ping name', () => {
11+
const result = createPingCommand();
12+
expect(result.name).toBe('ping');
13+
});
14+
15+
test('should create a command with correct description', () => {
16+
const result = createPingCommand();
17+
expect(result.description).toBe('Replies with pong!');
18+
});
19+
});

source/config/constants.test.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import {
2+
CONFIG,
3+
DICE_TYPES,
4+
CURRENCY_TYPES,
5+
CURRENCY_ABBREVIATIONS,
6+
CURRENCY_TO_GOLD_CONVERSION,
7+
DEFAULT_CONVERSION_FEE,
8+
ERROR_MESSAGES
9+
} from './constants.js';
10+
11+
describe('constants', () => {
12+
describe('CONFIG', () => {
13+
test('should have expected numeric values', () => {
14+
expect(CONFIG.MAX_DICE_QUANTITY).toBe(100);
15+
expect(CONFIG.MAX_DICE_SIDES).toBe(100);
16+
expect(CONFIG.MAX_ITEM_NAME_LENGTH).toBe(100);
17+
expect(CONFIG.MIN_SEARCH_TERM_LENGTH).toBe(2);
18+
expect(CONFIG.SIMILARITY_THRESHOLD).toBe(0.5);
19+
expect(CONFIG.DISCORD_MESSAGE_LIMIT).toBe(2000);
20+
expect(CONFIG.TRACKER_MESSAGE_LIMIT).toBe(1900);
21+
expect(CONFIG.CHUNK_SIZE_CALCULATION_THRESHOLD).toBe(100000);
22+
});
23+
24+
test('should have collection names object', () => {
25+
expect(CONFIG.COLLECTION_NAMES).toEqual({
26+
TRACKERS: "trackers",
27+
TRACKER_AUDIT_LOG: "tracker_audit_log",
28+
BANK: "bank",
29+
BANK_AUDIT_LOG: "bank_audit_log",
30+
BANK_SETTINGS: "bank_settings",
31+
});
32+
});
33+
});
34+
35+
describe('DICE_TYPES', () => {
36+
test('should contain expected dice types', () => {
37+
expect(DICE_TYPES).toEqual([4, 6, 8, 10, 12, 20, 100]);
38+
});
39+
40+
test('should be an array of numbers', () => {
41+
DICE_TYPES.forEach(type => {
42+
expect(typeof type).toBe('number');
43+
});
44+
});
45+
});
46+
47+
describe('CURRENCY_TYPES', () => {
48+
test('should contain expected currency types', () => {
49+
expect(CURRENCY_TYPES).toEqual(["platinum", "gold", "silver", "electrum", "copper"]);
50+
});
51+
52+
test('should be an array of strings', () => {
53+
CURRENCY_TYPES.forEach(type => {
54+
expect(typeof type).toBe('string');
55+
});
56+
});
57+
});
58+
59+
describe('CURRENCY_ABBREVIATIONS', () => {
60+
test('should have abbreviations for all currency types', () => {
61+
CURRENCY_TYPES.forEach(currency => {
62+
expect(CURRENCY_ABBREVIATIONS[currency]).toBeDefined();
63+
expect(typeof CURRENCY_ABBREVIATIONS[currency]).toBe('string');
64+
});
65+
});
66+
67+
test('should have expected abbreviations', () => {
68+
expect(CURRENCY_ABBREVIATIONS.platinum).toBe('pp');
69+
expect(CURRENCY_ABBREVIATIONS.gold).toBe('gp');
70+
expect(CURRENCY_ABBREVIATIONS.silver).toBe('sp');
71+
expect(CURRENCY_ABBREVIATIONS.electrum).toBe('ep');
72+
expect(CURRENCY_ABBREVIATIONS.copper).toBe('cp');
73+
});
74+
});
75+
76+
describe('CURRENCY_TO_GOLD_CONVERSION', () => {
77+
test('should have conversion rates for all currency types', () => {
78+
CURRENCY_TYPES.forEach(currency => {
79+
expect(CURRENCY_TO_GOLD_CONVERSION[currency]).toBeDefined();
80+
expect(typeof CURRENCY_TO_GOLD_CONVERSION[currency]).toBe('number');
81+
});
82+
});
83+
84+
test('should have expected conversion rates', () => {
85+
expect(CURRENCY_TO_GOLD_CONVERSION.platinum).toBe(10);
86+
expect(CURRENCY_TO_GOLD_CONVERSION.gold).toBe(1);
87+
expect(CURRENCY_TO_GOLD_CONVERSION.electrum).toBe(0.5);
88+
expect(CURRENCY_TO_GOLD_CONVERSION.silver).toBe(0.1);
89+
expect(CURRENCY_TO_GOLD_CONVERSION.copper).toBe(0.01);
90+
});
91+
});
92+
93+
describe('DEFAULT_CONVERSION_FEE', () => {
94+
test('should be a number', () => {
95+
expect(typeof DEFAULT_CONVERSION_FEE).toBe('number');
96+
});
97+
98+
test('should be 10% (0.1)', () => {
99+
expect(DEFAULT_CONVERSION_FEE).toBe(0.1);
100+
});
101+
});
102+
103+
describe('ERROR_MESSAGES', () => {
104+
test('should have all expected error message keys', () => {
105+
const expectedKeys = [
106+
'INVALID_QUANTITY',
107+
'ITEM_NAME_TOO_LONG',
108+
'ITEM_NOT_FOUND',
109+
'TRACKER_EMPTY',
110+
'SEARCH_TERM_TOO_SHORT',
111+
'NO_PERMISSION',
112+
'MAX_DICE_EXCEEDED',
113+
'MAX_SIDES_EXCEEDED',
114+
'INVALID_CURRENCY',
115+
'BANK_EMPTY',
116+
'INVALID_FEE_RATE',
117+
'SAME_CURRENCY_CONVERSION',
118+
'INSUFFICIENT_CONVERSION_BALANCE',
119+
'GENERIC_ERROR',
120+
'DATABASE_ERROR'
121+
];
122+
123+
expectedKeys.forEach(key => {
124+
expect(ERROR_MESSAGES[key]).toBeDefined();
125+
expect(typeof ERROR_MESSAGES[key]).toBe('string');
126+
});
127+
});
128+
129+
test('should have non-empty error messages', () => {
130+
Object.values(ERROR_MESSAGES).forEach(message => {
131+
expect(message.length).toBeGreaterThan(0);
132+
});
133+
});
134+
});
135+
});

source/utils/formatting.test.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import {
2+
formatUptime,
3+
formatBytes,
4+
formatDecimalCurrency
5+
} from './formatting.js';
6+
7+
describe('formatting utilities', () => {
8+
describe('formatUptime', () => {
9+
test('should format seconds only', () => {
10+
expect(formatUptime(30)).toBe('30s');
11+
expect(formatUptime(0)).toBe('0s');
12+
});
13+
14+
test('should format minutes and seconds', () => {
15+
expect(formatUptime(90)).toBe('1m 30s');
16+
expect(formatUptime(120)).toBe('2m');
17+
});
18+
19+
test('should format hours, minutes and seconds', () => {
20+
expect(formatUptime(3665)).toBe('1h 1m 5s');
21+
expect(formatUptime(3600)).toBe('1h');
22+
});
23+
24+
test('should format days, hours, minutes and seconds', () => {
25+
expect(formatUptime(90061)).toBe('1d 1h 1m 1s');
26+
expect(formatUptime(86400)).toBe('1d');
27+
});
28+
29+
test('should handle complex combinations', () => {
30+
expect(formatUptime(93784)).toBe('1d 2h 3m 4s');
31+
expect(formatUptime(259200)).toBe('3d');
32+
});
33+
34+
test('should floor decimal seconds', () => {
35+
expect(formatUptime(30.9)).toBe('30s');
36+
});
37+
});
38+
39+
describe('formatBytes', () => {
40+
test('should format bytes', () => {
41+
expect(formatBytes(0)).toBe('0 B');
42+
expect(formatBytes(512)).toBe('512.00 B');
43+
expect(formatBytes(1023)).toBe('1023.00 B');
44+
});
45+
46+
test('should format kilobytes', () => {
47+
expect(formatBytes(1024)).toBe('1.00 KB');
48+
expect(formatBytes(1536)).toBe('1.50 KB');
49+
});
50+
51+
test('should format megabytes', () => {
52+
expect(formatBytes(1048576)).toBe('1.00 MB');
53+
expect(formatBytes(1572864)).toBe('1.50 MB');
54+
});
55+
56+
test('should format gigabytes', () => {
57+
expect(formatBytes(1073741824)).toBe('1.00 GB');
58+
expect(formatBytes(1610612736)).toBe('1.50 GB');
59+
});
60+
61+
test('should format terabytes', () => {
62+
expect(formatBytes(1099511627776)).toBe('1.00 TB');
63+
});
64+
});
65+
66+
describe('formatDecimalCurrency', () => {
67+
test('should format with default prefix and suffix', () => {
68+
expect(formatDecimalCurrency(100.5)).toBe('$100.50');
69+
expect(formatDecimalCurrency(0)).toBe('$0.00');
70+
});
71+
72+
test('should format with custom prefix', () => {
73+
expect(formatDecimalCurrency(100.5, '€')).toBe('€100.50');
74+
expect(formatDecimalCurrency(100.5, '')).toBe('100.50');
75+
});
76+
77+
test('should format with custom suffix', () => {
78+
expect(formatDecimalCurrency(100.5, '$', 'USD')).toBe('$100.50 USD');
79+
});
80+
81+
test('should handle prefix spacing', () => {
82+
expect(formatDecimalCurrency(100.5, '$', '', true)).toBe('$ 100.50');
83+
});
84+
85+
test('should handle suffix spacing', () => {
86+
expect(formatDecimalCurrency(100.5, '$', 'USD', false, false)).toBe('$100.50USD');
87+
});
88+
89+
test('should format with both prefix and suffix spacing', () => {
90+
expect(formatDecimalCurrency(100.5, '€', 'EUR', true, false)).toBe('€ 100.50EUR');
91+
});
92+
93+
test('should round to 2 decimal places', () => {
94+
expect(formatDecimalCurrency(100.555)).toBe('$100.56');
95+
expect(formatDecimalCurrency(100.554)).toBe('$100.55');
96+
});
97+
});
98+
});

source/utils/similarity.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { calculateSimilarity } from './similarity.js';
2+
3+
describe('similarity utilities', () => {
4+
describe('calculateSimilarity', () => {
5+
test('should return 1 for identical strings', () => {
6+
expect(calculateSimilarity('hello', 'hello')).toBe(1);
7+
expect(calculateSimilarity('', '')).toBe(1);
8+
});
9+
10+
test('should return 0 for completely different strings', () => {
11+
expect(calculateSimilarity('abc', 'xyz')).toBe(0);
12+
});
13+
14+
test('should return 0 when one string is empty', () => {
15+
expect(calculateSimilarity('', 'hello')).toBe(0);
16+
expect(calculateSimilarity('hello', '')).toBe(0);
17+
});
18+
19+
test('should be case insensitive', () => {
20+
expect(calculateSimilarity('Hello', 'hello')).toBe(1);
21+
expect(calculateSimilarity('WORLD', 'world')).toBe(1);
22+
});
23+
24+
test('should handle single character differences', () => {
25+
expect(calculateSimilarity('cat', 'bat')).toBeCloseTo(2/3, 10);
26+
expect(calculateSimilarity('test', 'best')).toBe(0.75);
27+
});
28+
29+
test('should handle insertions', () => {
30+
expect(calculateSimilarity('cat', 'cats')).toBe(0.75);
31+
});
32+
33+
test('should handle deletions', () => {
34+
expect(calculateSimilarity('cats', 'cat')).toBe(0.75);
35+
});
36+
37+
test('should handle substitutions', () => {
38+
expect(calculateSimilarity('kitten', 'sitting')).toBeCloseTo(0.571, 2);
39+
});
40+
41+
test('should convert non-string inputs to strings', () => {
42+
expect(calculateSimilarity(123, '123')).toBe(1);
43+
expect(calculateSimilarity(123, 124)).toBeCloseTo(2/3, 2);
44+
});
45+
46+
test('should handle longer strings with optimized algorithm', () => {
47+
const longStr1 = 'a'.repeat(150);
48+
const longStr2 = 'a'.repeat(149) + 'b';
49+
50+
expect(calculateSimilarity(longStr1, longStr2)).toBeCloseTo(149/150, 2);
51+
});
52+
53+
test('should handle medium length strings', () => {
54+
const str1 = 'this is a test string for similarity';
55+
const str2 = 'this is a test string for similarity testing';
56+
57+
expect(calculateSimilarity(str1, str2)).toBeCloseTo(36/44, 2);
58+
});
59+
60+
test('should handle completely different long strings', () => {
61+
const str1 = 'a'.repeat(150);
62+
const str2 = 'b'.repeat(150);
63+
64+
expect(calculateSimilarity(str1, str2)).toBe(0);
65+
});
66+
});
67+
});

0 commit comments

Comments
 (0)