SPB Git

Repository Public

recur_test.go 28.7 KB 678 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/internal/maintenance/recur_test.go9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142package maintenance4344import (45	"strings"46	"testing"47	"time"4849	"github.com/google/uuid"50)5152// z parses a UTC instant "2006-01-02T15:04:05Z".53func z(s string) time.Time {54	t, err := time.Parse(time.RFC3339, s)55	if err != nil {56		panic(err)57	}58	return t.UTC()59}6061// win builds a valid window from UTC instants of its first occurrence.62func win(zone, rec, start, end string) Window {63	return Window{64		ID:         uuid.New(),65		Title:      "test",66		Category:   CategoryPlanned,67		StartsAt:   z(start),68		EndsAt:     z(end),69		Timezone:   zone,70		Recurrence: rec,71		Status:     StatusScheduled,72	}73}7475func until(w Window, s string) Window {76	u := z(s)77	w.RecurrenceUntil = &u78	return w79}8081func fmtOcc(occ []Occurrence) string {82	var sb strings.Builder83	for _, o := range occ {84		sb.WriteString(o.Start.Format(time.RFC3339) + " → " + o.End.Format(time.RFC3339) + "\n")85	}86	return sb.String()87}8889// pairs builds expected occurrences from start,end,start,end… strings.90func pairs(s ...string) []Occurrence {91	var out []Occurrence92	for i := 0; i+1 < len(s); i += 2 {93		out = append(out, Occurrence{z(s[i]), z(s[i+1])})94	}95	return out96}9798func sameOcc(a, b []Occurrence) bool {99	if len(a) != len(b) {100		return false101	}102	for i := range a {103		if !a[i].Start.Equal(b[i].Start) || !a[i].End.Equal(b[i].End) {104			return false105		}106	}107	return true108}109110func TestExpandExactInstants(t *testing.T) {111	const tor = "America/Toronto"112	// 01:00–01:30 local, every day since 2026-03-06 (EST, 06:00Z).113	daily := win(tor, RecurrenceDaily, "2026-03-06T06:00:00Z", "2026-03-06T06:30:00Z")114	// 01:00–01:30 local, every Sunday since 2026-03-01.115	weekly := win(tor, RecurrenceWeekly, "2026-03-01T06:00:00Z", "2026-03-01T06:30:00Z")116	// 01:00–05:00 local: spans the 02:00 transition.117	span := win(tor, RecurrenceDaily, "2026-03-06T06:00:00Z", "2026-03-06T10:00:00Z")118	// 02:30–03:00 local: inside the spring-forward gap on 2026-03-08.119	gap := win(tor, RecurrenceDaily, "2026-03-06T07:30:00Z", "2026-03-06T08:00:00Z")120	// 01:30–02:00 local: ambiguous start on 2026-11-01.121	amb := win(tor, RecurrenceDaily, "2026-10-30T05:30:00Z", "2026-10-30T06:00:00Z")122	// Monthly on the 31st, 01:00–02:00 local.123	m31 := win(tor, RecurrenceMonthly, "2026-01-31T06:00:00Z", "2026-01-31T07:00:00Z")124	m30 := win("UTC", RecurrenceMonthly, "2026-01-30T12:00:00Z", "2026-01-30T13:00:00Z")125	m29 := win("UTC", RecurrenceMonthly, "2027-01-29T12:00:00Z", "2027-01-29T13:00:00Z")126	utcDaily := win("UTC", RecurrenceDaily, "2026-01-01T10:00:00Z", "2026-01-01T11:00:00Z")127	// 09:00–10:00 JST.128	tokyo := win("Asia/Tokyo", RecurrenceDaily, "2026-01-01T00:00:00Z", "2026-01-01T01:00:00Z")129	// 01:00–01:30 local Sydney, since 2026-09-01 (AEST, +10).130	sydney := win("Australia/Sydney", RecurrenceDaily, "2026-08-31T15:00:00Z", "2026-08-31T15:30:00Z")131	single := win(tor, RecurrenceNone, "2026-05-01T10:00:00Z", "2026-05-01T11:00:00Z")132133	cases := []struct {134		name     string135		w        Window136		from, to string137		want     []Occurrence138	}{139		{"daily spring forward 2026", daily, "2026-03-07T00:00:00Z", "2026-03-10T00:00:00Z", pairs(140			"2026-03-07T06:00:00Z", "2026-03-07T06:30:00Z",141			"2026-03-08T06:00:00Z", "2026-03-08T06:30:00Z", // 01:00 EST, transition at 07:00Z142			"2026-03-09T05:00:00Z", "2026-03-09T05:30:00Z", // 01:00 EDT143		)},144		{"daily fall back 2026", daily, "2026-10-31T00:00:00Z", "2026-11-03T00:00:00Z", pairs(145			"2026-10-31T05:00:00Z", "2026-10-31T05:30:00Z",146			"2026-11-01T05:00:00Z", "2026-11-01T05:30:00Z", // first 01:00 (EDT)147			"2026-11-02T06:00:00Z", "2026-11-02T06:30:00Z",148		)},149		{"daily spring forward 2027", daily, "2027-03-13T00:00:00Z", "2027-03-16T00:00:00Z", pairs(150			"2027-03-13T06:00:00Z", "2027-03-13T06:30:00Z",151			"2027-03-14T06:00:00Z", "2027-03-14T06:30:00Z",152			"2027-03-15T05:00:00Z", "2027-03-15T05:30:00Z",153		)},154		{"daily fall back 2027", daily, "2027-11-06T00:00:00Z", "2027-11-09T00:00:00Z", pairs(155			"2027-11-06T05:00:00Z", "2027-11-06T05:30:00Z",156			"2027-11-07T05:00:00Z", "2027-11-07T05:30:00Z",157			"2027-11-08T06:00:00Z", "2027-11-08T06:30:00Z",158		)},159		{"weekly spring forward 2026", weekly, "2026-03-01T00:00:00Z", "2026-03-16T00:00:00Z", pairs(160			"2026-03-01T06:00:00Z", "2026-03-01T06:30:00Z",161			"2026-03-08T06:00:00Z", "2026-03-08T06:30:00Z",162			"2026-03-15T05:00:00Z", "2026-03-15T05:30:00Z",163		)},164		{"weekly fall back 2026", weekly, "2026-10-25T00:00:00Z", "2026-11-09T00:00:00Z", pairs(165			"2026-10-25T05:00:00Z", "2026-10-25T05:30:00Z",166			"2026-11-01T05:00:00Z", "2026-11-01T05:30:00Z",167			"2026-11-08T06:00:00Z", "2026-11-08T06:30:00Z",168		)},169		{"weekly spring forward 2027", weekly, "2027-03-07T00:00:00Z", "2027-03-22T00:00:00Z", pairs(170			"2027-03-07T06:00:00Z", "2027-03-07T06:30:00Z",171			"2027-03-14T06:00:00Z", "2027-03-14T06:30:00Z",172			"2027-03-21T05:00:00Z", "2027-03-21T05:30:00Z",173		)},174		{"weekly fall back 2027", weekly, "2027-10-31T00:00:00Z", "2027-11-15T00:00:00Z", pairs(175			"2027-10-31T05:00:00Z", "2027-10-31T05:30:00Z",176			"2027-11-07T05:00:00Z", "2027-11-07T05:30:00Z",177			"2027-11-14T06:00:00Z", "2027-11-14T06:30:00Z",178		)},179		{"window spanning spring forward is 3h", span, "2026-03-07T00:00:00Z", "2026-03-10T00:00:00Z", pairs(180			"2026-03-07T06:00:00Z", "2026-03-07T10:00:00Z",181			"2026-03-08T06:00:00Z", "2026-03-08T09:00:00Z", // 01:00 EST → 05:00 EDT182			"2026-03-09T05:00:00Z", "2026-03-09T09:00:00Z",183		)},184		{"window spanning fall back is 5h", span, "2026-10-31T00:00:00Z", "2026-11-03T00:00:00Z", pairs(185			"2026-10-31T05:00:00Z", "2026-10-31T09:00:00Z",186			"2026-11-01T05:00:00Z", "2026-11-01T10:00:00Z", // 01:00 EDT → 05:00 EST187			"2026-11-02T06:00:00Z", "2026-11-02T10:00:00Z",188		)},189		{"window spanning 2027 transitions", span, "2027-03-14T00:00:00Z", "2027-03-15T00:00:00Z", pairs(190			"2027-03-14T06:00:00Z", "2027-03-14T09:00:00Z",191		)},192		{"window spanning 2027 fall back", span, "2027-11-07T00:00:00Z", "2027-11-08T00:00:00Z", pairs(193			"2027-11-07T05:00:00Z", "2027-11-07T10:00:00Z",194		)},195		// 02:30 does not exist on 2026-03-08: read with the offset before the196		// transition (EST) → 07:30Z = 03:30 EDT. The wall-clock end (03:00197		// EDT = 07:00Z) is not after it, so End = Start + 30 min.198		{"non-existent 02:30 becomes 03:30 EDT", gap, "2026-03-07T00:00:00Z", "2026-03-10T00:00:00Z", pairs(199			"2026-03-07T07:30:00Z", "2026-03-07T08:00:00Z",200			"2026-03-08T07:30:00Z", "2026-03-08T08:00:00Z",201			"2026-03-09T06:30:00Z", "2026-03-09T07:00:00Z",202		)},203		// 01:30 happens twice on 2026-11-01: the first (EDT) instance is used;204		// the end 02:00 is unambiguous (EST) so the occurrence lasts 1 h 30.205		{"ambiguous 01:30 takes the first (EDT) instance", amb, "2026-10-31T00:00:00Z", "2026-11-03T00:00:00Z", pairs(206			"2026-10-31T05:30:00Z", "2026-10-31T06:00:00Z",207			"2026-11-01T05:30:00Z", "2026-11-01T07:00:00Z",208			"2026-11-02T06:30:00Z", "2026-11-02T07:00:00Z",209		)},210		{"monthly 31st through a year", m31, "2026-01-01T00:00:00Z", "2027-03-01T00:00:00Z", pairs(211			"2026-01-31T06:00:00Z", "2026-01-31T07:00:00Z",212			"2026-02-28T06:00:00Z", "2026-02-28T07:00:00Z",213			"2026-03-31T05:00:00Z", "2026-03-31T06:00:00Z",214			"2026-04-30T05:00:00Z", "2026-04-30T06:00:00Z",215			"2026-05-31T05:00:00Z", "2026-05-31T06:00:00Z",216			"2026-06-30T05:00:00Z", "2026-06-30T06:00:00Z",217			"2026-07-31T05:00:00Z", "2026-07-31T06:00:00Z",218			"2026-08-31T05:00:00Z", "2026-08-31T06:00:00Z",219			"2026-09-30T05:00:00Z", "2026-09-30T06:00:00Z",220			"2026-10-31T05:00:00Z", "2026-10-31T06:00:00Z",221			"2026-11-30T06:00:00Z", "2026-11-30T07:00:00Z",222			"2026-12-31T06:00:00Z", "2026-12-31T07:00:00Z",223			"2027-01-31T06:00:00Z", "2027-01-31T07:00:00Z",224			"2027-02-28T06:00:00Z", "2027-02-28T07:00:00Z",225		)},226		{"monthly 31st leap February 2028", m31, "2028-01-15T00:00:00Z", "2028-04-01T00:00:00Z", pairs(227			"2028-01-31T06:00:00Z", "2028-01-31T07:00:00Z",228			"2028-02-29T06:00:00Z", "2028-02-29T07:00:00Z",229			"2028-03-31T05:00:00Z", "2028-03-31T06:00:00Z",230		)},231		{"monthly 30th", m30, "2026-01-01T00:00:00Z", "2026-05-01T00:00:00Z", pairs(232			"2026-01-30T12:00:00Z", "2026-01-30T13:00:00Z",233			"2026-02-28T12:00:00Z", "2026-02-28T13:00:00Z",234			"2026-03-30T12:00:00Z", "2026-03-30T13:00:00Z",235			"2026-04-30T12:00:00Z", "2026-04-30T13:00:00Z",236		)},237		{"monthly 29th non-leap", m29, "2027-01-01T00:00:00Z", "2027-04-01T00:00:00Z", pairs(238			"2027-01-29T12:00:00Z", "2027-01-29T13:00:00Z",239			"2027-02-28T12:00:00Z", "2027-02-28T13:00:00Z",240			"2027-03-29T12:00:00Z", "2027-03-29T13:00:00Z",241		)},242		{"monthly 29th leap", m29, "2028-02-01T00:00:00Z", "2028-04-01T00:00:00Z", pairs(243			"2028-02-29T12:00:00Z", "2028-02-29T13:00:00Z",244			"2028-03-29T12:00:00Z", "2028-03-29T13:00:00Z",245		)},246		{"until inclusive", until(utcDaily, "2026-01-05T10:00:00Z"), "2026-01-01T00:00:00Z", "2026-02-01T00:00:00Z", pairs(247			"2026-01-01T10:00:00Z", "2026-01-01T11:00:00Z",248			"2026-01-02T10:00:00Z", "2026-01-02T11:00:00Z",249			"2026-01-03T10:00:00Z", "2026-01-03T11:00:00Z",250			"2026-01-04T10:00:00Z", "2026-01-04T11:00:00Z",251			"2026-01-05T10:00:00Z", "2026-01-05T11:00:00Z",252		)},253		{"until one second short", until(utcDaily, "2026-01-05T09:59:59Z"), "2026-01-03T00:00:00Z", "2026-02-01T00:00:00Z", pairs(254			"2026-01-03T10:00:00Z", "2026-01-03T11:00:00Z",255			"2026-01-04T10:00:00Z", "2026-01-04T11:00:00Z",256		)},257		{"edge: End == from excluded", utcDaily, "2026-01-02T11:00:00Z", "2026-01-03T10:00:00Z", nil},258		{"edge: Start == to excluded", utcDaily, "2026-01-02T09:00:00Z", "2026-01-02T10:00:00Z", nil},259		{"edge: one second inside the end", utcDaily, "2026-01-02T10:59:59Z", "2026-01-02T11:00:00Z", pairs(260			"2026-01-02T10:00:00Z", "2026-01-02T11:00:00Z",261		)},262		{"edge: one second inside the start", utcDaily, "2026-01-02T09:00:00Z", "2026-01-02T10:00:01Z", pairs(263			"2026-01-02T10:00:00Z", "2026-01-02T11:00:00Z",264		)},265		{"edge: empty range", utcDaily, "2026-01-02T10:30:00Z", "2026-01-02T10:30:00Z", nil},266		{"none: single occurrence", single, "2026-01-01T00:00:00Z", "2027-01-01T00:00:00Z", pairs(267			"2026-05-01T10:00:00Z", "2026-05-01T11:00:00Z",268		)},269		{"none: End == from excluded", single, "2026-05-01T11:00:00Z", "2027-01-01T00:00:00Z", nil},270		{"none: Start == to excluded", single, "2026-01-01T00:00:00Z", "2026-05-01T10:00:00Z", nil},271		{"before the series", utcDaily, "2025-01-01T00:00:00Z", "2025-12-31T00:00:00Z", nil},272		{"tokyo has no DST", tokyo, "2026-06-30T12:00:00Z", "2026-07-02T12:00:00Z", pairs(273			"2026-07-01T00:00:00Z", "2026-07-01T01:00:00Z",274			"2026-07-02T00:00:00Z", "2026-07-02T01:00:00Z",275		)},276		{"sydney DST start (October)", sydney, "2026-10-02T00:00:00Z", "2026-10-05T00:00:00Z", pairs(277			"2026-10-02T15:00:00Z", "2026-10-02T15:30:00Z", // Oct 3 01:00 AEST278			"2026-10-03T15:00:00Z", "2026-10-03T15:30:00Z", // Oct 4 01:00 AEST279			"2026-10-04T14:00:00Z", "2026-10-04T14:30:00Z", // Oct 5 01:00 AEDT280		)},281		{"sydney DST end (April)", sydney, "2027-04-02T00:00:00Z", "2027-04-05T00:00:00Z", pairs(282			"2027-04-02T14:00:00Z", "2027-04-02T14:30:00Z", // Apr 3 01:00 AEDT283			"2027-04-03T14:00:00Z", "2027-04-03T14:30:00Z", // Apr 4 01:00 AEDT284			"2027-04-04T15:00:00Z", "2027-04-04T15:30:00Z", // Apr 5 01:00 AEST285		)},286	}287	for _, c := range cases {288		t.Run(c.name, func(t *testing.T) {289			if err := Validate(c.w); err != nil {290				t.Fatalf("Validate: %v", err)291			}292			got, trunc, err := Expand(c.w, z(c.from), z(c.to), 0)293			if err != nil {294				t.Fatalf("Expand: %v", err)295			}296			if trunc {297				t.Fatalf("unexpected truncation")298			}299			if !sameOcc(got, c.want) {300				t.Fatalf("got:\n%swant:\n%s", fmtOcc(got), fmtOcc(c.want))301			}302			for _, o := range got {303				if o.Start.Location() != time.UTC || o.End.Location() != time.UTC {304					t.Fatalf("occurrence not in UTC: %v", o)305				}306			}307		})308	}309}310311func TestResolveWall(t *testing.T) {312	tor, err := time.LoadLocation("America/Toronto")313	if err != nil {314		t.Fatal(err)315	}316	cases := []struct {317		name       string318		d, h, mi   int319		m          time.Month320		want, wall string321	}{322		{"gap 02:30 → 03:30 EDT", 8, 2, 30, time.March, "2026-03-08T07:30:00Z", "03:30 EDT"},323		{"gap 02:00 → 03:00 EDT", 8, 2, 0, time.March, "2026-03-08T07:00:00Z", "03:00 EDT"},324		{"just before gap", 8, 1, 59, time.March, "2026-03-08T06:59:00Z", "01:59 EST"},325		{"just after gap", 8, 3, 0, time.March, "2026-03-08T07:00:00Z", "03:00 EDT"},326		{"ambiguous 01:30 → first (EDT)", 1, 1, 30, time.November, "2026-11-01T05:30:00Z", "01:30 EDT"},327		{"ambiguous 01:00 → first (EDT)", 1, 1, 0, time.November, "2026-11-01T05:00:00Z", "01:00 EDT"},328		{"after ambiguity 02:00 EST", 1, 2, 0, time.November, "2026-11-01T07:00:00Z", "02:00 EST"},329		{"before ambiguity 00:59 EDT", 1, 0, 59, time.November, "2026-11-01T04:59:00Z", "00:59 EDT"},330		{"day overflow Nov 31 = Dec 1", 31, 12, 0, time.November, "2026-12-01T17:00:00Z", "12:00 EST"},331	}332	for _, c := range cases {333		t.Run(c.name, func(t *testing.T) {334			got := resolveWall(tor, 2026, c.m, c.d, c.h, c.mi, 0, 0)335			if !got.Equal(z(c.want)) {336				t.Fatalf("got %s want %s", got.UTC().Format(time.RFC3339), c.want)337			}338			if w := got.In(tor).Format("15:04 MST"); w != c.wall {339				t.Fatalf("wall clock %q want %q", w, c.wall)340			}341			// Deterministic.342			if again := resolveWall(tor, 2026, c.m, c.d, c.h, c.mi, 0, 0); !again.Equal(got) {343				t.Fatalf("not deterministic")344			}345		})346	}347}348349func TestExpandTruncation(t *testing.T) {350	w := win("UTC", RecurrenceDaily, "2026-01-01T10:00:00Z", "2026-01-01T11:00:00Z")351	from, to := z("2026-01-01T00:00:00Z"), z("2026-01-31T00:00:00Z")352	got, trunc, err := Expand(w, from, to, 5)353	if err != nil || len(got) != 5 || !trunc {354		t.Fatalf("max=5: len=%d trunc=%v err=%v", len(got), trunc, err)355	}356	if !got[4].Start.Equal(z("2026-01-05T10:00:00Z")) {357		t.Fatalf("5th = %v", got[4].Start)358	}359	got, trunc, err = Expand(w, from, to, 30)360	if err != nil || len(got) != 30 || trunc {361		t.Fatalf("max=30: len=%d trunc=%v err=%v", len(got), trunc, err)362	}363	// Hard cap.364	got, trunc, err = Expand(w, from, z("2040-01-01T00:00:00Z"), 1<<30)365	if err != nil || len(got) != MaxOccurrences || !trunc {366		t.Fatalf("hard cap: len=%d trunc=%v err=%v", len(got), trunc, err)367	}368	for i := 1; i < len(got); i++ {369		if !got[i].Start.After(got[i-1].Start) {370			t.Fatalf("not ordered at %d", i)371		}372	}373}374375func TestExpandErrors(t *testing.T) {376	bad := win("Mars/Olympus", RecurrenceDaily, "2026-01-01T10:00:00Z", "2026-01-01T11:00:00Z")377	if _, _, err := Expand(bad, z("2026-01-01T00:00:00Z"), z("2026-02-01T00:00:00Z"), 10); err == nil {378		t.Fatal("expected error for bad zone")379	}380	bad = win("UTC", "yearly", "2026-01-01T10:00:00Z", "2026-01-01T11:00:00Z")381	if _, _, err := Expand(bad, z("2026-01-01T00:00:00Z"), z("2026-02-01T00:00:00Z"), 10); err == nil {382		t.Fatal("expected error for bad recurrence")383	}384	bad = win("UTC", RecurrenceDaily, "2026-01-01T10:00:00Z", "2026-01-01T10:00:00Z")385	if _, _, err := Expand(bad, z("2026-01-01T00:00:00Z"), z("2026-02-01T00:00:00Z"), 10); err == nil {386		t.Fatal("expected error for end<=start")387	}388	if _, ok := OccurrenceAt(bad, z("2026-01-01T10:00:00Z")); ok {389		t.Fatal("OccurrenceAt on invalid window")390	}391	if _, ok := Next(bad, z("2026-01-01T10:00:00Z")); ok {392		t.Fatal("Next on invalid window")393	}394}395396func TestFarFutureEfficiency(t *testing.T) {397	// 01:00–01:30 Toronto, daily since 2020-01-01.398	w := win("America/Toronto", RecurrenceDaily, "2020-01-01T06:00:00Z", "2020-01-01T06:30:00Z")399	if _, err := loadLocation(w.Timezone); err != nil { // warm the zone cache400		t.Fatal(err)401	}402	begin := time.Now()403	got, trunc, err := Expand(w, z("2035-07-01T00:00:00Z"), z("2035-07-04T00:00:00Z"), 0)404	nxt, okN := Next(w, z("2035-12-31T23:00:00Z"))405	at, okA := OccurrenceAt(w, z("2035-01-15T06:10:00Z"))406	elapsed := time.Since(begin)407	if err != nil || trunc {408		t.Fatalf("err=%v trunc=%v", err, trunc)409	}410	want := pairs(411		"2035-07-01T05:00:00Z", "2035-07-01T05:30:00Z",412		"2035-07-02T05:00:00Z", "2035-07-02T05:30:00Z",413		"2035-07-03T05:00:00Z", "2035-07-03T05:30:00Z",414	)415	if !sameOcc(got, want) {416		t.Fatalf("got:\n%swant:\n%s", fmtOcc(got), fmtOcc(want))417	}418	if !okN || !nxt.Start.Equal(z("2036-01-01T06:00:00Z")) {419		t.Fatalf("Next = %v %v", nxt, okN)420	}421	if !okA || !at.Start.Equal(z("2035-01-15T06:00:00Z")) {422		t.Fatalf("OccurrenceAt = %v %v", at, okA)423	}424	if elapsed > 50*time.Millisecond {425		t.Fatalf("too slow: %s", elapsed)426	}427428	// Monthly and weekly far-future jumps are exact too.429	m := win("America/Toronto", RecurrenceMonthly, "2020-01-31T06:00:00Z", "2020-01-31T07:00:00Z")430	got, _, err = Expand(m, z("2036-02-01T00:00:00Z"), z("2036-04-01T00:00:00Z"), 0)431	want = pairs(432		"2036-02-29T06:00:00Z", "2036-02-29T07:00:00Z",433		"2036-03-31T05:00:00Z", "2036-03-31T06:00:00Z",434	)435	if err != nil || !sameOcc(got, want) {436		t.Fatalf("monthly err=%v got:\n%swant:\n%s", err, fmtOcc(got), fmtOcc(want))437	}438	// 2020-01-01 is a Wednesday; 2035-07-04 is a Wednesday.439	wk := win("America/Toronto", RecurrenceWeekly, "2020-01-01T06:00:00Z", "2020-01-01T06:30:00Z")440	got, _, err = Expand(wk, z("2035-07-01T00:00:00Z"), z("2035-07-12T00:00:00Z"), 0)441	want = pairs(442		"2035-07-04T05:00:00Z", "2035-07-04T05:30:00Z",443		"2035-07-11T05:00:00Z", "2035-07-11T05:30:00Z",444	)445	if err != nil || !sameOcc(got, want) {446		t.Fatalf("weekly err=%v got:\n%swant:\n%s", err, fmtOcc(got), fmtOcc(want))447	}448}449450// TestJumpMatchesBruteForce compares the arithmetic jump with a plain451// iteration from occurrence 0.452func TestJumpMatchesBruteForce(t *testing.T) {453	ws := []Window{454		win("America/Toronto", RecurrenceDaily, "2024-01-10T04:30:00Z", "2024-01-10T09:00:00Z"),455		win("America/Toronto", RecurrenceWeekly, "2024-01-10T04:30:00Z", "2024-01-15T09:00:00Z"),456		win("Australia/Sydney", RecurrenceMonthly, "2024-01-30T13:30:00Z", "2024-02-12T09:00:00Z"),457		win("Europe/Paris", RecurrenceMonthly, "2024-01-31T00:30:00Z", "2024-01-31T03:00:00Z"),458	}459	for wi, w := range ws {460		if err := Validate(w); err != nil {461			t.Fatalf("window %d: %v", wi, err)462		}463		s, err := newSeries(w)464		if err != nil {465			t.Fatal(err)466		}467		var all []Occurrence468		for n := 0; n < 1500; n++ {469			all = append(all, s.at(n))470		}471		for i := 1; i < len(all); i++ {472			if !all[i].Start.After(all[i-1].Start) || all[i].End.Before(all[i-1].End) {473				t.Fatalf("window %d: series not monotonic at %d", wi, i)474			}475		}476		from := z("2024-01-01T00:00:00Z")477		for k := 0; k < 300; k++ {478			f := from.Add(time.Duration(k) * 53 * time.Hour)479			to := f.Add(time.Duration(k%9+1) * 31 * time.Hour)480			var want []Occurrence481			for _, o := range all {482				if o.End.After(f) && o.Start.Before(to) {483					want = append(want, o)484				}485			}486			got, _, err := Expand(w, f, to, 0)487			if err != nil || !sameOcc(got, want) {488				t.Fatalf("window %d range %s..%s err=%v got:\n%swant:\n%s", wi, f, to, err, fmtOcc(got), fmtOcc(want))489			}490		}491	}492}493494func TestOccurrenceLookupsConsistency(t *testing.T) {495	ws := []Window{496		win("America/Toronto", RecurrenceDaily, "2026-01-01T06:00:00Z", "2026-01-01T10:00:00Z"),497		win("America/Toronto", RecurrenceDaily, "2026-01-01T07:30:00Z", "2026-01-01T08:00:00Z"), // 02:30 local498		win("America/Toronto", RecurrenceDaily, "2026-01-01T06:30:00Z", "2026-01-01T07:00:00Z"), // 01:30 local499		win("America/Toronto", RecurrenceWeekly, "2026-01-04T06:00:00Z", "2026-01-06T10:00:00Z"),500		win("America/Toronto", RecurrenceMonthly, "2026-01-31T06:00:00Z", "2026-02-03T10:00:00Z"),501		win("Australia/Sydney", RecurrenceDaily, "2026-01-01T15:30:00Z", "2026-01-01T16:15:00Z"), // 02:30 local502		win("Asia/Tokyo", RecurrenceMonthly, "2026-01-29T15:30:00Z", "2026-01-29T16:15:00Z"),503		win("UTC", RecurrenceNone, "2026-06-01T15:30:00Z", "2026-06-01T16:15:00Z"),504		until(win("Europe/Paris", RecurrenceWeekly, "2026-01-01T01:30:00Z", "2026-01-01T02:15:00Z"), "2027-03-01T00:00:00Z"),505	}506	from, to := z("2026-01-01T00:00:00Z"), z("2028-03-01T00:00:00Z")507	for wi, w := range ws {508		if err := Validate(w); err != nil {509			t.Fatalf("window %d: %v", wi, err)510		}511		occ, trunc, err := Expand(w, from, to, 0)512		if err != nil || trunc || len(occ) == 0 {513			t.Fatalf("window %d: len=%d trunc=%v err=%v", wi, len(occ), trunc, err)514		}515		if !occ[0].Start.Equal(w.StartsAt) || !occ[0].End.Equal(w.EndsAt) {516			t.Fatalf("window %d: occurrence 0 is not [StartsAt, EndsAt)", wi)517		}518		for i, o := range occ {519			if !o.End.After(o.Start) {520				t.Fatalf("window %d occ %d: End <= Start", wi, i)521			}522			if got, ok := OccurrenceStarting(w, o.Start); !ok || got != o {523				t.Fatalf("window %d occ %d: OccurrenceStarting = %v %v, want %v", wi, i, got, ok, o)524			}525			if got, ok := OccurrenceAt(w, o.Start); !ok || got != o {526				t.Fatalf("window %d occ %d: OccurrenceAt(start) = %v %v, want %v", wi, i, got, ok, o)527			}528			if got, ok := OccurrenceAt(w, o.End.Add(-time.Nanosecond)); !ok || got != o {529				t.Fatalf("window %d occ %d: OccurrenceAt(end-1ns) = %v %v, want %v", wi, i, got, ok, o)530			}531			if _, ok := OccurrenceAt(w, o.Start.Add(-time.Nanosecond)); ok {532				t.Fatalf("window %d occ %d: OccurrenceAt(start-1ns) found something", wi, i)533			}534			if _, ok := OccurrenceAt(w, o.End); ok {535				t.Fatalf("window %d occ %d: OccurrenceAt(end) found something", wi, i)536			}537			if _, ok := OccurrenceStarting(w, o.Start.Add(time.Second)); ok {538				t.Fatalf("window %d occ %d: OccurrenceStarting(start+1s) found something", wi, i)539			}540			if got, ok := Next(w, o.Start.Add(-time.Nanosecond)); !ok || got != o {541				t.Fatalf("window %d occ %d: Next(start-1ns) = %v %v, want %v", wi, i, got, ok, o)542			}543			if i+1 < len(occ) {544				if got, ok := Next(w, o.Start); !ok || got != occ[i+1] {545					t.Fatalf("window %d occ %d: Next(start) = %v %v, want %v", wi, i, got, ok, occ[i+1])546				}547			}548		}549	}550551	// End of series.552	single := ws[7]553	if _, ok := Next(single, single.StartsAt); ok {554		t.Fatal("Next after a single occurrence")555	}556	if o, ok := Next(single, single.StartsAt.Add(-time.Hour)); !ok || !o.Start.Equal(single.StartsAt) {557		t.Fatal("Next before a single occurrence")558	}559	bounded := ws[8]560	if _, ok := Next(bounded, z("2027-03-01T00:00:00Z")); ok {561		t.Fatal("Next after until")562	}563	if o, ok := Next(bounded, z("2020-01-01T00:00:00Z")); !ok || !o.Start.Equal(bounded.StartsAt) {564		t.Fatal("Next long before the series must be occurrence 0")565	}566	if _, ok := OccurrenceStarting(bounded, z("2027-03-04T01:30:00Z")); ok {567		t.Fatal("OccurrenceStarting after until")568	}569}570571func TestValidate(t *testing.T) {572	ok := win("America/Toronto", RecurrenceDaily, "2026-01-01T06:00:00Z", "2026-01-01T10:00:00Z")573	mod := func(f func(*Window)) Window {574		w := ok575		f(&w)576		return w577	}578	early := z("2025-12-31T00:00:00Z")579	same := ok.StartsAt580	cases := []struct {581		name    string582		w       Window583		wantErr bool584	}{585		{"valid daily", ok, false},586		{"valid until == start", mod(func(w *Window) { w.RecurrenceUntil = &same }), false},587		{"valid none 14 days", mod(func(w *Window) { w.Recurrence = RecurrenceNone; w.EndsAt = w.StartsAt.Add(MaxDuration) }), false},588		{"valid monthly 14 days", mod(func(w *Window) { w.Recurrence = RecurrenceMonthly; w.EndsAt = w.StartsAt.Add(MaxDuration) }), false},589		{"valid weekly 6 days", mod(func(w *Window) { w.Recurrence = RecurrenceWeekly; w.EndsAt = w.StartsAt.Add(6 * 24 * time.Hour) }), false},590		{"valid title 160", mod(func(w *Window) { w.Title = strings.Repeat("é", 160) }), false},591		{"empty title", mod(func(w *Window) { w.Title = "" }), true},592		{"blank title", mod(func(w *Window) { w.Title = "   " }), true},593		{"title 161", mod(func(w *Window) { w.Title = strings.Repeat("é", 161) }), true},594		{"end == start", mod(func(w *Window) { w.EndsAt = w.StartsAt }), true},595		{"end < start", mod(func(w *Window) { w.EndsAt = w.StartsAt.Add(-time.Minute) }), true},596		{"more than 14 days", mod(func(w *Window) { w.Recurrence = RecurrenceNone; w.EndsAt = w.StartsAt.Add(MaxDuration + time.Second) }), true},597		{"bad zone", mod(func(w *Window) { w.Timezone = "Mars/Olympus" }), true},598		{"empty zone", mod(func(w *Window) { w.Timezone = "" }), true},599		{"Local zone", mod(func(w *Window) { w.Timezone = "Local" }), true},600		{"daily 25h", mod(func(w *Window) { w.EndsAt = w.StartsAt.Add(25 * time.Hour) }), true},601		{"daily 24h", mod(func(w *Window) { w.EndsAt = w.StartsAt.Add(24 * time.Hour) }), true},602		{"weekly 7 days", mod(func(w *Window) { w.Recurrence = RecurrenceWeekly; w.EndsAt = w.StartsAt.Add(7 * 24 * time.Hour) }), true},603		{"unknown recurrence", mod(func(w *Window) { w.Recurrence = "yearly" }), true},604		{"empty recurrence", mod(func(w *Window) { w.Recurrence = "" }), true},605		{"unknown category", mod(func(w *Window) { w.Category = "party" }), true},606		{"until before start", mod(func(w *Window) { w.RecurrenceUntil = &early }), true},607		{"until without recurrence", mod(func(w *Window) { w.Recurrence = RecurrenceNone; w.RecurrenceUntil = &same }), true},608	}609	for _, c := range cases {610		t.Run(c.name, func(t *testing.T) {611			err := Validate(c.w)612			if (err != nil) != c.wantErr {613				t.Fatalf("err = %v, wantErr = %v", err, c.wantErr)614			}615			if err != nil && !strings.HasPrefix(err.Error(), "maintenance: ") {616				t.Fatalf("undescriptive error %q", err)617			}618		})619	}620}621622func TestOverlaps(t *testing.T) {623	const tor = "America/Toronto"624	from, to := z("2026-01-01T00:00:00Z"), z("2027-01-01T00:00:00Z")625	// Sundays 01:00–03:00 local.626	sun := win(tor, RecurrenceWeekly, "2026-03-01T06:00:00Z", "2026-03-01T08:00:00Z")627	// Wednesdays 01:00–03:00 local.628	wed := win(tor, RecurrenceWeekly, "2026-03-04T06:00:00Z", "2026-03-04T08:00:00Z")629	// Every day 02:00–04:00 local, since Tuesday 2026-03-03.630	daily := win(tor, RecurrenceDaily, "2026-03-03T07:00:00Z", "2026-03-03T09:00:00Z")631	// Every day 05:00–06:00 local.632	dailyLate := win(tor, RecurrenceDaily, "2026-03-03T10:00:00Z", "2026-03-03T11:00:00Z")633	oneA := win("UTC", RecurrenceNone, "2026-05-01T10:00:00Z", "2026-05-01T12:00:00Z")634	oneB := win("UTC", RecurrenceNone, "2026-05-01T11:00:00Z", "2026-05-01T13:00:00Z")635	oneC := win("UTC", RecurrenceNone, "2026-05-01T12:00:00Z", "2026-05-01T13:00:00Z")636	// Monthly on the 4th 01:30–02:30 local: first meets wed on Wed 2026-03-04.637	monthly := win(tor, RecurrenceMonthly, "2026-02-04T06:30:00Z", "2026-02-04T07:30:00Z")638639	cases := []struct {640		name     string641		a, b     Window642		from, to time.Time643		want     bool644		at       string645	}{646		{"weekly on different weekdays", sun, wed, from, to, false, ""},647		{"daily vs weekly", daily, wed, from, to, true, "2026-03-04T07:00:00Z"},648		{"weekly vs daily (symmetric)", wed, daily, from, to, true, "2026-03-04T07:00:00Z"},649		// On 2026-03-08 (spring forward) the Sunday window is 01:00 EST–03:00650		// EDT = 06:00Z–07:00Z and the daily one starts at the non-existent651		// 02:00 → 07:00Z: they only touch. First real overlap: 2026-03-15.652		{"daily vs sunday weekly", daily, sun, from, to, true, "2026-03-15T06:00:00Z"},653		{"same days, disjoint hours", dailyLate, wed, from, to, false, ""},654		{"daily vs daily disjoint", daily, dailyLate, from, to, false, ""},655		{"singles overlapping", oneA, oneB, from, to, true, "2026-05-01T11:00:00Z"},656		{"singles touching", oneA, oneC, from, to, false, ""},657		{"overlap outside the range", oneA, oneB, z("2026-06-01T00:00:00Z"), to, false, ""},658		{"range limits the first overlap", daily, wed, z("2026-06-01T00:00:00Z"), to, true, "2026-06-03T06:00:00Z"},659		{"monthly vs weekly", monthly, wed, from, to, true, "2026-03-04T06:30:00Z"},660		{"self", sun, sun, from, to, true, "2026-03-01T06:00:00Z"},661		{"invalid window", Window{}, sun, from, to, false, ""},662	}663	for _, c := range cases {664		t.Run(c.name, func(t *testing.T) {665			got, at := Overlaps(c.a, c.b, c.from, c.to)666			if got != c.want {667				t.Fatalf("Overlaps = %v (%v), want %v", got, at, c.want)668			}669			if c.want && !at.Equal(z(c.at)) {670				t.Fatalf("first overlap at %s, want %s", at.Format(time.RFC3339), c.at)671			}672			if !c.want && !at.IsZero() {673				t.Fatalf("non-zero time without overlap")674			}675		})676	}677}678
Files main
Menu

Theme