Skip to content

Commit ceaca92

Browse files
authored
Merge pull request #50 from Pavel-Durov/fix/today-annotation-bot-message
Fix today anotation in games update bot message.
2 parents a40e78e + 578c355 commit ceaca92

File tree

5 files changed

+96
-53
lines changed

5 files changed

+96
-53
lines changed

src/date.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export function formatDate(date: Date): string {
1111
export function formatDateWithYear(date: Date): string {
1212
return format(date, 'EEE MMM d - HH:mm (yyyy)');
1313
}
14-
export function isToday(date: Date): boolean {
15-
const today = new Date();
14+
export function isToday(date: Date, today: Date): boolean {
1615
return (
1716
date.getDate() === today.getDate() &&
1817
date.getMonth() === today.getMonth() &&

src/format.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { formatDate, isToday, formatDateWithYear } from './date';
22
import { Fixture, FixturesUpdate } from './domain';
33

4-
function getFixtureLine({ date, leage, homeTeam, awayTeam }: Fixture): string {
4+
function getFixtureLine({ date, leage, homeTeam, awayTeam }: Fixture, today: Date): string {
55
const fdate = formatDate(date);
66
let result = '';
77
if (homeTeam === undefined || awayTeam === undefined) {
@@ -11,7 +11,7 @@ function getFixtureLine({ date, leage, homeTeam, awayTeam }: Fixture): string {
1111
} else {
1212
result = `*${fdate}* - ${homeTeam} VS ${awayTeam} (${leage})\n`;
1313
}
14-
if (isToday(date)) {
14+
if (isToday(date, today)) {
1515
result = `Today 👉 *${result}`;
1616
}
1717
return result;
@@ -22,15 +22,14 @@ function getFixtureLine({ date, leage, homeTeam, awayTeam }: Fixture): string {
2222
* @param update fixture update object
2323
* @returns formatted telegram richstring message
2424
*/
25-
export function fixturesToRichText(update: FixturesUpdate): string {
25+
export function fixturesToRichText(update: FixturesUpdate, today: Date): string {
2626
let result = `📟 *Update for ${formatDateWithYear(update.date)}*\n`;
2727
if (update.fixtures === undefined || update.fixtures.length === 0) {
2828
return `${result}*No upcoming games for the year*\n`;
2929
}
30-
31-
result += `⚽ *Games at ${update.venue} for this year*\n\n`;
30+
result += `⚽ *${update.date.getFullYear()} games at ${update.venue}*\n\n`;
3231
for (const fixture of update.fixtures) {
33-
result += getFixtureLine(fixture);
32+
result += getFixtureLine(fixture, today);
3433
}
3534
result += `\n📡 [Source](${update.source})\n`;
3635
return result;

src/scrape.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function scrapeArsenalFixtures(chatId?: string) {
6060
fixtures: fixtures.filter((f) => f.venue === VENUE && f.date > now),
6161
source: URL
6262
};
63-
const message = fixturesToRichText(update);
63+
const message = fixturesToRichText(update, now);
6464
logger.info(message);
6565
await sendRichText(message, chatId);
6666
} else {

test/date.test.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { describe, expect, it } from '@jest/globals';
2-
import { formatDate, parseDate } from '../src/date';
2+
import { formatDate, parseDate, isToday } from '../src/date';
33

44
describe('date', () => {
55
it('formatDate', () => {
66
expect(formatDate(new Date('2023-11-16T15:40:00.980Z'))).toBe('Thu Nov 16 - 15:40');
77
});
88
});
9+
10+
describe('isToday', () => {
11+
it('isToday', () => {
12+
expect(isToday(new Date('2023-11-16T15:40:00.980Z'), new Date('2023-11-16T15:40:00.980Z'))).toBe(true);
13+
expect(isToday(new Date('2023-11-16T15:40:00.980Z'), new Date('2023-11-17T15:40:00.980Z'))).toBe(false);
14+
});
15+
});

test/format.test.ts

+81-43
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,105 @@ import { fixturesToRichText } from '../src/format';
44
describe('Fixtures format', () => {
55
it('expect no games', () => {
66
expect(
7-
fixturesToRichText({
8-
fixtures: [],
9-
date: new Date('2023-11-16T15:40:00.980Z'),
10-
venue: 'Emirates Stadium',
11-
source: 'test.com'
12-
})
7+
fixturesToRichText(
8+
{
9+
fixtures: [],
10+
date: new Date('2023-11-16T15:40:00.980Z'),
11+
venue: 'Emirates Stadium',
12+
source: 'test.com'
13+
},
14+
new Date('2022-11-12T15:40:00.980Z')
15+
)
1316
).toBe(`📟 *Update for Thu Nov 16 - 15:40 (2023)*
1417
*No upcoming games for the year*
1518
`);
1619
});
1720
it('expect single game', () => {
1821
expect(
19-
fixturesToRichText({
20-
date: new Date('2023-11-16T15:40:00.980Z'),
21-
venue: 'Emirates Stadium',
22-
fixtures: [
23-
{
24-
date: new Date('2023-12-16T15:40:00.980Z'),
25-
venue: 'Emirates Stadium',
26-
leage: 'Premier League',
27-
homeTeam: 'Arsenal',
28-
awayTeam: 'Chelsea'
29-
}
30-
],
31-
source: 'test.com'
32-
})
22+
fixturesToRichText(
23+
{
24+
date: new Date('2023-11-16T15:40:00.980Z'),
25+
venue: 'Emirates Stadium',
26+
fixtures: [
27+
{
28+
date: new Date('2023-12-16T15:40:00.980Z'),
29+
venue: 'Emirates Stadium',
30+
leage: 'Premier League',
31+
homeTeam: 'Arsenal',
32+
awayTeam: 'Chelsea'
33+
}
34+
],
35+
source: 'test.com'
36+
},
37+
new Date('2022-11-12T15:40:00.980Z')
38+
)
3339
).toBe(`📟 *Update for Thu Nov 16 - 15:40 (2023)*
34-
⚽ *Games at Emirates Stadium for this year*
40+
⚽ *2023 games at Emirates Stadium*
3541
3642
*Sat Dec 16 - 15:40* - Arsenal VS Chelsea (Premier League)
3743
3844
📡 [Source](test.com)
3945
`);
4046
});
47+
4148
it('expect today annotation game', () => {
4249
expect(
43-
fixturesToRichText({
44-
date: new Date('2022-11-16T15:40:00.980Z'),
45-
venue: 'Emirates Stadium',
46-
fixtures: [
47-
{
48-
date: new Date('2022-12-01'),
49-
venue: 'Emirates Stadium',
50-
leage: 'Premier League',
51-
homeTeam: 'Arsenal',
52-
awayTeam: 'Chelsea'
53-
},
54-
{
55-
date: new Date('2022-11-16T15:40:00.980Z'),
56-
venue: 'Emirates Stadium',
57-
leage: 'Premier League',
58-
homeTeam: 'Arsenal',
59-
awayTeam: 'Tel Aviv'
60-
}
61-
],
62-
source: 'test.com'
63-
})
50+
fixturesToRichText(
51+
{
52+
date: new Date('2022-11-16T15:40:00.980Z'),
53+
venue: 'Emirates Stadium',
54+
fixtures: [
55+
{
56+
date: new Date('2022-12-01'),
57+
venue: 'Emirates Stadium',
58+
leage: 'Premier League',
59+
homeTeam: 'Arsenal',
60+
awayTeam: 'Chelsea'
61+
}
62+
],
63+
source: 'test.com'
64+
},
65+
new Date('2022-11-12T15:40:00.980Z')
66+
)
6467
).toBe(`📟 *Update for Wed Nov 16 - 15:40 (2022)*
65-
⚽ *Games at Emirates Stadium for this year*
68+
⚽ *2022 games at Emirates Stadium*
6669
6770
*Thu Dec 1 - 00:00* - Arsenal VS Chelsea (Premier League)
71+
72+
📡 [Source](test.com)
73+
`);
74+
});
75+
76+
it('expect today annotation game', () => {
77+
expect(
78+
fixturesToRichText(
79+
{
80+
date: new Date('2022-11-12T15:40:00.980Z'),
81+
venue: 'Emirates Stadium',
82+
fixtures: [
83+
{
84+
date: new Date('2022-11-12T15:40:00.980Z'),
85+
venue: 'Emirates Stadium',
86+
leage: 'Premier League',
87+
homeTeam: 'Arsenal',
88+
awayTeam: 'Chelsea'
89+
},
90+
{
91+
date: new Date('2022-11-16T15:40:00.980Z'),
92+
venue: 'Emirates Stadium',
93+
leage: 'Premier League',
94+
homeTeam: 'Arsenal',
95+
awayTeam: 'Tel Aviv'
96+
}
97+
],
98+
source: 'test.com'
99+
},
100+
new Date('2022-11-12T15:40:00.980Z')
101+
)
102+
).toBe(`📟 *Update for Sat Nov 12 - 15:40 (2022)*
103+
⚽ *2022 games at Emirates Stadium*
104+
105+
Today 👉 **Sat Nov 12 - 15:40* - Arsenal VS Chelsea (Premier League)
68106
*Wed Nov 16 - 15:40* - Arsenal VS Tel Aviv (Premier League)
69107
70108
📡 [Source](test.com)

0 commit comments

Comments
 (0)