package application import ( "context" "testing" "time" "img_infinite_canvas/internal/domain/design" "img_infinite_canvas/internal/infrastructure/memory" ) func TestAgentThreadsOnlyListsAgentConversationThreads(t *testing.T) { ctx := context.Background() repo := memory.NewProjectRepository() service := NewDesignService(repo, nil, nil, nil, nil) now := time.Now() project := design.Project{ ID: "project-agent-history", Title: "Agent history", Status: design.StatusReady, LastThreadID: "thread-remove-background", UpdatedAt: now, Messages: []design.Message{ {ID: "agent-user", Role: "user", Type: "user", Content: "官网设计", ThreadID: "thread-agent", CreatedAt: now.Add(-5 * time.Minute)}, {ID: "agent-assistant", Role: "assistant", Type: "assistant", Content: "可以继续设计。", ThreadID: "thread-agent", CreatedAt: now.Add(-4 * time.Minute)}, {ID: "remove-user", Role: "user", Type: "user", Content: "一键移除背景,保留主体完整边缘", ThreadID: "thread-remove-background", Name: "canvas_action", ToolHint: "canvas_action", CreatedAt: now.Add(-3 * time.Minute)}, {ID: "mockup-tool", Role: "tool", Type: "tool", Content: "生成 mockup 模型", ThreadID: "thread-mockup", Name: "mockup_model", ToolHint: "mockup_model", CreatedAt: now.Add(-2 * time.Minute)}, {ID: "text-tool", Role: "tool", Type: textExtractionResultMessageType, Content: "文字识别完成", ThreadID: "thread-text", Name: "text_extraction", CreatedAt: now.Add(-time.Minute)}, }, } if err := repo.Save(ctx, project); err != nil { t.Fatal(err) } threads, err := service.AgentThreads(ctx, project.ID) if err != nil { t.Fatal(err) } if len(threads) != 1 { t.Fatalf("expected only one agent thread, got %#v", threads) } if threads[0].AgentThreadID != "thread-agent" || threads[0].Text != "官网设计" { t.Fatalf("expected agent thread summary, got %#v", threads[0]) } } func TestAgentThreadsSummaryHidesPromptReferenceDirectives(t *testing.T) { ctx := context.Background() repo := memory.NewProjectRepository() service := NewDesignService(repo, nil, nil, nil, nil) now := time.Now() project := design.Project{ ID: "project-agent-reference-history", Title: "Agent history", Status: design.StatusReady, LastThreadID: "thread-agent", UpdatedAt: now, Messages: []design.Message{ { ID: "agent-user", Role: "user", Type: "user", Content: "Reference image 1 (image): http://localhost:19000/canvas-assets/reference.png 灰裤子拆成单件衣服,为了后面电商配图。\nSelected model: GPT Image 2", ThreadID: "thread-agent", CreatedAt: now, }, }, } if err := repo.Save(ctx, project); err != nil { t.Fatal(err) } threads, err := service.AgentThreads(ctx, project.ID) if err != nil { t.Fatal(err) } if len(threads) != 1 { t.Fatalf("expected one agent thread, got %#v", threads) } if threads[0].Text != "灰裤子拆成单件衣服,为了后面电商配图。" { t.Fatalf("expected visible prompt text only, got %q", threads[0].Text) } } func TestProjectScopedToAgentThreadKeepsOnlyCurrentConversationContext(t *testing.T) { now := time.Now() project := design.Project{ ID: "project-agent-context", Messages: []design.Message{ {ID: "old-user", Role: "user", Content: "旧对话", ThreadID: "thread-old", CreatedAt: now.Add(-3 * time.Minute)}, {ID: "current-user", Role: "user", Content: "当前对话", ThreadID: "thread-current", CreatedAt: now.Add(-2 * time.Minute)}, {ID: "current-action", Role: "user", Content: "一键移除背景", ThreadID: "thread-current", Name: "canvas_action", ToolHint: "canvas_action", CreatedAt: now.Add(-time.Minute)}, }, } scoped := projectScopedToAgentThread(project, "thread-current") if len(scoped.Messages) != 1 { t.Fatalf("expected only current agent message, got %#v", scoped.Messages) } if scoped.Messages[0].ID != "current-user" { t.Fatalf("expected current conversation message, got %#v", scoped.Messages[0]) } }