magpie on a cloud server in UTC read a routing group's rule hours on the
server's clock, so a Beijing user's 09:00-18:00 held at 17:00-02:00
Beijing time, with nothing saying so.
- provider.TimeWindow carries the IANA zone its hours were set in
("zone": "Asia/Shanghai", omitempty). Holds reads From, To and Days in
that zone (time.LoadLocation, cached; Go's embedded tzdata, so Windows
knows them too). A window with no zone, every one saved before this,
stays on magpie's own clock (time.Local) as before. A zone magpie
doesn't know is refused on save (clean); one in a file edited by hand
makes the window never hold, so the rule is as if absent, never always
on and never hours off. Text() adds " (Zone)"; Line() writes tz=.
- GUI: changing a rule's hours or days in the editor saves the browser's
zone (Intl resolvedOptions().timeZone); hours left alone keep theirs.
/api/groups gives magpie's clock (clock: zone, offset in seconds), and
when its offset differs from the hours' zone the Routing page tells the
zone beside them, on the group's rules and in the editor: "Asia/Shanghai
time" (zh "Asia/Shanghai 时间"), or "server time, UTC+0" (zh "服务器时间
UTC+0") for hours with no zone. Nothing is added when the clocks agree.
zh, zh-TW, ja, de.
- CLI and TUI: time=09:00-18:00@Asia/Shanghai or tz=Asia/Shanghai; with
none, this computer's zone ($TZ, time.Local, the /etc/localtime link)
when it has an IANA name, otherwise none (magpie's clock). An unknown
zone is refused with what one looks like; tz= alone needs time= or
days=. Usage text updated.
- docs/subsystems/gateway-routing.md: the zone rules.
Older magpie reading a shared or synced group ignores zone and reads the
hours on its own clock, as before.
Run in a sandbox HOME with pinned Go caches, first at base 9277c2567,
then again after the rebase onto origin/main d58053720 (vet, builds, the five packages,
routing-time-zone and i18n-keys all ok again there; then rebased onto
11907448e, which changes migrate_side.go and sessions only, and
internal/provider ok again, 139s):
- gofmt -l on the changed files: nothing. go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./...: clean. go build, GOOS=linux
go build -tags nogui, GOOS=windows go build: ok.
- go test -timeout 45m -count=1 ./internal/provider ./internal/cli
./internal/tui ./internal/gui ./internal/gateway: all ok (provider 170s,
cli 64s, tui 30s, gui 192s, gateway 513s; after the rebase 180s, 62s,
25s, 181s, 568s); logs kept whole and grepped, no FAIL.
- New tests: TestRuleTimeWindowInItsZone (window, past midnight and days
in a zone, instants in UTC), TestRuleTimeWindowWithoutZoneIsLocal,
TestRuleTimeWindowUnknownZone, TestParseRuleTimeZone, TestRuleInZoneHere,
TestGroupRuleTimeZoneCmd, TestRuleTimeWindowInItsZoneAtTurnStart
(gateway, ruleClock in UTC). Run under TZ=UTC, TZ=Asia/Shanghai and
TZ=America/Los_Angeles: ok. The zone used is Asia/Shanghai, or
America/New_York on a computer on Beijing's clock, so they are red on
any machine.
- GUI (playwright 1.62.0, chromium and webkit): routing-time-zone (new:
en, zh, zh-TW, ja, de, 420px wide, browser in Asia/Shanghai, server in
UTC and in Asia/Shanghai) 14 of 14; routing-time with the browser in
Asia/Shanghai, the typed rule saved with its zone; routing-time-went,
routing-day-window, routing-save, routing-rules-line, i18n-keys: ok;
TestCoreScriptBudget ok.
Broken by hand: Holds reading at.Local() instead of the zone made
TestRuleTimeWindowInItsZone red under TZ=UTC (Shanghai's 10:00 not held,
18:00 held) and under TZ=Asia/Shanghai (with New York), and
TestRuleTimeWindowInItsZoneAtTurnStart red (10:00 Beijing went to the
group's order). routing-time-zone.test.cjs run against origin/main's
client is red (no zone told beside the hours).
Reds not from this change, the same on an untouched origin/main 99de8e1ad
worktree: routing-pause chromium 7 of 7 (#3410); routing-time webkit "Add
a rule" outside the viewport, 1 of 3 runs on main too (#3465);
routing-rules-line webkit zh 1100 once under load, passes alone on both
(#3593, filed now).
Not tried: a real cloud server in UTC with a browser in Beijing (the GUI
test sets the browser's zone and the server's clock instead); the Windows
box; the whole GUI suite.
117 lines
4.8 KiB
Go
117 lines
4.8 KiB
Go
package gateway
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// A rule's hours are looked at as a turn begins, on the clock then: a turn
|
||
// begun inside them goes to its member and stays there for its tool
|
||
// rounds after they end; a turn begun outside them isn't the rule's (where
|
||
// the same conversation goes then is the group's Stays to say).
|
||
func TestRuleTimeWindowAtTurnStart(t *testing.T) {
|
||
now := time.Date(2026, 9, 29, 10, 0, 0, 0, time.Local) // a Tuesday
|
||
ruleClock = func() time.Time { return now }
|
||
t.Cleanup(func() { ruleClock = time.Now })
|
||
s, a, b := ruled(t, provider.Rule{Use: "b/big", Time: &provider.TimeWindow{From: "09:00", To: "18:00", Days: []string{"mon", "tue", "wed", "thu", "fri"}}})
|
||
post := func(body string, session string) (string, Route) {
|
||
t.Helper()
|
||
code, out := postAs(t, s, session, body)
|
||
if code != 200 {
|
||
t.Fatalf("%d %s", code, out)
|
||
}
|
||
return out, lastRoute(s)
|
||
}
|
||
// turn 1 begins inside the hours: the rule's member, the trace says why
|
||
out, r := post(chat("hello", nil, 0, ""), "sess")
|
||
if !strings.Contains(out, "from kb") || r.Rule == nil || r.Rule.N != 1 || r.Rule.Use != "b/big" || strings.Join(r.Rule.When, " · ") != "time 09:00–18:00 Mon–Fri" {
|
||
t.Fatalf("turn 1: %s %+v", out, r.Rule)
|
||
}
|
||
// its tool rounds after the hours end stay where it began
|
||
now = time.Date(2026, 9, 29, 18, 30, 0, 0, time.Local)
|
||
out, r = post(chat("hello", nil, 1, ""), "sess")
|
||
if !strings.Contains(out, "from kb") || !r.Rule.Held || r.Rule.N != 1 {
|
||
t.Fatalf("turn 1 tool round: %s %+v", out, r.Rule)
|
||
}
|
||
// turn 2 begins after them: no rule
|
||
if _, r = post(chat("hello", []string{"more"}, 0, ""), "sess"); r.Rule.N != 0 || r.Rule.Held {
|
||
t.Fatalf("turn 2: %+v", r.Rule)
|
||
}
|
||
// nor another conversation's: the group's order
|
||
if out, r = post(chat("hi", nil, 0, ""), "s2"); !strings.Contains(out, "from ka") || r.Rule.N != 0 {
|
||
t.Fatalf("after hours: %s %+v", out, r.Rule)
|
||
}
|
||
// a turn begun before them stays off the rule when they open mid-turn
|
||
now = time.Date(2026, 9, 30, 8, 59, 0, 0, time.Local)
|
||
if out, r = post(chat("hello", nil, 0, ""), "s3"); !strings.Contains(out, "from ka") || r.Rule.N != 0 {
|
||
t.Fatalf("before hours: %s %+v", out, r.Rule)
|
||
}
|
||
now = time.Date(2026, 9, 30, 9, 1, 0, 0, time.Local)
|
||
if out, r = post(chat("hello", nil, 1, ""), "s3"); !strings.Contains(out, "from ka") || !r.Rule.Held || r.Rule.N != 0 {
|
||
t.Fatalf("before hours, tool round: %s %+v", out, r.Rule)
|
||
}
|
||
// on a Saturday, the hours don't hold
|
||
now = time.Date(2026, 10, 3, 10, 0, 0, 0, time.Local)
|
||
if out, r = post(chat("hello", nil, 0, ""), "s4"); !strings.Contains(out, "from ka") || r.Rule.N != 0 {
|
||
t.Fatalf("saturday: %s %+v", out, r.Rule)
|
||
}
|
||
if a.n() != 4 || b.n() != 3 {
|
||
t.Fatalf("a %d b %d", a.n(), b.n())
|
||
}
|
||
}
|
||
|
||
// A rule's hours set in a zone hold at that zone's hours, on a server
|
||
// whose clock reads UTC (gaubeebangeel on X: magpie on a cloud server, its
|
||
// user in Beijing, the hours 8 off): the instants here are UTC, as such a
|
||
// server has them, and the zone is one hours away from this computer's.
|
||
func TestRuleTimeWindowInItsZoneAtTurnStart(t *testing.T) {
|
||
zone, loc := "", (*time.Location)(nil)
|
||
for _, name := range []string{"Asia/Shanghai", "America/New_York"} {
|
||
l, err := time.LoadLocation(name)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
_, a := time.Date(2026, 9, 29, 12, 0, 0, 0, l).Zone()
|
||
_, b := time.Date(2026, 9, 29, 12, 0, 0, 0, l).In(time.Local).Zone()
|
||
if d := a - b; d >= 4*3600 || d <= -4*3600 {
|
||
zone, loc = name, l
|
||
break
|
||
}
|
||
}
|
||
if loc == nil {
|
||
t.Fatal("no zone far from this computer's")
|
||
}
|
||
now := time.Date(2026, 9, 29, 10, 0, 0, 0, loc).UTC() // Tuesday 10:00 there
|
||
ruleClock = func() time.Time { return now }
|
||
t.Cleanup(func() { ruleClock = time.Now })
|
||
s, a, b := ruled(t, provider.Rule{Use: "b/big", Time: &provider.TimeWindow{From: "09:00", To: "18:00", Days: []string{"mon", "tue", "wed", "thu", "fri"}, Zone: zone}})
|
||
post := func(session string) (string, Route) {
|
||
t.Helper()
|
||
code, out := postAs(t, s, session, chat("hello", nil, 0, ""))
|
||
if code != 200 {
|
||
t.Fatalf("%d %s", code, out)
|
||
}
|
||
return out, lastRoute(s)
|
||
}
|
||
out, r := post("s1")
|
||
if !strings.Contains(out, "from kb") || r.Rule == nil || r.Rule.N != 1 || strings.Join(r.Rule.When, " · ") != "time 09:00–18:00 Mon–Fri ("+zone+")" {
|
||
t.Fatalf("10:00 in %s: %s %+v", zone, out, r.Rule)
|
||
}
|
||
// 8:30 there is outside, whatever the server's clock says
|
||
now = time.Date(2026, 9, 29, 8, 30, 0, 0, loc).UTC()
|
||
if out, r = post("s2"); !strings.Contains(out, "from ka") || r.Rule.N != 0 {
|
||
t.Fatalf("08:30 in %s: %s %+v", zone, out, r.Rule)
|
||
}
|
||
// 17:30 there is inside
|
||
now = time.Date(2026, 9, 29, 17, 30, 0, 0, loc).UTC()
|
||
if out, r = post("s3"); !strings.Contains(out, "from kb") || r.Rule.N != 1 {
|
||
t.Fatalf("17:30 in %s: %s %+v", zone, out, r.Rule)
|
||
}
|
||
if a.n() != 1 || b.n() != 2 {
|
||
t.Fatalf("a %d b %d", a.n(), b.n())
|
||
}
|
||
}
|