Skip to content

Commit

Permalink
Merge pull request #569 from 18F/mgwalker/fix-evermarch
Browse files Browse the repository at this point in the history
Fixes a couple of bugs related to UTC offsets and day-counting.
  • Loading branch information
mgwalker authored Jan 16, 2025
2 parents d885385 + 2b0f0c2 commit 9391f2b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/scripts/evermarch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:",
Expand Down
63 changes: 52 additions & 11 deletions src/scripts/evermarch.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const moment = require("moment-timezone");
const { getApp } = require("../utils/test");
const evermarch = require("./evermarch");

Expand Down Expand Up @@ -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" },
Expand All @@ -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" },
Expand All @@ -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" },
Expand Down

0 comments on commit 9391f2b

Please sign in to comment.