Skip to content

Fix today anotation in games update bot message. #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export function formatDate(date: Date): string {
export function formatDateWithYear(date: Date): string {
return format(date, 'EEE MMM d - HH:mm (yyyy)');
}
export function isToday(date: Date): boolean {
const today = new Date();
export function isToday(date: Date, today: Date): boolean {
return (
date.getDate() === today.getDate() &&
date.getMonth() === today.getMonth() &&
Expand Down
11 changes: 5 additions & 6 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { formatDate, isToday, formatDateWithYear } from './date';
import { Fixture, FixturesUpdate } from './domain';

function getFixtureLine({ date, leage, homeTeam, awayTeam }: Fixture): string {
function getFixtureLine({ date, leage, homeTeam, awayTeam }: Fixture, today: Date): string {
const fdate = formatDate(date);
let result = '';
if (homeTeam === undefined || awayTeam === undefined) {
Expand All @@ -11,7 +11,7 @@ function getFixtureLine({ date, leage, homeTeam, awayTeam }: Fixture): string {
} else {
result = `*${fdate}* - ${homeTeam} VS ${awayTeam} (${leage})\n`;
}
if (isToday(date)) {
if (isToday(date, today)) {
result = `Today 👉 *${result}`;
}
return result;
Expand All @@ -22,15 +22,14 @@ function getFixtureLine({ date, leage, homeTeam, awayTeam }: Fixture): string {
* @param update fixture update object
* @returns formatted telegram richstring message
*/
export function fixturesToRichText(update: FixturesUpdate): string {
export function fixturesToRichText(update: FixturesUpdate, today: Date): string {
let result = `📟 *Update for ${formatDateWithYear(update.date)}*\n`;
if (update.fixtures === undefined || update.fixtures.length === 0) {
return `${result}*No upcoming games for the year*\n`;
}

result += `⚽ *Games at ${update.venue} for this year*\n\n`;
result += `⚽ *${update.date.getFullYear()} games at ${update.venue}*\n\n`;
for (const fixture of update.fixtures) {
result += getFixtureLine(fixture);
result += getFixtureLine(fixture, today);
}
result += `\n📡 [Source](${update.source})\n`;
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function scrapeArsenalFixtures(chatId?: string) {
fixtures: fixtures.filter((f) => f.venue === VENUE && f.date > now),
source: URL
};
const message = fixturesToRichText(update);
const message = fixturesToRichText(update, now);
logger.info(message);
await sendRichText(message, chatId);
} else {
Expand Down
9 changes: 8 additions & 1 deletion test/date.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { describe, expect, it } from '@jest/globals';
import { formatDate, parseDate } from '../src/date';
import { formatDate, parseDate, isToday } from '../src/date';

describe('date', () => {
it('formatDate', () => {
expect(formatDate(new Date('2023-11-16T15:40:00.980Z'))).toBe('Thu Nov 16 - 15:40');
});
});

describe('isToday', () => {
it('isToday', () => {
expect(isToday(new Date('2023-11-16T15:40:00.980Z'), new Date('2023-11-16T15:40:00.980Z'))).toBe(true);
expect(isToday(new Date('2023-11-16T15:40:00.980Z'), new Date('2023-11-17T15:40:00.980Z'))).toBe(false);
});
});
124 changes: 81 additions & 43 deletions test/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,105 @@ import { fixturesToRichText } from '../src/format';
describe('Fixtures format', () => {
it('expect no games', () => {
expect(
fixturesToRichText({
fixtures: [],
date: new Date('2023-11-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
source: 'test.com'
})
fixturesToRichText(
{
fixtures: [],
date: new Date('2023-11-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
source: 'test.com'
},
new Date('2022-11-12T15:40:00.980Z')
)
).toBe(`📟 *Update for Thu Nov 16 - 15:40 (2023)*
*No upcoming games for the year*
`);
});
it('expect single game', () => {
expect(
fixturesToRichText({
date: new Date('2023-11-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
fixtures: [
{
date: new Date('2023-12-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
leage: 'Premier League',
homeTeam: 'Arsenal',
awayTeam: 'Chelsea'
}
],
source: 'test.com'
})
fixturesToRichText(
{
date: new Date('2023-11-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
fixtures: [
{
date: new Date('2023-12-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
leage: 'Premier League',
homeTeam: 'Arsenal',
awayTeam: 'Chelsea'
}
],
source: 'test.com'
},
new Date('2022-11-12T15:40:00.980Z')
)
).toBe(`📟 *Update for Thu Nov 16 - 15:40 (2023)*
⚽ *Games at Emirates Stadium for this year*
⚽ *2023 games at Emirates Stadium*

*Sat Dec 16 - 15:40* - Arsenal VS Chelsea (Premier League)

📡 [Source](test.com)
`);
});

it('expect today annotation game', () => {
expect(
fixturesToRichText({
date: new Date('2022-11-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
fixtures: [
{
date: new Date('2022-12-01'),
venue: 'Emirates Stadium',
leage: 'Premier League',
homeTeam: 'Arsenal',
awayTeam: 'Chelsea'
},
{
date: new Date('2022-11-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
leage: 'Premier League',
homeTeam: 'Arsenal',
awayTeam: 'Tel Aviv'
}
],
source: 'test.com'
})
fixturesToRichText(
{
date: new Date('2022-11-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
fixtures: [
{
date: new Date('2022-12-01'),
venue: 'Emirates Stadium',
leage: 'Premier League',
homeTeam: 'Arsenal',
awayTeam: 'Chelsea'
}
],
source: 'test.com'
},
new Date('2022-11-12T15:40:00.980Z')
)
).toBe(`📟 *Update for Wed Nov 16 - 15:40 (2022)*
⚽ *Games at Emirates Stadium for this year*
⚽ *2022 games at Emirates Stadium*

*Thu Dec 1 - 00:00* - Arsenal VS Chelsea (Premier League)

📡 [Source](test.com)
`);
});

it('expect today annotation game', () => {
expect(
fixturesToRichText(
{
date: new Date('2022-11-12T15:40:00.980Z'),
venue: 'Emirates Stadium',
fixtures: [
{
date: new Date('2022-11-12T15:40:00.980Z'),
venue: 'Emirates Stadium',
leage: 'Premier League',
homeTeam: 'Arsenal',
awayTeam: 'Chelsea'
},
{
date: new Date('2022-11-16T15:40:00.980Z'),
venue: 'Emirates Stadium',
leage: 'Premier League',
homeTeam: 'Arsenal',
awayTeam: 'Tel Aviv'
}
],
source: 'test.com'
},
new Date('2022-11-12T15:40:00.980Z')
)
).toBe(`📟 *Update for Sat Nov 12 - 15:40 (2022)*
⚽ *2022 games at Emirates Stadium*

Today 👉 **Sat Nov 12 - 15:40* - Arsenal VS Chelsea (Premier League)
*Wed Nov 16 - 15:40* - Arsenal VS Tel Aviv (Premier League)

📡 [Source](test.com)
Expand Down
Loading