Updates the archive generation to support the new folder structure: archives/<digest-slug>/<date>.html. Changes: - Updated 'OverviewTemplateData' to include 'DigestTitle' and 'DigestSlug'. - Updated 'ArchiveService' to use 'DigestSlug' in file paths. - Updated 'DigestService', 'View', and 'Processor' to propagate digest title/slug. - Updated tests to match new structure and signatures.
221 lines
7.5 KiB
Go
221 lines
7.5 KiB
Go
package digest
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"miniflux-digest/internal/digest/view"
|
|
"miniflux-digest/internal/models"
|
|
"miniflux-digest/internal/testutil"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"google.golang.org/genai"
|
|
)
|
|
|
|
type mockLLMService struct {
|
|
GenerateContentFunc func(ctx context.Context, prompt string, schema *genai.Schema) ([]byte, error)
|
|
GenerateContentWithResponseFunc func(ctx context.Context, prompt string, schema *genai.Schema, response interface{}) error
|
|
}
|
|
|
|
func (m *mockLLMService) GenerateContent(ctx context.Context, prompt string, schema *genai.Schema) ([]byte, error) {
|
|
if m.GenerateContentFunc != nil {
|
|
return m.GenerateContentFunc(ctx, prompt, schema)
|
|
}
|
|
return nil, errors.New("GenerateContentFunc not implemented")
|
|
}
|
|
|
|
func (m *mockLLMService) GenerateContentWithResponse(ctx context.Context, prompt string, schema *genai.Schema, response interface{}) error {
|
|
if m.GenerateContentWithResponseFunc != nil {
|
|
return m.GenerateContentWithResponseFunc(ctx, prompt, schema, response)
|
|
}
|
|
return errors.New("GenerateContentWithResponseFunc not implemented")
|
|
}
|
|
|
|
func createLLMGrouperMockEntries() []*models.Entry {
|
|
return []*models.Entry{
|
|
{ID: 1, Title: "Entry 1", FeedID: 100, FeedTitle: "Feed A", GroupID: 1, GroupTitle: "Category A"},
|
|
{ID: 2, Title: "Entry 2", FeedID: 100, FeedTitle: "Feed A", GroupID: 1, GroupTitle: "Category A"},
|
|
{ID: 3, Title: "Entry 3", FeedID: 200, FeedTitle: "Feed B", GroupID: 1, GroupTitle: "Category A"},
|
|
}
|
|
}
|
|
|
|
func TestDigestService_BuildDigestData(t *testing.T) {
|
|
mockLLM := &mockLLMService{
|
|
GenerateContentWithResponseFunc: func(ctx context.Context, prompt string, schema *genai.Schema, response interface{}) error {
|
|
switch schema {
|
|
case view.InitialGroupingResponseSchema:
|
|
if err := json.Unmarshal([]byte(`{
|
|
"groups": [
|
|
{
|
|
"title": "AI News",
|
|
"entry_ids": [1]
|
|
},
|
|
{
|
|
"title": "Go Lang",
|
|
"entry_ids": [2, 3]
|
|
}
|
|
]
|
|
}`), response); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
case view.SummaryResponseSchema:
|
|
if err := json.Unmarshal([]byte(`{"summary": "This is a summary."}`), response); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
case view.SubGroupingResponseSchema:
|
|
if err := json.Unmarshal([]byte(`{
|
|
"sub_groups": [
|
|
{"title": "Sub-group 1", "entry_ids": [1]}
|
|
]
|
|
}`), response); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
default:
|
|
return errors.New("unexpected LLM call")
|
|
}
|
|
},
|
|
}
|
|
digestService := NewDigestService(mockLLM)
|
|
|
|
entries := testutil.CreateMockEntries(0)
|
|
icons := map[int64]*models.FeedIcon{
|
|
101: {FeedID: 101, Data: "iconA"},
|
|
102: {FeedID: 102, Data: "iconB"},
|
|
}
|
|
|
|
t.Run("view=date", func(t *testing.T) {
|
|
overviewData := digestService.BuildDigestData(entries, icons, "date", "http://miniflux.test", "", nil, "")
|
|
if overviewData == nil {
|
|
t.Fatal("overviewData is nil")
|
|
}
|
|
if len(overviewData.PrimaryGroups) != 3 {
|
|
t.Errorf("Expected 3 primary groups, got %d", len(overviewData.PrimaryGroups))
|
|
}
|
|
dateGroup := testutil.FindPrimaryGroup(overviewData.PrimaryGroups, "Jan 1, 2024")
|
|
if dateGroup == nil {
|
|
t.Fatalf("Did not find expected primary group 'Jan 1, 2024'")
|
|
}
|
|
if len(dateGroup.SubGroups) != 1 {
|
|
t.Fatalf("Incorrect sub-groups for Jan 1, 2024: %+v", dateGroup)
|
|
}
|
|
firstSubGroup := dateGroup.SubGroups[0]
|
|
if !sort.SliceIsSorted(firstSubGroup.Entries, func(i, j int) bool {
|
|
return firstSubGroup.Entries[i].Date.Before(firstSubGroup.Entries[j].Date)
|
|
}) {
|
|
t.Errorf("Entries in sub-group are not sorted by date")
|
|
}
|
|
})
|
|
|
|
t.Run("view=category", func(t *testing.T) {
|
|
overviewData := digestService.BuildDigestData(entries, icons, "category", "http://miniflux.test", "", nil, "")
|
|
if overviewData == nil {
|
|
t.Fatal("overviewData is nil")
|
|
}
|
|
if len(overviewData.PrimaryGroups) != 3 {
|
|
t.Errorf("Expected 3 primary groups, got %d", len(overviewData.PrimaryGroups))
|
|
}
|
|
catAGroup := testutil.FindPrimaryGroup(overviewData.PrimaryGroups, "Category A")
|
|
if catAGroup == nil || len(catAGroup.SubGroups) != 3 {
|
|
t.Fatalf("Incorrect sub-groups for Category A: %+v", catAGroup)
|
|
}
|
|
firstSubGroup := catAGroup.SubGroups[0]
|
|
if !sort.SliceIsSorted(firstSubGroup.Entries, func(i, j int) bool {
|
|
return firstSubGroup.Entries[i].Date.Before(firstSubGroup.Entries[j].Date)
|
|
}) {
|
|
t.Errorf("Entries in sub-group are not sorted by date")
|
|
}
|
|
})
|
|
|
|
t.Run("view=ai", func(t *testing.T) {
|
|
aiEntries := createLLMGrouperMockEntries()
|
|
overviewData := digestService.BuildDigestData(aiEntries, icons, "ai", "http://miniflux.test", "", nil, "")
|
|
if overviewData == nil {
|
|
t.Fatal("overviewData is nil")
|
|
}
|
|
if len(overviewData.PrimaryGroups) != 2 {
|
|
t.Errorf("Expected 2 primary groups, got %d", len(overviewData.PrimaryGroups))
|
|
}
|
|
// Assert on sorted order
|
|
assert.Equal(t, "AI News", overviewData.PrimaryGroups[0].Title)
|
|
assert.Equal(t, "Go Lang", overviewData.PrimaryGroups[1].Title)
|
|
})
|
|
|
|
t.Run("view=ai - GroupAIEntries error fallback", func(t *testing.T) {
|
|
mockLLM := &mockLLMService{
|
|
GenerateContentWithResponseFunc: func(ctx context.Context, prompt string, schema *genai.Schema, response interface{}) error {
|
|
if schema == view.InitialGroupingResponseSchema {
|
|
return errors.New("mock LLM grouping error")
|
|
}
|
|
return errors.New("unexpected LLM call")
|
|
},
|
|
}
|
|
digestService := NewDigestService(mockLLM)
|
|
aiEntries := createLLMGrouperMockEntries()
|
|
overviewData := digestService.BuildDigestData(aiEntries, icons, "ai", "http://miniflux.test", "", nil, "")
|
|
|
|
if overviewData == nil {
|
|
t.Fatal("overviewData is nil")
|
|
}
|
|
if len(overviewData.PrimaryGroups) != 1 {
|
|
t.Errorf("Expected 1 primary group from date fallback, got %d", len(overviewData.PrimaryGroups))
|
|
}
|
|
dateGroup := testutil.FindPrimaryGroup(overviewData.PrimaryGroups, "Jan 1, 0001")
|
|
if dateGroup == nil {
|
|
t.Fatalf("Did not find expected primary group 'Jan 1, 0001' after date fallback")
|
|
}
|
|
})
|
|
|
|
t.Run("view=ai - sub-grouping error", func(t *testing.T) {
|
|
mockLLM := &mockLLMService{
|
|
GenerateContentWithResponseFunc: func(ctx context.Context, prompt string, schema *genai.Schema, response interface{}) error {
|
|
if schema == view.InitialGroupingResponseSchema {
|
|
if err := json.Unmarshal([]byte(`{
|
|
"groups": [
|
|
{
|
|
"title": "Test Group",
|
|
"entry_ids": [1, 2, 3]
|
|
}
|
|
]
|
|
}`), response); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
if schema == view.SummaryResponseSchema {
|
|
if err := json.Unmarshal([]byte(`{"summary": "This is a summary."}`), response); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
if schema == view.SubGroupingResponseSchema {
|
|
return errors.New("mock LLM sub-grouping error")
|
|
}
|
|
return errors.New("unexpected LLM call")
|
|
},
|
|
}
|
|
digestService := NewDigestService(mockLLM)
|
|
aiEntries := createLLMGrouperMockEntries()
|
|
overviewData := digestService.BuildDigestData(aiEntries, icons, "ai", "http://miniflux.test", "", nil, "")
|
|
|
|
if overviewData == nil {
|
|
t.Fatal("overviewData is nil")
|
|
}
|
|
if len(overviewData.PrimaryGroups) != 1 {
|
|
t.Errorf("Expected 1 primary group, got %d", len(overviewData.PrimaryGroups))
|
|
}
|
|
group := overviewData.PrimaryGroups[0]
|
|
if group.Summary != "This is a summary." {
|
|
t.Errorf("Expected group summary to be present, got %q", group.Summary)
|
|
}
|
|
if len(group.SubGroups) != 1 || group.SubGroups[0].Title != "Uncategorized" {
|
|
t.Errorf("Expected a single 'Uncategorized' sub-group on error, got %+v", group.SubGroups)
|
|
}
|
|
})
|
|
}
|
|
|