f903a3f53f
leo 回報 Windows daemon 三症狀,查完 ①③ 同根,根在「雲端控制地端」這個設計。 【①③ 真兇】雲端 extractor_config 是**全租戶共用一把 KV** (arcrun:portal.ts:43 portalTenant = worker 層級變數,不分用戶), 任一處設了 claude → 所有人的 daemon 都收到 claude。沒裝 Claude Code 的機器 FindClaudeBin 失敗 → 每檔萃取 failed、一張卡都沒建;想去 portal 改回 gemini, checkbox 卻恆 disabled(claude_available 恆 false,因 daemon 從未實作 report-capabilities 回報 → daemon_caps KV 永遠空)⇒ 用戶自己解不開。 awindhon 實證:雲端同步成功、Gemini key 有效、零張卡,config.json extractor="claude"。 【leo 裁示】「地端要用什麼模型就在 daemon 上輸入 API Key 設置,而不是雲端設置後 控制地端」「地端先限制 Gemini API Key 配合客戶要求」「雲端就是 Workers AI」。 本次(daemon 端): - addOrUpdateAccount 不再接受雲端下發的 extractor/gemini_api_key/llm_model, 只收連線欄位。t126「每帳號一份萃取設定」照舊保留——t126 修的是「存在哪一層」, 本次改的是「值從哪來」,兩者正交。 - 托盤新增「AI 設定…」:使用者自己填 Gemini API Key,寫本地 config 後立即生效。 - 萃取一律走 gemini:殘留的 extractor:"claude" 正規化為 gemma;claude 路退役。 ⚠️ 這不是「自動偵測有無 claude」(leo 07-27 已否決的 B 案),是整條路先不支援。 - 清掉隨之死亡的 claude_bin 回寫(死代碼=錯誤的環境信號)。 - 托盤單一實例(症狀②):pidfile + 跨平台 processAlive。 mac 之前不多開是借 macOS Launch Services 的巧合,Windows 沒有該層 ⇒ 每點一次多一個 icon。 Unix 用 signal 0(EPERM 也算活著,測試抓到的實際 bug)/Windows 用 OpenProcess+ExitCode。 測試:collector 全綠、tray 全綠。5 個原本用 claude stub 的測試改走**真實 gemma 路** (httptest 替身注入 gemmaBaseURL),不是改斷言充綠;t126②③ 兩案翻轉成 「雲端下發一律被忽略」的回歸守衛;新增 4 案 single-instance。 未送達:本 commit 只到 code,尚未打包出貨;雲端側(刪 portal AI 設定區塊、 extractor 下發、admin/extractor)未動,待部署授權。CP rag-beta 步驟仍為 ◐。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
228 lines
7.6 KiB
Go
228 lines
7.6 KiB
Go
// sync_status_test.go — t91/t92 狀態檔與 fallback 路徑邏輯單元測試。
|
||
package main
|
||
|
||
import (
|
||
"os"
|
||
"path/filepath"
|
||
"runtime"
|
||
"testing"
|
||
)
|
||
|
||
// ── FindClaudeBin fallback(t92)──────────────────────────────────────────────
|
||
|
||
// TestFindClaudeBinPathHit:hint 在 PATH 能找到時,直接回傳,不進 fallback。
|
||
func TestFindClaudeBinPathHit(t *testing.T) {
|
||
if runtime.GOOS == "windows" {
|
||
t.Skip("此測試僅在 Unix 執行")
|
||
}
|
||
dir := t.TempDir()
|
||
bin := filepath.Join(dir, "claude")
|
||
if err := os.WriteFile(bin, []byte("#!/bin/sh\necho fake"), 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
t.Setenv("PATH", dir+":"+os.Getenv("PATH"))
|
||
|
||
orig := claudeFallbackPaths
|
||
claudeFallbackPaths = nil
|
||
defer func() { claudeFallbackPaths = orig }()
|
||
|
||
found, err := FindClaudeBin("")
|
||
if err != nil {
|
||
t.Fatalf("PATH 有 claude 卻找不到:%v", err)
|
||
}
|
||
if found != bin {
|
||
t.Errorf("found=%q want=%q", found, bin)
|
||
}
|
||
}
|
||
|
||
// TestFindClaudeBinFallback:PATH 找不到時,依序掃 claudeFallbackPaths 並命中。
|
||
// 模擬 Finder 啟動的 GUI app PATH 不含 /opt/homebrew/bin 的情境(t92 根因)。
|
||
func TestFindClaudeBinFallback(t *testing.T) {
|
||
if runtime.GOOS == "windows" {
|
||
t.Skip("此測試僅在 Unix 執行")
|
||
}
|
||
dir := t.TempDir()
|
||
bin := filepath.Join(dir, "claude")
|
||
if err := os.WriteFile(bin, []byte("#!/bin/sh\necho fake"), 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
orig := claudeFallbackPaths
|
||
// 清單:第一個不存在(跳過)、第二個是 bin(命中)
|
||
claudeFallbackPaths = []string{filepath.Join(dir, "no-such"), bin}
|
||
defer func() { claudeFallbackPaths = orig }()
|
||
|
||
found, err := FindClaudeBin("/definitely/not/there")
|
||
if err != nil {
|
||
t.Fatalf("fallback 應找到 %s,got err=%v", bin, err)
|
||
}
|
||
if found != bin {
|
||
t.Errorf("found=%q want=%q", found, bin)
|
||
}
|
||
}
|
||
|
||
// TestFindClaudeBinAllMiss:PATH 和所有 fallback 都找不到 → 回誠實錯誤。
|
||
func TestFindClaudeBinAllMiss(t *testing.T) {
|
||
orig := claudeFallbackPaths
|
||
claudeFallbackPaths = []string{"/does/not/exist/claude"}
|
||
defer func() { claudeFallbackPaths = orig }()
|
||
|
||
_, err := FindClaudeBin("/also/does/not/exist")
|
||
if err == nil {
|
||
t.Fatal("全部找不到應回錯誤")
|
||
}
|
||
}
|
||
|
||
// TestFindClaudeBinNotExecutable:fallback 路徑存在但不可執行,不應採用。
|
||
func TestFindClaudeBinNotExecutable(t *testing.T) {
|
||
if runtime.GOOS == "windows" {
|
||
t.Skip("此測試僅在 Unix 執行")
|
||
}
|
||
dir := t.TempDir()
|
||
notExec := filepath.Join(dir, "claude")
|
||
if err := os.WriteFile(notExec, []byte("not exec"), 0o644); err != nil { // 0644 無執行位
|
||
t.Fatal(err)
|
||
}
|
||
orig := claudeFallbackPaths
|
||
claudeFallbackPaths = []string{notExec}
|
||
defer func() { claudeFallbackPaths = orig }()
|
||
|
||
_, err := FindClaudeBin("/no/such")
|
||
if err == nil {
|
||
t.Fatal("不可執行的檔不應被當成可用 claude")
|
||
}
|
||
}
|
||
|
||
// ── SyncStatus 狀態檔寫入/讀取(t91)────────────────────────────────────────
|
||
|
||
// TestSyncStatusRoundTrip:寫進去的結構讀出來完全相同。
|
||
func TestSyncStatusRoundTrip(t *testing.T) {
|
||
dir := t.TempDir()
|
||
path := filepath.Join(dir, "status.json")
|
||
|
||
want := SyncStatus{
|
||
LastSync: "2026-07-28T10:00:00Z",
|
||
ExtractedOK: 3,
|
||
ExtractFailed: 1,
|
||
Failures: []ExtractFail{
|
||
{Path: "重要文件.md", Error: "找不到 Claude 指令"},
|
||
},
|
||
ExtractorOK: false,
|
||
ExtractorError: "找不到 Claude 指令",
|
||
}
|
||
if err := SaveSyncStatus(path, want); err != nil {
|
||
t.Fatalf("SaveSyncStatus 失敗:%v", err)
|
||
}
|
||
got, err := LoadSyncStatus(path)
|
||
if err != nil {
|
||
t.Fatalf("LoadSyncStatus 失敗:%v", err)
|
||
}
|
||
if got.LastSync != want.LastSync {
|
||
t.Errorf("LastSync=%q want=%q", got.LastSync, want.LastSync)
|
||
}
|
||
if got.ExtractedOK != want.ExtractedOK || got.ExtractFailed != want.ExtractFailed {
|
||
t.Errorf("counts: ok=%d fail=%d, want ok=%d fail=%d",
|
||
got.ExtractedOK, got.ExtractFailed, want.ExtractedOK, want.ExtractFailed)
|
||
}
|
||
if len(got.Failures) != 1 || got.Failures[0].Path != "重要文件.md" {
|
||
t.Errorf("Failures=%+v", got.Failures)
|
||
}
|
||
if got.ExtractorOK || got.ExtractorError != want.ExtractorError {
|
||
t.Errorf("extractor: ok=%v err=%q", got.ExtractorOK, got.ExtractorError)
|
||
}
|
||
}
|
||
|
||
// TestLoadSyncStatusMissingFile:檔不存在回零值+error,不 panic。
|
||
func TestLoadSyncStatusMissingFile(t *testing.T) {
|
||
_, err := LoadSyncStatus(filepath.Join(t.TempDir(), "no-status.json"))
|
||
if err == nil {
|
||
t.Fatal("不存在的檔應回 error")
|
||
}
|
||
}
|
||
|
||
// TestStatusFilePath:依 manifest 路徑回傳同目錄 status.json。
|
||
func TestStatusFilePath(t *testing.T) {
|
||
got := StatusFilePath("/home/leo/.arcrun-rag/manifest.json")
|
||
want := "/home/leo/.arcrun-rag/status.json"
|
||
if got != want {
|
||
t.Errorf("StatusFilePath=%q want=%q", got, want)
|
||
}
|
||
}
|
||
|
||
// TestSyncNowSignalPath:依 manifest 路徑回傳同目錄 sync-now(t98)。
|
||
func TestSyncNowSignalPath(t *testing.T) {
|
||
got := SyncNowSignalPath("/home/leo/.arcrun-rag/manifest.json")
|
||
want := "/home/leo/.arcrun-rag/sync-now"
|
||
if got != want {
|
||
t.Errorf("SyncNowSignalPath=%q want=%q", got, want)
|
||
}
|
||
}
|
||
|
||
// ── RunDirectOnce 寫狀態檔(t91 整合)────────────────────────────────────────
|
||
|
||
// TestRunDirectOnceWritesStatus:RunDirectOnce 完成後應在 manifest 同目錄寫出 status.json。
|
||
// 使用空資料夾(無事件)確保不觸發 HTTP,只驗 extractor 預檢結果與 LastSync。
|
||
func TestRunDirectOnceWritesStatus(t *testing.T) {
|
||
// 空資料夾 → 零事件 → 無 HTTP 呼叫
|
||
root := t.TempDir()
|
||
manifestDir := t.TempDir()
|
||
// t176:extractor 寫 "claude" 也會被正規化成 gemma(claude 路已不支援),
|
||
// 故預檢看的是「Gemini 金鑰有沒有填」——這裡填了,ExtractorOK 應為 true。
|
||
cfg := &DirectConfig{
|
||
WatchFolders: []string{root},
|
||
Manifest: filepath.Join(manifestDir, "manifest.json"),
|
||
CypherURL: "https://unused.example", Namespace: "demo",
|
||
Extractor: "claude", GeminiAPIKey: "k-test",
|
||
MaxRemoved: DefaultMaxRemovedRatio,
|
||
}
|
||
RunDirectOnce(cfg, false)
|
||
|
||
statusPath := StatusFilePath(cfg.Manifest)
|
||
if _, err := os.Stat(statusPath); err != nil {
|
||
t.Fatalf("RunDirectOnce 後應存在 status.json:%v", err)
|
||
}
|
||
st, err := LoadSyncStatus(statusPath)
|
||
if err != nil {
|
||
t.Fatalf("LoadSyncStatus 失敗:%v", err)
|
||
}
|
||
if !st.ExtractorOK {
|
||
t.Errorf("可用 stub claude 時 ExtractorOK 應為 true,ExtractorError=%q", st.ExtractorError)
|
||
}
|
||
if st.LastSync == "" {
|
||
t.Error("LastSync 應非空")
|
||
}
|
||
// 空資料夾 → 零事件
|
||
if st.ExtractedOK != 0 || st.ExtractFailed != 0 {
|
||
t.Errorf("空資料夾應零計數,got ok=%d fail=%d", st.ExtractedOK, st.ExtractFailed)
|
||
}
|
||
}
|
||
|
||
// TestRunDirectOnceWritesStatusExtractorFail:找不到 claude 時 status 應記 ExtractorOK=false。
|
||
func TestRunDirectOnceWritesStatusExtractorFail(t *testing.T) {
|
||
orig := claudeFallbackPaths
|
||
claudeFallbackPaths = []string{} // 空 fallback,確保找不到
|
||
defer func() { claudeFallbackPaths = orig }()
|
||
|
||
root := t.TempDir()
|
||
manifestDir := t.TempDir()
|
||
cfg := &DirectConfig{
|
||
WatchFolders: []string{root},
|
||
Manifest: filepath.Join(manifestDir, "manifest.json"),
|
||
CypherURL: "https://unused.example", Namespace: "demo",
|
||
Extractor: "claude", ClaudeBin: "/no/such/claude",
|
||
MaxRemoved: DefaultMaxRemovedRatio,
|
||
}
|
||
RunDirectOnce(cfg, false)
|
||
|
||
st, err := LoadSyncStatus(StatusFilePath(cfg.Manifest))
|
||
if err != nil {
|
||
t.Fatalf("LoadSyncStatus 失敗:%v", err)
|
||
}
|
||
if st.ExtractorOK {
|
||
t.Error("找不到 claude 時 ExtractorOK 應為 false")
|
||
}
|
||
if st.ExtractorError == "" {
|
||
t.Error("ExtractorError 應有白話說明")
|
||
}
|
||
}
|