Files
magpie/internal/provider/grouprule_time_test.go
T
yetone 6c3f113cee routing: a rule's hours over give the conversation back; looser times (MinglunZhu on X)
MinglunZhu asked twice for a provider by time of day and said the time
setting in Routing rules is awkward. Three causes:

- A conversation a night rule sent to its member stayed there after the
  hours: no rule matched the new turn, so Auto affinity kept it on the
  night's member by its cache (Smart and every routing but In order).
  ruleFor now sets RuleHit.Ended when the last turn's rule had hours
  that are over, and affine lets the conversation go (Affinity.Why
  "hours"); the trace says so in en, zh, zh-TW, ja, de. Tool rounds of
  a turn begun inside the hours stay put; Session, Strict and In turn
  keep their own way.
- The time boxes took only ASCII "HH:MM", while their numeric keypad
  on a phone has no colon and a Chinese IME types ":". clockOf (GUI)
  and clockMinutes (CLI, saved files) now read "22", "2200", "930",
  "22:00", "22.00", "22点30" and full-width digits; "9:5" is still
  no time. time=22~08 takes ~ and ~ for the dash.
- A new rule starts with an intent box; hours took an × and a +. Adding
  another condition now drops the intent box while it is empty.

A time rule in a nested group doesn't give its conversation back yet:
#5977.

