|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +class Exa::CLI::Formatters::WebsetSearchFormatterTest < Minitest::Test |
| 6 | + def test_json_format_returns_json_string |
| 7 | + search = create_webset_search |
| 8 | + output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, "json") |
| 9 | + |
| 10 | + # Verify it's valid JSON |
| 11 | + parsed = JSON.parse(output) |
| 12 | + assert_equal "ws_search_123", parsed["id"] |
| 13 | + assert_equal "running", parsed["status"] |
| 14 | + assert_equal "AI startups", parsed["query"] |
| 15 | + end |
| 16 | + |
| 17 | + def test_pretty_format_shows_search_metadata |
| 18 | + search = create_webset_search |
| 19 | + output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, "pretty") |
| 20 | + |
| 21 | + # Verify it includes expected metadata |
| 22 | + assert_includes output, "Search ID: ws_search_123" |
| 23 | + assert_includes output, "Status: running" |
| 24 | + assert_includes output, "Query: AI startups" |
| 25 | + assert_includes output, "Behavior: override" |
| 26 | + assert_includes output, "Created: 2024-01-01T12:00:00Z" |
| 27 | + end |
| 28 | + |
| 29 | + def test_pretty_format_excludes_results_property |
| 30 | + search = create_webset_search |
| 31 | + output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, "pretty") |
| 32 | + |
| 33 | + # Verify .results is not accessed |
| 34 | + refute_includes output, "results" |
| 35 | + end |
| 36 | + |
| 37 | + def test_text_format_shows_key_fields |
| 38 | + search = create_webset_search |
| 39 | + output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, "text") |
| 40 | + |
| 41 | + # Verify it includes key fields |
| 42 | + assert_includes output, "ID: ws_search_123" |
| 43 | + assert_includes output, "Status: running" |
| 44 | + assert_includes output, "Query: AI startups" |
| 45 | + assert_includes output, "Behavior: override" |
| 46 | + end |
| 47 | + |
| 48 | + def test_default_format_is_json |
| 49 | + search = create_webset_search |
| 50 | + output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, nil) |
| 51 | + |
| 52 | + # Should default to JSON |
| 53 | + parsed = JSON.parse(output) |
| 54 | + assert_equal "ws_search_123", parsed["id"] |
| 55 | + end |
| 56 | + |
| 57 | + def test_toon_format_returns_toon_string |
| 58 | + search = create_webset_search |
| 59 | + output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, "toon") |
| 60 | + |
| 61 | + assert_instance_of String, output |
| 62 | + assert_includes output, "ws_search_123" |
| 63 | + assert_includes output, "AI startups" |
| 64 | + |
| 65 | + # TOON should be more compact than JSON |
| 66 | + json_output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, "json") |
| 67 | + assert output.length < json_output.length |
| 68 | + end |
| 69 | + |
| 70 | + def test_pretty_format_with_canceled_search |
| 71 | + search = create_canceled_webset_search |
| 72 | + output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, "pretty") |
| 73 | + |
| 74 | + assert_includes output, "Status: canceled" |
| 75 | + assert_includes output, "Canceled: 2024-01-01T13:00:00Z" |
| 76 | + assert_includes output, "Cancel Reason: User requested cancellation" |
| 77 | + end |
| 78 | + |
| 79 | + def test_pretty_format_with_custom_entity |
| 80 | + search = create_webset_search_with_entity |
| 81 | + output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, "pretty") |
| 82 | + |
| 83 | + assert_includes output, "Entity Type: custom" |
| 84 | + end |
| 85 | + |
| 86 | + def test_pretty_format_handles_nil_progress |
| 87 | + search = create_webset_search_without_progress |
| 88 | + # Ensure progress is nil |
| 89 | + assert_nil search.progress |
| 90 | + |
| 91 | + output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, "pretty") |
| 92 | + # Should not fail and should have valid output |
| 93 | + assert_includes output, "Query:" |
| 94 | + end |
| 95 | + |
| 96 | + private |
| 97 | + |
| 98 | + def create_webset_search |
| 99 | + Exa::Resources::WebsetSearch.new( |
| 100 | + id: "ws_search_123", |
| 101 | + object: "webset.search", |
| 102 | + status: "running", |
| 103 | + webset_id: "ws_456", |
| 104 | + query: "AI startups", |
| 105 | + entity: { "type" => "company" }, |
| 106 | + criteria: nil, |
| 107 | + count: 50, |
| 108 | + behavior: "override", |
| 109 | + exclude: nil, |
| 110 | + scope: nil, |
| 111 | + progress: 25, |
| 112 | + recall: false, |
| 113 | + metadata: nil, |
| 114 | + canceled_at: nil, |
| 115 | + canceled_reason: nil, |
| 116 | + created_at: "2024-01-01T12:00:00Z", |
| 117 | + updated_at: "2024-01-01T12:30:00Z" |
| 118 | + ) |
| 119 | + end |
| 120 | + |
| 121 | + def create_canceled_webset_search |
| 122 | + Exa::Resources::WebsetSearch.new( |
| 123 | + id: "ws_search_789", |
| 124 | + object: "webset.search", |
| 125 | + status: "canceled", |
| 126 | + webset_id: "ws_456", |
| 127 | + query: "tech companies", |
| 128 | + entity: nil, |
| 129 | + criteria: nil, |
| 130 | + count: nil, |
| 131 | + behavior: "override", |
| 132 | + exclude: nil, |
| 133 | + scope: nil, |
| 134 | + progress: 50, |
| 135 | + recall: false, |
| 136 | + metadata: nil, |
| 137 | + canceled_at: "2024-01-01T13:00:00Z", |
| 138 | + canceled_reason: "User requested cancellation", |
| 139 | + created_at: "2024-01-01T12:00:00Z", |
| 140 | + updated_at: "2024-01-01T13:00:00Z" |
| 141 | + ) |
| 142 | + end |
| 143 | + |
| 144 | + def create_webset_search_with_entity |
| 145 | + Exa::Resources::WebsetSearch.new( |
| 146 | + id: "ws_search_custom", |
| 147 | + object: "webset.search", |
| 148 | + status: "completed", |
| 149 | + webset_id: "ws_456", |
| 150 | + query: "vintage cars", |
| 151 | + entity: { "type" => "custom", "description" => "vintage cars" }, |
| 152 | + criteria: nil, |
| 153 | + count: 20, |
| 154 | + behavior: "append", |
| 155 | + exclude: nil, |
| 156 | + scope: nil, |
| 157 | + progress: 100, |
| 158 | + recall: false, |
| 159 | + metadata: nil, |
| 160 | + canceled_at: nil, |
| 161 | + canceled_reason: nil, |
| 162 | + created_at: "2024-01-01T12:00:00Z", |
| 163 | + updated_at: "2024-01-01T12:45:00Z" |
| 164 | + ) |
| 165 | + end |
| 166 | + |
| 167 | + def create_webset_search_without_progress |
| 168 | + Exa::Resources::WebsetSearch.new( |
| 169 | + id: "ws_search_no_progress", |
| 170 | + object: "webset.search", |
| 171 | + status: "created", |
| 172 | + webset_id: "ws_456", |
| 173 | + query: "test query", |
| 174 | + entity: nil, |
| 175 | + criteria: nil, |
| 176 | + count: nil, |
| 177 | + behavior: "override", |
| 178 | + exclude: nil, |
| 179 | + scope: nil, |
| 180 | + progress: nil, |
| 181 | + recall: false, |
| 182 | + metadata: nil, |
| 183 | + canceled_at: nil, |
| 184 | + canceled_reason: nil, |
| 185 | + created_at: "2024-01-01T12:00:00Z", |
| 186 | + updated_at: "2024-01-01T12:00:00Z" |
| 187 | + ) |
| 188 | + end |
| 189 | +end |
0 commit comments