forked from Xeio/IdleCodeRedeemer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeScanner.test.ts
More file actions
85 lines (69 loc) · 3.06 KB
/
codeScanner.test.ts
File metadata and controls
85 lines (69 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { describe, test, expect } from 'bun:test';
import { extractCodesFromText } from './codeScanner';
describe('extractCodesFromText', () => {
describe('12-character codes', () => {
test('matches a plain 12-char alphanumeric code', () => {
expect(extractCodesFromText('ABCD1234EFGH')).toEqual(['ABCD1234EFGH']);
});
test('matches a 12-char code with dashes and strips them', () => {
expect(extractCodesFromText('ABCD-1234-EFGH')).toEqual(['ABCD1234EFGH']);
});
});
describe('16-character codes', () => {
test('matches a plain 16-char code', () => {
expect(extractCodesFromText('ABCD1234EFGH5678')).toEqual(['ABCD1234EFGH5678']);
});
test('matches a 16-char code with dashes and strips them', () => {
expect(extractCodesFromText('ABCD-1234-EFGH-5678')).toEqual(['ABCD1234EFGH5678']);
});
});
describe('case normalisation', () => {
test('lowercased input is uppercased before matching', () => {
expect(extractCodesFromText('abcd1234efgh')).toEqual(['ABCD1234EFGH']);
});
test('mixed-case input is normalised', () => {
expect(extractCodesFromText('AbCd1234eFgH')).toEqual(['ABCD1234EFGH']);
});
});
describe('URL stripping', () => {
test('does not extract Twitch username as code', () => {
const text = 'LATU-EGIS-TOCK\n\nhttps://www.twitch.tv/dungeonscrawlers\n1x Electrum Chest';
expect(extractCodesFromText(text)).toEqual(['LATUEGISTOCK']);
});
test('does not extract long URL path segments as codes', () => {
const text = 'GOEL-ARNA-VIDS\nhttps://www.twitch.tv/jasoncharlesmiller\n1x Electrum Chest';
expect(extractCodesFromText(text)).toEqual(['GOELARNAVIDS']);
});
test('strips http URLs as well as https', () => {
const text = 'ABCD1234EFGH http://example.com/SOMETHINGLONG123456';
expect(extractCodesFromText(text)).toEqual(['ABCD1234EFGH']);
});
});
describe('Discord emoji stripping', () => {
test('strips static emoji tags before matching', () => {
const text = 'Redeem <:gem:123456789012345678> this: ABCD1234EFGH';
expect(extractCodesFromText(text)).toEqual(['ABCD1234EFGH']);
});
test('strips animated emoji tags before matching', () => {
const text = '<a:spin:123456789012345678> ABCD1234EFGH';
expect(extractCodesFromText(text)).toEqual(['ABCD1234EFGH']);
});
});
describe('edge cases', () => {
test('returns empty array for empty string', () => {
expect(extractCodesFromText('')).toEqual([]);
});
test('returns empty array when no code present', () => {
expect(extractCodesFromText('Hello, welcome to Idle Champions!')).toEqual([]);
});
test('returns multiple codes from one text', () => {
const result = extractCodesFromText('Code1: ABCD1234EFGH Code2: WXYZ5678IJKL');
expect(result).toHaveLength(2);
expect(result).toContain('ABCD1234EFGH');
expect(result).toContain('WXYZ5678IJKL');
});
test('strings shorter than 12 characters are not matched', () => {
expect(extractCodesFromText('ABCD1234')).toEqual([]);
});
});
});