22
33require "helper"
44require "json"
5- require "yaml"
65require_relative "../lib/search_index"
76
87describe SearchIndex do
98 before do
109 setup_tempdir
11- @bundle_dir = File . join ( TEMP_DIR , "pagefind" )
10+ @bundle_dir = File . join ( TEMP_DIR , "_site" , " pagefind")
1211 FileUtils . mkdir_p ( @bundle_dir )
1312 @entry_path = File . join ( @bundle_dir , "pagefind-entry.json" )
14- @config_path = File . join ( TEMP_DIR , "_config.yml" )
15- write_config ( fallback : "en" , unsupported : [ "bg" ] )
13+ @search_config = { "fallback" => "en" , "unsupported" => [ "bg" ] }
1614 end
1715
1816 after do
1917 teardown_tempdir
2018 end
2119
22- def write_config ( search )
23- File . write ( @config_path , YAML . dump ( { "search" => search . transform_keys ( &:to_s ) } ) )
24- end
25-
2620 def write_entry ( languages )
2721 File . write ( @entry_path , JSON . generate ( { "version" => "1.5.2" , "languages" => languages } ) )
2822 end
@@ -31,29 +25,48 @@ def read_entry
3125 JSON . parse ( File . read ( @entry_path ) )
3226 end
3327
34- def search_index
35- SearchIndex . new ( bundle_dir : @bundle_dir , config_path : @config_path )
28+ def search_index ( pagefind : SearchIndex ::PAGEFIND )
29+ SearchIndex . new ( bundle_dir : @bundle_dir , search_config : @search_config , pagefind : pagefind )
30+ end
31+
32+ describe "#build" do
33+ it "skips indexing when Pagefind is not installed" do
34+ index = search_index ( pagefind : File . join ( TEMP_DIR , "no-such-pagefind" ) )
35+
36+ result = nil
37+ _ , stderr = capture_io { result = index . build }
38+
39+ _ ( result ) . must_equal false
40+ _ ( stderr ) . must_match ( /npm install/ )
41+ end
3642 end
3743
3844 describe "#fallbacks" do
3945 it "maps every unsupported language to the fallback language" do
40- write_config ( fallback : "en" , unsupported : %w[ bg xx ] )
46+ @search_config = { " fallback" => "en" , " unsupported" => %w[ bg xx ] }
4147
4248 _ ( search_index . fallbacks ) . must_equal ( { "bg" => "en" , "xx" => "en" } )
4349 end
4450
4551 it "is empty when no language is unsupported" do
46- write_config ( fallback : "en" , unsupported : [ ] )
52+ @search_config = { " fallback" => "en" , " unsupported" => [ ] }
4753
4854 _ ( search_index . fallbacks ) . must_be_empty
4955 end
56+
57+ it "raises when no fallback language is configured" do
58+ @search_config = { "unsupported" => [ "bg" ] }
59+
60+ error = _ ( -> { search_index . fallbacks } ) . must_raise SearchIndex ::Error
61+ _ ( error . message ) . must_match ( /search.fallback is not configured/ )
62+ end
5063 end
5164
5265 describe "#apply_fallbacks" do
5366 it "points an unsupported language at the index it falls back to" do
5467 write_entry ( {
55- "en" => { "hash" => "en_abc" , "wasm" => "en" , "page_count" => 563 } ,
56- "ja" => { "hash" => "ja_def" , "wasm" => nil , "page_count" => 541 } ,
68+ "en" => { "hash" => "en_abc" , "wasm" => "en" , "page_count" => 560 } ,
69+ "ja" => { "hash" => "ja_def" , "wasm" => nil , "page_count" => 536 } ,
5770 } )
5871
5972 search_index . apply_fallbacks
@@ -64,8 +77,8 @@ def search_index
6477
6578 it "does not leave the fallback to the largest index" do
6679 write_entry ( {
67- "en" => { "hash" => "en_abc" , "wasm" => "en" , "page_count" => 563 } ,
68- "uk" => { "hash" => "uk_def" , "wasm" => nil , "page_count" => 564 } ,
80+ "en" => { "hash" => "en_abc" , "wasm" => "en" , "page_count" => 560 } ,
81+ "uk" => { "hash" => "uk_def" , "wasm" => nil , "page_count" => 900 } ,
6982 } )
7083
7184 search_index . apply_fallbacks
@@ -75,8 +88,8 @@ def search_index
7588
7689 it "leaves the other languages untouched" do
7790 write_entry ( {
78- "en" => { "hash" => "en_abc" , "wasm" => "en" , "page_count" => 563 } ,
79- "zh-cn" => { "hash" => "zh-cn_ghi" , "wasm" => nil , "page_count" => 281 } ,
91+ "en" => { "hash" => "en_abc" , "wasm" => "en" , "page_count" => 560 } ,
92+ "zh-cn" => { "hash" => "zh-cn_ghi" , "wasm" => nil , "page_count" => 276 } ,
8093 } )
8194
8295 search_index . apply_fallbacks
@@ -88,7 +101,7 @@ def search_index
88101
89102 it "warns when a language meant to be skipped was indexed anyway" do
90103 write_entry ( {
91- "en" => { "hash" => "en_abc" , "wasm" => "en" , "page_count" => 563 } ,
104+ "en" => { "hash" => "en_abc" , "wasm" => "en" , "page_count" => 560 } ,
92105 "bg" => { "hash" => "bg_xyz" , "wasm" => nil , "page_count" => 60 } ,
93106 } )
94107
@@ -99,7 +112,7 @@ def search_index
99112 end
100113
101114 it "raises when the index it falls back to is missing" do
102- write_entry ( { "ja" => { "hash" => "ja_def" , "wasm" => nil , "page_count" => 541 } } )
115+ write_entry ( { "ja" => { "hash" => "ja_def" , "wasm" => nil , "page_count" => 536 } } )
103116
104117 error = _ ( -> { search_index . apply_fallbacks } ) . must_raise SearchIndex ::Error
105118 _ ( error . message ) . must_match ( /missing index "en"/ )
@@ -112,17 +125,9 @@ def search_index
112125 _ ( error . message ) . must_match ( /did the site build/ )
113126 end
114127
115- it "raises when the bundle entry does not exist" do
116- error = _ ( -> { search_index . apply_fallbacks } ) . must_raise SearchIndex ::Error
117- _ ( error . message ) . must_match ( /npm run build-search/ )
118- end
119-
120- it "raises when the config has no search section" do
121- write_entry ( { "en" => { "hash" => "en_abc" , "wasm" => "en" , "page_count" => 563 } } )
122- File . write ( @config_path , YAML . dump ( { "url" => "https://www.ruby-lang.org" } ) )
123-
128+ it "raises when Pagefind wrote no index" do
124129 error = _ ( -> { search_index . apply_fallbacks } ) . must_raise SearchIndex ::Error
125- _ ( error . message ) . must_match ( /No search section / )
130+ _ ( error . message ) . must_match ( /wrote no index / )
126131 end
127132 end
128133end
0 commit comments