diff --git a/src/scripts/evermarch.js b/src/scripts/evermarch.js index 3c1636b..c878fc9 100644 --- a/src/scripts/evermarch.js +++ b/src/scripts/evermarch.js @@ -6,13 +6,13 @@ const moment = require("moment-timezone"); -const march1 = moment.tz("2020-03-01T00:00:00Z", "America/New_York"); +const march1 = moment.tz("2020-03-01T00:00:00", "America/New_York"); module.exports = (robot) => { robot.message(/evermarch/i, ({ message, say }) => { const now = moment.tz("America/New_York"); - const days = Math.ceil(moment.duration(now.diff(march1)).as("days")); + const days = now.diff(march1, "days") + 1; say({ icon_emoji: ":calendar-this-is-fine:", diff --git a/src/scripts/evermarch.test.js b/src/scripts/evermarch.test.js index 8052d4e..7040aea 100644 --- a/src/scripts/evermarch.test.js +++ b/src/scripts/evermarch.test.js @@ -1,3 +1,4 @@ +const moment = require("moment-timezone"); const { getApp } = require("../utils/test"); const evermarch = require("./evermarch"); @@ -26,11 +27,30 @@ describe("The Evermarch", () => { }); it("is correct starting March 2, 2020", () => { - // Go very slightly ahead of midnight because at exactly midnight, the diff - // rounds to exactly one day, so the ceil() logic doesn't yet count it as an - // extra day. But a millisecond later, it will. This seems fine. Millisecond - // precision is plenty. - jest.setSystemTime(Date.parse("2020-03-02T00:00:01Z")); + jest.setSystemTime( + moment.tz("2020-03-02T00:00:00", "America/New_York").toDate(), + ); + + const message = { + message: { thread_ts: "thread timestamp" }, + say: jest.fn(), + }; + + evermarch(app); + const handler = app.getHandler(); + handler(message); + + expect(message.say).toHaveBeenCalledWith({ + icon_emoji: ":calendar-this-is-fine:", + text: "Today is March 2, 2020, in the Evermarch reckoning.", + thread_ts: "thread timestamp", + }); + }); + + it("is correct at the very end of March 2, 2020", () => { + jest.setSystemTime( + moment.tz("2020-03-02T23:59:59", "America/New_York").toDate(), + ); const message = { message: { thread_ts: "thread timestamp" }, @@ -48,12 +68,31 @@ describe("The Evermarch", () => { }); }); + it("is correct at the very start of March 3, 2020", () => { + jest.setSystemTime( + moment.tz("2020-03-03T00:00:00", "America/New_York").toDate(), + ); + + const message = { + message: { thread_ts: "thread timestamp" }, + say: jest.fn(), + }; + + evermarch(app); + const handler = app.getHandler(); + handler(message); + + expect(message.say).toHaveBeenCalledWith({ + icon_emoji: ":calendar-this-is-fine:", + text: "Today is March 3, 2020, in the Evermarch reckoning.", + thread_ts: "thread timestamp", + }); + }); + it("is correct in the further future from March 1, 2020", () => { - // Go very slightly ahead of midnight because at exactly midnight, the diff - // rounds to exactly one day, so the ceil() logic doesn't yet count it as an - // extra day. But a millisecond later, it will. This seems fine. Millisecond - // precision is plenty. - jest.setSystemTime(Date.parse("2024-10-15T00:00:01Z")); + jest.setSystemTime( + moment.tz("2024-10-15T01:00:00", "America/New_York").toDate(), + ); const message = { message: { thread_ts: "thread timestamp" }, @@ -72,7 +111,9 @@ describe("The Evermarch", () => { }); it("is gets to March 2020, 2020, on the expected date", () => { - jest.setSystemTime(Date.parse("2025-09-10T12:00:00Z")); + jest.setSystemTime( + moment.tz("2025-09-10T00:00:00", "America/New_York").toDate(), + ); const message = { message: { thread_ts: "thread timestamp" },