Run (mote): go test ./internal/provider (ok); go test -run
'TestRuleHoursOver|TestRuleTime|TestRule|Affinity|StandIn|Session|TestParse|Clock'
./internal/gateway (ok); go vet darwin, GOOS=linux -tags nogui,
GOOS=windows for gateway and provider; gofmt. GUI, chromium and webkit:
routing-time (8/8 after the new trace test's fix), and 28 rule/affinity/
i18n files (routing-rule-*, routing-time*, routing-intent-entry,
routing-folder, routing-pause*, routing-smart-intent, routing-save*,
routing-rules-line, routing-affinity-*, routing-behind-why,
routing-kept-again, routing-session-folder, i18n-keys, i18n-en-no-cjk):
477/481, the 4 reds the new trace test's first wording, fixed and rerun.

Broken by hand: affine's "hours" case off -> TestRuleHoursOverGiveThe
ConversationBack red (Smart stays on kb by cache); origin's
grouprule.go/ruleparse.go -> TestRuleTimeTypedLoosely red; client.js's
"hours" case and the intent give-way off -> routing-time 0/4 in
chromium; the old clockOf -> routing-time red on "9:00".

Not tried: the real app with a real night/day pair of providers; the
full gateway package and full GUI suite (the full Go suite runs after
the tag); Windows run of the touched packages (vet only).
2026-11-10 22:57:20 +08:00

177 lines
7.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package provider
import (
"encoding/json"
"strings"
"testing"
"time"
)
// at is a local time on the week of Monday 2026-09-28.
func at(day, hour, min int) time.Time {
return time.Date(2026, 9, 27+day, hour, min, 0, 0, time.Local) // day 0 is Sunday
}
// A rule's hours hold on the machine's clock, past midnight when they end
// before they begin, and on the day they begin on.
func TestRuleTimeWindow(t *testing.T) {
day := TimeWindow{From: "09:00", To: "18:00", Days: []string{"mon", "tue", "wed", "thu", "fri"}}
night := TimeWindow{From: "22:00", To: "08:00", Days: []string{"fri"}}
whole := TimeWindow{From: "00:00", To: "00:00", Days: []string{"sat", "sun"}}
for _, c := range []struct {
w TimeWindow
at time.Time
want bool
}{
{day, at(1, 9, 0), true}, // Monday, as it opens
{day, at(1, 17, 59), true}, // Monday, before it ends
{day, at(1, 18, 0), false}, // as it ends
{day, at(1, 8, 59), false}, // before
{day, at(6, 12, 0), false}, // Saturday
{night, at(5, 23, 0), true}, // Friday night
{night, at(6, 3, 0), true}, // Saturday's early hours: Friday's window
{night, at(6, 8, 0), false}, // as it ends
{night, at(5, 3, 0), false}, // Friday's early hours: Thursday's window
{night, at(6, 23, 0), false},
{whole, at(0, 0, 0), true},
{whole, at(6, 23, 59), true},
{whole, at(1, 12, 0), false},
{TimeWindow{From: "22:00", To: "08:00"}, at(3, 7, 0), true},
{TimeWindow{From: "22:00", To: "08:00"}, at(3, 12, 0), false},
{day, time.Time{}, false}, // no time is in no hours
} {
if got := c.w.Holds(c.at); got != c.want {
t.Errorf("%s at %s: %v, want %v", c.w.Text(), c.at.Format("Mon 15:04"), got, c.want)
}
}
}
func TestRuleTimeCleanAndConditions(t *testing.T) {
rules, err := cleanRules([]Rule{
{Use: "a/x", Time: &TimeWindow{From: "9:00", To: "18:00", Days: []string{"Friday", "mon", "tue", "wed", "thu", "mon"}}},
{Use: "a/x", Time: &TimeWindow{From: "22:00", To: "08:00"}},
{Use: "a/x", Time: &TimeWindow{From: "00:00", To: "00:00", Days: []string{"sun", "sat"}}},
{Use: "a/x", Time: &TimeWindow{From: "08:00", To: "12:00", Days: []string{"mon", "tue", "wed", "thu", "fri", "sat", "sun"}}},
{Use: "a/x", Tokens: 100, Time: &TimeWindow{From: "08:00", To: "12:00", Days: []string{"mon", "wed", "thu", "fri"}}},
}, []string{"a/x"})
if err != nil {
t.Fatal(err)
}
want := []string{"time 09:00–18:00 Mon–Fri", "time 22:00–08:00", "time all day Sat,Sun", "time 08:00–12:00", "tokens ≥ 100 · time 08:00–12:00 Mon,Wed–Fri"}
for i, r := range rules {
if got := strings.Join(r.Conditions(), " · "); got != want[i] {
t.Errorf("rule %d: %q, want %q", i+1, got, want[i])
}
}
if d := rules[0].Time.Days; strings.Join(d, ",") != "mon,tue,wed,thu,fri" || rules[0].Time.From != "09:00" {
t.Errorf("cleaned: %+v", rules[0].Time)
}
if rules[3].Time.Days != nil {
t.Errorf("every day is no days: %+v", rules[3].Time)
}
for _, bad := range []TimeWindow{
{From: "25:00", To: "08:00"},
{From: "09:00", To: "8:5"},
{From: "09:00", To: "8 pm"},
{From: "24", To: "08:00"},
{From: "12345", To: "08:00"},
{From: "", To: "08:00"},
{From: "09:60", To: "10:00"},
{From: "09:00", To: "18:00", Days: []string{"funday"}},
{From: "10:00", To: "10:00"}, // the whole day, every day: no condition
} {
if _, err := cleanRules([]Rule{{Use: "a/x", Time: &bad}}, []string{"a/x"}); err == nil {
t.Errorf("%+v: no error", bad)
}
}
}
// Hours as a phone's number pad or a Chinese keyboard types them are the
// same hours (MinglunZhu on X: setting the time in a rule was awkward).
func TestRuleTimeTypedLoosely(t *testing.T) {
for typed, want := range map[string]string{
"22": "22:00", "8": "08:00", "2200": "22:00", "930": "09:30", "22:00": "22:00",
"22.30": "22:30", "22:30": "22:30", "22点": "22:00", "22点30": "22:30", "8点30分": "08:30",
"22h": "22:00", "9:00": "09:00", " 07:05 ": "07:05", "22:": "22:00",
} {
w := TimeWindow{From: typed, To: "12:00"}
if err := w.clean(); err != nil || w.From != want {
t.Errorf("%q: %q %v, want %q", typed, w.From, err, want)
}
}
if w, err := ParseTimeWindow("9-18"); err != nil || w.From != "09:00" || w.To != "18:00" {
t.Errorf("time=9-18: %+v %v", w, err)
}
w, err := ParseTimeWindow("22~8")
if err != nil || w.From != "22:00" || w.To != "08:00" {
t.Errorf("time=22~8: %+v %v", w, err)
}
}
// A rule with hours matches a request inside them only; one without a
// time (an older magpie's) is in none.
func TestRuleTimeMatches(t *testing.T) {
r := Rule{Use: "a/x", Time: &TimeWindow{From: "09:00", To: "18:00", Days: []string{"mon", "tue", "wed", "thu", "fri"}}}
if !r.Matches(RuleRequest{At: at(2, 10, 0)}) {
t.Error("inside the hours: no match")
}
if r.Matches(RuleRequest{At: at(2, 19, 0)}) || r.Matches(RuleRequest{At: at(0, 10, 0)}) || r.Matches(RuleRequest{}) {
t.Error("outside the hours: a match")
}
if i := MatchRule([]Rule{r, {Use: "a/y", Tokens: 10}}, RuleRequest{Tokens: 20, At: at(2, 20, 0)}); i != 1 {
t.Errorf("after hours: rule %d", i)
}
}
// Stored with omitempty: a rule without hours is written as before, and a
// window round-trips.
func TestRuleTimeJSON(t *testing.T) {
b, _ := json.Marshal(Rule{Use: "a/x", Tokens: 5})
if strings.Contains(string(b), "time") {
t.Errorf("no hours written: %s", b)
}
b, _ = json.Marshal(Rule{Use: "a/x", Time: &TimeWindow{From: "22:00", To: "08:00"}})
if string(b) != `{"use":"a/x","time":{"from":"22:00","to":"08:00"}}` {
t.Errorf("%s", b)
}
var r Rule
if err := json.Unmarshal(b, &r); err != nil || r.Time == nil || r.Time.From != "22:00" {
t.Errorf("%v %+v", err, r)
}
}
// Typed: time=… days=… — read back the same.
func TestParseRuleTime(t *testing.T) {
g := Group{ID: "g", Members: []string{"a/x", "b/y"}}
for _, c := range []struct{ line, want, cond string }{
{"use=b/y time=09:00-18:00 days=mon-fri", "use=b/y time=09:00-18:00 days=mon-fri", "time 09:00–18:00 Mon–Fri"},
{"use=b/y days=sat,sun", "use=b/y days=sat,sun", "time all day Sat,Sun"},
{"use=b/y days=fri-mon time=22:00–08:00", "use=b/y time=22:00-08:00 days=mon,fri-sun", "time 22:00–08:00 Mon,Fri–Sun"},
{"use=b/y time=22:00-08:00", "use=b/y time=22:00-08:00", "time 22:00–08:00"},
} {
r, _, _, err := ParseRule(g, RuleWords(c.line))
if err != nil {
t.Fatalf("%s: %v", c.line, err)
}
rs, err := cleanRules([]Rule{r}, g.Members)
if err != nil {
t.Fatalf("%s: %v", c.line, err)
}
if got := rs[0].Line(); got != c.want {
t.Errorf("%s: line %q, want %q", c.line, got, c.want)
}
if got := strings.Join(rs[0].Conditions(), " · "); got != c.cond {
t.Errorf("%s: %q, want %q", c.line, got, c.cond)
}
again, _, _, err := ParseRule(g, RuleWords(rs[0].Line()))
if err != nil || again.Time == nil || again.Time.From != rs[0].Time.From || again.Time.To != rs[0].Time.To {
t.Errorf("%s: read back %v %+v", c.line, err, again.Time)
}
}
for _, bad := range []string{"use=b/y time=9:5-18", "use=b/y time=09:00", "use=b/y days=someday", "use=b/y days="} {
if _, _, _, err := ParseRule(g, RuleWords(bad)); err == nil {
t.Errorf("%s: no error", bad)
}
}
}