Files
skillshare/tests/integration/update_batch_current_test.go
T
Colin MasonandWillie c4b183f6ef fix(update): read store in batch fast path (#569)
* fix(update): read store in batch fast path

The grouped update fast path read per-skill .skillshare-meta.json
sidecars. The migration to .metadata.json deletes those files, so the
check never matched and update --all reinstalled every skill on every
run.

Read the central store instead, and refresh version and commit there
instead of writing a sidecar. Require SKILL.md on disk so the update
restores a deleted install. --force now skips the check and reinstalls
everything.

* test(update): cover legacy grouped metadata keys

Grouped skills can still be stored under a basename key with a Group
field. Check that the current-skill lookup finds that entry and that the
version refresh updates it in place instead of adding a new key.

* feat(update): report up-to-date skills

The summary lumped skills the fast path skipped in with failures. Count
them as "up to date" in the summary and as up_to_date in --json
output.

* feat(update): log up-to-date count in oplog

The operations log recorded updated, skipped, pruned, and blocked counts
for update but not the new up-to-date count. Record it as up_to_date
so the log matches the summary and --json output.

* test(update): cover up-to-date batch skills

update --all reinstalled every grouped skill on every run. Check that
unchanged skills report as up to date in global and project mode, that
an upstream change updates only the changed skill, and that --force
reinstalls everything.

* docs(update): describe up-to-date batch skills

Describe how update --all and --group leave unchanged skills alone and
report them as up to date, the up_to_date JSON field, and the new
meaning of --force.

* fix(update): keep lock pins on tree-only matches

A tree-hash match after upstream HEAD moved refreshed the full commit in
the store. A project lock pin follows that field, so an unrelated
upstream commit rewrote skills.lock.json even though the skill's files
did not change. Refresh only the version, as the old sidecar refresh
did. check compares the version, so it still reports the skill as up to
date.

The help text, docs and skill reference said --all and --group skip
current skills. The skip lives in the batch path, which runs for any
update of several skills, and a single skill always takes the reinstall
path. Say that instead.

* fix(update): load the store once per batch

Each skill skipped as current reloaded .metadata.json before comparing
its version, so an unchanged batch reparsed the whole store once per
skill. Compare the version already read from the snapshot first, and
refresh the skills that do lag HEAD with one load and save after the
loop. An unchanged batch of 400 skills drops from about 520ms to 64ms.

The help text and docs said any update of several skills takes this
path. In global mode, named and glob targets carry no metadata and
reinstall on the standalone path. Name what does take it: --all,
--group, and several skills named in project mode.

---------

Co-authored-by: Willie <egg358358@gmail.com>
2026-10-14 23:36:56 +08:00

182 lines
6.2 KiB
Go

//go:build !online
package integration
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/install"
"skillshare/internal/testutil"
)
type updateCounts struct {
Updated int `json:"updated"`
UpToDate int `json:"up_to_date"`
Skipped int `json:"skipped"`
}
func runUpdateAllJSON(t *testing.T, sb *testutil.Sandbox, extra ...string) updateCounts {
t.Helper()
args := append([]string{"update", "--all", "--skip-audit", "--json"}, extra...)
result := sb.RunCLI(args...)
result.AssertSuccess(t)
var counts updateCounts
if err := json.Unmarshal([]byte(result.Stdout), &counts); err != nil {
t.Fatalf("parse update JSON: %v\n%s", err, result.Stdout)
}
return counts
}
// setupGroupedSkills pushes skills/<name> for each name to a bare repo and
// installs them, returning the work clone used to push later changes.
func setupGroupedSkills(t *testing.T, sb *testutil.Sandbox, names ...string) (remote, work string) {
t.Helper()
remote = filepath.Join(sb.Root, "grouped.git")
work = filepath.Join(sb.Root, "grouped-work")
gitInit(t, remote, true)
gitClone(t, remote, work)
for _, name := range names {
writeGroupedSkill(t, work, name, "v1")
}
gitAddCommit(t, work, "add skills")
gitPush(t, work)
for _, name := range names {
sb.RunCLI("install", "file://"+remote+"//skills/"+name, "--skip-audit").AssertSuccess(t)
}
return remote, work
}
func writeGroupedSkill(t *testing.T, work, name, marker string) {
t.Helper()
dir := filepath.Join(work, "skills", name)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
body := "---\nname: " + name + "\n---\n# " + name + " " + marker + "\n"
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(body), 0644); err != nil {
t.Fatal(err)
}
}
// update --all used to reinstall every grouped skill on every run because its
// "already current" check read sidecars that no longer exist.
func TestUpdateAll_UnchangedGroupedSkillsAreUpToDate(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
_, work := setupGroupedSkills(t, sb, "alpha", "beta")
if got := runUpdateAllJSON(t, sb); got.Updated != 0 || got.UpToDate != 2 {
t.Fatalf("unchanged upstream: got %+v, want 0 updated and 2 up to date", got)
}
for _, name := range []string{"alpha", "beta"} {
if _, err := os.Stat(filepath.Join(sb.SourcePath, name, install.MetaFileName)); !os.IsNotExist(err) {
t.Fatalf("%s: update must not write a sidecar, stat err=%v", name, err)
}
}
writeGroupedSkill(t, work, "alpha", "v2")
gitAddCommit(t, work, "change alpha")
gitPush(t, work)
if got := runUpdateAllJSON(t, sb); got.Updated != 1 || got.UpToDate != 1 {
t.Fatalf("one skill changed: got %+v, want 1 updated and 1 up to date", got)
}
if data := sb.ReadFile(filepath.Join(sb.SourcePath, "alpha", "SKILL.md")); !strings.Contains(data, "alpha v2") {
t.Fatalf("alpha not updated:\n%s", data)
}
if got := runUpdateAllJSON(t, sb, "--force"); got.Updated != 2 || got.UpToDate != 0 {
t.Fatalf("--force: got %+v, want 2 updated", got)
}
}
// Project mode shares the batch path; unchanged skills must stay up to date
// there too.
func TestUpdateProject_UnchangedGroupedSkillsAreUpToDate(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
remote := filepath.Join(sb.Root, "project-grouped.git")
work := filepath.Join(sb.Root, "project-grouped-work")
gitInit(t, remote, true)
gitClone(t, remote, work)
for _, name := range []string{"alpha", "beta"} {
writeGroupedSkill(t, work, name, "v1")
}
gitAddCommit(t, work, "add skills")
gitPush(t, work)
for _, name := range []string{"alpha", "beta"} {
sb.RunCLIInDir(projectRoot, "install", "file://"+remote+"//skills/"+name, "-p", "--skip-audit").AssertSuccess(t)
}
result := sb.RunCLIInDir(projectRoot, "update", "--all", "-p", "--skip-audit", "--json")
result.AssertSuccess(t)
var got updateCounts
if err := json.Unmarshal([]byte(result.Stdout), &got); err != nil {
t.Fatalf("parse update JSON: %v\n%s", err, result.Stdout)
}
if got.Updated != 0 || got.UpToDate != 2 {
t.Fatalf("project mode, unchanged upstream: got %+v, want 0 updated and 2 up to date", got)
}
// Several skills named in project mode take the same batch path.
result = sb.RunCLIInDir(projectRoot, "update", "alpha", "beta", "-p", "--skip-audit", "--json")
result.AssertSuccess(t)
got = updateCounts{}
if err := json.Unmarshal([]byte(result.Stdout), &got); err != nil {
t.Fatalf("parse update JSON: %v\n%s", err, result.Stdout)
}
if got.Updated != 0 || got.UpToDate != 2 {
t.Fatalf("project mode, named skills: got %+v, want 0 updated and 2 up to date", got)
}
}
// A project lock pins each skill's commit. An upstream commit that leaves a
// skill's folder alone must not move its pin, since the files did not change.
func TestUpdateProject_TreeMatchKeepsLockPin(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
remote := filepath.Join(sb.Root, "pinned-grouped.git")
work := filepath.Join(sb.Root, "pinned-grouped-work")
gitInit(t, remote, true)
gitClone(t, remote, work)
for _, name := range []string{"alpha", "beta"} {
writeGroupedSkill(t, work, name, "v1")
}
gitAddCommit(t, work, "add skills")
gitPush(t, work)
for _, name := range []string{"alpha", "beta"} {
sb.RunCLIInDir(projectRoot, "install", "file://"+remote+"//skills/"+name, "-p", "--skip-audit").AssertSuccess(t)
}
lockPath := filepath.Join(projectRoot, ".skillshare", install.LockFileName)
before, err := os.ReadFile(lockPath)
if err != nil {
t.Fatalf("project install wrote no lock: %v", err)
}
writeGroupedSkill(t, work, "unrelated", "v1")
gitAddCommit(t, work, "add an unrelated skill")
gitPush(t, work)
result := sb.RunCLIInDir(projectRoot, "update", "--all", "-p", "--skip-audit", "--json")
result.AssertSuccess(t)
var got updateCounts
if err := json.Unmarshal([]byte(result.Stdout), &got); err != nil {
t.Fatalf("parse update JSON: %v\n%s", err, result.Stdout)
}
if got.Updated != 0 || got.UpToDate != 2 {
t.Fatalf("unrelated upstream commit: got %+v, want 0 updated and 2 up to date", got)
}
if after, _ := os.ReadFile(lockPath); string(after) != string(before) {
t.Fatalf("lock changed on an unrelated upstream commit:\nbefore: %s\nafter: %s", before, after)
}
}