9e356e3d99
leo 2026-07-21 回「fyne」。交付「可交付的殼」: - collector/supervisor(純 stdlib,無 fyne):看守 collector direct 子行程、串 stdout JSON 出 狀態(看守中/上次同步/出錯自動重試/已暫停)、崩潰重起、Stop 同步等 loop 收掉(修 Stop→Start 換資料夾時舊 loop 覆寫新狀態的 race)。**sandbox `go test -race ./... 全綠(6 測,含 race)**, 既有 collector 測試零回歸。 - collector/cmd/arcrun-tray(獨立 module,隔開 fyne CGo 依賴):選單列/系統匣 icon+狀態+ 選資料夾(原生對話框)/暫停·繼續/打開資料夾;關窗縮背景;watch_folder 預設 ~/ArcrunRAG 避開 ~/Documents 的 TCC 禁區;wires supervisor。 誠實界定(mindset §7): - **in-process vs 子行程**:leo 偏好 in-process,但 collector 是 package main,真 in-process 需抽核心成 library(動已驗引擎有回歸風險)→ 本版走子行程看守(collector 零改、崩潰隔離),行為對用戶相同; in-process 抽取列可選後續,待 leo 裁。 - **fyne GUI 沙箱編不了**(CGo+GL)→ build/簽章/TCC 授權=leo/地端真機做(紅線②③); fyne API 細節以首次真機 build 為準。README 附建置/簽章/TCC 交接。
123 lines
3.9 KiB
Go
123 lines
3.9 KiB
Go
package supervisor
|
||
|
||
import (
|
||
"os"
|
||
"path/filepath"
|
||
"runtime"
|
||
"sync/atomic"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
// fakeCollector 寫一個假的 collector 執行檔(shell 腳本):忽略參數、印 n 輪 JSON。
|
||
// stayAlive=true → 印完 sleep(模擬常駐 daemon,狀態維持 watching);false → 印完即退出(觸發重起)。
|
||
func fakeCollector(t *testing.T, rounds int, stayAlive bool) string {
|
||
t.Helper()
|
||
if runtime.GOOS == "windows" {
|
||
t.Skip("fake collector script 走 POSIX shell;Windows 測略過(supervisor 邏輯與平台無關)")
|
||
}
|
||
dir := t.TempDir()
|
||
p := filepath.Join(dir, "fake-collector.sh")
|
||
body := "#!/bin/sh\n"
|
||
for i := 0; i < rounds; i++ {
|
||
body += "printf '{\"at\":\"2026-07-21T00:00:0" + itoa(i) + "Z\",\"folder\":\"/tmp/kb\",\"results\":[]}\\n'\n"
|
||
}
|
||
if stayAlive {
|
||
body += "sleep 2\n"
|
||
}
|
||
if err := os.WriteFile(p, []byte(body), 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
return p
|
||
}
|
||
|
||
func itoa(i int) string { return string(rune('0' + i)) }
|
||
|
||
func waitFor(t *testing.T, d time.Duration, cond func() bool, msg string) {
|
||
t.Helper()
|
||
deadline := time.Now().Add(d)
|
||
for time.Now().Before(deadline) {
|
||
if cond() {
|
||
return
|
||
}
|
||
time.Sleep(5 * time.Millisecond)
|
||
}
|
||
t.Fatalf("timeout waiting for: %s", msg)
|
||
}
|
||
|
||
func TestSupervisorParsesRoundsAndWatches(t *testing.T) {
|
||
bin := fakeCollector(t, 2, true)
|
||
s := New(bin, "cfg.json")
|
||
s.Backoff = 20 * time.Millisecond
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
waitFor(t, 2*time.Second, func() bool { return s.Status().Rounds >= 2 }, "2 rounds parsed")
|
||
st := s.Status()
|
||
if st.State != StateWatching {
|
||
t.Fatalf("expected watching, got %q", st.State)
|
||
}
|
||
if st.LastRoundAt.IsZero() {
|
||
t.Fatal("LastRoundAt should be parsed from stdout 'at'")
|
||
}
|
||
if st.LastRoundAt.Year() != 2026 {
|
||
t.Fatalf("LastRoundAt parse wrong: %v", st.LastRoundAt)
|
||
}
|
||
}
|
||
|
||
func TestSupervisorRestartsOnExit(t *testing.T) {
|
||
bin := fakeCollector(t, 1, false)
|
||
s := New(bin, "cfg.json")
|
||
s.Backoff = 20 * time.Millisecond
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
// 假 collector 印一輪就退出 → supervisor 視為非預期退出 → 重起
|
||
waitFor(t, 2*time.Second, func() bool { return s.Status().Restarts >= 2 }, "restarts accumulate")
|
||
}
|
||
|
||
func TestSupervisorStop(t *testing.T) {
|
||
bin := fakeCollector(t, 1, false)
|
||
s := New(bin, "cfg.json")
|
||
s.Backoff = 20 * time.Millisecond
|
||
s.Start()
|
||
waitFor(t, 2*time.Second, func() bool { return s.Status().Restarts >= 1 }, "running")
|
||
s.Stop()
|
||
waitFor(t, 2*time.Second, func() bool { return s.Status().State == StateStopped }, "stopped after Stop()")
|
||
// Stop 後不應再累加重起
|
||
r := s.Status().Restarts
|
||
time.Sleep(120 * time.Millisecond)
|
||
if s.Status().Restarts != r {
|
||
t.Fatalf("restarts kept growing after Stop: %d → %d", r, s.Status().Restarts)
|
||
}
|
||
}
|
||
|
||
// 換資料夾=Stop 緊接 Start;舊 loop 的 setState(stopped) 不可覆寫新 loop 的狀態。
|
||
func TestSupervisorStopThenStartNotLeftStopped(t *testing.T) {
|
||
bin := fakeCollector(t, 2, true) // stayAlive → 新 loop 進 watching
|
||
s := New(bin, "cfg.json")
|
||
s.Backoff = 20 * time.Millisecond
|
||
s.Start()
|
||
waitFor(t, 2*time.Second, func() bool { return s.Status().State == StateWatching }, "first watching")
|
||
s.Stop() // 同步等舊 loop 收掉
|
||
s.Start() // 立刻重起(模擬換資料夾)
|
||
defer s.Stop()
|
||
waitFor(t, 2*time.Second, func() bool { return s.Status().State == StateWatching }, "watching again after Stop→Start")
|
||
// 稍等,確認不會被舊 loop 尾巴改回 stopped
|
||
time.Sleep(100 * time.Millisecond)
|
||
if got := s.Status().State; got != StateWatching {
|
||
t.Fatalf("Stop→Start left state=%q (舊 loop 覆寫了新狀態)", got)
|
||
}
|
||
}
|
||
|
||
func TestSupervisorOnChangeFires(t *testing.T) {
|
||
bin := fakeCollector(t, 2, true)
|
||
s := New(bin, "cfg.json")
|
||
s.Backoff = 20 * time.Millisecond
|
||
var calls int32
|
||
s.SetOnChange(func(Status) { atomic.AddInt32(&calls, 1) })
|
||
s.Start()
|
||
defer s.Stop()
|
||
waitFor(t, 2*time.Second, func() bool { return atomic.LoadInt32(&calls) > 0 }, "onChange fired")
|
||
}
|