Files
bookhoard/internal/services/collection_service_test.go
T
John O'Keefe a86bdac87e refactor(collections): retire the legacy rules surface — tree-only storage
The pre-tree rules machinery is deleted: no row ever carried a rules
array or a non-default match mode (verified in the dev DB), the web
never read the envelope keys, and the Android app consumes none of it.
Dead code gets no documentation.

Removed:
- PUT /api/collections/:id/match-mode — endpoint, route, and the
  UpdateCollectionMatchMode query. The tree's root mode owns any|all.
- auto_assign_rules / rule_match_mode from every envelope
  (create/list/detail/update), from the create/update request shapes,
  and from the schema: both columns DROPPED (schema.sql carries
  idempotent DROP COLUMN IF EXISTS heals for existing DBs; sqlc
  regenerated).
- The rules CRUD service layer (AddRule/SetRuleEnabled/RemoveRule +
  validateRule + UpdateCollectionRulesJSON), ParseRules /
  EnabledRules / IsSmartCollection, RulesToFilterTree (+ the SSR
  preload branch), the legacy branch of EvaluateSmartMembership, and
  SmartEvalOptions.MatchMode. IsCollectionSmart = tree presence;
  create-path isSmart = collection_type=smart OR a filter_tree.

Kept (live consumers): POST /api/collections/preview still evaluates
rules payloads in memory (the custom-section builder's preview), and
include_auto stays on the list (the Android app sends it).

The custom-section builder's SAVE was already broken — its
stringified auto_assign_rules payload failed request binding (probed
live: 400 invalid request). Create now rejects that payload loudly
with a 400 naming the retirement, instead of silently creating a
hollow static section; custom-section-builder.md carries the flag.
The builder's tree migration remains the flagged follow-up.

Test-side (asymmetry rule — deleted behavior, deleted tests):
TestPodcastSmartCollections rewritten onto trees (mode lives in the
tree root), match-mode subtests and tree_wins_over_legacy_rules
deleted, TestRulesToFilterTree deleted, contract-pin envelope keys
retracted per the contract_pins 2026-10-31 retirement entry. Mirrors
hand-synced: openapi.yaml, bruno create/update, docs/api/*.md,
docs/schema.go, api.d.ts. Full container suite green (155s).
2026-10-31 19:06:48 -04:00

284 lines
6.3 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)
}
}
// TestRulesToFilterTree retired 2026-10-31 with the function it tested:
// rules-array storage is gone, the filter tree is the only smart
// membership. (Tests of deleted behavior are deleted — the asymmetry
// rule.)