Files
magpie/internal/gui/stepfun_api_test.go
T
yetoneandZhengJin cc09c5bd97 stepfun: a Step Plan sign-in is the provider's own, not its site's: two accounts on one site show their own windows (#2384)
stepfun-auth.json kept one platform session per site ("ai", "com"), so
every StepFun provider on a site shared it: bringing a sign-in to one
signed in all of them, two accounts showed the same windows, and signing
one out signed out all (fangzhengjin, #2384). Sessions are now kept by
provider id; the site, which platform is asked, is still the provider's
(StepFunSite). StepFunSignedIn, SaveStepFunSession, SaveStepFunPaste,
SignOutStepFun, stepfunNow and stepfunAsk take the id, and
stepPlanQuotas gives one card per signed-in provider, not one per site.

Nobody signs in again for the upgrade: a file still keyed by site is
given, on read, to the StepFun providers on that site (stepfunBySite) and
written back through the temp-and-rename write. One provider: it's
theirs. Several: each gets it, so nobody loses their windows, and the
user brings the other account's sign-in to the ones that are someone
else's. Those copies are one sign-in, so stepfunNow renews every entry
holding the same refresh token together; renewed apart, the platform
could end the token the first renewal replaced and sign the others out.
With no StepFun provider on the site yet (or providers.json unreadable)
the old entry waits as it is. A file that doesn't parse, or that holds an
entry of another shape, is read as no session and nothing but a new
sign-in writes over it, after lastgood.KeepUnparsed has kept it aside.

Every other place keyed by the provider follows: Delete drops its
session (a provider added later under the same id doesn't inherit it),
Rename moves it, and the GUI posts /api/stepfun/{id}/session and
/api/stepfun/{id}/signout, 404 for an id that isn't a StepFun
provider's (a site's name among them). Backup and sync never carried
stepfun-auth.json, so nothing changes there.

Built on fangzhengjin's stepfun-per-provider branch (996cf969e), which
rekeyed the session by id. Not taken from it: it made every existing user
sign in again, left Delete and Rename on the old keys (a renamed provider
lost its sign-in, a removed one's was inherited by a new provider of the
same id), let signout take any id, and its two-provider test checked
only the stored Account, never each provider's windows.

Run at this head (on origin/main a1f7b8c13), -tags nogui -p 1, sandbox HOME:
- go test ./internal/provider -run 'StepFun|StepPlan|UnreadState|Rename|Delete|PlanQuota': ok
- go test ./internal/gui -run 'StepFun|Provider|Route|Asset|Test.*Guard|I18n': ok
- GUI: stepfun-per-provider.test.cjs, chromium and webkit, en and zh: 4/4 ok
  (3 runs each)
- gofmt -l; go vet -tags nogui for darwin, linux, windows on
  internal/provider and internal/gui: clean
Broken by hand, each red: saving by site (TwoAccountsOneSite, plus the
old tests), no migration (OldSessionMoved), copies renewed apart
(renewed 2 times), Delete keeping the session, Rename not moving it,
SignOut writing with no session (UnparsedKept: written over as {}),
the old key not dropped, a partly-parsed file read (UnparsedKept),
the editor told by site and the signout route taking any provider
(TestStepFunSignInPerProvider), app.js posting the site
(stepfun-per-provider.test.cjs). Making the migration read providers.json
with load() instead of read() stayed green: an unread file gives no
provider to move to either way, so that case is covered by outcome only.
Not tried against the real StepFun platform: every test uses a fake.

Co-authored-by: ZhengJin <12680972+fangzhengjin@users.noreply.github.com>

Full suite: SUITE_LINE
2026-10-15 23:08:04 +08:00

80 lines
2.5 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A StepFun provider's Step Plan sign-in is its own (#2384): the editor is
// told each provider's, and signing one out, by its id, leaves the other
// on its site signed in. An id that isn't a StepFun provider's, a site's
// name among them, is no StepFun sign-in to post to.
func TestStepFunSignInPerProvider(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, p := range []provider.Provider{
{ID: "stepfun", Name: "StepFun", Key: "k1", Chat: "https://api.stepfun.ai/step_plan/v1"},
{ID: "stepfun-work", Name: "StepFun Work", Key: "k2", Chat: "https://api.stepfun.ai/step_plan/v1"},
{ID: "relay", Name: "Relay", Key: "k3", Chat: "http://127.0.0.1:1/v1"},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
session := `{"access":"a.b.c","refresh":"a.b.c","webId":"web-1","account":"4161"}`
auth := filepath.Join(filepath.Dir(provider.Path()), "stepfun-auth.json")
if err := os.WriteFile(auth, []byte(`{"stepfun":`+session+`,"stepfun-work":`+session+`}`), 0o600); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
providerRoutes(mux, nil)
signedIn := func() map[string]bool {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/providers", nil))
var got struct {
Providers []struct {
ID string
StepPlan *struct{ SignedIn bool }
}
}
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
out := map[string]bool{}
for _, p := range got.Providers {
if p.StepPlan != nil {
out[p.ID] = p.StepPlan.SignedIn
}
}
return out
}
if got := signedIn(); !got["stepfun"] || !got["stepfun-work"] || len(got) != 2 {
t.Fatalf("before: %v", got)
}
for _, id := range []string{"relay", "ai", "nobody"} {
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/stepfun/"+id+"/signout", strings.NewReader("{}")))
if w.Code != http.StatusNotFound {
t.Errorf("signout for %s: %d, want 404", id, w.Code)
}
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/stepfun/stepfun/signout", strings.NewReader("{}")))
if w.Code != 200 {
t.Fatalf("signout: %d %s", w.Code, w.Body)
}
if got := signedIn(); got["stepfun"] || !got["stepfun-work"] {
t.Fatalf("after signing stepfun out: %v", got)
}
}