internal/workflow:directLastActivity 排除跨 issue 引用事件(#34)
同步 agents #34/#38/#40 三個停滯判定修補到 Go 端:
- Source 介面新增 IssueTimeline(GET /repos/{o}/{r}/issues/{n}/timeline,分頁),
giteaapi.go 與 cmd/xcheck 同步實作
- 新增 directLastActivity:updated_at 晚於全部已知直接活動時拉 timeline 核對,
排除 comment_ref/issue_ref 引用事件後取最晚;全為引用事件回退已知直接活動
最晚時刻(agents #40);timeline 失敗保守沿用 updated_at
- considerIssue/considerPull 對自身 updated_at、childActivity 對子項 updated_at
(含子項 created_at 入已知活動)套用同一排除邏輯(agents #38)
- 回歸測試:子項 updated_at 被引用刷新(TestRunStalledChildXrefRefresh)、
timeline 全引用事件回退與拉取失敗保守路徑(TestRunStalledTimelineAllXrefFallback)
- 修正 workflow_test.go:timelines 須在 ScanStalled 呼叫前設定
驗證:go build/vet/test 全綠;xcheck 對真實 API 掃描,
stalled 輸出與 gitea.py(agents @ 78769ec)位元組一致(5833 bytes)。
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
// stalled_entry_test.go — RunStalled/ResolveIssueXrefs 高階入口的單元測試
|
||||
//(#7 CLI 接線;資料源注入假實作,離線可測)。
|
||||
// (#7 CLI 接線;資料源注入假實作,離線可測)。
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -14,10 +16,22 @@ type fakeStalledSource struct {
|
||||
username string
|
||||
// getIssueErr 模擬 GetIssue 失敗(xrefs 的 404 路徑)。
|
||||
getIssueErr error
|
||||
// timeline[owner/repo/number]:issue timeline 事件(無則回空清單,
|
||||
// 模擬 API 成功但無事件;timelineErr 模擬拉取失敗)。
|
||||
timeline map[string][]TimelineEvent
|
||||
timelineErr map[string]error
|
||||
}
|
||||
|
||||
func (f *fakeStalledSource) CurrentUser() (string, error) { return f.username, nil }
|
||||
|
||||
func (f *fakeStalledSource) IssueTimeline(owner, repo string, number int64) ([]TimelineEvent, error) {
|
||||
key := fmt.Sprintf("%s/%s/%d", owner, repo, number)
|
||||
if err := f.timelineErr[key]; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f.timeline[key], nil
|
||||
}
|
||||
|
||||
func (f *fakeStalledSource) GetIssue(owner, repo string, number int64) (Issue, error) {
|
||||
if f.getIssueErr != nil {
|
||||
return Issue{}, f.getIssueErr
|
||||
@@ -38,7 +52,7 @@ func (e *notFoundError) Error() string { return "404" }
|
||||
func newStalledFixture() *fakeStalledSource {
|
||||
return &fakeStalledSource{
|
||||
fakeSource: &fakeSource{
|
||||
orgs: []string{"alterminal"},
|
||||
orgs: []string{"alterminal"},
|
||||
repos: map[string][]Repo{"alterminal": {
|
||||
{Name: "agents", HasPullRequests: boolPtr(true)},
|
||||
{Name: "teai", HasPullRequests: boolPtr(true)},
|
||||
@@ -229,3 +243,92 @@ func TestResolveIssueXrefsEmptyBody(t *testing.T) {
|
||||
}
|
||||
_ = time.Now // time 僅供 iso/at 使用;保留 import。
|
||||
}
|
||||
|
||||
// TestRunStalledChildXrefRefresh 回歸測試(agents #38、teai #34):
|
||||
// 子項 updated_at 被跨 issue 引用(comment_ref)刷新時,子項有效活動
|
||||
// 應經 directLastActivity 排除引用事件後採計,父追蹤項的停滯時鐘
|
||||
// 不被虛假刷新。
|
||||
func TestRunStalledChildXrefRefresh(t *testing.T) {
|
||||
src := newStalledFixture()
|
||||
// 子項 teai#2:直接活動為 created(-500) 與留言(-90),
|
||||
// updated_at 卻是 -10(他處留言提及刷新)。
|
||||
issue := src.issues["alterminal/teai"][2]
|
||||
issue.UpdatedAt = iso(-10)
|
||||
src.issues["alterminal/teai"][2] = issue
|
||||
// timeline:-10 的刷新是 comment_ref(引用事件,排除),
|
||||
// -90 有直接事件(label)可對應 → 有效活動 -90。
|
||||
src.timeline = map[string][]TimelineEvent{
|
||||
"alterminal/teai/2": {
|
||||
{Type: "label", CreatedAt: iso(-90)},
|
||||
{Type: "comment_ref", CreatedAt: iso(-10)},
|
||||
},
|
||||
}
|
||||
// 門檻 1 小時:父項 last_activity 取子項有效活動 -90 → 停滯 1.5h 列入;
|
||||
// 若誤採被刷新的 -10,父項與子項都不會列入。
|
||||
items, err := RunStalled(src, 1, at(0))
|
||||
if err != nil {
|
||||
t.Fatalf("RunStalled: %v", err)
|
||||
}
|
||||
if len(items) != 2 {
|
||||
t.Fatalf("應有兩筆(父追蹤項+子項),得到 %d:%+v", len(items), items)
|
||||
}
|
||||
var parent *Item
|
||||
for i := range items {
|
||||
if items[i].Repo == "alterminal/agents" && items[i].Number == 1 {
|
||||
parent = &items[i]
|
||||
}
|
||||
}
|
||||
if parent == nil {
|
||||
t.Fatalf("找不到 agents#1:%+v", items)
|
||||
}
|
||||
if parent.Reason != "parent-tracking" {
|
||||
t.Fatalf("agents#1 應為 parent-tracking,得到 %s", parent.Reason)
|
||||
}
|
||||
if parent.StalledHours != 1.5 {
|
||||
t.Fatalf("父項停滯應以子項有效活動(-90m → 1.5h)計,得到 %.1f", parent.StalledHours)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunStalledTimelineAllXrefFallback 回歸測試(agents #40、teai #34):
|
||||
// issue 自身 updated_at 晚於全部已知直接活動、且 timeline 全為引用事件時,
|
||||
// 回退已知直接活動的最晚時刻(不高估活動、不低估停滯);
|
||||
// timeline 拉取失敗則保守沿用 updated_at。
|
||||
func TestRunStalledTimelineAllXrefFallback(t *testing.T) {
|
||||
src := newStalledFixture()
|
||||
setIssueBody(src, "alterminal", "agents", 1, "") // 移除父項引用,teai#2 獨立判定
|
||||
delete(src.comments, "alterminal/teai/2")
|
||||
// agents#1 給一則近期留言,避免本身停滯(10h)干擾本測試的斷言。
|
||||
src.comments["alterminal/agents/1"] = []Comment{
|
||||
{CreatedAt: iso(-10), User: &User{UserName: "alex"}},
|
||||
}
|
||||
// teai#2:僅有 created(-500) 此一已知直接活動,
|
||||
// updated_at -10 完全由他處提及刷新。
|
||||
issue := src.issues["alterminal/teai"][2]
|
||||
issue.UpdatedAt = iso(-10)
|
||||
src.issues["alterminal/teai"][2] = issue
|
||||
// 情境一:timeline 全為引用事件 → 回退 knownMax(created -500)
|
||||
// → 停滯 8.3h,超過預設門檻 4h,列入。
|
||||
src.timeline = map[string][]TimelineEvent{
|
||||
"alterminal/teai/2": {{Type: "issue_ref", CreatedAt: iso(-10)}},
|
||||
}
|
||||
items, err := RunStalled(src, 0, at(0))
|
||||
if err != nil {
|
||||
t.Fatalf("RunStalled: %v", err)
|
||||
}
|
||||
if len(items) != 1 || items[0].Number != 2 {
|
||||
t.Fatalf("全引用事件應回退直接活動 → teai#2 停滯 8.3h 列入,得到 %+v", items)
|
||||
}
|
||||
if items[0].StalledHours != 8.3 {
|
||||
t.Fatalf("回退 created(-500) → 停滯應 8.3h,得到 %.1f", items[0].StalledHours)
|
||||
}
|
||||
// 情境二:timeline 拉取失敗 → 保守沿用 updated_at(-10) → 0.2h 未達門檻,不列入。
|
||||
src.timeline = nil
|
||||
src.timelineErr = map[string]error{"alterminal/teai/2": errors.New("boom")}
|
||||
items, err = RunStalled(src, 0, at(0))
|
||||
if err != nil {
|
||||
t.Fatalf("RunStalled: %v", err)
|
||||
}
|
||||
if len(items) != 0 {
|
||||
t.Fatalf("timeline 失敗應保守沿用 updated_at → 不列入,得到 %+v", items)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user