package mestionora import ( "context" "errors" "testing" ) func TestMissingPolicyDoesNotReadOrInitialize(t *testing.T) { // A nil pool makes accidental construction, search or maintenance I/O fail. engine := NewSQLite(nil) ctx := context.Background() if hits, err := engine.SearchPathsInDomainFiltered(ctx, "d", "/", "needle", 20, nil); err != nil || len(hits) != 0 { t.Fatalf("missing page policy: %v %v", hits, err) } if hits, err := engine.SearchPathSuggestionsInDomain(ctx, "d", "/", "needle", 8, nil); err != nil || len(hits) != 0 { t.Fatalf("missing suggestion policy: %v %v", hits, err) } if hits, err := engine.SearchCityInDomain(ctx, "d", "/", "needle", 30, nil, SearchOptions{}); err != nil || len(hits) != 0 { t.Fatalf("missing city policy: %v %v", hits, err) } } func TestDeniedPagesDoNotLoadSummaryOrHeading(t *testing.T) { database := newSearchTestDB(t) _, err := database.Exec(` INSERT INTO objects(uuid,created_at) VALUES('o',1); INSERT INTO paths(domain_uuid,path,object_uuid) VALUES('d','/needle','o'); INSERT INTO revisions(uuid,object_uuid,filename,body,content_type,document_hash,created_at_ms,created_at) VALUES('r','o','needle.html','

PRIVATE_HEADING

PRIVATE_SUMMARY

','text/html','hash',1000,1); DROP TABLE revisions; `) if err != nil { t.Fatal(err) } engine := NewSQLite(database) deny := func(string) bool { return false } ctx := context.Background() // Candidates still exist in the independent index. Any attempt to hydrate // denied summaries/headings now fails because their source table is absent. if hits, err := engine.SearchPathsInDomainFiltered(ctx, "d", "/", "needle", 20, deny); err != nil || len(hits) != 0 { t.Fatalf("denied summary loaded: %v %v", hits, err) } if hits, err := engine.SearchPathSuggestionsInDomain(ctx, "d", "/", "needle", 8, deny); err != nil || len(hits) != 0 { t.Fatalf("denied heading loaded: %v %v", hits, err) } allow := func(string) bool { return true } if _, err := engine.SearchPathsInDomainFiltered(ctx, "d", "/", "needle", 20, allow); err == nil { t.Fatal("fixture did not exercise summary loading") } if _, err := engine.SearchPathSuggestionsInDomain(ctx, "d", "/", "needle", 8, allow); err == nil { t.Fatal("fixture did not exercise heading loading") } } func TestCancellationDuringPolicyNeverReturnsPartialSuccess(t *testing.T) { database := newSearchTestDB(t) if _, err := database.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid) VALUES('d','/needle','o')`); err != nil { t.Fatal(err) } for _, excerpts := range []bool{false, true} { ctx, cancel := context.WithCancel(context.Background()) allowed := func(string) bool { cancel(); return true } // Filter-only search has no later FTS statement to notice cancellation. hits, err := NewSQLite(database).searchPathsInDomainWithOptions(ctx, "d", "/", "", 20, allowed, SearchOptions{FileType: "other"}, excerpts) cancel() if !errors.Is(err, context.Canceled) || len(hits) != 0 { t.Fatalf("partial success after cancellation: %v %v", hits, err) } } } func TestFTSExpressionTreatsOperatorsAsData(t *testing.T) { for input, want := range map[string]string{ "": "", "alpha beta": `"alpha"* AND "beta"*`, `a"b OR`: `"a""b"* AND "OR"*`, `body:NEAR(x) -secret`: `"body:NEAR(x)"* AND "-secret"*`, "日本語": `"日本語"*`, } { if got := ftsPrefixExpression(input); got != want { t.Fatalf("%q: got %q want %q", input, got, want) } } } func TestSearchDoesNotMaintainIndexOnReadOnlyConnection(t *testing.T) { database := newSearchTestDB(t) database.SetMaxOpenConns(1) if _, err := database.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid) VALUES('d','/needle','o'); PRAGMA query_only=ON`); err != nil { t.Fatal(err) } engine := NewSQLite(database) results, err := engine.SearchPathsInDomainFiltered(context.Background(), "d", "/", "needle", 20, func(string) bool { return true }) if err != nil || len(results) != 1 { t.Fatalf("read-only search: %v %v", results, err) } // Schema maintenance is deliberately explicit, not performed by NewSQLite // or a query. An accidental write in those paths would fail the test above. if err := engine.EnsureSearchIndexSchema(); err == nil { t.Fatal("read-only fixture unexpectedly accepted maintenance") } }