package services import ( "context" "path/filepath" "testing" "time" "git.pengzhan.dev/haopengzhan/rsspipe/internal/db" "git.pengzhan.dev/haopengzhan/rsspipe/internal/events" "git.pengzhan.dev/haopengzhan/rsspipe/internal/models" ) func TestEntryServicePatchAndTransition(t *testing.T) { tempDBPath := filepath.Join(t.TempDir(), "test.db") database, err := db.InitDB(tempDBPath) if err != nil { t.Fatalf("failed to init db: %v", err) } defer func() { _ = database.Close() }() bus := events.NewEventBus() svc := NewEntryService(database, bus) // Create test category and feed cat, _ := database.CreateCategory("Tech", nil) feed, _ := database.CreateFeed("Test Feed", "https://example.com/rss", "https://example.com", "", &cat.ID) // Ingest an entry entry := models.FeedEntry{ ID: "e_test_1", FeedID: feed.ID, GUID: "guid_1", URL: "https://example.com/p/1", Title: "Test Post", Content: "Hello world", PublishedAt: time.Now().UTC(), } ingested, err := svc.IngestEntries(context.Background(), feed.ID, []models.FeedEntry{entry}, feed.Title, feed.SiteURL) if err != nil || len(ingested) == 0 { t.Fatalf("failed to ingest entry: %v", err) } // 1. Test patching tags isStarred := true updated, diff, err := svc.PatchEntry(context.Background(), ingested[0].ID, models.PatchEntryRequest{ IsStarred: &isStarred, Metadata: map[string]interface{}{ "tags": []string{"AI", "Kubernetes"}, }, }) if err != nil { t.Fatalf("failed to patch entry: %v", err) } if !updated.IsStarred { t.Errorf("expected entry to be starred") } if !diff.HasFieldChange("is_starred") { t.Errorf("expected diff to contain is_starred change") } // 2. Test patching variant addition (readability_full_text) fullTextVariant := &models.EntryVariant{ ContentType: "html", TextValue: "

Full article extracted content...

", } updated2, diff2, err := svc.PatchEntry(context.Background(), ingested[0].ID, models.PatchEntryRequest{ Variants: map[string]*models.EntryVariant{ "readability_full_text": fullTextVariant, }, }) if err != nil { t.Fatalf("failed to patch variant: %v", err) } if !diff2.HasVariantAddition("readability_full_text") { t.Errorf("expected diff2 to report variant addition of readability_full_text") } if updated2.Variants["readability_full_text"].TextValue == "" { t.Errorf("expected variant to be loaded in updated entry") } }