Files
magpie/internal/edit/unchanged_test.go
T
yetone 5a32923542 edit: a write of the bytes a file already holds leaves it alone (#3870)
Every SyncCatalog wrote agents' config files again with the same bytes:
a new mtime that wakes every watcher of the file (filememo, the Codex
daemon loop, the agent's own reload), and a temp-and-rename that swaps
the file under an agent reading it. #3870 named Kimi Code, Grok and
AtomCode; with every agent connected in a sandbox HOME, a second sync
rewrote 15 files: .claude/settings.json, Cline's providers.json,
CodeBuddy, Copilot BYOK, Factory, Qwen, Snow (both), WorkBuddy (both),
ZCode, Kimi Code, Grok, AtomCode and magpie's stash.json.

WriteAtomic, which every TOML, JSON, YAML and dotenv writer goes through,
now reads the target after the filter and BOM are applied and returns
when its bytes are the same. A real change is written as before.

Tests:
- internal/edit TestUnchangedWriteKeepsFile: TOML table/key/top, JSON,
  YAML and .env; mtime and inode kept on the same bytes, a changed value
  written. Red in all 6 with the check turned off by hand.
- internal/agent TestSecondSyncWritesNothing: every connectable agent
  connected, second SyncCatalog + dshWiredOnce writes no file. Red with
  the old WriteAtomic (15 files), green -count=3 with this one.

Run:
- At 68bbcc01c + this change: gofmt clean; go vet -tags nogui
  internal/edit and internal/agent for linux, windows, darwin ok;
  go test -timeout 45m over every importer of internal/edit (access,
  agent, autostart, backup, cli, codexcat, daemon, davsync, edit,
  gateway, library, mcpauth, plugin, profile, provider, settings,
  update, usage): all ok but provider's TestFetchAsksASharedBaseOnce
  and two siblings (red the same on unchanged origin/main ad3833512,
  3 of 3: #3854) and update's TestRelaunchOpenTriedAgain (TempDir
  cleanup under load ~90, passes 3 of 3 alone: #3881).
- At the pushed head (rebased on ad3833512): gofmt, 3-OS vet, all of
  internal/edit, and internal/agent's new test plus Disconnect, Codex
  hand-edit, Kimi, Grok and AtomCode tests: ok. internal/gui, which
  imports internal/edit, was not run.

Not tried: the real Kimi Code / Grok / AtomCode apps watching the file.
2026-10-27 17:26:30 +08:00

64 lines
2.0 KiB
Go

package edit
import (
"os"
"path/filepath"
"testing"
"time"
)
// A write of the bytes a file already holds leaves it alone: its mtime
// and inode stay, so a watcher isn't woken and an agent reading it keeps
// its file (#3870). Each format's setter is tried, then a real change.
func TestUnchangedWriteKeepsFile(t *testing.T) {
old := time.Now().Add(-time.Hour).Truncate(time.Second)
for _, tc := range []struct {
name, file string
set func(string, any) error
}{
{"toml table", "config.toml", func(p string, v any) error {
return SetTOMLTable(p, "providers.magpie", KV{Path: "base_url", Value: v})
}},
{"toml key", "config.toml", func(p string, v any) error {
return SetTOMLKey(p, "providers.magpie", "base_url", v)
}},
{"toml top", "config.toml", func(p string, v any) error {
return SetTOMLTop(p, KV{Path: "model", Value: v})
}},
{"json", "settings.json", func(p string, v any) error {
return SetJSON(p, KV{Path: "env.BASE_URL", Value: v})
}},
{"yaml", "config.yaml", func(p string, v any) error {
return SetYAML(p, KV{Path: "model.base_url", Value: v})
}},
{"env", ".env", func(p string, v any) error {
return SetEnvFile(p, KV{Path: "BASE_URL", Value: v})
}},
} {
t.Run(tc.name, func(t *testing.T) {
p := filepath.Join(t.TempDir(), tc.file)
if err := tc.set(p, "http://127.0.0.1:3425"); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(p, old, old); err != nil {
t.Fatal(err)
}
before, _ := os.Stat(p)
if err := tc.set(p, "http://127.0.0.1:3425"); err != nil {
t.Fatal(err)
}
after, _ := os.Stat(p)
if !after.ModTime().Equal(old) || !os.SameFile(before, after) {
t.Fatalf("same bytes rewrote the file: mtime %v (was %v), same file %v",
after.ModTime(), old, os.SameFile(before, after))
}
if err := tc.set(p, "http://127.0.0.1:3426"); err != nil {
t.Fatal(err)
}
if st, _ := os.Stat(p); st.ModTime().Equal(old) {
t.Fatal("a real change wasn't written")
}
})
}
}