@@ -216,6 +216,21 @@ void findOptimalCharging4Hours() {
216216 assertThat (output ).contains ("12,5" );
217217 }
218218
219+ @ Test
220+ void chargingWindowDoesNotUseNextDay_whenNextDayUnavailable () {
221+ // Only today's 3 hours, request 2h window -> should compute within these only
222+ String mockJsonToday = """
223+ [{"SEK_per_kWh":0.20,"EUR_per_kWh":0.02,"EXR":10.0,"time_start":"2025-09-04T00:00:00+02:00","time_end":"2025-09-04T01:00:00+02:00"},
224+ {"SEK_per_kWh":0.10,"EUR_per_kWh":0.01,"EXR":10.0,"time_start":"2025-09-04T01:00:00+02:00","time_end":"2025-09-04T02:00:00+02:00"},
225+ {"SEK_per_kWh":0.15,"EUR_per_kWh":0.015,"EXR":10.0,"time_start":"2025-09-04T02:00:00+02:00","time_end":"2025-09-04T03:00:00+02:00"}]""" ;
226+ ElpriserAPI .setMockResponse (mockJsonToday );
227+ Main .main (new String []{"--zone" , "SE3" , "--date" , "2025-09-04" , "--charging" , "2h" });
228+ String output = bos .toString ();
229+ // Best 2h window should be 01-03 (0.10 + 0.15)
230+ assertThat (output ).contains ("Påbörja laddning" );
231+ assertThat (output ).contains ("01:00" );
232+ }
233+
219234 @ Test
220235 void findOptimalCharging8Hours () {
221236 // Create mock data with 12 hours to allow for 8-hour window
@@ -298,15 +313,20 @@ void handleNoDataAvailable() {
298313 }
299314
300315 @ Test
301- void handleMultipleDaysData () {
302- // Mock response with data for two days
303- String mockJson = """
316+ void handleMultipleDaysData_includesNextDayForCharging () {
317+ // This test ensures charging window can span days when next day data exists
318+ LocalDate today = LocalDate .of (2025 , 9 , 4 );
319+ LocalDate tomorrow = today .plusDays (1 );
320+
321+ String mockJsonToday = """
304322 [{"SEK_per_kWh":0.30,"EUR_per_kWh":0.03,"EXR":10.0,"time_start":"2025-09-04T22:00:00+02:00","time_end":"2025-09-04T23:00:00+02:00"},
305- {"SEK_per_kWh":0.25,"EUR_per_kWh":0.025,"EXR":10.0,"time_start":"2025-09-04T23:00:00+02:00","time_end":"2025-09-05T00:00:00+02:00"},
306- {"SEK_per_kWh":0.10,"EUR_per_kWh":0.01,"EXR":10.0,"time_start":"2025-09-05T00:00:00+02:00","time_end":"2025-09-05T01:00:00+02:00"},
323+ {"SEK_per_kWh":0.25,"EUR_per_kWh":0.025,"EXR":10.0,"time_start":"2025-09-04T23:00:00+02:00","time_end":"2025-09-05T00:00:00+02:00"}]""" ;
324+ String mockJsonTomorrow = """
325+ [{"SEK_per_kWh":0.10,"EUR_per_kWh":0.01,"EXR":10.0,"time_start":"2025-09-05T00:00:00+02:00","time_end":"2025-09-05T01:00:00+02:00"},
307326 {"SEK_per_kWh":0.15,"EUR_per_kWh":0.015,"EXR":10.0,"time_start":"2025-09-05T01:00:00+02:00","time_end":"2025-09-05T02:00:00+02:00"}]""" ;
308327
309- ElpriserAPI .setMockResponse (mockJson );
328+ ElpriserAPI .setMockResponseForDate (today , mockJsonToday );
329+ ElpriserAPI .setMockResponseForDate (tomorrow , mockJsonTomorrow );
310330
311331 Main .main (new String []{"--zone" , "SE3" , "--date" , "2025-09-04" , "--charging" , "4h" });
312332
@@ -315,6 +335,29 @@ void handleMultipleDaysData() {
315335 // Should be able to find optimal charging window across day boundary
316336 }
317337
338+ @ Test
339+ void chargingWindowSpansToNextDay_whenCheapestCrossesMidnight () {
340+ // Prices set so best 2h window is 23:00-01:00 (0.50 at 22, 0.20 at 23, 0.05 at 00, 0.40 at 01)
341+ LocalDate today = LocalDate .of (2025 , 9 , 4 );
342+ LocalDate tomorrow = today .plusDays (1 );
343+
344+ String mockJsonToday = """
345+ [{"SEK_per_kWh":0.50,"EUR_per_kWh":0.05,"EXR":10.0,"time_start":"2025-09-04T22:00:00+02:00","time_end":"2025-09-04T23:00:00+02:00"},
346+ {"SEK_per_kWh":0.20,"EUR_per_kWh":0.02,"EXR":10.0,"time_start":"2025-09-04T23:00:00+02:00","time_end":"2025-09-05T00:00:00+02:00"}]""" ;
347+ String mockJsonTomorrow = """
348+ [{"SEK_per_kWh":0.05,"EUR_per_kWh":0.005,"EXR":10.0,"time_start":"2025-09-05T00:00:00+02:00","time_end":"2025-09-05T01:00:00+02:00"},
349+ {"SEK_per_kWh":0.40,"EUR_per_kWh":0.04,"EXR":10.0,"time_start":"2025-09-05T01:00:00+02:00","time_end":"2025-09-05T02:00:00+02:00"}]""" ;
350+
351+ ElpriserAPI .setMockResponseForDate (today , mockJsonToday );
352+ ElpriserAPI .setMockResponseForDate (tomorrow , mockJsonTomorrow );
353+
354+ Main .main (new String []{"--zone" , "SE3" , "--date" , "2025-09-04" , "--charging" , "2h" });
355+ String output = bos .toString ();
356+ assertThat (output ).contains ("Påbörja laddning" );
357+ // Expect start at 23:00 (23 + 00 window is cheapest)
358+ assertThat (output ).contains ("23:00" );
359+ }
360+
318361 @ Test
319362 public void testHourlyMinMaxPrices () {
320363 List <Double > quarterHourPrices = new ArrayList <>();
0 commit comments