1+ # frozen_string_literal: true
2+
3+ require_relative '../spec_helper'
4+
5+ describe 'NlSearchModels' , :integration do
6+ # These tests require external API access and should not run on CI by default
7+ next unless ENV [ 'OPENAI_API_KEY' ]
8+
9+ let ( :client ) do
10+ Typesense ::Client . new (
11+ nodes : [ { host : 'localhost' , port : '8108' , protocol : 'http' } ] ,
12+ api_key : 'xyz' ,
13+ connection_timeout_seconds : 10
14+ )
15+ end
16+
17+
18+ def create_model_schema ( id_suffix = nil )
19+ model_id = id_suffix ? "test_openai_model_#{ id_suffix } " : "test_openai_model_#{ Time . now . to_i } _#{ rand ( 1000 ) } "
20+ {
21+ 'id' => model_id ,
22+ 'model_name' => 'openai/gpt-4.1' ,
23+ 'api_key' => ENV [ 'OPENAI_API_KEY' ] ,
24+ 'max_bytes' => 16000 ,
25+ 'temperature' => 0.0
26+ }
27+ end
28+
29+ def cleanup_model ( model_id )
30+ client . nl_search_models [ model_id ] . delete
31+ rescue Typesense ::Error ::ObjectNotFound
32+ # Model doesn't exist, that's fine
33+ end
34+
35+ before do
36+ WebMock . disable!
37+ end
38+
39+ after do
40+ WebMock . enable!
41+ end
42+
43+ it 'can create a nl search model' do
44+ model_schema = create_model_schema ( 'create_test' )
45+
46+ begin
47+ response = client . nl_search_models . create ( model_schema )
48+ expect ( response [ 'id' ] ) . to eq ( model_schema [ 'id' ] )
49+ expect ( response [ 'model_name' ] ) . to eq ( 'openai/gpt-4.1' )
50+ expect ( response [ 'max_bytes' ] ) . to eq ( 16000 )
51+ expect ( response [ 'temperature' ] ) . to eq ( 0.0 )
52+ ensure
53+ cleanup_model ( model_schema [ 'id' ] )
54+ end
55+ end
56+
57+ it 'can retrieve a specific nl search model' do
58+ model_schema = create_model_schema ( 'retrieve_test' )
59+
60+ begin
61+ client . nl_search_models . create ( model_schema )
62+ response = client . nl_search_models [ model_schema [ 'id' ] ] . retrieve
63+ expect ( response [ 'id' ] ) . to eq ( model_schema [ 'id' ] )
64+ expect ( response [ 'model_name' ] ) . to eq ( 'openai/gpt-4.1' )
65+ ensure
66+ cleanup_model ( model_schema [ 'id' ] )
67+ end
68+ end
69+
70+ it 'can retrieve all nl search models' do
71+ model_schema = create_model_schema ( 'list_test' )
72+
73+ begin
74+ client . nl_search_models . create ( model_schema )
75+
76+ response = client . nl_search_models . retrieve
77+ expect ( response ) . to be_an ( Array )
78+ expect ( response . length ) . to be >= 1
79+
80+ model_ids = response . map { |model | model [ 'id' ] }
81+ expect ( model_ids ) . to include ( model_schema [ 'id' ] )
82+ ensure
83+ cleanup_model ( model_schema [ 'id' ] )
84+ end
85+ end
86+
87+ it 'can update a nl search model' do
88+ model_schema = create_model_schema ( 'update_test' )
89+
90+ begin
91+ client . nl_search_models . create ( model_schema )
92+
93+ update_schema = {
94+ 'temperature' => 0.5 ,
95+ 'system_prompt' => 'Updated system prompt for electronics search'
96+ }
97+
98+ response = client . nl_search_models [ model_schema [ 'id' ] ] . update ( update_schema )
99+ expect ( response [ 'temperature' ] ) . to eq ( 0.5 )
100+ expect ( response [ 'system_prompt' ] ) . to eq ( 'Updated system prompt for electronics search' )
101+ ensure
102+ cleanup_model ( model_schema [ 'id' ] )
103+ end
104+ end
105+
106+ it 'can delete a nl search model' do
107+ model_schema = create_model_schema ( 'delete_test' )
108+
109+ client . nl_search_models . create ( model_schema )
110+
111+ response = client . nl_search_models [ model_schema [ 'id' ] ] . delete
112+ expect ( response [ 'id' ] ) . to eq ( model_schema [ 'id' ] )
113+
114+ expect {
115+ client . nl_search_models [ model_schema [ 'id' ] ] . retrieve
116+ } . to raise_error ( Typesense ::Error ::ObjectNotFound )
117+ end
118+ end
0 commit comments