## 主体是 gofmt
`server/` 下 20 个文件的改动里,**142/147 行是注释与缩进** ——
Go 1.19+ 的 gofmt 把块注释里的 `// x` 规范成 `//\tx`。
功能零变化。
## 一处实质:init.sql 补 parent_session_id
`ALTER TABLE sessions ADD COLUMN IF NOT EXISTS parent_session_id
TEXT REFERENCES sessions(session_id)`
远端已在 `db/migrate.go` 的 `sqliteAddColumns` 结构迁移通道里加了同一列;
init.sql 是**全新库**的建库脚本,缺这列会让新库少一个字段。
- `IF NOT EXISTS` ⇒ 与结构迁移通道幂等,两者并存无害
- 默认空 = 根节点;**不据历史数据猜父子**(猜错会让线索拓扑失真,比没有更糟)
## 新增 handler/ratelimit_test.go(162 行)
速率限制的判据。此前这一块没有独立判据。
## 验证(本机实跑,非推断)
go build ./... 通过
go vet ./internal/{handler,repo,db,sse}/ 通过
go test -count=1(handler/repo/db/sse/blob)
handler 41.3s ok
repo 101.9s ok
db 1.1s ok
sse 0.01s ok
blob 0.08s ok
`-count=1` 是刻意的:第一遍跑出来全是 `(cached)`,那种绿灯不算数。
480 lines
18 KiB
Go
480 lines
18 KiB
Go
package repo
|
||
|
||
import (
|
||
"context"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/agentmail/gateway/internal/db"
|
||
"github.com/google/uuid"
|
||
)
|
||
|
||
// seedPlatformAgent 注册一个 Agent,平台会话镜像与它绑定。
|
||
// 与 quota_test.go 的 seedAgent 区分开:那个要指定 default_rounds,这里不关心。
|
||
func seedPlatformAgent(t *testing.T, name string) {
|
||
t.Helper()
|
||
if _, err := db.DB.ExecContext(context.Background(),
|
||
`INSERT INTO agents (agent_name, secret, platform, status) VALUES ($1, 'x', $1, 'online')`,
|
||
name); err != nil {
|
||
t.Fatalf("seed agent %s: %v", name, err)
|
||
}
|
||
}
|
||
|
||
// seedSessionWS 建一个带 workspace 与别名的会话。
|
||
func seedSessionWS(t *testing.T, alias, workspace, subject string) uuid.UUID {
|
||
t.Helper()
|
||
var id uuid.UUID
|
||
err := db.DB.QueryRowContext(context.Background(), `
|
||
INSERT INTO sessions (session_alias, workspace, from_agent, subject, alias_source)
|
||
VALUES ($1, $2, 'admin', $3, 'platform')
|
||
RETURNING session_id
|
||
`, alias, workspace, subject).Scan(&id)
|
||
if err != nil {
|
||
t.Fatalf("seed session %s: %v", alias, err)
|
||
}
|
||
return id
|
||
}
|
||
|
||
// seedMailWS 插一封带明确 to_workspace 的邮件。
|
||
func seedMailWS(t *testing.T, sessionID uuid.UUID, from, to, toWS, subject string) uuid.UUID {
|
||
t.Helper()
|
||
var id uuid.UUID
|
||
err := db.DB.QueryRowContext(context.Background(), `
|
||
INSERT INTO mails (session_id, from_name, from_workspace, to_name, to_workspace,
|
||
subject, body, cc_list, created_at)
|
||
VALUES ($1, $2, '', $3, $4, $5, 'body', '[]', $6)
|
||
RETURNING mail_id
|
||
`, sessionID, from, to, toWS, subject, nextSeedTime()).Scan(&id)
|
||
if err != nil {
|
||
t.Fatalf("seed mail: %v", err)
|
||
}
|
||
return id
|
||
}
|
||
|
||
// 这个测试是「会话别名没有正确显示曾经发生在工作区下的会话」那次故障的回归。
|
||
//
|
||
// 旧实现按 mails 反推工作区,条件是
|
||
// `to_workspace = $path OR from_workspace = $path`。
|
||
// 而 Agent 回信时 from_workspace 存的是 **Agent 名**(如 "dsh")而不是路径,
|
||
// 于是一旦会话里只剩 Agent 的回信可匹配,反推就落空、别名列不出来。
|
||
// 现在 workspace 存在会话自己身上,与邮件里那些脏数据无关。
|
||
func TestSuggestSessionCandidatesUsesSessionWorkspace(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "dsh")
|
||
|
||
sid := seedSessionWS(t, "brisk-harbor", "/home/program/agentmail", "缓存选型")
|
||
// 只有 Agent 的回信:from_workspace 是脏的(Agent 名),to_workspace 是人类(空)
|
||
if _, err := db.DB.ExecContext(context.Background(), `
|
||
INSERT INTO mails (session_id, from_name, from_workspace, to_name, to_workspace,
|
||
subject, body, cc_list, created_at)
|
||
VALUES ($1, 'dsh', 'dsh', 'admin', '', 'Re: 缓存选型', 'body', '[]', $2)
|
||
`, sid, nextSeedTime()); err != nil {
|
||
t.Fatalf("seed agent reply: %v", err)
|
||
}
|
||
|
||
got, err := SuggestSessionCandidates(context.Background(), "admin", "dsh", "/home/program/agentmail")
|
||
if err != nil {
|
||
t.Fatalf("SuggestSessionCandidates: %v", err)
|
||
}
|
||
if len(got) != 1 {
|
||
t.Fatalf("应有 1 个候选,实际 %d —— 会话的 workspace 列没被用上", len(got))
|
||
}
|
||
if got[0].Alias != "brisk-harbor" {
|
||
t.Errorf("别名错误:%q", got[0].Alias)
|
||
}
|
||
if got[0].Source != "mail" {
|
||
t.Errorf("来源应为 mail,实际 %q", got[0].Source)
|
||
}
|
||
if got[0].Title != "缓存选型" {
|
||
t.Errorf("标题应带出来:%q", got[0].Title)
|
||
}
|
||
}
|
||
|
||
// 历史会话的 workspace 列是空的(新加的列),必须回退到 mails.to_workspace 反推,
|
||
// 否则升级后所有老会话一夜之间从候选列表里消失。
|
||
func TestSuggestSessionCandidatesFallsBackToMails(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "opencode")
|
||
|
||
// workspace 留空,模拟升级前建立的会话
|
||
sid := seedSessionWS(t, "legacy-thread", "", "老线索")
|
||
seedMailWS(t, sid, "admin", "opencode", "/home/legacy", "老线索")
|
||
|
||
got, err := SuggestSessionCandidates(context.Background(), "admin", "opencode", "/home/legacy")
|
||
if err != nil {
|
||
t.Fatalf("SuggestSessionCandidates: %v", err)
|
||
}
|
||
if len(got) != 1 || got[0].Alias != "legacy-thread" {
|
||
t.Fatalf("老会话应能靠 mails 反推出来,实际 %+v", got)
|
||
}
|
||
}
|
||
|
||
// 工作区不匹配的会话不能出现:候选项点下去就会被填进 session 位,
|
||
// 而 session 位是三态语义 —— 指向别处的会话会直接 404「无法送达」。
|
||
func TestSuggestSessionCandidatesFiltersByWorkspace(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "dsh")
|
||
|
||
mine := seedSessionWS(t, "here-thread", "/home/a", "本区")
|
||
seedMailWS(t, mine, "admin", "dsh", "/home/a", "本区")
|
||
other := seedSessionWS(t, "there-thread", "/home/b", "别区")
|
||
seedMailWS(t, other, "admin", "dsh", "/home/b", "别区")
|
||
|
||
got, err := SuggestSessionCandidates(context.Background(), "admin", "dsh", "/home/a")
|
||
if err != nil {
|
||
t.Fatalf("SuggestSessionCandidates: %v", err)
|
||
}
|
||
if len(got) != 1 || got[0].Alias != "here-thread" {
|
||
t.Fatalf("只应给出本工作区的会话,实际 %+v", got)
|
||
}
|
||
}
|
||
|
||
// path 为空(地址写成 `dsh` 而不带 @/path)时不按工作区过滤:
|
||
// 用户还没写到 path 段就该看到全部可续的会话。
|
||
func TestSuggestSessionCandidatesEmptyPathReturnsAll(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "dsh")
|
||
|
||
a := seedSessionWS(t, "ws-a", "/home/a", "A")
|
||
seedMailWS(t, a, "admin", "dsh", "/home/a", "A")
|
||
b := seedSessionWS(t, "ws-b", "/home/b", "B")
|
||
seedMailWS(t, b, "admin", "dsh", "/home/b", "B")
|
||
|
||
got, err := SuggestSessionCandidates(context.Background(), "admin", "dsh", "")
|
||
if err != nil {
|
||
t.Fatalf("SuggestSessionCandidates: %v", err)
|
||
}
|
||
if len(got) != 2 {
|
||
t.Fatalf("path 为空应给出全部 2 条,实际 %d", len(got))
|
||
}
|
||
}
|
||
|
||
// 平台侧会话(人直接在 opencode/DSH 界面上开的)经心跳上报后也要能被选中 ——
|
||
// 这正是「定期从 agent 平台同步会话」要解决的问题。
|
||
func TestSuggestSessionCandidatesIncludesPlatformMirror(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "opencode")
|
||
|
||
now := time.Now()
|
||
err := ReplacePlatformSessions(context.Background(), "opencode", []PlatformSession{
|
||
{PlatformID: "ses_1", Workspace: "/home/program/agentmail", Slug: "witty-planet",
|
||
Title: "重构导入路径", MailDriven: false, UpdatedAt: &now},
|
||
{PlatformID: "ses_2", Workspace: "/home/other", Slug: "brave-comet",
|
||
Title: "别的工作区", MailDriven: false, UpdatedAt: &now},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("ReplacePlatformSessions: %v", err)
|
||
}
|
||
|
||
got, err := SuggestSessionCandidates(context.Background(), "admin", "opencode", "/home/program/agentmail")
|
||
if err != nil {
|
||
t.Fatalf("SuggestSessionCandidates: %v", err)
|
||
}
|
||
if len(got) != 1 {
|
||
t.Fatalf("应有 1 个平台候选,实际 %d:%+v", len(got), got)
|
||
}
|
||
if got[0].Alias != "witty-planet" || got[0].Source != "platform" {
|
||
t.Errorf("平台候选错误:%+v", got[0])
|
||
}
|
||
if got[0].Title != "重构导入路径" {
|
||
t.Errorf("标题应带出来:%q", got[0].Title)
|
||
}
|
||
}
|
||
|
||
// 同一别名两边都有时保留 mail 来源:它是「一定送得到」的保证,
|
||
// 镜像只是平台的说法。但镜像的标题该补上去 —— 平台标题通常比会话主题更贴切。
|
||
func TestSuggestSessionCandidatesMailWinsOverMirror(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "opencode")
|
||
|
||
// 本侧线索:有别名但主题为空
|
||
sid := seedSessionWS(t, "witty-planet", "/home/x", "")
|
||
seedMailWS(t, sid, "admin", "opencode", "/home/x", "某事")
|
||
|
||
now := time.Now()
|
||
if err := ReplacePlatformSessions(context.Background(), "opencode", []PlatformSession{
|
||
{PlatformID: "ses_1", Workspace: "/home/x", Slug: "witty-planet",
|
||
Title: "平台侧的标题", UpdatedAt: &now},
|
||
}); err != nil {
|
||
t.Fatalf("ReplacePlatformSessions: %v", err)
|
||
}
|
||
|
||
got, err := SuggestSessionCandidates(context.Background(), "admin", "opencode", "/home/x")
|
||
if err != nil {
|
||
t.Fatalf("SuggestSessionCandidates: %v", err)
|
||
}
|
||
if len(got) != 1 {
|
||
t.Fatalf("同名应合并成 1 条,实际 %d:%+v", len(got), got)
|
||
}
|
||
if got[0].Source != "mail" {
|
||
t.Errorf("应保留 mail 来源(它保证送得到),实际 %q", got[0].Source)
|
||
}
|
||
if got[0].Title != "平台侧的标题" {
|
||
t.Errorf("本侧标题为空时应补上镜像的:%q", got[0].Title)
|
||
}
|
||
}
|
||
|
||
// 上报是整表替换:平台侧删掉的会话必须从候选列表里消失。
|
||
// 增量合并会让它永远留着,而 session 位指向一条不存在的会话会直接 404。
|
||
func TestReplacePlatformSessionsIsFullReplace(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "dsh")
|
||
ctx := context.Background()
|
||
|
||
if err := ReplacePlatformSessions(ctx, "dsh", []PlatformSession{
|
||
{PlatformID: "s1", Workspace: "/w", Slug: "one"},
|
||
{PlatformID: "s2", Workspace: "/w", Slug: "two"},
|
||
}); err != nil {
|
||
t.Fatalf("首次上报: %v", err)
|
||
}
|
||
if got, _ := SuggestSessionCandidates(ctx, "admin", "dsh", "/w"); len(got) != 2 {
|
||
t.Fatalf("首次上报应有 2 条,实际 %d", len(got))
|
||
}
|
||
|
||
// 第二次只报一条:另一条在平台侧已被删除
|
||
if err := ReplacePlatformSessions(ctx, "dsh", []PlatformSession{
|
||
{PlatformID: "s1", Workspace: "/w", Slug: "one"},
|
||
}); err != nil {
|
||
t.Fatalf("二次上报: %v", err)
|
||
}
|
||
got, _ := SuggestSessionCandidates(ctx, "admin", "dsh", "/w")
|
||
if len(got) != 1 || got[0].Alias != "one" {
|
||
t.Fatalf("整表替换失效,实际 %+v", got)
|
||
}
|
||
}
|
||
|
||
// 无 slug 的平台会话不进候选:slug 是填进 session 位的值,
|
||
// 没有它这一项点下去只能得到一个空的 session 段。
|
||
func TestPlatformSessionsWithoutSlugAreSkipped(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "dsh")
|
||
ctx := context.Background()
|
||
|
||
if err := ReplacePlatformSessions(ctx, "dsh", []PlatformSession{
|
||
{PlatformID: "s1", Workspace: "/w", Slug: ""},
|
||
{PlatformID: "s2", Workspace: "/w", Slug: "named"},
|
||
}); err != nil {
|
||
t.Fatalf("上报: %v", err)
|
||
}
|
||
got, _ := SuggestSessionCandidates(ctx, "admin", "dsh", "/w")
|
||
if len(got) != 1 || got[0].Alias != "named" {
|
||
t.Fatalf("无 slug 的应被跳过,实际 %+v", got)
|
||
}
|
||
}
|
||
|
||
// ★★ (d1) 上报**非空** list 时,不得删除其它 workspace 的行。
|
||
//
|
||
// 背景(docs/DEBTS.json `platform-mirror-replace-domain-too-wide` 补记之八/之九):
|
||
// `ReplacePlatformSessions` 的 DELETE 域是整个 `agent_name`,而**上报域**是每个上报者自己那个
|
||
// workspace —— 两者不等("上报域 ⊊ 替换域")。opencode 的插件按 directory 实例化、
|
||
// `AGENT_NAME` 却是单一常量,于是每个实例的心跳都会把别的目录**整批擦掉**。
|
||
// 活库实测:200 样本 × 0.3s 里 25% 的时刻镜像为空、9 个目录轮流出场。
|
||
//
|
||
// ★ 这条**不依赖**请求级 scope 字段:每项 `PlatformSession` 自带 `Workspace`,
|
||
//
|
||
// 所以**今天就能写、现在就是红、修好即绿**。请求级字段只与 (d2)(上报 `[]`)有关。
|
||
//
|
||
// ⚠️ 本条**当前为红**——它断言的正是那个尚未修复的缺陷。
|
||
//
|
||
// 按本仓惯例,红的判据要么标 `[FAIL]` 形态、要么随修复一起转绿;
|
||
// 这里用 `t.Errorf` 而不是 `t.Fatalf`,好让"为什么红"直接显示在测试输出里。
|
||
func TestReplacePlatformSessionsKeepsOtherWorkspaces(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "opencode")
|
||
ctx := context.Background()
|
||
|
||
// 上报者 1:/A 下两条
|
||
if err := ReplacePlatformSessions(ctx, "opencode", []PlatformSession{
|
||
{PlatformID: "a1", Workspace: "/A", Slug: "a1"},
|
||
{PlatformID: "a2", Workspace: "/A", Slug: "a2"},
|
||
}); err != nil {
|
||
t.Fatalf("A 上报: %v", err)
|
||
}
|
||
|
||
// 上报者 2:只报 /B(list **非空**,每项都带 Workspace)
|
||
if err := ReplacePlatformSessions(ctx, "opencode", []PlatformSession{
|
||
{PlatformID: "b1", Workspace: "/B", Slug: "b1"},
|
||
}); err != nil {
|
||
t.Fatalf("B 上报: %v", err)
|
||
}
|
||
|
||
// 其它 workspace 的行数不得变化
|
||
var aCount int
|
||
if err := db.DB.QueryRowContext(ctx,
|
||
`SELECT count(*) FROM agent_platform_sessions WHERE agent_name = $1 AND workspace = $2`,
|
||
"opencode", "/A").Scan(&aCount); err != nil {
|
||
t.Fatalf("统计 /A: %v", err)
|
||
}
|
||
if aCount != 2 {
|
||
t.Errorf("(d1) 上报 /B 之后 /A 的行数 = %d,期望 2 —— 其它 workspace 被误擦;"+
|
||
"现存 workspace = %v(DELETE 域应与上报域一致,即只清本次上报的那些)",
|
||
aCount, workspacesOf(t, ctx, "opencode"))
|
||
}
|
||
}
|
||
|
||
// workspacesOf 列出某 agent 当前镜像里的全部 workspace(与行数),供失败信息里列出**元素**
|
||
// 而不是只给一个数 —— 范围要靠元素才可复核(清单 ⑫′)。
|
||
func workspacesOf(t *testing.T, ctx context.Context, agent string) map[string]int {
|
||
t.Helper()
|
||
rows, err := db.DB.QueryContext(ctx,
|
||
`SELECT workspace, count(*) FROM agent_platform_sessions WHERE agent_name = $1 GROUP BY workspace`,
|
||
agent)
|
||
if err != nil {
|
||
t.Fatalf("列 workspace: %v", err)
|
||
}
|
||
defer rows.Close()
|
||
out := map[string]int{}
|
||
for rows.Next() {
|
||
var ws string
|
||
var n int
|
||
if err := rows.Scan(&ws, &n); err != nil {
|
||
t.Fatalf("scan: %v", err)
|
||
}
|
||
out[ws] = n
|
||
}
|
||
return out
|
||
}
|
||
|
||
// 上报里的重复 platform_id 不该让整次事务失败(主键冲突)。
|
||
func TestReplacePlatformSessionsDedupes(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "dsh")
|
||
ctx := context.Background()
|
||
|
||
if err := ReplacePlatformSessions(ctx, "dsh", []PlatformSession{
|
||
{PlatformID: "dup", Workspace: "/w", Slug: "first"},
|
||
{PlatformID: "dup", Workspace: "/w", Slug: "second"},
|
||
}); err != nil {
|
||
t.Fatalf("重复 id 不该报错: %v", err)
|
||
}
|
||
got, _ := SuggestSessionCandidates(ctx, "admin", "dsh", "/w")
|
||
if len(got) != 1 || got[0].Alias != "first" {
|
||
t.Fatalf("应保留第一条,实际 %+v", got)
|
||
}
|
||
}
|
||
|
||
// SetSessionWorkspace 只在为空时写入:会话的工作区在建立时就定下了,
|
||
// 之后不该被一封发往别处的邮件改掉 —— 那会让它在候选列表里凭空换工作区。
|
||
func TestSetSessionWorkspaceDoesNotOverwrite(t *testing.T) {
|
||
setupTestDB(t)
|
||
ctx := context.Background()
|
||
|
||
sid := seedSessionWS(t, "fixed-ws", "/home/original", "某事")
|
||
if err := SetSessionWorkspace(ctx, sid, "/home/hijacked"); err != nil {
|
||
t.Fatalf("SetSessionWorkspace: %v", err)
|
||
}
|
||
var ws string
|
||
if err := db.DB.QueryRowContext(ctx,
|
||
`SELECT workspace FROM sessions WHERE session_id = $1`, sid).Scan(&ws); err != nil {
|
||
t.Fatalf("read back: %v", err)
|
||
}
|
||
if ws != "/home/original" {
|
||
t.Errorf("已有 workspace 被覆盖成 %q", ws)
|
||
}
|
||
|
||
// 空的那种要能补上(历史会话回填)
|
||
empty := seedSessionWS(t, "empty-ws", "", "某事")
|
||
if err := SetSessionWorkspace(ctx, empty, "/home/filled"); err != nil {
|
||
t.Fatalf("SetSessionWorkspace(empty): %v", err)
|
||
}
|
||
if err := db.DB.QueryRowContext(ctx,
|
||
`SELECT workspace FROM sessions WHERE session_id = $1`, empty).Scan(&ws); err != nil {
|
||
t.Fatalf("read back: %v", err)
|
||
}
|
||
if ws != "/home/filled" {
|
||
t.Errorf("空 workspace 未被补上,实际 %q", ws)
|
||
}
|
||
}
|
||
|
||
// CreateSession 要把 workspace 存下来 —— 这是整条链的起点,
|
||
// 漏在这里的话后面所有查询都只能靠 mails 反推。
|
||
func TestCreateSessionStoresWorkspace(t *testing.T) {
|
||
setupTestDB(t)
|
||
ctx := context.Background()
|
||
|
||
id, err := CreateSession(ctx, nil, "admin", "带工作区", "/home/program/agentmail")
|
||
if err != nil {
|
||
t.Fatalf("CreateSession: %v", err)
|
||
}
|
||
var ws string
|
||
if err := db.DB.QueryRowContext(ctx,
|
||
`SELECT workspace FROM sessions WHERE session_id = $1`, id).Scan(&ws); err != nil {
|
||
t.Fatalf("read back: %v", err)
|
||
}
|
||
if ws != "/home/program/agentmail" {
|
||
t.Errorf("workspace 未落库:%q", ws)
|
||
}
|
||
}
|
||
|
||
// 归档的会话不进候选:归档就是「这条线索结束了」,
|
||
// 还出现在补全里等于邀请用户往一条已关闭的线索里发信。
|
||
func TestSuggestSessionCandidatesExcludesArchived(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "dsh")
|
||
ctx := context.Background()
|
||
|
||
sid := seedSessionWS(t, "done-thread", "/home/a", "已完成")
|
||
seedMailWS(t, sid, "admin", "dsh", "/home/a", "已完成")
|
||
if _, err := db.DB.ExecContext(ctx,
|
||
`UPDATE sessions SET status = 'archived' WHERE session_id = $1`, sid); err != nil {
|
||
t.Fatalf("archive: %v", err)
|
||
}
|
||
|
||
got, _ := SuggestSessionCandidates(ctx, "admin", "dsh", "/home/a")
|
||
if len(got) != 0 {
|
||
t.Fatalf("归档会话不该出现,实际 %+v", got)
|
||
}
|
||
}
|
||
|
||
/*
|
||
★ 2026-09-26 补:一次上报**不带任何 workspace** 时,不得清掉任何工作区。
|
||
|
||
# 为什么这条独立于 TestReplacePlatformSessionsKeepsOtherWorkspaces
|
||
|
||
那条判据测的是"报了 /B 不动 /A"。而本次修的 DELETE 域计算里有一条**只在
|
||
wsOrder 为空时生效**的分支(`if len(wsOrder) > 0`)—— 现有判据**碰不到它**:
|
||
所有既有用例传进来的 list 都带 workspace。
|
||
|
||
实测:把那行改成 `if len(wsOrder) >= 0`(即空清单也按 agent 清,退回缺陷),
|
||
**全部既有判据仍然绿**。这就是"判据覆盖不到的分支"的样子。
|
||
|
||
# 为什么空清单必须"什么都不删"
|
||
|
||
上报方(`client.session.list({query: directory ? {directory} : undefined})`)
|
||
在拿不到目录时可以回一个**没有 workspace 字段**的列表。此时无从判断该清谁。
|
||
按 agent 清 ⇒ 回到缺陷本身(清掉所有工作区);什么都不清 ⇒ 最坏是
|
||
这个工作区的镜像陈旧,而陈旧的后果是"候选里多了已删的会话"(点下去 404),
|
||
比"清错别人的"轻得多。
|
||
|
||
方向与整表替换那条一致:**宁可少清,不可错清。**
|
||
*/
|
||
func TestReplacePlatformSessionsWithNoWorkspaceKeepsEverything(t *testing.T) {
|
||
setupTestDB(t)
|
||
seedPlatformAgent(t, "opencode")
|
||
ctx := context.Background()
|
||
|
||
// 先有 /A 与 /B 两处镜像
|
||
for _, ws := range []string{"/A", "/B"} {
|
||
if err := ReplacePlatformSessions(ctx, "opencode", []PlatformSession{
|
||
{PlatformID: ws + "-1", Workspace: ws, Slug: "s"},
|
||
}); err != nil {
|
||
t.Fatalf("%s 上报: %v", ws, err)
|
||
}
|
||
}
|
||
|
||
// 一次**不带 workspace** 的上报(一项都没有)
|
||
if err := ReplacePlatformSessions(ctx, "opencode", []PlatformSession{
|
||
{PlatformID: "x", Slug: "no-ws"},
|
||
}); err != nil {
|
||
t.Fatalf("无 workspace 上报: %v", err)
|
||
}
|
||
|
||
got := workspacesOf(t, ctx, "opencode")
|
||
for _, ws := range []string{"/A", "/B"} {
|
||
if got[ws] != 1 {
|
||
t.Errorf("上报不带 workspace 时 %s 的镜像被清了(现存 %v)——"+
|
||
"此时无从判断该清谁,必须什么都不删", ws, got)
|
||
}
|
||
}
|
||
}
|