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).
This commit is contained in:
1 parent
ab98015190
commit
a86bdac87e
27 files changed
+239
-1093
No files matched your search
@@ -2,12 +2,9 @@ package services
|
||||
|
||||
import (
|
||||
"bookhoard/internal/database"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEvaluateRules_Equality(t *testing.T) {
|
||||
@@ -280,44 +277,7 @@ func TestEvaluateRules_PriorityBoost(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesToFilterTree(t *testing.T) {
|
||||
t.Run("episode_rules_map_mechanically", func(t *testing.T) {
|
||||
rules := []Rule{
|
||||
{ID: "1", Field: "feed", Operator: "in", Value: "feed-a, feed-b"},
|
||||
{ID: "2", Field: "played", Operator: "equals", Value: "false"},
|
||||
{ID: "3", Field: "downloaded", Operator: "equals", Value: "true"},
|
||||
{ID: "4", Field: "published_within", Operator: "greater_than", Value: "7"},
|
||||
}
|
||||
tree, err := RulesToFilterTree(rules, RuleMatchAll)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, RuleMatchAll, tree.Mode, "the collection's match mode becomes the tree mode")
|
||||
assert.Equal(t, ScopeEpisodes, tree.Scope, "episode rules force the episodes scope")
|
||||
require.Len(t, tree.Children, 4)
|
||||
assert.Equal(t, FilterChild{Field: "feed", Op: OpIn, Values: []string{"feed-a", "feed-b"}}, tree.Children[0])
|
||||
assert.Equal(t, FilterChild{Field: "played", Op: OpIsFalse}, tree.Children[1])
|
||||
assert.Equal(t, FilterChild{Field: "downloaded", Op: OpIsTrue}, tree.Children[2])
|
||||
assert.Equal(t, FilterChild{Field: "published_date", Op: OpWithinDays, Values: []string{"7"}}, tree.Children[3])
|
||||
// The converted tree parses under the strict engine.
|
||||
b, _ := json.Marshal(tree)
|
||||
_, err = ParseFilterTree(string(b))
|
||||
require.NoError(t, err, "converted tree must satisfy the engine grammar")
|
||||
})
|
||||
|
||||
t.Run("book_rules_map_and_stay_books_scope", func(t *testing.T) {
|
||||
rules := []Rule{
|
||||
{Field: "genre", Operator: "equals", Value: "fantasy"},
|
||||
{Field: "author", Operator: "contains", Value: "Voss"},
|
||||
{Field: "copyright_year", Operator: "greater_than", Value: "1990"},
|
||||
{Field: "tags", Operator: "starts_with", Value: "dyst"},
|
||||
}
|
||||
tree, err := RulesToFilterTree(rules, RuleMatchAny)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "", tree.Scope, "no episode rules → the books scope (legacy default)")
|
||||
assert.Equal(t, RuleMatchAny, tree.Mode)
|
||||
assert.Equal(t, FilterChild{Field: "genre", Op: OpIs, Values: []string{"fantasy"}}, tree.Children[0])
|
||||
assert.Equal(t, FilterChild{Field: "copyright_year", Op: OpAtLeast, Values: []string{"1990"}}, tree.Children[2])
|
||||
// Documented fallback: rules ops without an engine equivalent
|
||||
// (starts_with/ends_with) become contains.
|
||||
assert.Equal(t, OpContains, tree.Children[3].Op)
|
||||
})
|
||||
}
|
||||
// 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.)
|
||||
Reference in new issue
Block a user