Skip to content

Commit 9507898

Browse files
committed
Add tests
1 parent e248f11 commit 9507898

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2+
import { checkTimeInRange } from "../../../src/common/datetime/check_time";
3+
import type { HomeAssistant } from "../../../src/types";
4+
import {
5+
NumberFormat,
6+
TimeFormat,
7+
FirstWeekday,
8+
DateFormat,
9+
TimeZone,
10+
} from "../../../src/data/translation";
11+
12+
describe("checkTimeInRange", () => {
13+
let mockHass: HomeAssistant;
14+
15+
beforeEach(() => {
16+
mockHass = {
17+
locale: {
18+
language: "en-US",
19+
number_format: NumberFormat.language,
20+
time_format: TimeFormat.language,
21+
date_format: DateFormat.language,
22+
time_zone: TimeZone.local,
23+
first_weekday: FirstWeekday.language,
24+
},
25+
config: {
26+
time_zone: "America/Los_Angeles",
27+
},
28+
} as HomeAssistant;
29+
});
30+
31+
afterEach(() => {
32+
vi.useRealTimers();
33+
});
34+
35+
describe("time ranges within same day", () => {
36+
it("should return true when current time is within range", () => {
37+
// Set time to 10:00 AM
38+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
39+
40+
expect(checkTimeInRange(mockHass, "08:00", "17:00")).toBe(true);
41+
});
42+
43+
it("should return false when current time is before range", () => {
44+
// Set time to 7:00 AM
45+
vi.setSystemTime(new Date(2024, 0, 15, 7, 0, 0));
46+
47+
expect(checkTimeInRange(mockHass, "08:00", "17:00")).toBe(false);
48+
});
49+
50+
it("should return false when current time is after range", () => {
51+
// Set time to 6:00 PM
52+
vi.setSystemTime(new Date(2024, 0, 15, 18, 0, 0));
53+
54+
expect(checkTimeInRange(mockHass, "08:00", "17:00")).toBe(false);
55+
});
56+
});
57+
58+
describe("time ranges crossing midnight", () => {
59+
it("should return true when current time is before midnight", () => {
60+
// Set time to 11:00 PM
61+
vi.setSystemTime(new Date(2024, 0, 15, 23, 0, 0));
62+
63+
expect(checkTimeInRange(mockHass, "22:00", "06:00")).toBe(true);
64+
});
65+
66+
it("should return true when current time is after midnight", () => {
67+
// Set time to 3:00 AM
68+
vi.setSystemTime(new Date(2024, 0, 15, 3, 0, 0));
69+
70+
expect(checkTimeInRange(mockHass, "22:00", "06:00")).toBe(true);
71+
});
72+
73+
it("should return false when outside the range", () => {
74+
// Set time to 10:00 AM
75+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
76+
77+
expect(checkTimeInRange(mockHass, "22:00", "06:00")).toBe(false);
78+
});
79+
});
80+
81+
describe("only 'after' condition", () => {
82+
it("should return true when after specified time", () => {
83+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
84+
expect(checkTimeInRange(mockHass, "08:00")).toBe(true);
85+
});
86+
87+
it("should return false when before specified time", () => {
88+
vi.setSystemTime(new Date(2024, 0, 15, 6, 0, 0));
89+
expect(checkTimeInRange(mockHass, "08:00")).toBe(false);
90+
});
91+
});
92+
93+
describe("only 'before' condition", () => {
94+
it("should return true when before specified time", () => {
95+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
96+
expect(checkTimeInRange(mockHass, undefined, "17:00")).toBe(true);
97+
});
98+
99+
it("should return false when after specified time", () => {
100+
vi.setSystemTime(new Date(2024, 0, 15, 18, 0, 0));
101+
expect(checkTimeInRange(mockHass, undefined, "17:00")).toBe(false);
102+
});
103+
});
104+
105+
describe("weekday filtering", () => {
106+
it("should return true on matching weekday", () => {
107+
// January 15, 2024 is a Monday
108+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
109+
110+
expect(checkTimeInRange(mockHass, undefined, undefined, ["mon"])).toBe(
111+
true
112+
);
113+
});
114+
115+
it("should return false on non-matching weekday", () => {
116+
// January 15, 2024 is a Monday
117+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
118+
119+
expect(checkTimeInRange(mockHass, undefined, undefined, ["tue"])).toBe(
120+
false
121+
);
122+
});
123+
124+
it("should work with multiple weekdays", () => {
125+
// January 15, 2024 is a Monday
126+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
127+
128+
expect(
129+
checkTimeInRange(mockHass, undefined, undefined, ["mon", "wed", "fri"])
130+
).toBe(true);
131+
});
132+
});
133+
134+
describe("combined time and weekday conditions", () => {
135+
it("should return true when both match", () => {
136+
// January 15, 2024 is a Monday at 10:00 AM
137+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
138+
139+
expect(checkTimeInRange(mockHass, "08:00", "17:00", ["mon"])).toBe(true);
140+
});
141+
142+
it("should return false when time matches but weekday doesn't", () => {
143+
// January 15, 2024 is a Monday at 10:00 AM
144+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
145+
146+
expect(checkTimeInRange(mockHass, "08:00", "17:00", ["tue"])).toBe(false);
147+
});
148+
149+
it("should return false when weekday matches but time doesn't", () => {
150+
// January 15, 2024 is a Monday at 6:00 AM
151+
vi.setSystemTime(new Date(2024, 0, 15, 6, 0, 0));
152+
153+
expect(checkTimeInRange(mockHass, "08:00", "17:00", ["mon"])).toBe(false);
154+
});
155+
});
156+
157+
describe("no conditions", () => {
158+
it("should return true when no conditions specified", () => {
159+
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
160+
161+
expect(checkTimeInRange(mockHass)).toBe(true);
162+
});
163+
});
164+
});

0 commit comments

Comments
 (0)