Found reviewing magpie-community/plugins#658 (AStudio's and Agnes Code's
sign-ins wait up to 5 minutes for the vendor's desktop app).
Cancel didn't end a plugin sign-in. The window's Cancel (and the sign-in's
timeout) ended plugin.Finish's context, but "callback" was not a
cancellable call, so the host never heard: the plugin's wait went on, and
a session file written after Cancel still saved the account. Now
authorize, callback and apiKey are cancellable as gatewayRequest is
(host.go `cancellable`): magpie writes `abort`, host.js (`signInSteps`)
drops whatever the plugin answers after it, so nothing is saved, and
counts the call done at once. The plugin's wait is told too: every fetch
it makes for the sign-in carries the sign-in's signal
(`whileSigningIn`), and magpie's hook passes the signal as
callback(code, { signal }) / authorize(inputs, { signal }), which OpenCode
doesn't, so a plugin can stop polling a file. A plugin that ignores the
signal keeps running until its own deadline, but its answer is dropped.
An oauth method's authorize that returned { type: "failed", error } (no
callback) was stored and its missing callback called, so the user saw
"s.a.callback is not a function". host.js now answers that, or any result
with no callback, as the sign-in's failure, and plugin.Authorize returns
ErrFailed with the plugin's reason, for auto and code methods; an api
method's authorize already did. The GUI's start shows it as its failure,
`magpie plugin login` prints it.
Docs: provider-plugins.md, and the plugin guide (en, zh, ja) on failing
from authorize and the signal.
Not changed: #3083's timeout field, #4678's model/usage task lifetime.
Tests (testdata/signinend, no network; the vendor is testenv.FakeVendor):
TestCancelledSignInSavesNothing, TestCancelledSignInEndsThePluginsWait,
TestCancelledSignInEndsItsFetch, TestSignInFailsWithThePluginsReason
(internal/plugin), TestCancelledPluginSignInSavesNothing
(internal/provider, through StartPluginSignIn/CancelSignIn).
Run on mote (macOS 27), at this change on d73f2254d (the new tests and vet of both packages again after the rebase on 897e88d7e, which touches neither):
- go test -count=1 -timeout 45m ./internal/plugin/ ./internal/provider/:
ok (134s, 145s); the new tests -v: all PASS.
- ./internal/cli ok; ./internal/gui FAIL only in
TestMainWindowCorners/macOS_26_SDK, #3949 (macOS 27 on mote; this
change touches no GUI code).
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...: ok;
gofmt -l: nothing.
- GUI (Chromium + WebKit): plugin-signin, plugin-signin-no-url,
plugin-signin-opened (en, zh, zh-TW, ja, de), plugin-signin-scrolls,
plugin-keyless-signin, plugin-moved-signin, signin-paste-plugin,
signin-close, i18n-keys: 108/108.
Red on the old code (origin/main's host.js, host.go, providers.go): all
four plugin tests (an account saved after Cancel; the plugin's wait and
its fetch not ended, timed out; methods 2/3/4 said "s.a.callback is not
a function"), and TestCancelledPluginSignInSavesNothing.
Broken by hand: host.go's `cancellable` back to gatewayRequest only, all
three cancel tests red; the whileSigningIn line out of the fetch wrapper,
TestCancelledSignInEndsItsFetch red.
Not tried: the real AStudio and Agnes Code plugins (plugins#658) against
this host; the site's plugin guide isn't deployed by this commit.
173 lines
5.4 KiB
Go
173 lines
5.4 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// signInEnd adds testdata/signinend, whose sign-ins wait on $HOME/go as
|
|
// AStudio's waits on its desktop app's session file (plugins#658).
|
|
func signInEnd(t *testing.T) (context.Context, string) {
|
|
t.Helper()
|
|
sandbox(t)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
t.Cleanup(cancel)
|
|
abs, _ := filepath.Abs("testdata/signinend/index.js")
|
|
if _, err := Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return ctx, os.Getenv("HOME")
|
|
}
|
|
|
|
// markedWithin is whether the plugin wrote $HOME/name within d.
|
|
func markedWithin(home, name string, d time.Duration) bool {
|
|
for end := time.Now().Add(d); ; {
|
|
if _, err := os.Stat(filepath.Join(home, name)); err == nil {
|
|
return true
|
|
}
|
|
if time.Now().After(end) {
|
|
return false
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// cancelledFinish starts the method's sign-in and gives its wait up once
|
|
// the plugin is waiting (it marks $HOME/waiting, or its vendor is asked),
|
|
// as the GUI's Cancel does (signInFlow.stop).
|
|
func cancelledFinish(t *testing.T, ctx context.Context, method int, waiting <-chan struct{}) {
|
|
t.Helper()
|
|
a, err := Authorize(ctx, "signinend", method, nil, NewAccount)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wait, cancel := context.WithCancel(ctx)
|
|
go func() {
|
|
if waiting != nil {
|
|
<-waiting
|
|
} else {
|
|
markedWithin(os.Getenv("HOME"), "waiting", 30*time.Second)
|
|
}
|
|
cancel()
|
|
}()
|
|
if _, err := Finish(wait, a.Session, ""); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("Finish after cancel = %v, want context.Canceled", err)
|
|
}
|
|
}
|
|
|
|
// #5016: a sign-in cancelled in magpie saved the account all the same
|
|
// when the plugin's wait came true after it. What the plugin answers once
|
|
// the sign-in is given up is dropped.
|
|
func TestCancelledSignInSavesNothing(t *testing.T) {
|
|
ctx, home := signInEnd(t)
|
|
cancelledFinish(t, ctx, 0, nil)
|
|
if err := os.WriteFile(filepath.Join(home, "go"), nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !markedWithin(home, "returned", 30*time.Second) {
|
|
t.Fatal("the plugin's wait never came true")
|
|
}
|
|
// one more answer from the host, after the callback's result was dealt with
|
|
if _, err := Providers(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if SignedIn("signinend") {
|
|
b, _ := os.ReadFile(AuthPath())
|
|
t.Fatalf("a cancelled sign-in was saved: %s", b)
|
|
}
|
|
// and the next sign-in is the user's
|
|
a, err := Authorize(ctx, "signinend", 0, nil, NewAccount)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := Finish(ctx, a.Session, ""); err != nil || got != (Saved{"signinend", "signinend"}) {
|
|
t.Fatalf("Finish = %+v, %v", got, err)
|
|
}
|
|
if b, _ := os.ReadFile(AuthPath()); !strings.Contains(string(b), `"tok-late"`) {
|
|
t.Fatalf("auth file: %s", b)
|
|
}
|
|
}
|
|
|
|
// #5016: the plugin's wait is told the sign-in is over (magpie's hook:
|
|
// callback(code, { signal })), so it stops instead of waiting out its own
|
|
// minutes.
|
|
func TestCancelledSignInEndsThePluginsWait(t *testing.T) {
|
|
ctx, home := signInEnd(t)
|
|
cancelledFinish(t, ctx, 1, nil)
|
|
if !markedWithin(home, "aborted", 20*time.Second) {
|
|
t.Fatal("the plugin's wait was never told the sign-in was cancelled")
|
|
}
|
|
}
|
|
|
|
// #5016: a sign-in that polls its vendor stops polling once cancelled:
|
|
// the fetch it made for the sign-in fails.
|
|
func TestCancelledSignInEndsItsFetch(t *testing.T) {
|
|
asked := make(chan struct{})
|
|
gone := make(chan struct{})
|
|
testenv.FakeVendor(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
close(asked)
|
|
select {
|
|
case <-r.Context().Done():
|
|
close(gone)
|
|
case <-time.After(time.Minute):
|
|
}
|
|
}))
|
|
ctx, home := signInEnd(t)
|
|
cancelledFinish(t, ctx, 7, asked)
|
|
if !markedWithin(home, "fetch-aborted", 20*time.Second) {
|
|
t.Fatal("the sign-in's fetch went on after it was cancelled")
|
|
}
|
|
select {
|
|
case <-gone:
|
|
case <-time.After(20 * time.Second):
|
|
t.Fatal("the vendor's request was never closed")
|
|
}
|
|
if b, _ := os.ReadFile(filepath.Join(home, "fetch-aborted")); string(b) != "AbortError" {
|
|
t.Errorf("the fetch failed with %q, want AbortError", b)
|
|
}
|
|
}
|
|
|
|
// plugins#658: an oauth method's authorize that fails ({ type: "failed",
|
|
// error }) reached the user as "s.a.callback is not a function"; the
|
|
// plugin's reason is the error, for every method type.
|
|
func TestSignInFailsWithThePluginsReason(t *testing.T) {
|
|
ctx, _ := signInEnd(t)
|
|
for _, c := range []struct {
|
|
method int
|
|
want string
|
|
}{
|
|
{2, "this AStudio account is banned"},
|
|
{3, "Agnes Code signs in on Windows only"},
|
|
{4, "signinend's sign-in gave nothing to finish it with"},
|
|
} {
|
|
a, err := Authorize(ctx, "signinend", c.method, nil, NewAccount)
|
|
if err == nil {
|
|
_, err = Finish(ctx, a.Session, "x")
|
|
}
|
|
if !errors.Is(err, ErrFailed) || !strings.HasSuffix(err.Error(), ": "+c.want) {
|
|
t.Errorf("method %d: %v, want the sign-in failed: %s", c.method, err, c.want)
|
|
}
|
|
}
|
|
if _, err := APIKey(ctx, "signinend", 5, nil, "k", NewAccount); !errors.Is(err, ErrFailed) || !strings.HasSuffix(err.Error(), ": that key is revoked") {
|
|
t.Errorf("api method: %v", err)
|
|
}
|
|
a, err := Authorize(ctx, "signinend", 6, nil, NewAccount)
|
|
if err != nil || a.Method != "code" {
|
|
t.Fatalf("Authorize = %+v, %v", a, err)
|
|
}
|
|
if _, err := Finish(ctx, a.Session, "x"); !errors.Is(err, ErrFailed) || !strings.HasSuffix(err.Error(), ": that code expired") {
|
|
t.Errorf("code callback: %v", err)
|
|
}
|
|
if SignedIn("signinend") {
|
|
t.Error("a failed sign-in was saved")
|
|
}
|
|
}
|