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
147 lines (120 loc) · 5.47 KB
/
codeScanner.test.ts
File metadata and controls
147 lines (120 loc) · 5.47 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test';
import { extractCodesFromText, scanMessageForCodes } from './codeScanner';
import { db, initializeDatabase } from '../database/db';
import { redeemedCodes, pendingCodes, users } from '../database/schema/index';
import { codeManager } from '../database/codeManager';
import type { Message } from 'discord.js';
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([]);
});
});
});
// ---------------------------------------------------------------------------
// scanMessageForCodes — integration tests (uses DB)
// ---------------------------------------------------------------------------
const USER = 'discord-scanner-user';
/** Build a minimal Discord Message-like object with just a content string. */
function fakeMessage(content: string): Message {
return { content } as unknown as Message;
}
beforeAll(() => {
initializeDatabase();
});
beforeEach(() => {
db.delete(pendingCodes).run();
db.delete(redeemedCodes).run();
db.delete(users).run();
});
describe('scanMessageForCodes', () => {
test('returns new codes not yet in the database', async () => {
const codes = await scanMessageForCodes(fakeMessage('ABCD1234EFGH'));
expect(codes).toEqual(['ABCD1234EFGH']);
});
test('returns empty array when the code is already redeemed', async () => {
db.insert(users).values({ discordId: USER, userId: '111', userHash: 'hash' }).run();
await codeManager.addRedeemedCode('ABCD1234EFGH', USER, 'Success');
const codes = await scanMessageForCodes(fakeMessage('ABCD1234EFGH'));
expect(codes).toEqual([]);
});
test('returns empty array for a message with no codes', async () => {
const codes = await scanMessageForCodes(fakeMessage('Hello world!'));
expect(codes).toEqual([]);
});
test('returns multiple new codes from one message', async () => {
const codes = await scanMessageForCodes(fakeMessage('Code 1: ABCD1234EFGH Code 2: WXYZ5678IJKL'));
expect(codes).toHaveLength(2);
expect(codes).toContain('ABCD1234EFGH');
expect(codes).toContain('WXYZ5678IJKL');
});
test('filters out already-redeemed codes while returning new ones', async () => {
db.insert(users).values({ discordId: USER, userId: '111', userHash: 'hash' }).run();
await codeManager.addRedeemedCode('ABCD1234EFGH', USER, 'Success');
const codes = await scanMessageForCodes(fakeMessage('ABCD1234EFGH WXYZ5678IJKL'));
expect(codes).toEqual(['WXYZ5678IJKL']);
});
test('returns empty array on empty message', async () => {
expect(await scanMessageForCodes(fakeMessage(''))).toEqual([]);
});
});