Files
magpie/internal/catalog/peak_test.go
T
yetone 643b525469 usage: a DeepSeek call at peak hours costs double its off-peak list price
treeMan on Discord: DeepSeek bills off-peak at half the peak rate, and the
Usage page didn't follow it. The catalog's list prices for these models
are already the off-peak ones: models.dev's DeepSeek, Command Code and
OpenCode Go entries give the off-peak rate as the price and a factor-2
multiplier for peak hours, and api.json leaves the multiplier out
(deepseek-flash: $0.15/$0.60/$0.003, DeepSeek's page says $0.30/$1.20/
$0.006 at peak). So every peak call was counted at half its cost. Now a
call made from 01:00 to 04:00 or 06:00 to 10:00 UTC, Monday to Friday,
costs twice the list price. The call's own timestamp decides.

Which providers (provider.PeakOf, by base URL, or by preset/id only for a
provider with no URL, such as a plugin's or a removed one):
- DeepSeek's API (api.deepseek.com), every model, except on Chinese
  public holidays (catalog.PeakDeepSeek);
- Command Code (api.commandcode.ai, the commandcode preset and the
  commandcode-plan plugin): deepseek-v4.1-flash, -v4.1-flash-fast,
  -v4-flash, -v4-flash-vision-exp and -v4-pro, every weekday, since its
  docs name no holiday. deepseek-v4-flash-fast is one price at every hour;
- OpenCode Go (opencode.ai/zen/go): deepseek-v4.1-flash and
  deepseek-v4-pro, every weekday.
Unchanged: OpenCode Zen (a flat $0.30/$1.20), ZenMux, Cline Pass,
OrcaRouter and every other relay, a price the user set (ModelPrices
returns before a Peak is set), a "same as" price and every other model.
The provider's price rate still applies.

Holidays: catalog.cnHolidays has 2026 from the State Council's notice
(2025-11-04). TestCNHolidaysCoverThisYear fails on 2027-01-01 unless that
year's notice, due in November, is added. A year missing from the table
counts no holidays.

Where the hour is applied (catalog.Price.In): Record.CostAt (Usage's
totals and groups), the packed gateway blocks (priceBlock), OTLP's
exported cost and budget's estimate (now). The Sessions page sums per
model without per-call times and stays at the list price. The model
editor's price box says when a list price counts double (four languages;
app.js kept under its budget by shortening, not by a raise).

Run at the tree pushed (parent 32f5c3369), on mote:
- gofmt -l clean
- go vet on the six packages
- go test of catalog, usage and otlp whole, and provider and gui -run
  Peak|PriceIn|CNHolidays|Price|TestCoreScriptBudget, with TZ=UTC and
  with TZ=Asia/Shanghai: all ok
At 76b18f68d (the same change before the last two rebases), on mote:
- go vet ./internal/..., GOOS=linux -tags nogui and GOOS=windows vet of
  ./..., and the darwin, linux nogui and windows builds: ok
- catalog, provider, usage and otlp whole, in both zones: ok
- internal/gui: TestCoreScriptBudget was red (app.js 108 bytes over),
  fixed by shortening the hint. Ten AppKit window tests were killed at
  their 30s bar at load ~130 (TestDockJoinLeadsCommandTab,
  TestMainWindowCorners and others); this change doesn't touch them.
GUI:
- model-price-edit (now checks both peak hints and none on a plain list
  price), model-price-tiers, provider-price-rate and i18n-keys: Chromium
  ok on mote.
- WebKit on mote couldn't launch at all (browserType.launch timed out at
  180s, 69 WebKit processes from other runs), so the three price files ran
  in WebKit on Linux (wsl-run): 8 of 8 ok.
- The whole GUI suite (both engines) at ff23724f3 was still running at
  load 110 to 150 when this was pushed, 119 red of ~7200. Every file
  checked so far is a timeout under load or in #4304 / #3476.
  model-chip-steady is red the same way on origin/main 0911a65e7.

Broken by hand, each red:
- CostAt without .In(r.Time): TestCostAtTheCallsHour (0.753 at peak,
  want 1.506)
- priceBlock without .In(p.Time): its block half
- Peak.At ignoring holidays: TestPeakHours on 2026-10-05
- priceOf never setting Peak: TestPriceOfPeak
- OpenCode Go's model list dropped: TestPeakOf (kimi-k3 "weekdays")

Not tried: a real call to DeepSeek, Command Code or OpenCode Go at peak
and off-peak to compare against their bills; a full go test ./...; the
holiday rule's handling of the swapped working weekends (DeepSeek says
Monday to Friday, so a worked Saturday stays off-peak here); ZenMux,
whose own docs give no schedule.
2026-10-30 15:22:55 +08:00

116 lines
4.3 KiB
Go

