fix(weixin): fallback sync cursor store when home is unusable

This commit is contained in:
Alix-007 2026-03-26 02:01:23 +08:00
parent 7b3f47128f
commit 12d0bc2514
2 changed files with 43 additions and 1 deletions

View file

@ -41,7 +41,24 @@ type contextTokensFile struct {
}
func picoclawHomeDir() string {
return config.GetHome()
if home := os.Getenv(config.EnvHome); home != "" {
return home
}
if userHome, err := os.UserHomeDir(); err == nil && userHome != "" {
home := filepath.Join(userHome, ".picoclaw")
syncDir := filepath.Join(home, "channels", "weixin", "sync")
mkErr := os.MkdirAll(syncDir, 0o700)
if mkErr == nil {
return home
}
logger.WarnCF("weixin", "Default picoclaw home is not writable; using temp directory for sync cursor", map[string]any{
"path": home,
"error": mkErr.Error(),
})
}
return filepath.Join(os.TempDir(), "picoclaw")
}
func genWeixinAccountKey(cfg config.WeixinConfig) string {

View file

@ -7,7 +7,9 @@ import (
"errors"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"testing"
"time"
@ -269,6 +271,29 @@ func TestBuildWeixinSyncBufPathUsesPicoclawHome(t *testing.T) {
}
}
func TestBuildWeixinSyncBufPathFallsBackWhenHomeIsUnusable(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("HOME handling differs on windows")
}
t.Setenv(config.EnvHome, "")
unusableHome := filepath.Join(t.TempDir(), "home-file")
if err := os.WriteFile(unusableHome, []byte("not-a-dir"), 0o600); err != nil {
t.Fatalf("WriteFile(unusableHome) error = %v", err)
}
t.Setenv("HOME", unusableHome)
wxCfg := config.WeixinConfig{
BaseURL: "https://ilinkai.weixin.qq.com/",
}
wxCfg.SetToken("token-123")
got := buildWeixinSyncBufPath(wxCfg)
wantDir := filepath.Join(os.TempDir(), "picoclaw", "channels", "weixin", "sync")
if filepath.Dir(got) != wantDir {
t.Fatalf("sync path dir = %q, want %q", filepath.Dir(got), wantDir)
}
}
func TestSessionPauseGuard(t *testing.T) {
ch := &WeixinChannel{
typingCache: make(map[string]typingTicketCacheEntry),