|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 github.com/MaloneTalk |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package io.github.malonetalk.agent.tools; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | +import static org.mockito.ArgumentMatchers.any; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.when; |
| 27 | + |
| 28 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | +import java.net.http.HttpClient; |
| 30 | +import java.net.http.HttpResponse; |
| 31 | +import java.time.Clock; |
| 32 | +import java.time.Instant; |
| 33 | +import java.time.LocalDate; |
| 34 | +import java.time.ZoneId; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +class DateInfoToolTest { |
| 38 | + |
| 39 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 40 | + private final Clock fixedClock = |
| 41 | + Clock.fixed(Instant.parse("2026-08-12T00:00:00Z"), ZoneId.of("Asia/Shanghai")); |
| 42 | + |
| 43 | + // --- Date diff --- |
| 44 | + |
| 45 | + @Test |
| 46 | + void dateDiff_positive() { |
| 47 | + DateInfoTool tool = new DateInfoTool(objectMapper, fixedClock, mock(HttpClient.class)); |
| 48 | + var diff = tool.resolveDateDiff(LocalDate.of(2026, 8, 12), LocalDate.of(2026, 8, 15)); |
| 49 | + assertEquals(3, diff.calendarDays()); |
| 50 | + assertEquals(3, diff.absoluteCalendarDays()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void dateDiff_negativeWhenEndBeforeStart() { |
| 55 | + DateInfoTool tool = new DateInfoTool(objectMapper, fixedClock, mock(HttpClient.class)); |
| 56 | + var diff = tool.resolveDateDiff(LocalDate.of(2026, 8, 15), LocalDate.of(2026, 8, 12)); |
| 57 | + assertEquals(-3, diff.calendarDays()); |
| 58 | + assertEquals(3, diff.absoluteCalendarDays()); |
| 59 | + } |
| 60 | + |
| 61 | + // --- Day classification: holiday API available --- |
| 62 | + |
| 63 | + @Test |
| 64 | + void publicHoliday() { |
| 65 | + var tool = |
| 66 | + toolWithHolidayApi("{\"code\":0,\"holiday\":{\"holiday\":true,\"name\":\"国庆节\"}}"); |
| 67 | + var info = tool.resolve("2026-10-01", null, "Asia/Shanghai"); |
| 68 | + assertEquals("2026-10-01", info.date()); |
| 69 | + assertEquals("PUBLIC_HOLIDAY", info.dayType()); |
| 70 | + assertEquals("国庆节", info.holidayName()); |
| 71 | + assertTrue(info.holidayDataAvailable()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void adjustedWorkday() { |
| 76 | + var tool = |
| 77 | + toolWithHolidayApi("{\"code\":0,\"holiday\":{\"holiday\":false,\"name\":\"调休\"}}"); |
| 78 | + var info = tool.resolve("2026-10-10", null, "Asia/Shanghai"); |
| 79 | + assertEquals("ADJUSTED_WORKDAY", info.dayType()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void regularWorkday() { |
| 84 | + var tool = |
| 85 | + toolWithHolidayApi( |
| 86 | + "{\"code\":0,\"holiday\":null}"); // API returns null holiday for regular |
| 87 | + // days |
| 88 | + var info = tool.resolve("2026-08-12", null, "Asia/Shanghai"); |
| 89 | + assertEquals("WORKDAY", info.dayType()); |
| 90 | + assertNull(info.holidayName()); |
| 91 | + assertTrue(info.holidayDataAvailable()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void weekend() { |
| 96 | + var tool = toolWithHolidayApi("{\"code\":0,\"holiday\":null}"); |
| 97 | + var info = tool.resolve("2026-08-15", null, "Asia/Shanghai"); // Saturday |
| 98 | + assertEquals("WEEKEND", info.dayType()); |
| 99 | + } |
| 100 | + |
| 101 | + // --- Holiday API unavailable → graceful degradation --- |
| 102 | + |
| 103 | + @Test |
| 104 | + @SuppressWarnings("unchecked") |
| 105 | + void holidayApiUnavailable_fallsBackToWeekdayClassification() throws Exception { |
| 106 | + HttpClient httpClient = mock(HttpClient.class); |
| 107 | + HttpResponse<String> mockResponse = mock(HttpResponse.class); |
| 108 | + when(httpClient.send(any(), any(HttpResponse.BodyHandler.class))).thenReturn(mockResponse); |
| 109 | + when(mockResponse.statusCode()).thenReturn(503); |
| 110 | + |
| 111 | + var tool = new DateInfoTool(objectMapper, fixedClock, httpClient); |
| 112 | + var info = tool.resolve("2026-08-12", null, "Asia/Shanghai"); |
| 113 | + |
| 114 | + assertFalse(info.holidayDataAvailable()); |
| 115 | + assertEquals("WORKDAY", info.dayType()); |
| 116 | + assertNull(info.holidayName()); |
| 117 | + } |
| 118 | + |
| 119 | + // --- Resolve defaults to today when start_date omitted --- |
| 120 | + |
| 121 | + @Test |
| 122 | + void omittedStartDate_defaultsToToday() { |
| 123 | + var tool = toolWithHolidayApi("{\"code\":0,\"holiday\":null}"); |
| 124 | + var info = tool.resolve(null, null, "Asia/Shanghai"); |
| 125 | + assertEquals("2026-08-12", info.date()); // fixedClock is 2026-08-12 |
| 126 | + } |
| 127 | + |
| 128 | + // --- Date diff integrated --- |
| 129 | + |
| 130 | + @Test |
| 131 | + void withEndDate_includesDiff() { |
| 132 | + var tool = toolWithHolidayApi("{\"code\":0,\"holiday\":null}"); |
| 133 | + var info = tool.resolve("2026-08-12", "2026-08-15", "Asia/Shanghai"); |
| 134 | + assertEquals(3, info.dateDiff().calendarDays()); |
| 135 | + } |
| 136 | + |
| 137 | + // --- helper --- |
| 138 | + |
| 139 | + @SuppressWarnings("unchecked") |
| 140 | + private DateInfoTool toolWithHolidayApi(String jsonBody) { |
| 141 | + try { |
| 142 | + HttpClient httpClient = mock(HttpClient.class); |
| 143 | + HttpResponse<String> mockResponse = mock(HttpResponse.class); |
| 144 | + when(httpClient.send(any(), any(HttpResponse.BodyHandler.class))) |
| 145 | + .thenReturn(mockResponse); |
| 146 | + when(mockResponse.statusCode()).thenReturn(200); |
| 147 | + when(mockResponse.body()).thenReturn(jsonBody); |
| 148 | + return new DateInfoTool(objectMapper, fixedClock, httpClient); |
| 149 | + } catch (Exception e) { |
| 150 | + throw new RuntimeException(e); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments