package repo import ( "context" "testing" ) // platform_session_id 必须只发给归属方 —— 生产上 pi 的会话 id 被推给了抄送方 dsh, // DSH 在自己磁盘上找不到那个文件,按 N-8 抛错,邮件静默消失。 func TestPlatformSessionFor_ReturnsOwnerFromMirror(t *testing.T) { setupTestDB(t) ctx := context.Background() seedPlatformAgent(t, "pi") seedPlatformAgent(t, "dsh") // pi 上报一条平台会话 if err := ReplacePlatformSessions(ctx, "pi", []PlatformSession{ {PlatformID: "pid-pi-1", Workspace: "/w", Slug: "项目定位", Title: "项目定位"}, }); err != nil { t.Fatalf("ReplacePlatformSessions: %v", err) } id, err := AdoptPlatformSession(ctx, "pi", "pid-pi-1", "项目定位", "/w", "项目定位") if err != nil { t.Fatalf("AdoptPlatformSession: %v", err) } pid, owner := PlatformSessionFor(ctx, id) if pid != "pid-pi-1" { t.Errorf("platformID = %q, want pid-pi-1", pid) } if owner != "pi" { t.Errorf("owner = %q, want pi(镜像里 agent_name=pi)", owner) } } // 未接管的普通会话不该返回任何 platform id。 func TestPlatformSessionFor_PlainSession(t *testing.T) { setupTestDB(t) ctx := context.Background() seedAgent(t, "dsh", 20) id, err := CreateSession(ctx, nil, "dsh", "普通会话", "/w") if err != nil { t.Fatalf("CreateSession: %v", err) } pid, owner := PlatformSessionFor(ctx, id) if pid != "" || owner != "" { t.Errorf("got (%q,%q), want ('','')", pid, owner) } } // 镜像那行被整表替换掉(平台侧删了会话)时退回 sessions.from_agent, // 而不是让 owner 变空 —— 变空会让归属方也收不到 platform_session_id。 func TestPlatformSessionFor_MirrorGoneFallsBackToFromAgent(t *testing.T) { setupTestDB(t) ctx := context.Background() seedPlatformAgent(t, "pi") if err := ReplacePlatformSessions(ctx, "pi", []PlatformSession{ {PlatformID: "pid-pi-2", Workspace: "/w", Slug: "s2", Title: "t2"}, }); err != nil { t.Fatalf("ReplacePlatformSessions: %v", err) } id, err := AdoptPlatformSession(ctx, "pi", "pid-pi-2", "s2", "/w", "t2") if err != nil { t.Fatalf("AdoptPlatformSession: %v", err) } // 平台侧删了这条会话 → 心跳整表替换成空 if err := ReplacePlatformSessions(ctx, "pi", []PlatformSession{}); err != nil { t.Fatalf("ReplacePlatformSessions(empty): %v", err) } pid, owner := PlatformSessionFor(ctx, id) if pid != "pid-pi-2" { t.Errorf("platformID = %q, want pid-pi-2", pid) } if owner != "pi" { t.Errorf("owner = %q, want pi(退回 sessions.from_agent)", owner) } }