Files
skillshare/tests/integration/diff_test.go
T
Willie c638b46096 fix(sync): replace copy-mode copies with links when switching back to merge (#506)
* fix(sync): replace copy-mode copies with links when switching back to merge

Switching a target from copy back to merge left every copy whose entry name
did not change as a real folder. Merge sync treated it as the user's own
folder, so status and doctor kept saying "run skillshare sync" while sync
changed nothing, and only `sync --force` turned it into a link.

Copy mode already replaces merge-mode links with copies on its own, so merge
sync now replaces a folder the manifest records with a checksum (a copy-mode
copy) with a link, without --force. Folders the manifest does not record, or
records as a link, stay untouched. The pre-sync target backup is unchanged.

The CLI and dashboard diffs name these entries as copies sync replaces
instead of suggesting --force, and the sync-modes docs describe the switch.

* fix(sync): keep copy-mode copies the user edited when switching to merge

The first version replaced any folder the manifest recorded as a copy-mode
copy. The manifest checksum is only a record, so a copy edited since copy
mode made it was deleted too, and `sync --json` and project-mode dashboard
sync take no pre-sync backup to restore it from.

Replace the folder only while it still hashes to the recorded checksum
(Manifest.OwnsCopy). An edited copy is kept and `sync --force` still
replaces it; diff keeps suggesting --force for it. A copy whose name: was
rewritten by prefixed naming, or that was made with custom ignore patterns,
no longer hashes to the source checksum and is kept the same way.

* fix(ui): say a copy-mode copy in a merge target is replaced by a link

The Sync page picks a row's text from the diff item's action alone. The
copy-mode copy that merge sync now replaces comes from the server as an
update, which the page worded as "Link is broken or points elsewhere. Will
be relinked" even though no link exists yet.

Match the server's reason and show "Copy made by copy mode. Will be replaced
by a link" instead, in all eleven dashboard languages.

* docs(sync): say only unedited copy-mode copies are replaced on merge sync

The sync-modes paragraph promised that copies copy mode made are replaced
with links without --force. Merge sync keeps a copy that no longer hashes to
the checksum copy mode recorded, so an edited copy, or one whose name: copy
mode rewrote for prefixed naming, still needs --force.

State the three cases in sync-modes and qualify the diff Modified and Local
override labels, in English and the ja, ko, zh-Hans and zh-Hant copies.

* fix(sync): keep the copy until its replacement link exists

Replacing a copy-mode copy removed the folder first and then created the
link. If creating the link failed (no symlink or junction rights on a
Windows or network filesystem), sync returned an error with the skill gone
from the target. This path now also runs on a plain sync, not only on
--force.

Park the folder beside the entry, create the link, then delete the parked
folder; on failure the folder is moved back. A parked folder left by a run
that died mid-way may be the only copy, so the next sync stops with a message
instead of deleting it. The manifest also forgets the copy-mode mtime and
naming records of an entry that is now a link.

* test(sourcefs): register replaceWithLink's target writes in the raw-write ratchet

The previous commit moved the merge-mode folder replacement into
replaceWithLink, which the ratchet reports as new raw os calls and the old
os.RemoveAll allowance as stale. All of them act on the sync target, not the
skills source, like the neighbouring entries.

* fix(sync): bound the parked copy's name and document the naming exception

The folder parked while a link is created was named after the entry, so an
entry near the 255-byte filename limit (a long flattened nested skill)
could not be moved aside and its replacement failed. The parked name is now
a fixed-length hash of the entry name.

sync-modes also says what happens when `target_naming` changes in the same
sync: the copy gets a new entry name and the prune pass removes it as an
orphan whether or not it was edited, as it did before this change.
2026-10-10 00:13:29 +08:00

554 lines
14 KiB
Go

//go:build !online
package integration
import (
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/testutil"
)
func TestDiff_NoTargets_SaysSo(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill1", map[string]string{"SKILL.md": "# Skill 1"})
sb.WriteConfig("source: " + sb.SourcePath + "\ntargets: {}\n")
result := sb.RunCLI("diff", "--no-tui")
result.AssertSuccess(t)
result.AssertOutputContains(t, "No targets configured")
result.AssertOutputNotContains(t, "No differences")
}
func TestDiff_LocalOnlySkill_ShowsDifference(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
targetPath := sb.CreateTarget("claude")
// Create local skill in target only
localSkillPath := filepath.Join(targetPath, "local-skill")
os.MkdirAll(localSkillPath, 0755)
os.WriteFile(filepath.Join(localSkillPath, "SKILL.md"), []byte("# Local"), 0644)
sb.WriteConfig(`source: ` + sb.SourcePath + `
mode: merge
targets:
claude:
path: ` + targetPath + `
`)
result := sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputContains(t, "claude")
}
func TestDiff_TargetWithoutSkills_DoesNotSuggestTargetAdd(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill1", map[string]string{"SKILL.md": "# Skill 1"})
target := sb.CreateTarget("mytool")
sb.WriteConfig("source: " + sb.SourcePath + "\ntargets:\n mytool:\n path: " + target + "\n skills: {enabled: false}\n")
result := sb.RunCLI("diff", "--no-tui")
result.AssertSuccess(t)
result.AssertOutputContains(t, "no target takes skills")
result.AssertOutputNotContains(t, "target add")
}
func TestDiff_Sections_ShareOneLabelWidth(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill1", map[string]string{"SKILL.md": "# Skill 1"})
claude := sb.CreateTarget("claude")
cursor := sb.CreateTarget("cursor")
// Only claude has a local-only skill, so its section has the longer label.
os.MkdirAll(filepath.Join(claude, "local-skill"), 0755)
os.WriteFile(filepath.Join(claude, "local-skill", "SKILL.md"), []byte("# Local"), 0644)
sb.WriteConfig(`source: ` + sb.SourcePath + `
mode: merge
targets:
claude:
path: ` + claude + `
cursor:
path: ` + cursor + `
`)
result := sb.RunCLI("diff", "--no-tui")
result.AssertSuccess(t)
var columns []int
for _, line := range strings.Split(result.Stdout, "\n") {
if strings.HasPrefix(line, " New ") {
columns = append(columns, strings.Index(line, "skill1"))
}
}
if len(columns) != 2 || columns[0] != columns[1] {
t.Fatalf("expected both New rows to align, got columns %v in:\n%s", columns, result.Stdout)
}
}
func TestDiff_SpecificTarget_ShowsOnlyThat(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill1", map[string]string{"SKILL.md": "# Skill 1"})
claudePath := sb.CreateTarget("claude")
codexPath := sb.CreateTarget("codex")
sb.WriteConfig(`source: ` + sb.SourcePath + `
mode: merge
targets:
claude:
path: ` + claudePath + `
codex:
path: ` + codexPath + `
`)
result := sb.RunCLI("diff", "--target", "claude")
result.AssertSuccess(t)
result.AssertOutputContains(t, "claude")
result.AssertOutputNotContains(t, "codex")
}
func TestDiff_TargetNotFound_ReturnsError(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
result := sb.RunCLI("diff", "--target", "nonexistent")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "not found")
}
func TestDiff_NoConfig_ReturnsError(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
os.Remove(sb.ConfigPath)
result := sb.RunCLI("diff")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "init")
}
func TestDiff_CopyMode_DetectsContentDrift(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
skillDir := sb.CreateSkill("skill-a", map[string]string{
"SKILL.md": "# Original",
})
targetPath := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + targetPath + `
mode: copy
`)
// Sync to establish manifest with checksum
sb.RunCLI("sync").AssertSuccess(t)
// Diff should show fully synced
result := sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputContains(t, "in sync")
// Modify source content
os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte("# Modified"), 0644)
// Diff should now detect content drift
result = sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputContains(t, "Modified")
}
func TestDiff_CopyMode_EmptyManifest(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{
"SKILL.md": "# Skill A",
})
targetPath := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + targetPath + `
mode: copy
`)
// Run diff WITHOUT syncing first — empty manifest, but mode is copy
result := sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputContains(t, "New")
}
func TestDiff_CopyMode_DetectsDeletedTargetDir(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{
"SKILL.md": "# Skill A",
})
targetPath := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + targetPath + `
mode: copy
`)
// Sync to establish manifest
sb.RunCLI("sync").AssertSuccess(t)
// Manually delete the target skill directory
os.RemoveAll(filepath.Join(targetPath, "skill-a"))
// Diff should detect the deleted directory, NOT report "Fully synced"
result := sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputContains(t, "Restore")
result.AssertOutputNotContains(t, "in sync")
}
func TestDiff_MultiTarget_SameResult_Grouped(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{"SKILL.md": "# A"})
sb.CreateSkill("skill-b", map[string]string{"SKILL.md": "# B"})
claudePath := sb.CreateTarget("claude")
agentsPath := sb.CreateTarget("agents")
sb.WriteConfig(`source: ` + sb.SourcePath + `
mode: merge
targets:
agents:
path: ` + agentsPath + `
claude:
path: ` + claudePath + `
`)
result := sb.RunCLI("diff")
result.AssertSuccess(t)
// Both targets should be merged into one header
result.AssertOutputContains(t, "agents, claude")
// Items should only appear once (not duplicated)
out := result.Stdout
countA := strings.Count(out, "skill-a")
countB := strings.Count(out, "skill-b")
if countA != 1 {
t.Errorf("expected skill-a to appear once, got %d times in:\n%s", countA, out)
}
if countB != 1 {
t.Errorf("expected skill-b to appear once, got %d times in:\n%s", countB, out)
}
}
func TestDiff_MultiTarget_DifferentResult_Separate(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{"SKILL.md": "# A"})
sb.CreateSkill("skill-b", map[string]string{"SKILL.md": "# B"})
claudePath := sb.CreateTarget("claude")
cursorPath := sb.CreateTarget("cursor")
// Sync skill-a to cursor only (via symlink) so the diff results differ
os.Symlink(filepath.Join(sb.SourcePath, "skill-a"), filepath.Join(cursorPath, "skill-a"))
sb.WriteConfig(`source: ` + sb.SourcePath + `
mode: merge
targets:
claude:
path: ` + claudePath + `
cursor:
path: ` + cursorPath + `
`)
result := sb.RunCLI("diff")
result.AssertSuccess(t)
// Each target should have its own header (not merged)
result.AssertOutputContains(t, "claude")
result.AssertOutputContains(t, "cursor")
// They should NOT be merged into one line
result.AssertOutputNotContains(t, "claude, cursor")
result.AssertOutputNotContains(t, "cursor, claude")
}
func TestDiff_MultiTarget_AllSynced_Merged(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{"SKILL.md": "# A"})
claudePath := sb.CreateTarget("claude")
agentsPath := sb.CreateTarget("agents")
// Sync both targets
os.Symlink(filepath.Join(sb.SourcePath, "skill-a"), filepath.Join(claudePath, "skill-a"))
os.Symlink(filepath.Join(sb.SourcePath, "skill-a"), filepath.Join(agentsPath, "skill-a"))
sb.WriteConfig(`source: ` + sb.SourcePath + `
mode: merge
targets:
agents:
path: ` + agentsPath + `
claude:
path: ` + claudePath + `
`)
result := sb.RunCLI("diff")
result.AssertSuccess(t)
// Both fully synced targets should be merged into one line
result.AssertRowContains(t, "agents, claude", "in sync")
}
func TestDiff_CopyMode_DetectsNonDirectoryTargetEntry(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{
"SKILL.md": "# Skill A",
})
targetPath := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + targetPath + `
mode: copy
`)
// Sync to establish manifest
sb.RunCLI("sync").AssertSuccess(t)
// Replace managed directory with a regular file
os.RemoveAll(filepath.Join(targetPath, "skill-a"))
os.WriteFile(filepath.Join(targetPath, "skill-a"), []byte("not-a-dir"), 0644)
result := sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputContains(t, "not a directory")
result.AssertOutputNotContains(t, "synced")
}
func TestDiff_CopyMode_ShowsFileStat(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{
"SKILL.md": "# Original",
"prompt.md": "original prompt",
})
targetPath := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + targetPath + `
mode: copy
`)
sb.RunCLI("sync").AssertSuccess(t)
// Modify source
os.WriteFile(filepath.Join(sb.SourcePath, "skill-a", "SKILL.md"), []byte("# Changed"), 0644)
result := sb.RunCLI("diff", "--stat")
result.AssertSuccess(t)
result.AssertOutputContains(t, "SKILL.md")
}
func TestDiff_PatchFlag_ShowsUnifiedDiff(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{
"SKILL.md": "# Original\nline2\nline3",
})
targetPath := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + targetPath + `
mode: copy
`)
sb.RunCLI("sync").AssertSuccess(t)
// Modify source
os.WriteFile(filepath.Join(sb.SourcePath, "skill-a", "SKILL.md"), []byte("# Modified\nline2\nnew line"), 0644)
result := sb.RunCLI("diff", "--patch")
result.AssertSuccess(t)
result.AssertOutputContains(t, "Modified")
result.AssertOutputContains(t, "SKILL.md")
}
func TestDiff_NewLabels_ShowCorrectSymbols(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("new-skill", map[string]string{"SKILL.md": "# New"})
targetPath := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
mode: merge
targets:
claude:
path: ` + targetPath + `
`)
result := sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputContains(t, "New")
}
func TestDiff_ShowsSummary(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{"SKILL.md": "# A"})
claudePath := sb.CreateTarget("claude")
agentsPath := sb.CreateTarget("agents")
// Sync agents only
os.Symlink(filepath.Join(sb.SourcePath, "skill-a"), filepath.Join(agentsPath, "skill-a"))
sb.WriteConfig(`source: ` + sb.SourcePath + `
mode: merge
targets:
agents:
path: ` + agentsPath + `
claude:
path: ` + claudePath + `
`)
result := sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputContains(t, "2 targets: 1 to sync, 1 in sync")
}
func TestDiff_MergeMode_HiddenSourceSkill_ShowsSynced(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{"SKILL.md": "---\nname: skill-a\n---\n# A"})
sb.CreateNestedSkill(".system/example", map[string]string{"SKILL.md": "---\nname: example\n---\n# Example"})
targetPath := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
target_naming: flat
targets:
claude:
path: ` + targetPath + `
mode: merge
`)
sb.RunCLI("sync").AssertSuccess(t)
result := sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputContains(t, "in sync")
result.AssertOutputNotContains(t, ".system__example")
}
// singleTargetConfig is a config with one claude target in the given mode.
func singleTargetConfig(source, targetPath, mode string) string {
return `source: ` + source + `
targets:
claude:
path: ` + targetPath + `
mode: ` + mode + `
`
}
func TestDiff_MergeMode_CopyModeCopy_SyncReplacesWithoutForce(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{"SKILL.md": "---\nname: skill-a\n---\n# A"})
targetPath := sb.CreateTarget("claude")
config := func(mode string) string { return singleTargetConfig(sb.SourcePath, targetPath, mode) }
sb.WriteConfig(config("copy"))
sb.RunCLI("sync").AssertSuccess(t)
sb.WriteConfig(config("merge"))
result := sb.RunCLI("diff")
result.AssertSuccess(t)
result.AssertOutputNotContains(t, "--force")
sb.RunCLI("sync").AssertSuccess(t)
if !sb.IsSymlink(filepath.Join(targetPath, "skill-a")) {
t.Error("sync should turn the copy-mode copy back into a link without --force")
}
}
func TestSync_MergeMode_EditedCopyModeCopy_NeedsForce(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{"SKILL.md": "---\nname: skill-a\n---\n# A"})
targetPath := sb.CreateTarget("claude")
config := func(mode string) string { return singleTargetConfig(sb.SourcePath, targetPath, mode) }
sb.WriteConfig(config("copy"))
sb.RunCLI("sync").AssertSuccess(t)
edited := filepath.Join(targetPath, "skill-a", "SKILL.md")
os.WriteFile(edited, []byte("# my edits"), 0644)
sb.WriteConfig(config("merge"))
sb.RunCLI("sync").AssertSuccess(t)
if sb.IsSymlink(filepath.Join(targetPath, "skill-a")) {
t.Fatal("an edited copy must survive sync without --force")
}
if got, _ := os.ReadFile(edited); string(got) != "# my edits" {
t.Errorf("edits lost: %q", got)
}
sb.RunCLI("sync", "--force").AssertSuccess(t)
if !sb.IsSymlink(filepath.Join(targetPath, "skill-a")) {
t.Error("--force should replace the edited copy")
}
}
func TestDiff_AllArgument_DiffsEverything(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill-a", map[string]string{"SKILL.md": "# A"})
claudePath := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + claudePath + `
`)
result := sb.RunCLI("diff", "all", "--no-tui")
result.AssertSuccess(t)
result.AssertRowContains(t, "New", "skill-a")
}