The daily briefing needs AND: today's rules engine was OR-only. The
match semantics become a per-collection column
(collections.rule_match_mode, 'any' legacy default | 'all'), flipped on
its own endpoint (PUT /api/collections/:id/match-mode, owner-only —
nothing else on the row changes) and surfaced in the rules page as an
ANY/ALL toggle with matching copy.
Episode rule fields join the vocabulary (plan item 2.4): feed (the
multi-select rides the new `in` operator — comma-separated feed ids),
played (the canonical finished predicate — viewer progress ≥ 0.999,
same as hide_finished), downloaded, published_within (N days), plus
title/description contains. The rules candidate query now carries the
viewer's progress (LEFT JOIN reading_progress) so played/unplayed is
per-user, and TestRules/preview speak the SAME evaluator: the handler's
duplicated field switch is deleted and delegates to
CollectionService.FieldMatches — one vocabulary, one semantics.
Legitimate semantics note: in ANY mode `played:false` matches unplayed
BOOKS too (everything with no progress row) — correct per-rule
behavior, and exactly why the briefing mode exists.
Backward compatibility: every legacy row defaults to 'any' (the old
OR), UpdateCollection keeps the stored mode when the form doesn't send
one, and the evaluator treats absent data honestly (no date → no
match; no progress → unplayed).
Pinned by TestPodcastSmartCollections: the AND-mode briefing
(feed∈{A} ∧ unplayed ∧ <7d = exactly the new episode), OR-mode
widening, owner-only flips, the preview honoring the mode, and
downloaded/contains rules. Also fixes a latent bug the new query
surfaced: UpdateCollectionViewSettings referenced a nonexistent
updated_at column (never exercised until now — the new sibling query
was written without it).
279 lines
6.1 KiB
Go
279 lines
6.1 KiB
Go
package services
|
|
|
|
import (
|
|
"bookhoard/internal/database"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func TestEvaluateRules_Equality(t *testing.T) {
|
|
service := &CollectionService{}
|
|
|
|
mediaItem := database.MediaItems{
|
|
Genre: pgtype.Text{String: "Science Fiction", Valid: true},
|
|
}
|
|
|
|
rules := []Rule{
|
|
{
|
|
ID: "rule-1",
|
|
Field: "genre",
|
|
Operator: "equals",
|
|
Value: "Science Fiction",
|
|
Priority: 5,
|
|
},
|
|
}
|
|
|
|
evaluations := service.EvaluateRules(mediaItem, 0, rules)
|
|
|
|
if len(evaluations) != 1 {
|
|
t.Fatalf("expected 1 evaluation, got %d", len(evaluations))
|
|
}
|
|
|
|
if !evaluations[0].Matches {
|
|
t.Errorf("expected rule to match, but it didn't")
|
|
}
|
|
|
|
if evaluations[0].Confidence <= 0.85 {
|
|
t.Errorf("expected confidence > 0.85, got %f", evaluations[0].Confidence)
|
|
}
|
|
}
|
|
|
|
func TestEvaluateRules_NoMatch(t *testing.T) {
|
|
service := &CollectionService{}
|
|
|
|
mediaItem := database.MediaItems{
|
|
Genre: pgtype.Text{String: "Fantasy", Valid: true},
|
|
}
|
|
|
|
rules := []Rule{
|
|
{
|
|
ID: "rule-1",
|
|
Field: "genre",
|
|
Operator: "equals",
|
|
Value: "Science Fiction",
|
|
Priority: 5,
|
|
},
|
|
}
|
|
|
|
evaluations := service.EvaluateRules(mediaItem, 0, rules)
|
|
|
|
if len(evaluations) != 1 {
|
|
t.Fatalf("expected 1 evaluation, got %d", len(evaluations))
|
|
}
|
|
|
|
if evaluations[0].Matches {
|
|
t.Errorf("expected rule not to match, but it did")
|
|
}
|
|
|
|
if evaluations[0].Confidence != 0 {
|
|
t.Errorf("expected confidence 0, got %f", evaluations[0].Confidence)
|
|
}
|
|
}
|
|
|
|
func TestEvaluateRules_Contains(t *testing.T) {
|
|
service := &CollectionService{}
|
|
|
|
mediaItem := database.MediaItems{
|
|
Series: pgtype.Text{String: "The Expanse", Valid: true},
|
|
}
|
|
|
|
rules := []Rule{
|
|
{
|
|
ID: "rule-1",
|
|
Field: "series",
|
|
Operator: "contains",
|
|
Value: "Expanse",
|
|
Priority: 7,
|
|
},
|
|
}
|
|
|
|
evaluations := service.EvaluateRules(mediaItem, 0, rules)
|
|
|
|
if len(evaluations) != 1 {
|
|
t.Fatalf("expected 1 evaluation, got %d", len(evaluations))
|
|
}
|
|
|
|
if !evaluations[0].Matches {
|
|
t.Errorf("expected rule to match, but it didn't")
|
|
}
|
|
|
|
if evaluations[0].Confidence <= 0.9 {
|
|
t.Errorf("expected confidence > 0.9 with priority boost, got %f", evaluations[0].Confidence)
|
|
}
|
|
}
|
|
|
|
func TestEvaluateRules_PriorityOrder(t *testing.T) {
|
|
service := &CollectionService{}
|
|
|
|
mediaItem := database.MediaItems{
|
|
Genre: pgtype.Text{String: "Science Fiction", Valid: true},
|
|
Series: pgtype.Text{String: "Foundation", Valid: true},
|
|
}
|
|
|
|
rules := []Rule{
|
|
{
|
|
ID: "rule-1",
|
|
Field: "genre",
|
|
Operator: "equals",
|
|
Value: "Science Fiction",
|
|
Priority: 5,
|
|
},
|
|
{
|
|
ID: "rule-2",
|
|
Field: "series",
|
|
Operator: "equals",
|
|
Value: "Foundation",
|
|
Priority: 9,
|
|
},
|
|
}
|
|
|
|
evaluations := service.EvaluateRules(mediaItem, 0, rules)
|
|
|
|
if len(evaluations) != 2 {
|
|
t.Fatalf("expected 2 evaluations, got %d", len(evaluations))
|
|
}
|
|
|
|
if evaluations[0].RuleID != "rule-2" {
|
|
t.Errorf("expected rule-2 (higher priority) to be first, got %s", evaluations[0].RuleID)
|
|
}
|
|
|
|
if evaluations[1].RuleID != "rule-1" {
|
|
t.Errorf("expected rule-1 (lower priority) to be second, got %s", evaluations[1].RuleID)
|
|
}
|
|
|
|
if evaluations[0].Confidence <= evaluations[1].Confidence {
|
|
t.Errorf("expected higher priority rule to have higher confidence")
|
|
}
|
|
}
|
|
|
|
func TestEvaluateRules_CopyrightYear(t *testing.T) {
|
|
service := &CollectionService{}
|
|
|
|
mediaItem := database.MediaItems{
|
|
CopyrightYear: pgtype.Int4{Int32: 2020, Valid: true},
|
|
}
|
|
|
|
rules := []Rule{
|
|
{
|
|
ID: "rule-1",
|
|
Field: "copyright_year",
|
|
Operator: "greater_than",
|
|
Value: "2019",
|
|
Priority: 5,
|
|
},
|
|
}
|
|
|
|
evaluations := service.EvaluateRules(mediaItem, 0, rules)
|
|
|
|
if len(evaluations) != 1 {
|
|
t.Fatalf("expected 1 evaluation, got %d", len(evaluations))
|
|
}
|
|
|
|
if !evaluations[0].Matches {
|
|
t.Errorf("expected rule to match (2020 > 2019), but it didn't")
|
|
}
|
|
}
|
|
|
|
func TestEvaluateRules_MissingField(t *testing.T) {
|
|
service := &CollectionService{}
|
|
|
|
mediaItem := database.MediaItems{}
|
|
|
|
rules := []Rule{
|
|
{
|
|
ID: "rule-1",
|
|
Field: "genre",
|
|
Operator: "equals",
|
|
Value: "Science Fiction",
|
|
Priority: 5,
|
|
},
|
|
}
|
|
|
|
evaluations := service.EvaluateRules(mediaItem, 0, rules)
|
|
|
|
if len(evaluations) != 1 {
|
|
t.Fatalf("expected 1 evaluation, got %d", len(evaluations))
|
|
}
|
|
|
|
if evaluations[0].Matches {
|
|
t.Errorf("expected rule not to match when field is missing, but it did")
|
|
}
|
|
}
|
|
|
|
func TestEvaluateRules_CaseInsensitive(t *testing.T) {
|
|
service := &CollectionService{}
|
|
|
|
mediaItem := database.MediaItems{
|
|
Genre: pgtype.Text{String: "science fiction", Valid: true},
|
|
}
|
|
|
|
rules := []Rule{
|
|
{
|
|
ID: "rule-1",
|
|
Field: "genre",
|
|
Operator: "equals",
|
|
Value: "SCIENCE FICTION",
|
|
Priority: 5,
|
|
},
|
|
}
|
|
|
|
evaluations := service.EvaluateRules(mediaItem, 0, rules)
|
|
|
|
if len(evaluations) != 1 {
|
|
t.Fatalf("expected 1 evaluation, got %d", len(evaluations))
|
|
}
|
|
|
|
if !evaluations[0].Matches {
|
|
t.Errorf("expected case-insensitive match, but it didn't match")
|
|
}
|
|
}
|
|
|
|
func TestEvaluateRules_PriorityBoost(t *testing.T) {
|
|
service := &CollectionService{}
|
|
|
|
mediaItem := database.MediaItems{
|
|
Genre: pgtype.Text{String: "Science Fiction", Valid: true},
|
|
}
|
|
|
|
rules := []Rule{
|
|
{
|
|
ID: "rule-1",
|
|
Field: "genre",
|
|
Operator: "equals",
|
|
Value: "Science Fiction",
|
|
Priority: 1,
|
|
},
|
|
{
|
|
ID: "rule-2",
|
|
Field: "genre",
|
|
Operator: "equals",
|
|
Value: "Science Fiction",
|
|
Priority: 10,
|
|
},
|
|
}
|
|
|
|
evaluations := service.EvaluateRules(mediaItem, 0, rules)
|
|
|
|
if len(evaluations) != 2 {
|
|
t.Fatalf("expected 2 evaluations, got %d", len(evaluations))
|
|
}
|
|
|
|
// Higher priority (10) should give higher confidence than lower priority (1)
|
|
// Genre base confidence is 0.9
|
|
// Priority 1: 0.9 + 0.01 = 0.91
|
|
// Priority 10: 0.9 + 0.1 = 1.0 (capped)
|
|
if evaluations[0].Confidence != 1.0 {
|
|
t.Errorf("expected higher priority confidence to be 1.0, got %f", evaluations[0].Confidence)
|
|
}
|
|
|
|
if evaluations[1].Confidence != 0.91 {
|
|
t.Errorf("expected lower priority confidence to be 0.91, got %f", evaluations[1].Confidence)
|
|
}
|
|
|
|
// The higher priority rule should come first (sorted by priority descending)
|
|
if evaluations[0].RuleID != "rule-2" {
|
|
t.Errorf("expected higher priority rule to be first, got %s", evaluations[0].RuleID)
|
|
}
|
|
}
|