gateway-key: a key's monthly limit can be set from the CLI again
`magpie gateway-key limit <id> month --cost 5` (and `add month`) failed with "an N-hour cycle is a whole number of hours …: \"month\"": parseKeyLimit cut an h off any window ending in one before it looked for a named period, and month ends in an h. A named period (day, week, month) is now taken as itself before the <N>d and <N>h forms are read. Found while trying every command for the magpie-setup skill in a sandbox HOME. Run: go test ./internal/cli -run TestGatewayKey -count=1 ok; the new month cases in TestGatewayKeyLimitCLI fail on the old parseKeyLimit (reverted by hand) and pass with it. Tried the built binary in a sandbox HOME: limit <id> month --cost 5 sets "this month".
This commit is contained in:
@@ -296,7 +296,9 @@ func perPhrase(l *access.Limit) string {
|
||||
// --cost and --cache-reads.
|
||||
func parseKeyLimit(window string, rest []string, usage error) (*access.Limit, error) {
|
||||
lim := &access.Limit{Period: window}
|
||||
if n, ok := strings.CutSuffix(window, "d"); ok {
|
||||
if window == "day" || window == "week" || window == "month" {
|
||||
// a period by its name: month ends in an h, and is no count of hours
|
||||
} else if n, ok := strings.CutSuffix(window, "d"); ok {
|
||||
days, err := strconv.Atoi(n)
|
||||
if err != nil || days < 1 || days > access.MaxDays {
|
||||
return nil, fmt.Errorf("an N-day cycle is a whole number of days from 1 to %d, like 10d: %q", access.MaxDays, window)
|
||||
|
||||
@@ -208,6 +208,16 @@ func TestGatewayKeyLimitCLI(t *testing.T) {
|
||||
t.Error("accepted", args)
|
||||
}
|
||||
}
|
||||
// month ends in an h, and is the month, not a count of hours
|
||||
if got, err = call("limit", id, "month", "--cost", "20"); err != nil || !strings.Contains(got, "this month") {
|
||||
t.Fatal(got, err)
|
||||
}
|
||||
if keys, _ = access.List(); keys[0].Limit == nil || keys[0].Limit.Period != "month" || keys[0].Limit.Cost != 20 {
|
||||
t.Fatalf("limit kept as %+v", keys[0].Limit)
|
||||
}
|
||||
if got, err = call("limit", id, "add", "week", "--tokens", "1m"); err != nil || !strings.Contains(got, "Limit 2") {
|
||||
t.Fatal(got, err)
|
||||
}
|
||||
if got, err := call("limit", id, "off"); err != nil || !strings.Contains(got, "no limit") {
|
||||
t.Fatal(got, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user