Skip to content

Commit 3577f31

Browse files
test: add generator template tests and fix zeitwerk warning
Added tests for the Rails generator template files to ensure they contain the expected content. Fixed a Zeitwerk warning by ignoring the generators directory in the loader. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 487d4e4 commit 3577f31

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ bundle exec rspec
133133

134134
# Run a specific test file
135135
bundle exec rspec spec/ruby_llm/chat_spec.rb
136+
137+
# Run generator template tests
138+
bundle exec rspec spec/lib/generators/ruby_llm/template_files_spec.rb
136139
```
137140

138141
### Recording VCR Cassettes

lib/ruby_llm.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
loader.ignore("#{__dir__}/tasks")
2121
loader.ignore("#{__dir__}/ruby_llm/railtie")
2222
loader.ignore("#{__dir__}/ruby_llm/active_record")
23+
loader.ignore("#{__dir__}/generators")
2324
loader.setup
2425

2526
# A delightful Ruby interface to modern AI language models.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'fileutils'
5+
6+
RSpec.describe "Generator template files", type: :generator do
7+
# Use the actual template directory
8+
let(:template_dir) { "/Users/kieranklaassen/rails/ruby_llm/lib/generators/ruby_llm/install/templates" }
9+
10+
describe "migration templates" do
11+
it "has expected migration template files" do
12+
expected_files = [
13+
"create_chats_migration.rb",
14+
"create_messages_migration.rb",
15+
"create_tool_calls_migration.rb"
16+
]
17+
18+
expected_files.each do |file|
19+
expect(File.exist?(File.join(template_dir, file))).to be(true), "Expected template file #{file} to exist"
20+
end
21+
end
22+
23+
it "has proper migration content" do
24+
chat_migration = File.read(File.join(template_dir, "create_chats_migration.rb"))
25+
expect(chat_migration).to include("create_table :chats")
26+
expect(chat_migration).to include("t.string :model_id")
27+
28+
message_migration = File.read(File.join(template_dir, "create_messages_migration.rb"))
29+
expect(message_migration).to include("create_table :messages")
30+
expect(message_migration).to include("t.references :chat")
31+
expect(message_migration).to include("t.string :role")
32+
expect(message_migration).to include("t.text :content")
33+
34+
tool_call_migration = File.read(File.join(template_dir, "create_tool_calls_migration.rb"))
35+
expect(tool_call_migration).to include("create_table :tool_calls")
36+
expect(tool_call_migration).to include("t.references :message")
37+
expect(tool_call_migration).to include("t.string :tool_call_id")
38+
expect(tool_call_migration).to include("t.string :name")
39+
expect(tool_call_migration).to include("t.jsonb :arguments")
40+
end
41+
end
42+
43+
describe "model templates" do
44+
it "has expected model template files" do
45+
expected_files = [
46+
"chat_model.rb",
47+
"message_model.rb",
48+
"tool_call_model.rb"
49+
]
50+
51+
expected_files.each do |file|
52+
expect(File.exist?(File.join(template_dir, file))).to be(true), "Expected template file #{file} to exist"
53+
end
54+
end
55+
56+
it "has proper acts_as declarations in model templates" do
57+
chat_content = File.read(File.join(template_dir, "chat_model.rb"))
58+
expect(chat_content).to include("acts_as_chat")
59+
60+
message_content = File.read(File.join(template_dir, "message_model.rb"))
61+
expect(message_content).to include("acts_as_message")
62+
63+
tool_call_content = File.read(File.join(template_dir, "tool_call_model.rb"))
64+
expect(tool_call_content).to include("acts_as_tool_call")
65+
end
66+
end
67+
68+
describe "initializer template" do
69+
it "has expected initializer template file" do
70+
expect(File.exist?(File.join(template_dir, "initializer.rb"))).to be(true)
71+
end
72+
73+
it "has proper configuration content" do
74+
initializer_content = File.read(File.join(template_dir, "initializer.rb"))
75+
expect(initializer_content).to include("RubyLLM.configure do |config|")
76+
expect(initializer_content).to include("config.openai_api_key")
77+
expect(initializer_content).to include("ENV[\"OPENAI_API_KEY\"]")
78+
expect(initializer_content).to include("config.anthropic_api_key")
79+
end
80+
end
81+
82+
describe "generator file structure" do
83+
it "has proper directory structure" do
84+
generator_file = "/Users/kieranklaassen/rails/ruby_llm/lib/generators/ruby_llm/install_generator.rb"
85+
expect(File.exist?(generator_file)).to be(true)
86+
87+
generator_content = File.read(generator_file)
88+
expect(generator_content).to include("class InstallGenerator < Rails::Generators::Base")
89+
expect(generator_content).to include("include Rails::Generators::Migration")
90+
expect(generator_content).to include("def create_migration_files")
91+
expect(generator_content).to include("def create_model_files")
92+
expect(generator_content).to include("def create_initializer")
93+
end
94+
end
95+
end

0 commit comments

Comments
 (0)