package catalog
import (
"testing"
"time"
)
// DeepSeek's peak hours are 01:00 to 04:00 and 06:00 to 10:00 UTC, Monday
// to Friday: each edge is checked a minute either side, on a weekday, a
// weekend day and a Chinese public holiday, and at the time the call says,
// in whatever zone it is written in, never the machine's.
func TestPeakHours(t *testing.T) {
at := func(day string, hm string) time.Time {
v, err := time.Parse("2006-01-02 15:04", day+" "+hm)
if err != nil {
t.Fatal(err)
}
return v
}
edges := map[string]bool{
"00:59": false, "01:00": true, "03:59": true, "04:00": false,
"05:59": false, "06:00": true, "09:59": true, "10:00": false,
"00:00": false, "23:59": false, "12:00": false,
}
for _, c := range []struct {
day string
deepseek bool // whether DeepSeek's peak hours apply that day
weekdays bool // whether every-weekday ones do
}{
{"2026-10-29", true, true}, // a Thursday
{"2026-10-26", true, true}, // a Monday
{"2026-10-30", true, true}, // a Friday
{"2026-10-31", false, false}, // a Saturday
{"2026-11-01", false, false}, // a Sunday
{"2026-10-05", false, true}, // a Monday in the National Day break
{"2026-02-16", false, true}, // a Monday in the Spring Festival break
{"2026-10-09", true, true}, // a Friday after the break
} {
for hm, peak := range edges {
tm := at(c.day, hm)
if got := PeakDeepSeek.At(tm); got != (peak && c.deepseek) {
t.Errorf("deepseek %s %s: peak %v", c.day, hm, got)
}
if got := PeakWeekdays.At(tm); got != (peak && c.weekdays) {
t.Errorf("weekdays %s %s: peak %v", c.day, hm, got)
}
// the same instant written in Shanghai's and New York's time
for _, z := range []string{"Asia/Shanghai", "America/New_York"} {
loc, err := time.LoadLocation(z)
if err != nil {
loc = time.FixedZone(z, map[string]int{"Asia/Shanghai": 8, "America/New_York": -4}[z]*3600)
}
if got := PeakDeepSeek.At(tm.In(loc)); got != (peak && c.deepseek) {
t.Errorf("deepseek %s %s in %s: peak %v", c.day, hm, z, got)
}
}
if Peak("").At(tm) {
t.Errorf("no peak %s %s: peak", c.day, hm)
}
}
}
if PeakDeepSeek.At(time.Time{}) {
t.Error("a call with no time is at peak")
}
}
// A price with no Peak holds at every hour; a peak-billed one doubles in
// its peak hours, every part of it, tiers too, and the price In gives is
// one that holds (In again doesn't double it twice).
func TestPriceIn(t *testing.T) {
peak := time.Date(2026, 10, 29, 2, 0, 0, 0, time.UTC)
off := time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC)
flash := Price{Input: 0.15, Output: 0.6, CacheRead: 0.003, Peak: PeakDeepSeek}
if got, want := flash.In(peak), (Price{Input: 0.3, Output: 1.2, CacheRead: 0.006}); !got.Same(want) {
t.Errorf("peak: %+v, want %+v", got, want)
}
if got := flash.In(peak).In(peak); got.Input != 0.3 {
t.Errorf("In twice: %+v", got)
}
if got, want := flash.In(off), (Price{Input: 0.15, Output: 0.6, CacheRead: 0.003}); !got.Same(want) {
t.Errorf("off-peak: %+v, want %+v", got, want)
}
if got := flash.In(time.Time{}); got.Input != 0.15 {
t.Errorf("no time: %+v", got)
}
// a model of another vendor at the same hours costs what it did
other := Price{Input: 3, Output: 15, CacheRead: 0.3, CacheWrite: 3.75}
for _, tm := range []time.Time{peak, off} {
if got := other.In(tm); !got.Same(other) {
t.Errorf("%v: %+v, want %+v", tm, got, other)
}
if a, b := other.In(tm).CostSplit(1000, 1000, 1000, 1000, 0), other.CostSplit(1000, 1000, 1000, 1000, 0); a != b {
t.Errorf("%v: cost %v, want %v", tm, a, b)
}
}
if a, b := flash.In(peak).CostSplit(1e6, 1e6, 0, 0, 0), flash.In(off).CostSplit(1e6, 1e6, 0, 0, 0); a != 2*b {
t.Errorf("peak cost %v, off-peak %v: want twice", a, b)
}
}
// cnHolidays needs each year's days added once the State Council's notice
// is out (each November): this fails in a year it doesn't have, so the
// table is never silently a year behind.
func TestCNHolidaysCoverThisYear(t *testing.T) {
if y := time.Now().In(chinaTime).Year(); len(cnHolidays[y]) == 0 {
t.Fatalf("cnHolidays has no %d: add the State Council's notice for it", y)
}
for y, days := range cnHolidays {
for _, d := range days {
if _, err := time.Parse("2006-01-02", time.Date(y, 1, 1, 0, 0, 0, 0, time.UTC).Format("2006")+"-"+d); err != nil {
t.Errorf("%d %q: %v", y, d, err)
}
}
}
}