Files
magpie/internal/plugin/gitrev_test.go
T
yetone 7af45761f7 plugin, gui: a plugin from a git repository shows Update only when its repository has new commits (ARNO on Discord)
ARNO installed opencode-codearts-auth as github:yuweihao17/codearts. Its row
on Plugins always had an Update button, and still did right after updating
it to the newest commit. The page drew Update for every git plugin whatever
its commit, since npm has no version to compare for one, and nothing asked
the repository. ba0e2a8a fixed the same complaint for pinned npm plugins
only.

A git plugin is now compared by commit:
- internal/plugin/gitrev.go reads the commit Bun installed: the package's
  .bun-tag (owner-repo-<sha7> for GitHub's, the full sha for a clone),
  else bun.lock's "#<sha>". It asks where the spec's branch or tag (the
  default branch without #ref) is now: GitHub's API (commits?per_page=1
  [&sha=ref]) for github:o/r, o/r, https://github.com/o/r and
  git+https/git+ssh/git:// URLs on github.com; git ls-remote for gitlab:,
  bitbucket: and any other git URL (an annotated tag's commit, not the
  tag's). A spec pinned to a commit is that commit, asked of nobody.
  Answers are kept 10 minutes (GitHub allows 60 unauthenticated requests
  an hour).
- updates.go: the hourly CheckUpdates and the Check for updates button
  (CheckNow) report a git plugin as current or update, with its commit
  and the repository's head, in place of the old "git" status. A waiting
  git update is dropped once the installed commit is the head.
- GUI: /api/plugins/git gives the heads, asked after load and after an
  install or update. The row shows the commit installed. Update, the
  "New commits" chip, the Discover card's Update and Update all show only
  when the repository is at another commit. Five new strings are in zh,
  zh-TW, ja and de.
- Docs: the Updates list in site/public/docs (en, zh, ja) says how a git
  plugin's update is told.

Tarball specs are unaffected, since magpie plugin add refuses them as not
a package name.

Run:
- go test -tags nogui ./internal/plugin ./internal/gui (temp HOME): ok.
- New TestGitPluginUpdateOnlyWhenItsRepositoryMoved, for
  github:yuweihao17/codearts, its #main form, owner/repo, the
  https://github.com URL, git+file and git+file#main, against a fake
  GitHub API and bare repositories. After add, nothing waits. A new
  commit at the same version is an update. After Upgrade, nothing waits
  again.
- TestGitCommitAsBunWritesIt uses ARNO's literal bun.lock line and
  .bun-tag. TestGitSource covers spec parsing and the pinned-sha form.
- Broke GitBehind by hand (any installed commit is behind): the new test
  goes red in every form. Restored.
- GUI: plugin-git.test.cjs now checks no Update at the head, then New
  commits with Update and Update all once the head moves, then none after
  Update. It went red with the old condition put back. All 28 plugin-*
  files plus click-scroll, i18n-keys, gui-ja and gui-de pass in Chromium
  and WebKit.
- Real case, sandbox HOME, magpie web on a spare port: magpie plugin add
  github:yuweihao17/codearts installed c594984. /api/plugins/git and
  /api/plugins/check say current at c594984 against the real GitHub, and
  nothing is waiting. In both engines the row shows "v0.1.0 c594984" with
  no Update and no Update all.
- gofmt -l clean; go vet -tags nogui darwin, GOOS=linux and GOOS=windows
  on internal/plugin and internal/gui.
2026-10-11 01:14:19 +08:00

208 lines
7.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package plugin
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
// ARNO on Discord: 我安装了 "opencode-codearts-auth": "github:yuweihao17/codearts",
// 插件页面一直有更新按钮,我点击更新更新到最新版了,还是一直提示有更新. A plugin
// from a git repository is out of date only when its repository is at
// another commit than the one bun installed: right after it is added, and
// right after it is updated, nothing waits; a new commit to the repository
// (its package.json's version left as it was, as that repository's is) is
// an update, and updating takes it away. Each way of naming a repository
// bun takes: GitHub's (asked of GitHub's API), with a branch, and a git URL
// (asked with git ls-remote).
func TestGitPluginUpdateOnlyWhenItsRepositoryMoved(t *testing.T) {
for _, form := range []string{
"github:yuweihao17/codearts", // ARNO's
"github:yuweihao17/codearts#main",
"yuweihao17/codearts",
"https://github.com/yuweihao17/codearts",
"git+file",
"git+file#main",
} {
// (a # in the name would be in t.TempDir()'s path, and so the URL)
t.Run(strings.ReplaceAll(form, "#", " at "), func(t *testing.T) {
github, _ := gitSandbox(t)
bare := filepath.Join(github, "yuweihao17", "codearts.git")
repo := newGitRepo(t, bare)
spec := form
if strings.HasPrefix(form, "git+file") {
u := "git+file://" + filepath.ToSlash(bare)
if !strings.HasPrefix(u, "git+file:///") {
u = "git+file:///" + strings.TrimPrefix(u, "git+file://")
}
spec = u + strings.TrimPrefix(form, "git+file")
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
if _, err := Add(ctx, spec); err != nil {
t.Fatal(err)
}
head := func() string {
out, err := exec.Command("git", "-C", bare, "rev-parse", "HEAD").Output()
if err != nil {
t.Fatal(err)
}
return strings.TrimSpace(string(out))
}
if c := GitCommit(spec); c == "" || !SameCommit(c, head()) {
t.Fatalf("GitCommit = %q, want the repository's %s", c, head())
}
check := func(want string) VersionCheck {
t.Helper()
cs := CheckNow(ctx).Plugins
if len(cs) != 1 || cs[0].Status != want {
t.Fatalf("Check for updates said %+v, want %s", cs, want)
}
return cs[0]
}
waiting := func(want int) {
t.Helper()
if w := PendingUpdates().Waiting; len(w) != want {
t.Fatalf("waiting = %+v, want %d", w, want)
}
}
// just added: up to date, to the page and to the hourly look
if c := check("current"); c.Head != short(head()) || c.Commit != short(head()) {
t.Fatalf("check = %+v, want commit and head %s", c, short(head()))
}
if _, err := CheckUpdates(ctx); err != nil {
t.Fatal(err)
}
waiting(0)
// a new commit, the version in package.json the same
repo.commit("README.md", "# my-oc-plugin\n\nA fix.\n")
if c := check("update"); c.Head != short(head()) || SameCommit(c.Commit, c.Head) {
t.Fatalf("check = %+v, want an update to %s", c, short(head()))
}
waiting(1)
if u, err := CheckUpdates(ctx); err != nil || len(u.Waiting) != 1 || !u.Waiting[0].Git || u.Waiting[0].Latest != short(head()) {
t.Fatalf("CheckUpdates = %+v, %v", u, err)
}
// updated: nothing waits, before anyone asks again and after
if err := Upgrade(ctx, Name(spec)); err != nil {
t.Fatal(err)
}
if c := GitCommit(spec); !SameCommit(c, head()) {
t.Fatalf("after Update, GitCommit = %q, want %s", c, head())
}
waiting(0)
check("current")
if _, err := CheckUpdates(ctx); err != nil {
t.Fatal(err)
}
waiting(0)
})
}
}
// commit changes a file of the plugin's repository and pushes it.
func (r gitRepo) commit(file, text string) {
r.t.Helper()
if err := os.WriteFile(filepath.Join(r.work, file), []byte(text), 0o644); err != nil {
r.t.Fatal(err)
}
r.git(r.work, "add", "-A")
r.git(r.work, "commit", "-qm", "change "+file)
r.git(r.work, "push", "-q", r.bare, "HEAD:main")
}
// The commit bun installed is read as bun 1.4 writes it, from ARNO's own
// install: the package's .bun-tag, else bun.lock.
func TestGitCommitAsBunWritesIt(t *testing.T) {
dir := t.TempDir()
t.Setenv("HOME", dir)
t.Setenv("USERPROFILE", dir)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
const spec = "github:yuweihao17/codearts"
write := func(p, s string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
t.Fatal(err)
}
}
write(filepath.Join(Dir(), "package.json"), `{
"name": "magpie-plugins",
"private": true,
"dependencies": {
"opencode-codearts-auth": "github:yuweihao17/codearts"
}
}`)
write(filepath.Join(Dir(), "bun.lock"), `{
"lockfileVersion": 2,
"configVersion": 1,
"workspaces": {
"": {
"name": "magpie-plugins",
"dependencies": {
"opencode-codearts-auth": "github:yuweihao17/codearts",
},
},
},
"packages": {
"opencode-codearts-auth": ["opencode-codearts-auth@github:yuweihao17/codearts#c594984", {}, "yuweihao17-codearts-c594984", "sha512-uKnSoZPhr6F9hTkoND/T/ovUho5xCv1FEnaPuL36ZRyzaYbGIwTESjuMF1dHJXE/NkNwPxvTNCw7MSkff7CTxQ=="],
}
}
`)
write(filepath.Join(Dir(), "node_modules", "opencode-codearts-auth", "package.json"), `{"name": "opencode-codearts-auth", "version": "0.1.0"}`)
if c := GitCommit(spec); c != "c594984" {
t.Fatalf("from bun.lock: %q", c)
}
write(filepath.Join(Dir(), "node_modules", "opencode-codearts-auth", ".bun-tag"), "yuweihao17-codearts-8fd2d9a")
if c := GitCommit(spec); c != "8fd2d9a" {
t.Fatalf("from .bun-tag: %q", c)
}
if !SameCommit("c594984", "c594984218aa") || SameCommit("c594984", "8fd2d9a88d") || SameCommit("", "c594984") {
t.Fatal("SameCommit")
}
if GitBehind(spec, "8fd2d9a88d0000000000000000000000000000ff") || !GitBehind(spec, "c594984218") {
t.Fatal("GitBehind")
}
}
// Where each spec's repository is asked, and a spec pinned to a commit is
// never asked at all.
func TestGitSource(t *testing.T) {
for spec, want := range map[string][3]string{
"github:yuweihao17/codearts": {"yuweihao17/codearts", "", ""},
"github:yuweihao17/codearts#v1.2.0": {"yuweihao17/codearts", "", "v1.2.0"},
"yuweihao17/codearts#main": {"yuweihao17/codearts", "", "main"},
"https://github.com/yuweihao17/codearts": {"yuweihao17/codearts", "", ""},
"https://github.com/yuweihao17/codearts.git": {"yuweihao17/codearts", "", ""},
"git+https://github.com/yuweihao17/codearts.git#dev": {"yuweihao17/codearts", "", "dev"},
"git+ssh://git@github.com/yuweihao17/codearts.git": {"yuweihao17/codearts", "", ""},
"git://github.com/yuweihao17/codearts.git": {"yuweihao17/codearts", "", ""},
"gitlab:owner/repo": {"", "https://gitlab.com/owner/repo", ""},
"bitbucket:owner/repo#x": {"", "https://bitbucket.org/owner/repo", "x"},
"https://gitlab.com/owner/repo": {"", "https://gitlab.com/owner/repo", ""},
"git+https://example.com/a/b.git#v2": {"", "https://example.com/a/b.git", "v2"},
"git+file:///tmp/repo.git": {"", "file:///tmp/repo.git", ""},
} {
gh, remote, ref := gitSource(spec)
if got := [3]string{gh, remote, ref}; got != want {
t.Errorf("gitSource(%q) = %q, want %q", spec, got, want)
}
}
// pinned to a commit: that commit, nobody asked (GitHub isn't there)
api := githubAPI
githubAPI = "http://127.0.0.1:1"
defer func() { githubAPI = api }()
if sha, err := remoteHead(context.Background(), "github:yuweihao17/codearts#9BF77F96F2"); err != nil || sha != "9bf77f96f2" {
t.Fatalf("pinned: %q %v", sha, err)
}
}