provider: removing or turning off a moved subscription's plugin moves nothing back
The built-ins are being retired (the owner, 10-16: 陆续把内置Provider下线掉),
so the last way back to one is gone. MoveBack, moveBack and releasePlugin
are removed, with the Left flag they set.
- SetPluginOff only turns the plugin off. A moved subscription stays
moved, off with it.
- RemovePlugin records the moved ids as the new state "removed". The
record keeps its accounts, which plugin.Remove leaves in the plugin's
auth store. Nothing moves or installs it by itself: MoveRetiring
skips it and keepMovedCurrent reads only "plugin".
- HandOver turns a removed record whose plugin is installed and current
back into "plugin", record and all. So does Adopt (the editor's Move).
- MoveRetiring moves an old "back" record again, whatever Left said.
- Plugins page, rm/off confirms: "{names} stops working until the plugin
is installed again. Its accounts are kept." / "... while the plugin
is off." The status after removing no longer says it is back on the
built-in.
- Strings: five old strings dropped ("Moving back…" among them), three
new ones in zh, zh-TW, ja and de.
- `magpie plugins rm`/`off` say the same in their output.
- Docs: provider-plugins.md (the states table, the Move-back paragraph)
and AGENTS.md.
Tests:
- TestNoWayBack is new. It covers a "back" record moved again, the
plugin turned off (still moved, accounts kept), removed (removed, not
reinstalled by keepMovedCurrent or MoveRetiring), installed again
(HandOver), and removed again then Adopt.
- TestReleasePlugin and TestMoveBackKeepsLapse asserted the way back
and are deleted.
- TestMove, TestHandOver, TestMoveTidiesItsPlugin and
TestAdoptBeforeSigningIn no longer call MoveBack. TestKeepMovedCurrent
uses the removed state.
- plugin-release.test.cjs checks the new asks and that the status names
no built-in.
Run:
- Broken by hand:
- With HEAD's migrate.go, TestNoWayBack is red at "turning the plugin
off moved it back".
- With HandOver's removed branch disabled, it is red at "installed
again: map[], moved false".
- go test -tags nogui ./internal/provider -run
'TestMove|TestNoWayBack|TestHandOver|TestKeepMovedCurrent|TestAdopt|TestInstallPlugin|TestWorkBuddyCheckin|Plugin|Migrat':
ok.
- GUI (NODE_PATH playwright, --test-concurrency=1): plugin-release,
plugin-move and i18n-keys, chromium and webkit: 17/17.
- gofmt -l clean; GOOS=linux and windows go vet of provider, gui and cli.
- Full suite: go test -tags nogui ./... -count=1 -timeout 90m at this commit rebased on 9b04c4e20 (with the TestFailOpen fix after it): 66 packages ok, none red. Before that fix, at bff7cbd24, 65 ok and internal/middleware TestFailOpen red under load (fixed at its cause in the next commit).
This commit is contained in:
@@ -58,17 +58,17 @@ Two cases decide it:
|
||||
(`unusedSub` in app.js), so it is installed from the Plugins page. A user
|
||||
who installs the plugin themselves is moved too
|
||||
(`provider.HandOver`): at once when the built-in has no accounts, from the
|
||||
gateway's hourly loop (`KeepRetiringMoved`) when it has, unless they moved
|
||||
back or the plugin is signed in under its own `-plugin` id already. Until
|
||||
gateway's hourly loop (`KeepRetiringMoved`) when it has, unless the
|
||||
plugin is signed in under its own `-plugin` id already. Until
|
||||
then the Add sheet shows the plugin's tile only (`replacedSub` in app.js).
|
||||
Since 10-16 the built-ins are being retired: the hourly loop's
|
||||
`MoveRetiring` moves every subscription with a mover that still has
|
||||
built-in accounts, a user who moved back included, and there is no way
|
||||
back from the GUI or CLI. Only removing or turning off the plugin puts the
|
||||
built-in back (`Left`), and that is left alone.
|
||||
back. Turning the plugin off turns the subscription off with it; removing
|
||||
it records `removed` (accounts kept with the plugin's sign-ins), and
|
||||
installing it again gives the subscription back.
|
||||
- **Not moved:** the user is still signed in through the built-in: the
|
||||
move hasn't run yet, it failed (tried again after six hours), or they
|
||||
removed the plugin.
|
||||
move hasn't run yet, or it failed (tried again after six hours).
|
||||
|
||||
For a moved subscription, the plugin does everything: sign-in, refresh,
|
||||
models, requests, usage and errors. It runs in the plugin host
|
||||
|
||||
@@ -116,22 +116,23 @@ How the store handles picks and names:
|
||||
|
||||
## How ownership changes
|
||||
|
||||
Migration records use four states:
|
||||
Migration records use five states:
|
||||
|
||||
| State | Meaning |
|
||||
| --- | --- |
|
||||
| `moving` | Migration is in progress or was interrupted. An explicit retry first rolls back the interrupted move. |
|
||||
| `plugin` | The plugin owns the built-in provider id. This is the only state recognized by `Moved`. |
|
||||
| `back` | Ownership returned to the built-in. `MoveRetiring` moves it again, unless `Left` says the user removed or turned off the plugin. |
|
||||
| `back` | Ownership returned to the built-in by an older magpie. `MoveRetiring` moves it again. |
|
||||
| `failed` | Migration failed and the built-in continues serving. Automatic retry waits six hours. |
|
||||
| `removed` | The user removed the plugin of a moved subscription. Its accounts stay with the plugin's sign-ins and the record keeps them. Nothing moves or installs it by itself; the plugin installed again (`HandOver`) or the editor's Move (`Adopt`) makes it `plugin` again, record and all. |
|
||||
|
||||
The editor's "Move to plugin" action calls `provider.Adopt`. With accounts, adoption delegates to `Move`, which installs the plugin, imports every account and commits plugin ownership. Each account is tried through the plugin, and none holds the move back, since the built-in is retired: one the vendor refuses goes along marked lapsed, and one already lapsed or that fails its check otherwise (its vendor unreachable) goes along untried, for the plugin's own reads to judge. No model the built-in served is checked against the plugin's list. Only an install failure, the plugin not serving the id, or a sign-in the plugin won't import fails the move, which then puts everything back (`putBack`) and is tried again after six hours. A `why` from an older magpie's failed move (`lapsed`, `unserved`, `account`) is still read and translated by the page. With no accounts, adoption verifies the package and the provider it serves, then records plugin ownership without account checks.
|
||||
|
||||
Installing the plugin independently can also hand over ownership through `provider.HandOver`. The package must be enabled, meet the mover's minimum version, and serve the expected provider. After add, update, or upgrade, the GUI calls `HandOver(ctx, false)`, which can adopt a built-in with no accounts. Existing built-in accounts wait for `HandOver(ctx, true)` in gateway maintenance.
|
||||
|
||||
`KeepRetiringMoved` first runs after 20 seconds, then hourly. Each cycle maintains moved plugin versions, calls `MoveRetiring`, then performs automatic handover. `MoveRetiring` moves every subscription with a mover (`retiring`, which is `MovableIDs`) that has built-in accounts, waiting six hours after a failed attempt. The built-ins are being retired (the owner, 10-16: 陆续把内置Provider下线掉), so a `back` record is moved again too; only one with `Left` set, a move back made by removing or turning off the plugin, is left. Handover preserves a plugin already signed in under its separate `-plugin` id and skips existing migration records except a `failed` record old enough to retry.
|
||||
`KeepRetiringMoved` first runs after 20 seconds, then hourly. Each cycle maintains moved plugin versions, calls `MoveRetiring`, then performs automatic handover. `MoveRetiring` moves every subscription with a mover (`retiring`, which is `MovableIDs`) that has built-in accounts, waiting six hours after a failed attempt. The built-ins are being retired (the owner, 10-16: 陆续把内置Provider下线掉), so a `back` record is moved again too; a `removed` one is left until the user installs the plugin. Handover turns a `removed` record whose plugin is installed and current back into `plugin`, preserves a plugin already signed in under its separate `-plugin` id and skips existing migration records except a `failed` record old enough to retry.
|
||||
|
||||
There is no move back from the GUI or the CLI: `provider/moveback` and `magpie plugins move-back` answer that the built-ins are retired. `MoveBack` remains for removing or turning off a plugin. It restores built-in ownership and records `back` (with `Left`). Compatible accounts added through the plugin can return too; incompatible accounts can remain with the plugin under its separate id. Removing or disabling a plugin first moves affected built-ins back and stops if that fails. A package installed by the user remains after moving back; a migration-installed package is removed only when no remaining signed-in or moved provider needs it.
|
||||
There is no move back: `provider/moveback` and `magpie plugins move-back` answer that the built-ins are retired, and removing or turning off the plugin doesn't move anything back either. Turned off, the plugin keeps the subscription `plugin`, off with it. Removed (`RemovePlugin`), the subscription is recorded `removed`, its accounts kept in the plugin's auth store, since `plugin.Remove` leaves sign-ins until the user signs out. The Plugins page and `magpie plugins rm`/`off` say the subscription stops working and its accounts are kept. Rolling back a failed move (`putBack`, through each mover's `back` and `settle`) is the only path that writes to the built-in's logins.
|
||||
|
||||
Removing a plugin also sets aside the model picks its providers left in `providers.json` (#2057, `plugin_left.go`). Before, nothing listed those records and `magpie provider rm` couldn't find them, so sync and backup asked for a sign-in to them on every run (`NotSignedIn`).
|
||||
|
||||
|
||||
@@ -67,14 +67,14 @@ func pluginCmd(args []string) error {
|
||||
return errors.New(pluginUsage)
|
||||
}
|
||||
rest[0] = installedName(rest[0])
|
||||
back := provider.MovedOnto(rest[0])
|
||||
moved := provider.MovedOnto(rest[0])
|
||||
if err := provider.RemovePlugin(ctx, rest[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range back {
|
||||
fmt.Println(green.Render("✓"), id, "is back on its built-in")
|
||||
}
|
||||
fmt.Println(green.Render("✓"), "removed", rest[0])
|
||||
for _, id := range moved {
|
||||
fmt.Println(faint.Render(" " + id + " is off until the plugin is added again; its accounts are kept"))
|
||||
}
|
||||
return nil
|
||||
case "update", "upgrade":
|
||||
if err := plugin.Update(ctx); err != nil {
|
||||
@@ -88,17 +88,15 @@ func pluginCmd(args []string) error {
|
||||
return errors.New(pluginUsage)
|
||||
}
|
||||
rest[0] = installedName(rest[0])
|
||||
var back []string
|
||||
if sub == "off" {
|
||||
back = provider.MovedOnto(rest[0])
|
||||
}
|
||||
if err := provider.SetPluginOff(ctx, rest[0], sub == "off"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range back {
|
||||
fmt.Println(green.Render("✓"), id, "is back on its built-in")
|
||||
}
|
||||
fmt.Println(green.Render("✓"), rest[0], "is", sub)
|
||||
if sub == "off" {
|
||||
for _, id := range provider.MovedOnto(rest[0]) {
|
||||
fmt.Println(faint.Render(" " + id + " is off with it; its accounts are kept"))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
case "options", "config":
|
||||
if len(rest) < 1 || len(rest) > 2 {
|
||||
|
||||
+12
-20
@@ -2267,11 +2267,9 @@ const I18N = {
|
||||
"Removes the plugin and what it installed; its sign-ins are kept until you sign out": "移除插件及其安装的内容;它的登录会保留,直到你退出登录",
|
||||
"Serves {names}": "承载 {names}",
|
||||
"{names} runs on this plugin in place of the built-in": "{names} 正由此插件代替内置运行",
|
||||
"{names} goes back to the built-in, with its accounts, before the plugin is removed.": "移除插件前,{names} 会带着它的账号回到内置的。",
|
||||
"{names} goes back to the built-in, with its accounts, before the plugin is switched off.": "停用插件前,{names} 会带着它的账号回到内置的。",
|
||||
"{names} goes back to the built-in first, then the plugin is removed": "先把 {names} 移回内置的,再移除插件",
|
||||
"{names} is back on the built-in": "{names} 已回到内置的",
|
||||
"Moving back…": "正在移回…",
|
||||
"{names} stops working until the plugin is installed again. Its accounts are kept.": "{names} 会停用,直到再次安装插件。它的账号会保留。",
|
||||
"{names} stops working while the plugin is off. Its accounts are kept.": "插件停用期间,{names} 也会停用。它的账号会保留。",
|
||||
"{names} runs on this plugin: removed, it stops working until the plugin is installed again": "{names} 运行在这个插件上:移除后,它会停用,直到再次安装插件",
|
||||
"Add a plugin": "添加插件",
|
||||
"Downloading Bun {v}, which plugins run on, then installing…": "正在下载插件运行所需的 Bun {v},然后安装…",
|
||||
"Couldn't ask the plugins: {error}": "无法读取插件:{error}",
|
||||
@@ -5923,11 +5921,9 @@ const I18N = {
|
||||
"Removes the plugin and what it installed; its sign-ins are kept until you sign out": "移除外掛及其安裝的內容;它的登入會保留,直到你退出登入",
|
||||
"Serves {names}": "承載 {names}",
|
||||
"{names} runs on this plugin in place of the built-in": "{names} 正由此外掛代替內建執行",
|
||||
"{names} goes back to the built-in, with its accounts, before the plugin is removed.": "移除外掛前,{names} 會帶著它的帳號回到內建的。",
|
||||
"{names} goes back to the built-in, with its accounts, before the plugin is switched off.": "停用外掛前,{names} 會帶著它的帳號回到內建的。",
|
||||
"{names} goes back to the built-in first, then the plugin is removed": "先把 {names} 移回內建的,再移除外掛",
|
||||
"{names} is back on the built-in": "{names} 已回到內建的",
|
||||
"Moving back…": "正在移回…",
|
||||
"{names} stops working until the plugin is installed again. Its accounts are kept.": "{names} 會停用,直到再次安裝外掛。它的帳號會保留。",
|
||||
"{names} stops working while the plugin is off. Its accounts are kept.": "外掛停用期間,{names} 也會停用。它的帳號會保留。",
|
||||
"{names} runs on this plugin: removed, it stops working until the plugin is installed again": "{names} 執行在這個外掛上:移除後,它會停用,直到再次安裝外掛",
|
||||
"Add a plugin": "新增外掛",
|
||||
"Downloading Bun {v}, which plugins run on, then installing…": "正在下載外掛執行所需的 Bun {v},然後安裝…",
|
||||
"Couldn't ask the plugins: {error}": "無法讀取外掛:{error}",
|
||||
@@ -9602,11 +9598,9 @@ const I18N = {
|
||||
"Removes the plugin and what it installed; its sign-ins are kept until you sign out": "プラグインとそれがインストールしたものを削除します。サインインはサインアウトするまで残ります",
|
||||
"Serves {names}": "{names} を提供",
|
||||
"{names} runs on this plugin in place of the built-in": "{names} は組み込みの代わりにこのプラグインで動作しています",
|
||||
"{names} goes back to the built-in, with its accounts, before the plugin is removed.": "プラグインを削除する前に、{names} はアカウントごと組み込みに戻ります。",
|
||||
"{names} goes back to the built-in, with its accounts, before the plugin is switched off.": "プラグインをオフにする前に、{names} はアカウントごと組み込みに戻ります。",
|
||||
"{names} goes back to the built-in first, then the plugin is removed": "先に {names} を組み込みに戻し、その後プラグインを削除します",
|
||||
"{names} is back on the built-in": "{names} は組み込みに戻りました",
|
||||
"Moving back…": "戻しています…",
|
||||
"{names} stops working until the plugin is installed again. Its accounts are kept.": "{names} はプラグインを再インストールするまで使えなくなります。アカウントは保持されます。",
|
||||
"{names} stops working while the plugin is off. Its accounts are kept.": "プラグインがオフの間、{names} は使えません。アカウントは保持されます。",
|
||||
"{names} runs on this plugin: removed, it stops working until the plugin is installed again": "{names} はこのプラグインで動いています。削除すると、再インストールするまで使えなくなります",
|
||||
"Add a plugin": "プラグインを追加",
|
||||
"Downloading Bun {v}, which plugins run on, then installing…": "プラグインの実行に使う Bun {v} をダウンロードしてからインストールします…",
|
||||
"Couldn't ask the plugins: {error}": "プラグインを取得できませんでした:{error}",
|
||||
@@ -13291,11 +13285,9 @@ const I18N = {
|
||||
"Removes the plugin and what it installed; its sign-ins are kept until you sign out": "Entfernt die Erweiterung und alles, was sie installiert hat; ihre Anmeldungen bleiben bis zur Abmeldung erhalten",
|
||||
"Serves {names}": "Stellt {names} bereit",
|
||||
"{names} runs on this plugin in place of the built-in": "{names} läuft über diese Erweiterung statt über die integrierte Version",
|
||||
"{names} goes back to the built-in, with its accounts, before the plugin is removed.": "{names} wechselt samt Konten zur integrierten Version zurück, bevor die Erweiterung entfernt wird.",
|
||||
"{names} goes back to the built-in, with its accounts, before the plugin is switched off.": "{names} wechselt samt Konten zur integrierten Version zurück, bevor die Erweiterung ausgeschaltet wird.",
|
||||
"{names} goes back to the built-in first, then the plugin is removed": "{names} wechselt zuerst zur integrierten Version zurück, dann wird die Erweiterung entfernt",
|
||||
"{names} is back on the built-in": "{names} läuft wieder über die integrierte Version",
|
||||
"Moving back…": "Wird zurückverschoben…",
|
||||
"{names} stops working until the plugin is installed again. Its accounts are kept.": "{names} funktioniert erst wieder, wenn die Erweiterung erneut installiert ist. Die Konten bleiben erhalten.",
|
||||
"{names} stops working while the plugin is off. Its accounts are kept.": "{names} funktioniert nicht, solange die Erweiterung ausgeschaltet ist. Die Konten bleiben erhalten.",
|
||||
"{names} runs on this plugin: removed, it stops working until the plugin is installed again": "{names} läuft über diese Erweiterung: Entfernt funktioniert es erst wieder, wenn sie erneut installiert ist",
|
||||
"Add a plugin": "Erweiterung hinzufügen",
|
||||
"Downloading Bun {v}, which plugins run on, then installing…": "Bun {v} für Erweiterungen wird heruntergeladen, danach wird installiert…",
|
||||
"Couldn't ask the plugins: {error}": "Erweiterungen konnten nicht abgefragt werden: {error}",
|
||||
|
||||
@@ -1103,8 +1103,9 @@
|
||||
c.title = (isGit(e.spec) ? t("{head} is the newest commit of {repo}", { head: ck.head || e.head || "", repo: e.spec }) : t("v{v} is the newest on npm", { v: e.version })) + "\n" + t("Last checked {time}", { time: checkedAt() });
|
||||
nm.append(c);
|
||||
}
|
||||
// the built-in subscriptions moved onto it: taking it away moves them
|
||||
// back, so the row says it carries them
|
||||
// the built-in subscriptions moved onto it: taking it away or off
|
||||
// stops them, as there is no built-in to go back to, so the row says
|
||||
// it carries them
|
||||
const moved = (e.moved || []).map((id) => SUBS.find((x) => x.agent === id)?.name || id);
|
||||
const names = moved.join(t(", "));
|
||||
if (moved.length && !e.off) {
|
||||
@@ -1145,7 +1146,7 @@
|
||||
sub.textContent = t("{names} can run on it again, with the same accounts.", { names: cands.map((c) => c.name).join(t(", ")) });
|
||||
sub.classList.add("ask");
|
||||
} else if (ask) {
|
||||
sub.textContent = t(ask === "remove" ? "{names} goes back to the built-in, with its accounts, before the plugin is removed." : "{names} goes back to the built-in, with its accounts, before the plugin is switched off.", { names });
|
||||
sub.textContent = t(ask === "remove" ? "{names} stops working until the plugin is installed again. Its accounts are kept." : "{names} stops working while the plugin is off. Its accounts are kept.", { names });
|
||||
sub.classList.add("ask");
|
||||
} else if (e.off) sub.textContent = t("Off");
|
||||
else if (e.error) { sub.textContent = t("Didn't load: {error}", { error: e.error }); sub.classList.add("bad"); sub.title = e.error; }
|
||||
@@ -1213,12 +1214,11 @@
|
||||
s.onclick = () => pluginSignIn(out.id);
|
||||
val.append(s);
|
||||
}
|
||||
const back = () => { if (moved.length) status(t("{names} is back on the built-in", { names }), "ok"); };
|
||||
// switched on again: the built-ins that went back when it went off
|
||||
// are offered its way again, in the row
|
||||
// switched on again: a built-in it can carry is offered its way, in
|
||||
// the row
|
||||
const again = () => { if (movableOf(pkg).length) { asking = { pkg, op: "move" }; draw(); } };
|
||||
const off = () => act(pkg, "off", { spec: e.spec, off: !e.off }, e.off ? again : back);
|
||||
const remove = () => act(pkg, "remove", { spec: e.spec }, () => { status(t("{name} removed", { name: l?.name || pkg }), "ok"); back(); });
|
||||
const off = () => act(pkg, "off", { spec: e.spec, off: !e.off }, e.off ? again : null);
|
||||
const remove = () => act(pkg, "remove", { spec: e.spec }, () => status(t("{name} removed", { name: l?.name || pkg }), "ok"));
|
||||
if (ask) {
|
||||
// asked in the row, not a dialog: the user sees which go back
|
||||
const yes = el("button", "text primary", t(ask === "move" ? "Move" : ask === "remove" ? "Remove" : "Switch off"));
|
||||
@@ -1241,11 +1241,11 @@
|
||||
};
|
||||
val.append(o);
|
||||
}
|
||||
const onoff = el("button", "text", b === "off" && !e.off && moved.length ? t("Moving back…") : t(e.off ? "Switch on" : "Switch off"));
|
||||
const onoff = el("button", "text", t(e.off ? "Switch on" : "Switch off"));
|
||||
onoff.disabled = !!b;
|
||||
onoff.onclick = () => { if (!e.off && moved.length) { asking = { pkg, op: "off" }; draw(); } else off(); };
|
||||
const rm = el("button", "text quiet", b === "remove" ? t(moved.length ? "Moving back…" : "Removing…") : t("Remove"));
|
||||
rm.title = moved.length ? t("{names} goes back to the built-in first, then the plugin is removed", { names }) : t("Removes the plugin and what it installed; its sign-ins are kept until you sign out");
|
||||
const rm = el("button", "text quiet", b === "remove" ? t("Removing…") : t("Remove"));
|
||||
rm.title = moved.length ? t("{names} runs on this plugin: removed, it stops working until the plugin is installed again", { names }) : t("Removes the plugin and what it installed; its sign-ins are kept until you sign out");
|
||||
rm.disabled = busy.size > 0;
|
||||
rm.onclick = async () => {
|
||||
if (moved.length) {
|
||||
|
||||
@@ -106,8 +106,8 @@ type pluginEntryJSON struct {
|
||||
// differ, whatever the version in its package.json says
|
||||
Commit string `json:"commit,omitempty"`
|
||||
Head string `json:"head,omitempty"`
|
||||
// Moved are the built-in subscriptions moved onto it, which go back
|
||||
// to themselves when it is removed or turned off
|
||||
// Moved are the built-in subscriptions moved onto it, which stop
|
||||
// with it when it is removed or turned off (no built-in to go back to)
|
||||
Moved []string `json:"moved"`
|
||||
// AutoUpdated is the update magpie made to it by itself lately
|
||||
AutoUpdated *plugin.Updated `json:"autoUpdated,omitempty"`
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||||
// A plugin a built-in subscription is moved onto says so in Installed, and
|
||||
// removing it or switching it off asks first, in the row, naming the
|
||||
// subscription that goes back to the built-in with its accounts; only a
|
||||
// yes sends the request. English and Chinese; the API is faked here.
|
||||
// subscription that stops working with it (there is no built-in to go
|
||||
// back to) and saying its accounts are kept; only a yes sends the request.
|
||||
// English and Chinese; the API is faked here.
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs/promises");
|
||||
const path = require("node:path");
|
||||
@@ -55,10 +56,10 @@ function server(lang, asked) {
|
||||
|
||||
const L = {
|
||||
en: { installed: "Installed", serves: "Serves Zed", off: "Switch off", remove: "Remove", cancel: "Cancel",
|
||||
askOff: "Zed goes back to the built-in, with its accounts, before the plugin is switched off.",
|
||||
askRemove: "Zed goes back to the built-in, with its accounts, before the plugin is removed.", back: "Zed is back on the built-in" },
|
||||
askOff: "Zed stops working while the plugin is off. Its accounts are kept.",
|
||||
askRemove: "Zed stops working until the plugin is installed again. Its accounts are kept.", removed: "removed" },
|
||||
zh: { installed: "已安装", serves: "承载 Zed", off: "停用", remove: "移除", cancel: "取消",
|
||||
askOff: "停用插件前,Zed 会带着它的账号回到内置的。", askRemove: "移除插件前,Zed 会带着它的账号回到内置的。", back: "Zed 已回到内置的" },
|
||||
askOff: "插件停用期间,Zed 也会停用。它的账号会保留。", askRemove: "Zed 会停用,直到再次安装插件。它的账号会保留。", removed: "已移除" },
|
||||
};
|
||||
|
||||
const shot = async (loc, name) => {
|
||||
@@ -100,13 +101,14 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
|
||||
await zed.locator(".sub.ask").waitFor({ state: "detached" });
|
||||
assert.deepEqual(asked, [], "nothing sent before a yes");
|
||||
|
||||
// removing asks too; yes moves Zed back and removes the plugin
|
||||
// removing asks too; yes removes the plugin, nothing moved back
|
||||
await zed.locator("button", { hasText: new RegExp("^" + w.remove + "$") }).click();
|
||||
await zed.locator(".sub.ask", { hasText: w.askRemove }).waitFor();
|
||||
await zed.locator("button.primary", { hasText: w.remove }).click();
|
||||
await page.waitForFunction(() => document.querySelectorAll("#view-plugins .pm-row").length === 1);
|
||||
assert.deepEqual(asked, [["remove", { spec: ZED + "@0.1.2" }]]);
|
||||
await page.locator("#status", { hasText: w.back }).waitFor();
|
||||
await page.locator("#status", { hasText: w.removed }).waitFor();
|
||||
assert.doesNotMatch(await page.locator("#status").innerText(), /built-in|内置/);
|
||||
assert.equal(await view.evaluate((e) => e.scrollTop), top, "a click doesn't move the page");
|
||||
assert.deepEqual(asked.filter(([k]) => k === "fetched"), []);
|
||||
assert.deepEqual(errors, []);
|
||||
|
||||
+51
-216
@@ -53,8 +53,12 @@ import (
|
||||
const (
|
||||
MoveMoving = "moving" // under way, or cut short (put back at the next try)
|
||||
MovePlugin = "plugin" // the plugin has the id
|
||||
MovedBack = "back" // moved back to the built-in: moved again by itself, unless Left
|
||||
MovedBack = "back" // moved back to the built-in by an older magpie: moved again by itself
|
||||
MoveFailed = "failed" // the last try failed: the built-in carries on
|
||||
// MoveRemoved is a moved one whose plugin the user removed: its
|
||||
// accounts stay with the plugin's sign-ins, and the plugin installed
|
||||
// again has it at once (HandOver). Nothing installs it by itself.
|
||||
MoveRemoved = "removed"
|
||||
)
|
||||
|
||||
// retiring are the built-ins magpie moves onto their plugins by itself:
|
||||
@@ -77,12 +81,9 @@ type Migration struct {
|
||||
Backup []savedLogin `json:"backup,omitempty"`
|
||||
// Kept is what else of the built-in's the move set aside (Kiro's key).
|
||||
Kept json.RawMessage `json:"kept,omitempty"`
|
||||
// Installed is whether the move installed the plugin: going back takes
|
||||
// it away again when it serves nothing else.
|
||||
// Installed is whether the move installed the plugin: a failed move
|
||||
// takes it away again.
|
||||
Installed bool `json:"installed,omitempty"`
|
||||
// Left is a move back made by removing the plugin or turning it off:
|
||||
// the user's own choice, which no move undoes by itself.
|
||||
Left bool `json:"left,omitempty"`
|
||||
// Host is the built-in's API host, which the plugin's provider shows.
|
||||
Host string `json:"host,omitempty"`
|
||||
// Why is a failed move's reason, for the page to say in the user's
|
||||
@@ -512,7 +513,8 @@ func Move(ctx context.Context, id string) error {
|
||||
|
||||
// Adopt puts the built-in id onto its plugin before any account of it is
|
||||
// signed in: the plugin installed, and the subscription signs in through
|
||||
// it from now on, as a moved one does. One with accounts is moved instead.
|
||||
// it from now on, as a moved one does. One with accounts is moved instead,
|
||||
// and one whose plugin the user removed is the plugin's again.
|
||||
func Adopt(ctx context.Context, id string) error {
|
||||
if settings.AccountDiscoveryOff() {
|
||||
return errAccountDiscoveryOff
|
||||
@@ -544,7 +546,14 @@ func Adopt(ctx context.Context, id string) error {
|
||||
}
|
||||
if err == nil {
|
||||
err = setMigration(id, func(m *Migration) {
|
||||
*m = Migration{State: MovePlugin, Package: mv.pkg, At: time.Now().UTC().Truncate(time.Second), Installed: !had}
|
||||
at := time.Now().UTC().Truncate(time.Second)
|
||||
// one whose plugin the user removed: its accounts are with
|
||||
// the plugin's sign-ins still, and the record says which
|
||||
if m.State == MoveRemoved {
|
||||
m.State, m.At, m.Installed = MovePlugin, at, !had
|
||||
return
|
||||
}
|
||||
*m = Migration{State: MovePlugin, Package: mv.pkg, At: at, Installed: !had}
|
||||
})
|
||||
}
|
||||
if err != nil && !had {
|
||||
@@ -892,186 +901,10 @@ func handBack(ctx context.Context, id string, mv *mover, accts []movedAccount, k
|
||||
return users, nil, nil
|
||||
}
|
||||
|
||||
// MoveBack gives the id back to the built-in: its accounts as they were
|
||||
// set aside, with what the plugin has of them now (newer tokens, accounts
|
||||
// signed in to since), in the plugin's order; then the plugin signs out.
|
||||
func MoveBack(ctx context.Context, id string) error {
|
||||
return moveBack(ctx, id, true)
|
||||
}
|
||||
|
||||
// moveBack is MoveBack; tidy takes away a plugin the move installed that
|
||||
// serves nothing else now, which releasePlugin, about to remove or turn
|
||||
// off the plugin itself, leaves to its caller.
|
||||
func moveBack(ctx context.Context, id string, tidy bool) error {
|
||||
if settings.AccountDiscoveryOff() {
|
||||
return errAccountDiscoveryOff
|
||||
}
|
||||
mv := movers[id]
|
||||
if mv == nil {
|
||||
return fmt.Errorf("%s isn't a built-in subscription", id)
|
||||
}
|
||||
unlock, err := lockMoves()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer unlock()
|
||||
m, ok := MigrationOf(id)
|
||||
if !ok || m.State != MovePlugin {
|
||||
return nil
|
||||
}
|
||||
var order []plugin.Account
|
||||
if pp, ok := pluginOfAgent("plugin:" + id); ok {
|
||||
for _, l := range pluginLogins(pp) {
|
||||
order = append(order, l.acct)
|
||||
}
|
||||
}
|
||||
keys := slices.Collect(maps.Keys(plugin.Auths(id)))
|
||||
sort.SliceStable(keys, func(i, j int) bool {
|
||||
if a, b := indexOfKey(order, keys[i]), indexOfKey(order, keys[j]); a != b {
|
||||
return a < b
|
||||
}
|
||||
return keys[i] < keys[j]
|
||||
})
|
||||
onOf := pluginOn(id)
|
||||
// the backup first, for back to write into the accounts it came from
|
||||
prep := func(ls []savedLogin, _ map[string]string) []savedLogin {
|
||||
for _, b := range m.Backup {
|
||||
// the agent's own sign-in is one row: one written while moved
|
||||
// (the agent signed in to another account meanwhile) gives
|
||||
// way to the one set aside, which takes the agent's user anew
|
||||
if b.own() {
|
||||
ls = slices.DeleteFunc(ls, func(l savedLogin) bool { return l.Agent == b.Agent && l.own() && !sameMoved(l, b) })
|
||||
}
|
||||
if !slices.ContainsFunc(ls, func(l savedLogin) bool { return sameMoved(l, b) }) {
|
||||
ls = append(ls, b)
|
||||
}
|
||||
}
|
||||
return ls
|
||||
}
|
||||
arrange := func(ls []savedLogin, users map[string]string) []savedLogin {
|
||||
// an account the vendor refused through the plugin goes back
|
||||
// marked, as the built-in had marked it: back writes the plugin's
|
||||
// sign-in as one just made, but the plugin's mark is the newer word
|
||||
lapsed := map[string]string{}
|
||||
for _, l := range ls {
|
||||
if u := users[l.Home]; l.Agent == "plugin:"+id && u != "" && l.Lapsed != "" {
|
||||
lapsed[strings.ToLower(u)] = l.Lapsed
|
||||
}
|
||||
}
|
||||
// the plugin's rows of the accounts going back go with them, or
|
||||
// the accounts page lists each twice, the plugin's copy empty
|
||||
ls = slices.DeleteFunc(ls, func(l savedLogin) bool {
|
||||
_, back := users[l.Home]
|
||||
return l.Agent == "plugin:"+id && (back || len(keys) == 0)
|
||||
})
|
||||
firstUser := ""
|
||||
for i, k := range keys {
|
||||
user := users[k]
|
||||
if user == "" {
|
||||
continue
|
||||
}
|
||||
if firstUser == "" && i == 0 {
|
||||
firstUser = user
|
||||
}
|
||||
for j := range ls {
|
||||
if slices.Contains(mv.agents, ls[j].Agent) && strings.EqualFold(ls[j].User, user) {
|
||||
if on, ok := onOf[k]; ok {
|
||||
ls[j].On = on
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for j := range ls {
|
||||
if t := lapsed[strings.ToLower(ls[j].User)]; t != "" && slices.Contains(mv.agents, ls[j].Agent) && !ls[j].own() {
|
||||
ls[j].Lapsed = t
|
||||
}
|
||||
}
|
||||
if firstUser != "" {
|
||||
for j := range ls {
|
||||
if slices.Contains(mv.agents, ls[j].Agent) {
|
||||
ls[j].First = strings.EqualFold(ls[j].User, firstUser) && !ls[j].own()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ls
|
||||
}
|
||||
// the backup goes back even with no account left in the plugin
|
||||
if len(keys) == 0 {
|
||||
loginsMu.Lock()
|
||||
err := writeLogins(arrange(prep(readLogins(), nil), nil))
|
||||
loginsMu.Unlock()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_, errs, err := handBack(ctx, id, mv, m.Accounts, keys, nil, prep, arrange)
|
||||
if err != nil {
|
||||
tidyMoved(id)
|
||||
return err
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
// the plugin keeps the id and every account
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
}
|
||||
if mv.give != nil && len(m.Kept) > 0 {
|
||||
if err := mv.give(m.Kept); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := setMigration(id, func(m *Migration) {
|
||||
*m = Migration{State: MovedBack, Package: mv.pkg, At: time.Now().UTC().Truncate(time.Second), Left: !tidy}
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if tidy && m.Installed && !servesAnything(mv.pkg) {
|
||||
if err := removePlugin(context.WithoutCancel(ctx), mv.pkg); err != nil {
|
||||
log.Printf("removing %s, installed by %s's move: %s", mv.pkg, id, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// servesAnything is whether the plugin package has an account signed in
|
||||
// to any of its providers, or a built-in moved onto it.
|
||||
func servesAnything(pkg string) bool {
|
||||
for _, p := range plugin.Cached() {
|
||||
if plugin.PackageName(p.Spec) == pkg && len(plugin.Auths(p.ID)) > 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, id := range OnPlugins() {
|
||||
if m, _ := MigrationOf(id); m.Package == pkg {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func indexOfKey(order []plugin.Account, key string) int {
|
||||
if i := slices.IndexFunc(order, func(a plugin.Account) bool { return a.Key == key }); i >= 0 {
|
||||
return i
|
||||
}
|
||||
return len(order)
|
||||
}
|
||||
|
||||
// pluginOn is which of the plugin provider id's accounts are on.
|
||||
func pluginOn(id string) map[string]bool {
|
||||
out := map[string]bool{}
|
||||
loginsMu.Lock()
|
||||
defer loginsMu.Unlock()
|
||||
for _, l := range readLogins() {
|
||||
if l.Agent == "plugin:"+id {
|
||||
out[l.Home] = l.On || l.First
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// MoveRetiring moves the built-ins being retired that have accounts, at
|
||||
// start-up: once, or again moveRetry after one failed. One moved back is
|
||||
// moved again (the way back is gone from the editor and the command line),
|
||||
// except one moved back by the user taking its plugin away or off.
|
||||
// start-up: once, or again moveRetry after one failed. One an older magpie
|
||||
// moved back is moved again: there is no way back, as the built-in is
|
||||
// going. One whose plugin the user removed waits for them to install it.
|
||||
func MoveRetiring(ctx context.Context) map[string]error {
|
||||
if settings.AccountDiscoveryOff() {
|
||||
return nil
|
||||
@@ -1083,7 +916,7 @@ func MoveRetiring(ctx context.Context) map[string]error {
|
||||
case ok && m.State == MovePlugin:
|
||||
tidyMoved(id)
|
||||
continue
|
||||
case ok && m.State == MovedBack && m.Left:
|
||||
case ok && m.State == MoveRemoved:
|
||||
continue
|
||||
case ok && m.State == MoveFailed && time.Since(m.At) < moveRetry:
|
||||
continue
|
||||
@@ -1106,8 +939,9 @@ func MoveRetiring(ctx context.Context) map[string]error {
|
||||
// back on any failure, the built-in carrying on). One the plugin has
|
||||
// accounts of its own for already is left as it is, since its models are
|
||||
// in use as <id>-plugin's and the id changing would leave agents asking
|
||||
// for a provider gone; the add sheet lists it once regardless. One the
|
||||
// user moved back stays built-in, and a failed move waits moveRetry. No
|
||||
// for a provider gone; the add sheet lists it once regardless. One moved
|
||||
// whose plugin the user removed is the plugin's again once they install it,
|
||||
// its accounts still with the plugin, and a failed move waits moveRetry. No
|
||||
// plugin is installed for it: the user's own install is what says to.
|
||||
func HandOver(ctx context.Context, accounts bool) map[string]error {
|
||||
if settings.AccountDiscoveryOff() {
|
||||
@@ -1118,7 +952,16 @@ func HandOver(ctx context.Context, accounts bool) map[string]error {
|
||||
asked := false
|
||||
for _, id := range MovableIDs() {
|
||||
mv := movers[id]
|
||||
if m, ok := MigrationOf(id); ok && (m.State != MoveFailed || time.Since(m.At) < moveRetry) {
|
||||
m, ok := MigrationOf(id)
|
||||
if ok && m.State == MoveRemoved {
|
||||
if pluginReady(mv) {
|
||||
out[id] = setMigration(id, func(m *Migration) {
|
||||
m.State, m.At, m.Installed = MovePlugin, time.Now().UTC().Truncate(time.Second), false
|
||||
})
|
||||
}
|
||||
continue
|
||||
}
|
||||
if ok && (m.State != MoveFailed || time.Since(m.At) < moveRetry) {
|
||||
continue
|
||||
}
|
||||
if !pluginReady(mv) {
|
||||
@@ -1224,31 +1067,27 @@ func MovedOnto(name string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// releasePlugin moves the built-ins on the plugin named back first, so
|
||||
// taking the plugin away leaves none of them without its accounts.
|
||||
func releasePlugin(ctx context.Context, name string) error {
|
||||
for _, id := range MovedOnto(name) {
|
||||
if err := moveBack(ctx, id, false); err != nil {
|
||||
return fmt.Errorf("moving %s back to the built-in: %w", id, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemovePlugin removes the plugin named, the built-ins moved onto it moved
|
||||
// back to themselves first, and sets aside the model picks of its
|
||||
// providers it leaves in providers.json (plugin_left.go).
|
||||
// RemovePlugin removes the plugin named and sets aside the model picks of
|
||||
// its providers it leaves in providers.json (plugin_left.go). A built-in
|
||||
// moved onto it isn't moved back, as the built-in is going: it is
|
||||
// MoveRemoved, its accounts kept with the plugin's sign-ins for when the
|
||||
// plugin is installed again.
|
||||
func RemovePlugin(ctx context.Context, name string) error {
|
||||
if err := releasePlugin(ctx, name); err != nil {
|
||||
return err
|
||||
}
|
||||
// what it served is read while it is still on the list
|
||||
moved := MovedOnto(name)
|
||||
spec := installedSpec(name)
|
||||
ids, known := pluginRecordIDs(spec)
|
||||
pkg := plugin.PackageName(spec)
|
||||
if err := plugin.Remove(ctx, name); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range moved {
|
||||
if err := setMigration(id, func(m *Migration) {
|
||||
m.State, m.At = MoveRemoved, time.Now().UTC().Truncate(time.Second)
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s is removed; %s still reads as on it: %w", name, id, err)
|
||||
}
|
||||
}
|
||||
if spec == "" || !known {
|
||||
return nil
|
||||
}
|
||||
@@ -1258,13 +1097,9 @@ func RemovePlugin(ctx context.Context, name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetPluginOff turns the plugin named off, the built-ins moved onto it
|
||||
// moved back to themselves first, or back on.
|
||||
func SetPluginOff(ctx context.Context, name string, off bool) error {
|
||||
if off {
|
||||
if err := releasePlugin(ctx, name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// SetPluginOff turns the plugin named off, or back on. A built-in moved
|
||||
// onto it stays moved, off with it: nothing moves it back, as the built-in
|
||||
// is going.
|
||||
func SetPluginOff(_ context.Context, name string, off bool) error {
|
||||
return plugin.SetOff(name, off)
|
||||
}
|
||||
|
||||
@@ -11,8 +11,9 @@ import (
|
||||
// (ARNO on Discord: Qoder CN listed twice, the built-in and the plugin's,
|
||||
// once the Qoder plugin served it): one with no accounts at once, one with
|
||||
// accounts only when asked to move them, through Move. One the plugin is
|
||||
// signed in to under its own id already stays as it is, as does one the
|
||||
// user moved back, and nothing happens while the plugin isn't installed.
|
||||
// signed in to under its own id already stays as it is; one moved whose
|
||||
// plugin the user removed is the plugin's again once it is installed, and
|
||||
// nothing happens while the plugin isn't installed.
|
||||
func TestHandOver(t *testing.T) {
|
||||
ctx, abs, installs := fakePlugin(t)
|
||||
reset := func(ls ...savedLogin) {
|
||||
@@ -35,15 +36,19 @@ func TestHandOver(t *testing.T) {
|
||||
if out := HandOver(ctx, false); out["fakeco"] != nil || !Moved("fakeco") {
|
||||
t.Fatalf("no accounts: %v, moved %v", out, Moved("fakeco"))
|
||||
}
|
||||
// moved back by the user: it stays built-in
|
||||
if err := MoveBack(ctx, "fakeco"); err != nil {
|
||||
// the user removed the plugin: nothing moves or installs it until they
|
||||
// install it again, and then it is the plugin's at once
|
||||
if err := RemovePlugin(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out := HandOver(ctx, true); len(out) != 0 || Moved("fakeco") {
|
||||
t.Fatalf("moved back: %v, moved %v", out, Moved("fakeco"))
|
||||
if out := HandOver(ctx, true); len(out) != 0 || Moved("fakeco") || pluginListed(abs) {
|
||||
t.Fatalf("plugin removed: %v, moved %v, listed %v", out, Moved("fakeco"), pluginListed(abs))
|
||||
}
|
||||
if !pluginListed(abs) {
|
||||
t.Fatal("moving back took away the plugin the user installed")
|
||||
if _, err := plugin.Add(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out := HandOver(ctx, false); out["fakeco"] != nil || !Moved("fakeco") {
|
||||
t.Fatalf("installed again: %v, moved %v", out, Moved("fakeco"))
|
||||
}
|
||||
|
||||
// the built-in has an account: moved only when accounts are asked for
|
||||
@@ -57,9 +62,6 @@ func TestHandOver(t *testing.T) {
|
||||
|
||||
// the plugin signed in on its own, the built-in not moved: left as it
|
||||
// is, its models in use under the plugin's own id
|
||||
if err := MoveBack(ctx, "fakeco"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reset()
|
||||
if _, err := plugin.Import(ctx, "fakeco", map[string]any{"type": "oauth", "refresh": "r-b", "access": "a", "expires": 9e15, "accountId": "b@fake"}); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -2,7 +2,6 @@ package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -14,60 +13,6 @@ import (
|
||||
"github.com/yetone/magpie/internal/testenv"
|
||||
)
|
||||
|
||||
// An account the vendor refused while it ran on the plugin goes back to
|
||||
// the built-in marked, though back writes its sign-in as one just made
|
||||
// (every real mover clears the mark there): the built-in had marked it
|
||||
// the same way, and it would show signed in until its next refusal.
|
||||
func TestMoveBackKeepsLapse(t *testing.T) {
|
||||
bun := testenv.Bun(t)
|
||||
claudeHome(t)
|
||||
t.Setenv("MAGPIE_BUN", bun)
|
||||
t.Cleanup(plugin.Settle)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
|
||||
if _, err := plugin.Add(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := plugin.Providers(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fakeMover(t)
|
||||
mv := movers["fakeco"]
|
||||
mv.pkg = abs
|
||||
back := mv.back
|
||||
mv.back = func(ls []savedLogin, user string, auth map[string]any) ([]savedLogin, string, error) {
|
||||
ls, u, err := back(ls, user, auth)
|
||||
for i := range ls {
|
||||
if ls[i].Agent == "fakeco" && ls[i].User == u {
|
||||
ls[i].Lapsed = "" // as zed's, factory's and mimo's back do
|
||||
}
|
||||
}
|
||||
return ls, u, err
|
||||
}
|
||||
loginsMu.Lock()
|
||||
if err := writeLogins([]savedLogin{fakeLogin("a@fake", "r-a", true, true), fakeLogin("b@fake", "r-b", false, true)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loginsMu.Unlock()
|
||||
if err := Move(ctx, "fakeco"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m, _ := MigrationOf("fakeco")
|
||||
for _, ma := range m.Accounts {
|
||||
if ma.User == "b@fake" {
|
||||
notePluginLapse(mustPlugin(t), ma.Key, http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
if err := MoveBack(ctx, "fakeco"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := fakeSaved(t)
|
||||
if s["b@fake"].Lapsed == "" || s["a@fake"].Lapsed != "" {
|
||||
t.Fatalf("moved back: a lapsed %q, b lapsed %q", s["a@fake"].Lapsed, s["b@fake"].Lapsed)
|
||||
}
|
||||
}
|
||||
|
||||
// An account whose sign-in the vendor refuses while the move reads the
|
||||
// plugin's models (a models hook throwing signIn: expired, as Zed's does
|
||||
// on a refused model token) moves along marked, as the built-in would
|
||||
|
||||
@@ -13,10 +13,10 @@ import (
|
||||
"github.com/yetone/magpie/internal/update"
|
||||
)
|
||||
|
||||
// moveAndBack does what Move and MoveBack do with a mover's accounts, but
|
||||
// for the plugin in between: the accounts go out, the built-in's saved ones
|
||||
// go aside, extra (sign-ins made through the plugin since) join them, and
|
||||
// every one comes back. It gives the accounts that went out and the saved
|
||||
// moveAndBack does what Move, and putBack after a failed one, do with a
|
||||
// mover's accounts, but for the plugin in between: the accounts go out,
|
||||
// the built-in's saved ones go aside, extra (sign-ins made through the
|
||||
// plugin since) join them, and every one comes back. It gives the accounts that went out and the saved
|
||||
// accounts before and after.
|
||||
func moveAndBack(t *testing.T, id string, extra ...map[string]any) (out []Moving, before, after []savedLogin) {
|
||||
t.Helper()
|
||||
|
||||
@@ -209,44 +209,6 @@ func TestMoveToPlugin(t *testing.T) {
|
||||
Moved("fakeco"), len(plugin.Cached()), len(pluginUsageLogins(mustPlugin(t))), qs)
|
||||
}
|
||||
|
||||
// the user reorders on the plugin, and it renews a's token; back, the
|
||||
// built-in has both
|
||||
if err := switchPluginLogin(mustPlugin(t), "a@fake"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for k, a := range plugin.Auths("fakeco") {
|
||||
if a["accountId"] == "a@fake" {
|
||||
if _, err := plugin.Check(ctx, "fakeco", k); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := MoveBack(ctx, "fakeco"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m, _ := MigrationOf("fakeco"); m.State != MovedBack || Moved("fakeco") {
|
||||
t.Fatalf("after moving back: %+v", m)
|
||||
}
|
||||
s := fakeSaved(t)
|
||||
if len(s) != 4 || !strings.HasPrefix(fakeToken(s["a@fake"]), "rot-a++") || plugin.SignedIn("fakeco") || !s["a@fake"].First || s["b@fake"].First || !s["b@fake"].On || s["d@fake"].On {
|
||||
t.Fatalf("the built-in's accounts moved back: %+v", s)
|
||||
}
|
||||
if plugin.SignedIn("fakeco") {
|
||||
t.Fatal("the plugin kept accounts moved back")
|
||||
}
|
||||
loginsMu.Lock()
|
||||
for _, l := range readLogins() {
|
||||
if l.Agent == "plugin:fakeco" {
|
||||
t.Errorf("the plugin's row of %s stayed after moving back", l.User)
|
||||
}
|
||||
}
|
||||
loginsMu.Unlock()
|
||||
// the built-in is retired: one moved back is moved again by itself
|
||||
retiring = func() []string { return []string{"fakeco"} }
|
||||
t.Cleanup(func() { retiring = MovableIDs })
|
||||
if errs := MoveRetiring(ctx); errs["fakeco"] != nil || !Moved("fakeco") {
|
||||
t.Fatalf("MoveRetiring left one moved back built-in: %v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
// A move cut short (magpie quit mid-way) is put back before it is tried
|
||||
@@ -411,7 +373,8 @@ func TestMovedBuiltinQuiet(t *testing.T) {
|
||||
}
|
||||
|
||||
// A moved built-in's plugin is updated to the version its move needs, as
|
||||
// the built-in came up to date with magpie; one moved back isn't touched.
|
||||
// the built-in came up to date with magpie; one whose plugin the user
|
||||
// removed isn't installed again.
|
||||
func TestKeepMovedCurrent(t *testing.T) {
|
||||
claudeHome(t)
|
||||
fakeMover(t)
|
||||
@@ -421,10 +384,10 @@ func TestKeepMovedCurrent(t *testing.T) {
|
||||
asked = append(asked, pkg+"@"+min)
|
||||
return nil
|
||||
}
|
||||
_ = setMigration("fakeco", func(m *Migration) { m.State = MovedBack })
|
||||
_ = setMigration("fakeco", func(m *Migration) { m.State = MoveRemoved })
|
||||
keepMovedCurrent(context.Background())
|
||||
if len(asked) != 0 {
|
||||
t.Fatalf("one moved back was updated: %v", asked)
|
||||
t.Fatalf("one whose plugin was removed was installed: %v", asked)
|
||||
}
|
||||
_ = setMigration("fakeco", func(m *Migration) { m.State = MovePlugin })
|
||||
keepMovedCurrent(context.Background())
|
||||
@@ -521,66 +484,3 @@ func TestInstallPluginKeepsTheUsersPin(t *testing.T) {
|
||||
t.Fatalf("a plugin the user turned off was asked for: %v", asked)
|
||||
}
|
||||
}
|
||||
|
||||
// Removing the plugin a built-in is moved onto, or turning it off, moves
|
||||
// the built-in back first: its accounts go back to it rather than out of
|
||||
// sight with the plugin.
|
||||
func TestReleasePlugin(t *testing.T) {
|
||||
bun := testenv.Bun(t)
|
||||
for _, op := range []string{"off", "remove"} {
|
||||
t.Run(op, func(t *testing.T) {
|
||||
claudeHome(t)
|
||||
t.Setenv("MAGPIE_BUN", bun)
|
||||
t.Cleanup(plugin.Settle)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
|
||||
if _, err := plugin.Add(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := plugin.Providers(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fakeMover(t)
|
||||
movers["fakeco"].pkg = abs
|
||||
loginsMu.Lock()
|
||||
if err := writeLogins([]savedLogin{fakeLogin("a@fake", "r-a", true, true), fakeLogin("b@fake", "r-b", false, true)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loginsMu.Unlock()
|
||||
if err := Move(ctx, "fakeco"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := MovedOnto(abs); len(got) != 1 || got[0] != "fakeco" {
|
||||
t.Fatalf("moved onto the plugin: %v", got)
|
||||
}
|
||||
var err error
|
||||
if op == "off" {
|
||||
err = SetPluginOff(ctx, abs, true)
|
||||
} else {
|
||||
err = RemovePlugin(ctx, abs)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Moved("fakeco") {
|
||||
t.Fatal("still moved onto a plugin that is gone")
|
||||
}
|
||||
if s := fakeSaved(t); len(s) != 2 || !s["a@fake"].First {
|
||||
t.Fatalf("the built-in's accounts: %+v", s)
|
||||
}
|
||||
if op == "off" && (len(plugin.Load().Plugins) != 1 || !plugin.Load().Plugins[0].Off) {
|
||||
t.Fatalf("not turned off: %+v", plugin.Load().Plugins)
|
||||
}
|
||||
if op == "remove" && len(plugin.Load().Plugins) != 0 {
|
||||
t.Fatalf("not removed: %+v", plugin.Load().Plugins)
|
||||
}
|
||||
// the user took the plugin away: no move undoes that by itself
|
||||
retiring = func() []string { return []string{"fakeco"} }
|
||||
t.Cleanup(func() { retiring = MovableIDs })
|
||||
if errs := MoveRetiring(ctx); len(errs) != 0 || Moved("fakeco") {
|
||||
t.Fatalf("moved again onto a plugin the user took %s: %v", op, errs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +65,7 @@ func TestMoveEveryAccountLapsed(t *testing.T) {
|
||||
|
||||
// A move that installed the plugin and then failed takes it away again,
|
||||
// leaving no plugin provider saying it's signed out; one that got through
|
||||
// keeps it, and going back takes it away when it serves nothing else. A
|
||||
// plugin the user had installed stays either way.
|
||||
// keeps it. A plugin the user had installed stays either way.
|
||||
func TestMoveTidiesItsPlugin(t *testing.T) {
|
||||
ctx, abs, _ := fakePlugin(t)
|
||||
setFakeLogins(t, fakeLogin("a@fake", "r-a", true, true), fakeLogin("n@fake", "r-noimport", false, true))
|
||||
@@ -87,17 +86,13 @@ func TestMoveTidiesItsPlugin(t *testing.T) {
|
||||
if m, _ := MigrationOf("fakeco"); !m.Installed || !pluginListed(abs) {
|
||||
t.Fatalf("after moving: installed %v, listed %v", m.Installed, pluginListed(abs))
|
||||
}
|
||||
if err := MoveBack(ctx, "fakeco"); err != nil {
|
||||
if err := RemovePlugin(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pluginListed(abs) {
|
||||
t.Fatal("moving back left the plugin the move installed")
|
||||
}
|
||||
if s := fakeSaved(t); len(s) != 1 || !strings.HasPrefix(fakeToken(s["a@fake"]), "r-a") {
|
||||
t.Fatalf("the built-in's account moved back: %+v", s)
|
||||
}
|
||||
_ = plugin.SignOut(ctx, "fakeco", "")
|
||||
os.Remove(migrationsPath())
|
||||
|
||||
// the user's own plugin: a failed move leaves it, as does going back
|
||||
// the user's own plugin: a failed move leaves it
|
||||
if _, err := plugin.Add(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -110,11 +105,80 @@ func TestMoveTidiesItsPlugin(t *testing.T) {
|
||||
if err := Move(ctx, "fakeco"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := MoveBack(ctx, "fakeco"); err != nil || !pluginListed(abs) {
|
||||
t.Fatalf("moving back off the user's plugin: %v, listed %v", err, pluginListed(abs))
|
||||
if m, _ := MigrationOf("fakeco"); m.Installed || !pluginListed(abs) {
|
||||
t.Fatalf("moved onto the user's plugin: installed %v, listed %v", m.Installed, pluginListed(abs))
|
||||
}
|
||||
}
|
||||
|
||||
// There is no way back to a built-in, as each is going. One an older
|
||||
// magpie moved back is moved again by itself. The plugin turned off keeps
|
||||
// it moved, off with it; the plugin removed leaves its accounts with the
|
||||
// plugin's sign-ins, nothing moving or installing it again, and the plugin
|
||||
// installed again (or the editor's Move) has it at once, accounts and all.
|
||||
func TestNoWayBack(t *testing.T) {
|
||||
ctx, abs, installs := fakePlugin(t)
|
||||
retiring = func() []string { return []string{"fakeco"} }
|
||||
t.Cleanup(func() { retiring = MovableIDs })
|
||||
setFakeLogins(t, fakeLogin("a@fake", "r-a", true, true))
|
||||
_ = setMigration("fakeco", func(m *Migration) { *m = Migration{State: MovedBack, Package: abs} })
|
||||
if errs := MoveRetiring(ctx); errs["fakeco"] != nil || !Moved("fakeco") {
|
||||
t.Fatalf("one moved back by an older magpie: %v, moved %v", errs, Moved("fakeco"))
|
||||
}
|
||||
signedIn := func(when string) {
|
||||
t.Helper()
|
||||
if len(fakeSaved(t)) != 0 || len(plugin.Auths("fakeco")) != 1 {
|
||||
t.Fatalf("%s: built-in's %+v, plugin's %v", when, fakeSaved(t), plugin.Auths("fakeco"))
|
||||
}
|
||||
}
|
||||
|
||||
if err := SetPluginOff(ctx, abs, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !Moved("fakeco") {
|
||||
t.Fatal("turning the plugin off moved it back")
|
||||
}
|
||||
signedIn("plugin off")
|
||||
if err := SetPluginOff(ctx, abs, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := RemovePlugin(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m, _ := MigrationOf("fakeco"); m.State != MoveRemoved || Moved("fakeco") || pluginListed(abs) || len(m.Accounts) != 1 {
|
||||
t.Fatalf("plugin removed: %+v, listed %v", m, pluginListed(abs))
|
||||
}
|
||||
signedIn("plugin removed")
|
||||
was := *installs
|
||||
keepMovedCurrent(ctx)
|
||||
if errs := MoveRetiring(ctx); len(errs) != 0 || *installs != was || pluginListed(abs) {
|
||||
t.Fatalf("magpie put back a plugin the user removed: %v, %d installs", errs, *installs-was)
|
||||
}
|
||||
|
||||
if _, err := plugin.Add(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out := HandOver(ctx, false); out["fakeco"] != nil || !Moved("fakeco") {
|
||||
t.Fatalf("installed again: %v, moved %v", out, Moved("fakeco"))
|
||||
}
|
||||
if m, _ := MigrationOf("fakeco"); len(m.Accounts) != 1 || m.Installed {
|
||||
t.Fatalf("installed again by the user: %+v", m)
|
||||
}
|
||||
signedIn("installed again")
|
||||
|
||||
// removed again, then the editor's Move: Adopt, the accounts kept
|
||||
if err := RemovePlugin(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Adopt(ctx, "fakeco"); err != nil || !Moved("fakeco") || !pluginListed(abs) {
|
||||
t.Fatalf("adopted after removing: %v, moved %v", err, Moved("fakeco"))
|
||||
}
|
||||
if m, _ := MigrationOf("fakeco"); len(m.Accounts) != 1 || !m.Installed {
|
||||
t.Fatalf("adopted after removing: %+v", m)
|
||||
}
|
||||
signedIn("adopted")
|
||||
}
|
||||
|
||||
// The move tries each account through the vendor, and none holds it
|
||||
// back: one whose usage read the vendor refuses goes along marked, as a
|
||||
// lapsed one does; one the plugin couldn't reach its vendor for (its
|
||||
@@ -204,8 +268,7 @@ func TestMovedShowsBuiltinHost(t *testing.T) {
|
||||
|
||||
// A built-in with no account signed in goes onto its plugin as it is: the
|
||||
// plugin installed and the subscription its from then on, so the first
|
||||
// sign-in is the plugin's. Going back takes the plugin it installed away.
|
||||
// One with an account is moved.
|
||||
// sign-in is the plugin's. One with an account is moved.
|
||||
func TestAdoptBeforeSigningIn(t *testing.T) {
|
||||
ctx, abs, installs := fakePlugin(t)
|
||||
if err := Adopt(ctx, "fakeco"); err != nil {
|
||||
@@ -220,13 +283,7 @@ func TestAdoptBeforeSigningIn(t *testing.T) {
|
||||
if err := Adopt(ctx, "fakeco"); err != nil || *installs != 1 {
|
||||
t.Fatalf("adopting again: %v, %d installs", err, *installs)
|
||||
}
|
||||
if err := MoveBack(ctx, "fakeco"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Moved("fakeco") || pluginListed(abs) {
|
||||
t.Fatalf("back: moved %v, listed %v", Moved("fakeco"), pluginListed(abs))
|
||||
}
|
||||
|
||||
os.Remove(migrationsPath())
|
||||
setFakeLogins(t, fakeLogin("a@fake", "r-a", true, true))
|
||||
if err := Adopt(ctx, "fakeco"); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user