-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e52c8f
commit 46bea77
Showing
10 changed files
with
570 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
MORNING: | ||
time: | ||
hours: 7 | ||
minutes: 0 | ||
message: 'マルナナマルマル、朝だ。…司令官、まだ寝てるの?' | ||
NOON: | ||
time: | ||
hours: 12 | ||
minutes: 0 | ||
message: 'Полдень. 今日は何を作ってるんだい?' | ||
MIDNIGHT: | ||
time: | ||
hours: 2 | ||
minutes: 0 | ||
message: 'マルフタマルマル、もう遅いね。眠かったらどうぞ。私の膝を貸そうか。' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { parse } from 'yaml'; | ||
|
||
import { messageTypes, SignalSchedule } from '../service/time-signal.js'; | ||
|
||
const messageFields = ['time', 'message'] as const; | ||
const timeFields = ['hours', 'minutes'] as const; | ||
|
||
const validate = (object: unknown): object is SignalSchedule => { | ||
if (!(typeof object === 'object' && object !== null)) { | ||
return false; | ||
} | ||
for (const key of messageTypes) { | ||
if (!Object.hasOwn(object, key)) { | ||
return false; | ||
} | ||
const entryUnsafe = (object as Record<typeof key, unknown>)[key]; | ||
if (!(typeof entryUnsafe === 'object' && entryUnsafe !== null)) { | ||
return false; | ||
} | ||
for (const field of messageFields) { | ||
if (!Object.hasOwn(entryUnsafe, field)) { | ||
return false; | ||
} | ||
} | ||
const { time: timeUnsafe, message } = entryUnsafe as Record< | ||
(typeof messageFields)[number], | ||
unknown | ||
>; | ||
if (!(typeof message === 'string' && 0 < message.length)) { | ||
return false; | ||
} | ||
if (!(typeof timeUnsafe === 'object' && timeUnsafe !== null)) { | ||
return false; | ||
} | ||
const { hours, minutes } = timeUnsafe as Record< | ||
(typeof timeFields)[number], | ||
unknown | ||
>; | ||
if ( | ||
!( | ||
typeof hours === 'number' && | ||
Number.isInteger(hours) && | ||
0 <= hours && | ||
hours < 24 | ||
) | ||
) { | ||
return false; | ||
} | ||
if ( | ||
!( | ||
typeof minutes === 'number' && | ||
Number.isInteger(minutes) && | ||
0 <= minutes && | ||
minutes < 60 | ||
) | ||
) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
export const loadSchedule = (path: string[]): SignalSchedule => { | ||
const fileText = readFileSync(join(...path), { | ||
encoding: 'utf-8', | ||
flag: 'r' | ||
}); | ||
const parsed = parse(fileText) as unknown; | ||
if (!validate(parsed)) { | ||
console.error(parsed); | ||
throw new Error('invalid signal schedule'); | ||
} | ||
return parsed; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { describe, expect, test, vi } from 'vitest'; | ||
|
||
import { MockClock } from '../adaptor/clock.js'; | ||
import { ScheduleRunner } from '../runner/schedule.js'; | ||
import type { StandardOutput } from './output.js'; | ||
import { SignalSchedule, startTimeSignal } from './time-signal.js'; | ||
|
||
describe('time signal reported', () => { | ||
const clock = new MockClock(new Date(Date.UTC(2020, 0, 1, 0, 0))); | ||
const runner = new ScheduleRunner(clock); | ||
const output: StandardOutput = { sendEmbed: () => Promise.resolve() }; | ||
test('at now', () => { | ||
const sendEmbed = vi.spyOn(output, 'sendEmbed'); | ||
const schedule: SignalSchedule = { | ||
MORNING: { | ||
time: { | ||
hours: 8, | ||
minutes: 0 | ||
}, | ||
message: 'hoge' | ||
}, | ||
NOON: { | ||
time: { | ||
hours: 12, | ||
minutes: 0 | ||
}, | ||
message: 'fuga' | ||
}, | ||
MIDNIGHT: { | ||
time: { | ||
hours: 21, | ||
minutes: 0 | ||
}, | ||
message: 'foo' | ||
} | ||
}; | ||
|
||
startTimeSignal({ runner, clock, schedule, output }); | ||
clock.placeholder = new Date(Date.UTC(2020, 0, 1, 3, 1)); | ||
runner.consume(); | ||
|
||
expect(sendEmbed).toHaveBeenCalledOnce(); | ||
expect(sendEmbed).toHaveBeenCalledWith({ | ||
title: 'はらちょ時報システム', | ||
description: 'fuga', | ||
footer: '2020/01/01 12:01:00 JST' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Temporal, toTemporalInstant } from '@js-temporal/polyfill'; | ||
import { setTimeout } from 'timers/promises'; | ||
|
||
import type { | ||
Clock, | ||
ScheduleRunner, | ||
ScheduleTask | ||
} from '../runner/schedule.js'; | ||
import type { StandardOutput } from './output.js'; | ||
|
||
export const messageTypes = ['MORNING', 'NOON', 'MIDNIGHT'] as const; | ||
|
||
export type SignalMessageType = (typeof messageTypes)[number]; | ||
|
||
/** | ||
* 時報を送る時分を Asia/Tokyo のタイムゾーンで表す。 | ||
*/ | ||
export interface SignalTime { | ||
hours: number; | ||
minutes: number; | ||
} | ||
|
||
const intoDate = ({ hours, minutes }: SignalTime, clock: Clock): Date => { | ||
const nowInstant = toTemporalInstant.call(clock.now()); | ||
const now = nowInstant.toZonedDateTimeISO('Asia/Tokyo'); | ||
let hasHoursMinutes = now.withPlainTime({ | ||
hour: hours, | ||
minute: minutes | ||
}); | ||
if (Temporal.Instant.compare(nowInstant, hasHoursMinutes.toInstant()) > 0) { | ||
hasHoursMinutes = hasHoursMinutes.add(Temporal.Duration.from({ days: 1 })); | ||
} | ||
return new Date(hasHoursMinutes.epochMilliseconds); | ||
}; | ||
|
||
export interface SignalMessage { | ||
time: SignalTime; | ||
message: string; | ||
} | ||
|
||
export type SignalSchedule = Record<SignalMessageType, SignalMessage>; | ||
|
||
const reportTimeSignal = | ||
({ | ||
signalMessage, | ||
clock, | ||
output | ||
}: { | ||
signalMessage: SignalMessage; | ||
clock: Clock; | ||
output: StandardOutput; | ||
}): ScheduleTask => | ||
async (): Promise<Date> => { | ||
const nowInstant = toTemporalInstant.call(clock.now()); | ||
const now = nowInstant.toZonedDateTimeISO('Asia/Tokyo'); | ||
await output.sendEmbed({ | ||
title: 'はらちょ時報システム', | ||
description: signalMessage.message, | ||
footer: now.toLocaleString('ja-JP', { | ||
year: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
timeZoneName: 'short' | ||
}) | ||
}); | ||
await setTimeout(60 * 1000); | ||
return intoDate(signalMessage.time, clock); | ||
}; | ||
|
||
export const startTimeSignal = ({ | ||
runner, | ||
clock, | ||
schedule, | ||
output | ||
}: { | ||
runner: ScheduleRunner; | ||
clock: Clock; | ||
schedule: SignalSchedule; | ||
output: StandardOutput; | ||
}) => { | ||
for (const messageType of messageTypes) { | ||
const signalMessage = schedule[messageType]; | ||
const reportTask = reportTimeSignal({ signalMessage, clock, output }); | ||
const firstSignalDate = intoDate(signalMessage.time, clock); | ||
runner.runOnNextTime( | ||
`TIME_SIGNAL_${messageType}`, | ||
reportTask, | ||
firstSignalDate | ||
); | ||
} | ||
}; |
Oops, something went wrong.