The same Discord (#feedback) user: a custom provider's video model still couldn't be chosen. A custom provider's video model was already found (seedance, kling, veo, wan, hailuo ids by name, or marked by hand with right-click › Makes › Video model, #1399), but generate_video always used AutoVideomaker's pick, the first video model of the first provider, with nowhere to choose another, as Settings › Images does for images. settings.VideoGen (provider/model or a routing group) is now the model a request that names none makes its video with, while it resolves; else AutoVideomaker's, as before. Image generation turned off turns video off with it, as before. Settings › Models › Images gets a Video generation row under Image generation: Automatic (naming the automatic pick) and every model Magpie Image can make videos with. A pick that no longer makes videos (provider gone or off, model unmarked) says so and the automatic one is used in its place; the settings API refuses a model that makes none, and keeps the pick when a save doesn't send it. The row is drawn by videogen.js, imported when Settings draws Image generation, with one call in app.js: drawn in app.js it put the core scripts 616 bytes over TestCoreScriptBudget (1,534,418 > 1,533,802, found by the first full run), and the budget is not raised. Run on mote: - At this change onbceeabd93(rebased ontoc630d9a29, which changes only the gateway key page): gofmt -l clean; GOOS=windows go vet and GOOS=linux go vet -tags nogui of the changed packages: ok. go test -run 'Budget|VideoGen|Videomaker|TestSettingsSave|TestKeysAreWhatTheSaveWrites' in settings, settingsapi, gateway, gui: ok. GUI: video-gen-pick (32 subtests: chromium and webkit; en, zh, zh-TW, ja, de at 1100px, en and zh at 400px), old-webkit, i18n-en-no-cjk and the six image/Settings files (image-timeout-setting, images-codex-one, images-library-way, images-missing, image-models-hide, image-mcp): 220 of 220 ok. - At 95a3fa3e0 (the change before the videogen.js move and the rebase): go test -timeout 45m ./internal/settings ./internal/settingsapi ./internal/gateway ./internal/gui: ok except TestCoreScriptBudget (fixed above). i18n-keys at 4394ea1b9: only the known plugin-words red (#5332). - Not run at this head: the whole of those four packages and the 183 GUI files a Settings change reaches; the suite lock on mote was an hour deep in other sessions' suites. Broken by hand, each red: - videomaker ignoring VideoGen: TestVideoGenPicksAMarkedModel. - settingsapi not keeping a pick a save doesn't send: TestVideoGenCheckedAndKept. - No Video generation row: video-gen-pick.test.cjs, 30 of 32. - videoGen left out of the Settings page's prefsKeep: TestSettingsSaveKeepsWhatItDoesNotSend ("VideoGen: not sent by the Settings page"), found before it was added. Not tried: a real video generation with a custom provider's model (the gateway tests hold the vendor's replies).
340 lines
11 KiB
Go
340 lines
11 KiB
Go
package settingsapi
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"os"
|
||
"path/filepath"
|
||
"reflect"
|
||
"slices"
|
||
"strings"
|
||
"testing"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
"github.com/yetone/magpie/internal/testenv"
|
||
)
|
||
|
||
func TestMain(m *testing.M) { testenv.Main(m) }
|
||
|
||
// home gives the test a home of its own, with no settings.json yet.
|
||
func home(t *testing.T) {
|
||
t.Helper()
|
||
root := t.TempDir()
|
||
testenv.SetHome(t, root)
|
||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, ".config"))
|
||
t.Setenv("APPDATA", filepath.Join(root, "AppData", "Roaming"))
|
||
}
|
||
|
||
// sample is a value of t that isn't was, as JSON carries it.
|
||
func sample(t reflect.Type, key string, was reflect.Value) reflect.Value {
|
||
v := reflect.New(t).Elem()
|
||
switch t.Kind() {
|
||
case reflect.Bool:
|
||
v.SetBool(!was.Bool())
|
||
case reflect.String:
|
||
s := "x"
|
||
switch key {
|
||
case "vision", "imageGen", "searcher", "searcherThen":
|
||
s = "off" // a model is checked against the catalog
|
||
case "videoGen":
|
||
s = "studio/clip-1" // a video model, videoModel's
|
||
}
|
||
for _, c := range Choices[key] {
|
||
if c != was.String() {
|
||
s = c
|
||
break
|
||
}
|
||
}
|
||
v.SetString(s)
|
||
case reflect.Int, reflect.Int64, reflect.Int32:
|
||
v.SetInt(was.Int() + 7)
|
||
case reflect.Float64, reflect.Float32:
|
||
v.SetFloat(was.Float() + 1.5)
|
||
case reflect.Pointer:
|
||
elem := reflect.Zero(t.Elem())
|
||
if !was.IsNil() {
|
||
elem = was.Elem()
|
||
}
|
||
v.Set(reflect.New(t.Elem()))
|
||
v.Elem().Set(sample(t.Elem(), key, elem))
|
||
case reflect.Slice:
|
||
el := sample(t.Elem(), key, reflect.Zero(t.Elem()))
|
||
v.Set(reflect.Append(reflect.MakeSlice(t, 0, 1), el))
|
||
case reflect.Map:
|
||
v.Set(reflect.MakeMap(t))
|
||
v.SetMapIndex(sample(t.Key(), "", reflect.Zero(t.Key())), sample(t.Elem(), "", reflect.Zero(t.Elem())))
|
||
case reflect.Struct:
|
||
for i := range t.NumField() {
|
||
if f := t.Field(i); f.IsExported() {
|
||
v.Field(i).Set(sample(f.Type, "", reflect.Zero(f.Type)))
|
||
}
|
||
}
|
||
}
|
||
return v
|
||
}
|
||
|
||
// Keys are the fields POST /api/settings saves from what it's sent, no
|
||
// more and no fewer: each field of settings.Settings is sent changed, on
|
||
// its own, and the ones the save takes are compared with Keys. A field
|
||
// added to Settings that the save takes (or carries) without Keys knowing
|
||
// fails here, and so `magpie settings` can't fall behind the window
|
||
// (#4745).
|
||
func TestKeysAreWhatTheSaveWrites(t *testing.T) {
|
||
home(t)
|
||
videoModel(t)
|
||
cur := settings.Defaults()
|
||
curJSON, err := json.Marshal(cur)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
st := reflect.TypeFor[settings.Settings]()
|
||
var written []string
|
||
for i := range st.NumField() {
|
||
f := st.Field(i)
|
||
key, _, _ := strings.Cut(f.Tag.Get("json"), ",")
|
||
if key == "" || key == "-" {
|
||
continue
|
||
}
|
||
was := reflect.ValueOf(cur).Field(i)
|
||
val := sample(f.Type, key, was)
|
||
b, err := json.Marshal(val.Interface())
|
||
if err != nil {
|
||
t.Fatalf("%s: %v", key, err)
|
||
}
|
||
var m map[string]json.RawMessage
|
||
_ = json.Unmarshal(curJSON, &m)
|
||
m[key] = b
|
||
body, _ := json.Marshal(m)
|
||
in, err := Merge(body, cur, false)
|
||
if err != nil {
|
||
t.Fatalf("%s sent as %s: %v", key, b, err)
|
||
}
|
||
if !reflect.DeepEqual(reflect.ValueOf(in).Field(i).Interface(), was.Interface()) {
|
||
written = append(written, key)
|
||
}
|
||
}
|
||
// settings.Save keeps the file's own: the page can send it, and it
|
||
// is never written by this save (SetSessionReads writes it)
|
||
written = slices.DeleteFunc(written, func(k string) bool { return k == "sessionReads" })
|
||
got, want := slices.Sorted(slices.Values(written)), slices.Sorted(slices.Values(Keys))
|
||
if !slices.Equal(got, want) {
|
||
t.Errorf("the save writes keys Keys doesn't name, or Keys names keys it doesn't write:\n only saved: %v\n only in Keys: %v",
|
||
minus(got, want), minus(want, got))
|
||
}
|
||
for _, k := range Keys {
|
||
if _, ok := Field(k); !ok {
|
||
t.Errorf("Keys names %q, which settings.Settings doesn't have", k)
|
||
}
|
||
}
|
||
if len(Keys) != len(slices.Compact(slices.Sorted(slices.Values(Keys)))) {
|
||
t.Error("Keys names a key twice")
|
||
}
|
||
}
|
||
|
||
// videoModel sets up a provider whose clip-1 the user marked a video
|
||
// model (#1399), so studio/clip-1 is one videoGen can name.
|
||
func videoModel(t *testing.T) {
|
||
t.Helper()
|
||
if err := provider.Save(provider.Provider{ID: "studio", Name: "Studio", Chat: "https://studio.example/v1", Key: "sk-studio", Models: []string{"chat-1", "clip-1"}}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := provider.SetModelKind("studio/clip-1", provider.KindVideo); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
|
||
// Settings › Images' video model: a model that makes videos is saved, one
|
||
// that doesn't (a chat model, or none such) is refused, and a save from
|
||
// another row, which doesn't send it, keeps it.
|
||
func TestVideoGenCheckedAndKept(t *testing.T) {
|
||
home(t)
|
||
videoModel(t)
|
||
for _, bad := range []string{"studio/chat-1", "studio/nope", "clip-1"} {
|
||
if _, _, err := Apply([]byte(`{"videoGen":"`+bad+`"}`), false); err == nil {
|
||
t.Errorf("%s was taken as a video model", bad)
|
||
}
|
||
}
|
||
if _, _, err := Apply([]byte(`{"videoGen":"studio/clip-1"}`), false); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, _, err := Apply([]byte(`{"theme":"dark"}`), false); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if got := settings.Load().VideoGen; got != "studio/clip-1" {
|
||
t.Fatalf("videoGen %q after another row's save", got)
|
||
}
|
||
if _, _, err := Apply([]byte(`{"videoGen":""}`), false); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if got := settings.Load().VideoGen; got != "" {
|
||
t.Fatalf("Automatic left videoGen %q", got)
|
||
}
|
||
}
|
||
|
||
func minus(a, b []string) []string {
|
||
var out []string
|
||
for _, k := range a {
|
||
if !slices.Contains(b, k) {
|
||
out = append(out, k)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
// Setting one key through Body leaves every other the user set as it
|
||
// is, and a key the save keeps when it isn't sent is set even to its
|
||
// zero value.
|
||
func TestBodySetsOneKeyOnly(t *testing.T) {
|
||
home(t)
|
||
s := settings.Defaults()
|
||
s.Theme, s.NoBird, s.RedactShape, s.Currency, s.Proxy = "dark", true, true, "cny", "http://127.0.0.1:7890"
|
||
two := 2
|
||
s.RefusalRetries = &two
|
||
if err := settings.Save(s); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
set := func(key, value string) settings.Settings {
|
||
t.Helper()
|
||
cur, err := settings.LoadChecked()
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
body, err := Body(cur, key, json.RawMessage(value))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, _, err := Apply(body, false); err != nil {
|
||
t.Fatalf("%s=%s: %v", key, value, err)
|
||
}
|
||
return settings.Load()
|
||
}
|
||
got := set("noBird", "false")
|
||
if got.NoBird {
|
||
t.Error("noBird set to false stayed on: a zero value was read as not sent")
|
||
}
|
||
got = set("lang", `"ja"`)
|
||
if got.Lang != "ja" || got.Theme != "dark" || !got.RedactShape || got.Currency != "cny" ||
|
||
got.Proxy != "http://127.0.0.1:7890" || got.RefusalRetries == nil || *got.RefusalRetries != 2 {
|
||
t.Errorf("setting lang changed what the user set: %+v", got)
|
||
}
|
||
}
|
||
|
||
// A value the save refuses writes nothing, whichever way it came in.
|
||
func TestRefusedValueWritesNothing(t *testing.T) {
|
||
home(t)
|
||
s := settings.Defaults()
|
||
s.Theme = "dark"
|
||
if err := settings.Save(s); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
before, err := os.ReadFile(settings.Path())
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
for key, value := range map[string]string{
|
||
"theme": `"neon"`,
|
||
"lang": `"xx"`,
|
||
"trayUsageEvery": `4`,
|
||
"vision": `"nobody/nothing"`,
|
||
"searcher": `"nobody/nothing"`,
|
||
"usageAlert": `101`,
|
||
"updateChannel": `"nightly"`,
|
||
} {
|
||
cur := settings.Load()
|
||
body, err := Body(cur, key, json.RawMessage(value))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, _, err := Apply(body, false); err == nil {
|
||
t.Errorf("%s=%s was saved", key, value)
|
||
}
|
||
after, _ := os.ReadFile(settings.Path())
|
||
if !bytes.Equal(before, after) {
|
||
t.Fatalf("%s=%s was refused but settings.json changed", key, value)
|
||
}
|
||
}
|
||
if _, err := Body(settings.Load(), "port", json.RawMessage(`1`)); err == nil {
|
||
t.Error("Body took port, which has a route of its own")
|
||
}
|
||
}
|
||
|
||
// "Don't ask again" on the Quit question (zhujiu on Discord) holds across
|
||
// a save from a page that doesn't send quitNoAsk (the tray panel, or a
|
||
// page from before it), and Settings › General's Ask before quitting
|
||
// sending it false turns the question back on.
|
||
func TestQuitNoAskKeptUnlessSent(t *testing.T) {
|
||
cur := settings.Defaults()
|
||
cur.QuitNoAsk = true
|
||
in, err := Merge([]byte(`{"theme":"dark"}`), cur, false)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if !in.QuitNoAsk {
|
||
t.Error("a save that didn't send quitNoAsk turned the Quit question back on")
|
||
}
|
||
in, err = Merge([]byte(`{"theme":"dark","quitNoAsk":false}`), cur, false)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if in.QuitNoAsk {
|
||
t.Error("quitNoAsk sent false left the Quit question off")
|
||
}
|
||
}
|
||
|
||
// What a reply's model name says is replyModel (ZoeCats on Discord). The
|
||
// page sends it; a page from before it sends memberModel, which changes
|
||
// it only when that page's switch was moved, so its save keeps a vendor
|
||
// name it can't show; and `magpie settings reset replyModel` (Body over
|
||
// a file with memberModel written beside it) is the model asked for.
|
||
func TestReplyModelSaves(t *testing.T) {
|
||
for _, c := range []struct {
|
||
name string
|
||
cur string // replyModel saved now
|
||
body string
|
||
want string // replyModel saved, "" the model asked for
|
||
}{
|
||
{"page picks vendor", settings.ReplyMember, `{"replyModel":"vendor","memberModel":true}`, settings.ReplyVendor},
|
||
{"page picks asked", settings.ReplyMember, `{"replyModel":""}`, ""},
|
||
{"page picks member", "", `{"replyModel":"member"}`, settings.ReplyMember},
|
||
{"old page, vendor kept", settings.ReplyVendor, `{"memberModel":false}`, settings.ReplyVendor},
|
||
{"old page turns it on", settings.ReplyVendor, `{"memberModel":true}`, settings.ReplyMember},
|
||
{"old page turns it off", settings.ReplyMember, `{"memberModel":false}`, ""},
|
||
{"old page, member kept", settings.ReplyMember, `{"memberModel":true}`, settings.ReplyMember},
|
||
{"another page's save", settings.ReplyVendor, `{"theme":"dark"}`, settings.ReplyVendor},
|
||
} {
|
||
t.Run(c.name, func(t *testing.T) {
|
||
home(t)
|
||
if err := settings.Save(settings.Settings{ReplyModel: c.cur}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, _, err := Apply([]byte(c.body), false); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if got := settings.Load().ReplyModel; got != c.want {
|
||
t.Errorf("replyModel %q, want %q", got, c.want)
|
||
}
|
||
})
|
||
}
|
||
t.Run("reset", func(t *testing.T) {
|
||
home(t)
|
||
if err := settings.Save(settings.Settings{ReplyModel: settings.ReplyMember}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
cur := settings.Load()
|
||
if !cur.MemberModel {
|
||
t.Fatal("memberModel isn't written beside replyModel member")
|
||
}
|
||
body, err := Body(cur, "replyModel", json.RawMessage(`""`))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, _, err := Apply(body, false); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if got := settings.Load().ReplyModel; got != "" {
|
||
t.Errorf("reset left replyModel %q", got)
|
||
}
|
||
})
|
||
}
|