fix(weixin): fallback sync cursor store when home is unusable
This commit is contained in:
parent
7b3f47128f
commit
12d0bc2514
2 changed files with 43 additions and 1 deletions
|
|
@ -41,7 +41,24 @@ type contextTokensFile struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func picoclawHomeDir() string {
|
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 {
|
func genWeixinAccountKey(cfg config.WeixinConfig) string {
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,9 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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) {
|
func TestSessionPauseGuard(t *testing.T) {
|
||||||
ch := &WeixinChannel{
|
ch := &WeixinChannel{
|
||||||
typingCache: make(map[string]typingTicketCacheEntry),
|
typingCache: make(map[string]typingTicketCacheEntry),
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue