tests: the Codex process-list stale tests skip Windows, where the row's copy is the daemon's last check, tested on every OS (#1388)

TestCodexStaleOnlyForMagpiesChange, TestCodexStaleCopiesSayWhichIsLeft
and TestCodexDaemonHelperIsNoStaleCopy test copies told apart from the
process list. StaleCopies lists no processes on Windows (a PowerShell run
each look); there it returns codexDaemonBehind, the daemon as
KeepCodexDaemonCurrent last found it. So the three failed on Windows.

RaykieLee's f3e0672fd added Windows branches that seed codexDaemonSeen
with the answer and assert it back, so on Windows the tests named for
"only magpie's change" and "which copy is left" pass whatever those
guarantees do; its keepCodexDaemon mock is never called there, and one
branch leaves the seeded check behind for later tests. Taken instead:
the three skip on Windows and say why, and
TestCodexStaleOnWindowsIsTheDaemonsLastCheck drives the Windows path
itself, through codexDaemonCheck with keepCodexDaemon faked: none before
a check, none when not behind, the daemon (with its start) when behind,
none after CodexDaemonRestarted, none when magpie restarted it, none once
the check is older than three loops. It reads StaleCopies on Windows and
codexDaemonBehind elsewhere, and resets codexDaemonSeen when done.

Run: go test ./internal/agent on macOS green. Broken by hand, each red:
codexDaemonBehind ignoring Restarted, ignoring the check's age, and
CodexDaemonRestarted not clearing.
Windows box (Windows 11 x64, amd64 test binary of the whole package,
sandbox HOME/USERPROFILE/APPDATA/LOCALAPPDATA/TEMP): main plus the
series' base fails 21 tests; with the whole series only
TestInstallWithoutNodeParses (bash there is WSL's, whose default distro
has no /bin/bash) and the two TestReasonixCLI* (no Go on the box) fail,
both environmental and listed as untested by RaykieLee too. The three skip there
and TestCodexStaleOnWindowsIsTheDaemonsLastCheck passes.

Co-authored-by: RaykieLee <42793273+RaykieLee@users.noreply.github.com>
This commit is contained in:
yetone
2026-10-13 05:48:39 +08:00
co-authored by RaykieLee
parent 9650cd50c1
commit 688c062edf
+82
View File
@@ -1,19 +1,31 @@
package agent
import (
"context"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// staleOnWindows is why the tests of copies told from the process list
// don't run on Windows: StaleCopies lists no processes there, only the
// daemon as last checked (TestCodexStaleOnWindowsIsTheDaemonsLastCheck).
const staleOnWindows = "Windows lists no processes for StaleCopies; see TestCodexStaleOnWindowsIsTheDaemonsLastCheck"
// Codex writes config.toml itself (a project's trust, a notice seen), so a
// copy reopened after magpie's change and then writing there was still
// told to reopen; an app-server daemon left running for another
// CODEX_HOME was counted too (#729). Only magpie's own change counts, for
// copies of this home's Codex.
func TestCodexStaleOnlyForMagpiesChange(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip(staleOnWindows)
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
@@ -73,6 +85,9 @@ func TestCodexStaleOnlyForMagpiesChange(t *testing.T) {
// left is told apart, so the row can say which one and how it is reopened,
// and one app's processes are one copy, not one per process.
func TestCodexStaleCopiesSayWhichIsLeft(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip(staleOnWindows)
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
@@ -137,6 +152,9 @@ func TestCodexStaleCopiesSayWhichIsLeft(t *testing.T) {
// and only killing the process cleared the row. A daemon helper is no
// copy to reopen.
func TestCodexDaemonHelperIsNoStaleCopy(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip(staleOnWindows)
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
@@ -171,3 +189,67 @@ func TestCodexDaemonHelperIsNoStaleCopy(t *testing.T) {
t.Fatalf("a terminal codex: %d stale, want 1", n)
}
}
// On Windows, where listing processes is a PowerShell run, the Codex row's
// stale copy is the daemon as KeepCodexDaemonCurrent last found it (#1388):
// behind magpie's change, until it is restarted, while the check is
// recent. Elsewhere the same last check is kept for the row, beside the
// process list.
func TestCodexStaleOnWindowsIsTheDaemonsLastCheck(t *testing.T) {
home, _ := codexHome(t, "", "")
dir := filepath.Join(home, ".codex")
cfg, list := filepath.Join(dir, "config.toml"), filepath.Join(dir, "magpie-models.json")
os.WriteFile(cfg, []byte("model_provider = \"magpie\"\nmodel_catalog_json = \""+tomlString(list)+"\"\n"), 0o600)
os.WriteFile(list, []byte(`{"models":[{"slug":"magpie/a"}]}`), 0o644)
wasKeep, wasRunning := keepCodexDaemon, running
t.Cleanup(func() {
keepCodexDaemon, running = wasKeep, wasRunning
codexDaemonSeen.Lock()
codexDaemonSeen.at, codexDaemonSeen.check = time.Time{}, provider.CodexDaemonCheck{}
codexDaemonSeen.Unlock()
})
running = func(string) []runningProc { return nil }
since := time.Now().Add(-3 * time.Hour)
answer := provider.CodexDaemonCheck{}
keepCodexDaemon = func(context.Context, string, time.Time) provider.CodexDaemonCheck { return answer }
// the copies the row shows: on Windows StaleCopies itself
copies := func() []StaleCopy {
if runtime.GOOS == "windows" {
return codex(home).StaleCopies()
}
return codexDaemonBehind()
}
if got := copies(); len(got) != 0 {
t.Fatalf("never checked: %+v, want none", got)
}
codexDaemonCheck(cfg)
if got := copies(); len(got) != 0 {
t.Fatalf("a daemon on magpie's list: %+v, want none", got)
}
answer = provider.CodexDaemonCheck{Behind: true, Attached: 1, Since: since}
codexDaemonCheck(cfg)
if got := copies(); len(got) != 1 || got[0].Kind != "daemon" || !got[0].Since.Equal(since) {
t.Fatalf("a daemon behind: %+v, want the daemon since %v", got, since)
}
// restarted by the user from the row
CodexDaemonRestarted()
if got := copies(); len(got) != 0 {
t.Fatalf("restarted: %+v, want none", got)
}
// restarted by magpie, no session on it
answer = provider.CodexDaemonCheck{Behind: true, Restarted: true, Since: since}
codexDaemonCheck(cfg)
if got := copies(); len(got) != 0 {
t.Fatalf("restarted by magpie: %+v, want none", got)
}
// a check from longer ago than the loop looks says nothing now
answer = provider.CodexDaemonCheck{Behind: true, Attached: 1, Since: since}
codexDaemonCheck(cfg)
codexDaemonSeen.Lock()
codexDaemonSeen.at = time.Now().Add(-4 * codexDaemonEvery)
codexDaemonSeen.Unlock()
if got := copies(); len(got) != 0 {
t.Fatalf("an old check: %+v, want none", got)
}
}