|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe Ragnar::CLI::Umap do |
| 6 | + let(:config) do |
| 7 | + instance_double( |
| 8 | + Ragnar::Config, |
| 9 | + database_path: "./ragnar_database", |
| 10 | + models_dir: "/tmp/ragnar_test_models" |
| 11 | + ) |
| 12 | + end |
| 13 | + let(:model_path) { File.join(config.models_dir, "umap_model.bin") } |
| 14 | + |
| 15 | + before do |
| 16 | + allow(Ragnar::Config).to receive(:instance).and_return(config) |
| 17 | + end |
| 18 | + |
| 19 | + describe "train" do |
| 20 | + let(:processor) { instance_double(Ragnar::UmapProcessor, model_path: model_path) } |
| 21 | + |
| 22 | + before do |
| 23 | + allow(Ragnar::UmapProcessor).to receive(:new).and_return(processor) |
| 24 | + end |
| 25 | + |
| 26 | + it "trains UMAP model with default options" do |
| 27 | + stats = { embeddings_count: 100, original_dims: 768, reduced_dims: 50 } |
| 28 | + allow(processor).to receive(:train).and_return(stats) |
| 29 | + |
| 30 | + expect { |
| 31 | + Ragnar::CLI.start(["umap", "train"]) |
| 32 | + }.not_to raise_error |
| 33 | + end |
| 34 | + |
| 35 | + it "passes custom options to the processor" do |
| 36 | + stats = { embeddings_count: 100, original_dims: 768, reduced_dims: 10 } |
| 37 | + expect(Ragnar::UmapProcessor).to receive(:new).with( |
| 38 | + db_path: "./ragnar_database", |
| 39 | + model_path: model_path |
| 40 | + ).and_return(processor) |
| 41 | + |
| 42 | + expect(processor).to receive(:train).with( |
| 43 | + n_components: 10, |
| 44 | + n_neighbors: 5, |
| 45 | + min_dist: 0.1 |
| 46 | + ).and_return(stats) |
| 47 | + |
| 48 | + Ragnar::CLI.start(["umap", "train", "--n-components", "10", "--n-neighbors", "5"]) |
| 49 | + end |
| 50 | + |
| 51 | + it "uses custom model path when provided" do |
| 52 | + custom_path = "/custom/path/model.bin" |
| 53 | + stats = { embeddings_count: 100, original_dims: 768, reduced_dims: 50 } |
| 54 | + |
| 55 | + expect(Ragnar::UmapProcessor).to receive(:new).with( |
| 56 | + db_path: "./ragnar_database", |
| 57 | + model_path: custom_path |
| 58 | + ).and_return(processor) |
| 59 | + |
| 60 | + allow(processor).to receive(:train).and_return(stats) |
| 61 | + |
| 62 | + Ragnar::CLI.start(["umap", "train", "--model-path", custom_path]) |
| 63 | + end |
| 64 | + |
| 65 | + it "uses custom db path when provided" do |
| 66 | + custom_db = "/custom/db/path" |
| 67 | + stats = { embeddings_count: 100, original_dims: 768, reduced_dims: 50 } |
| 68 | + |
| 69 | + expect(Ragnar::UmapProcessor).to receive(:new).with( |
| 70 | + db_path: custom_db, |
| 71 | + model_path: model_path |
| 72 | + ).and_return(processor) |
| 73 | + |
| 74 | + allow(processor).to receive(:train).and_return(stats) |
| 75 | + |
| 76 | + Ragnar::CLI.start(["umap", "train", "--db-path", custom_db]) |
| 77 | + end |
| 78 | + |
| 79 | + it "handles training errors gracefully" do |
| 80 | + allow(processor).to receive(:train).and_raise("No embeddings found") |
| 81 | + |
| 82 | + expect { |
| 83 | + Ragnar::CLI.start(["umap", "train"]) |
| 84 | + }.to raise_error(SystemExit) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe "apply" do |
| 89 | + let(:processor) { instance_double(Ragnar::UmapProcessor) } |
| 90 | + |
| 91 | + before do |
| 92 | + allow(Ragnar::UmapProcessor).to receive(:new).and_return(processor) |
| 93 | + allow(File).to receive(:exist?).and_call_original |
| 94 | + end |
| 95 | + |
| 96 | + it "applies UMAP model when model exists" do |
| 97 | + allow(File).to receive(:exist?).with(model_path).and_return(true) |
| 98 | + stats = { processed: 50, skipped: 0, errors: 0 } |
| 99 | + allow(processor).to receive(:apply).and_return(stats) |
| 100 | + |
| 101 | + expect { |
| 102 | + Ragnar::CLI.start(["umap", "apply"]) |
| 103 | + }.not_to raise_error |
| 104 | + end |
| 105 | + |
| 106 | + it "passes batch size option to the processor" do |
| 107 | + allow(File).to receive(:exist?).with(model_path).and_return(true) |
| 108 | + stats = { processed: 50, skipped: 0, errors: 0 } |
| 109 | + |
| 110 | + expect(processor).to receive(:apply).with(batch_size: 200).and_return(stats) |
| 111 | + |
| 112 | + Ragnar::CLI.start(["umap", "apply", "--batch-size", "200"]) |
| 113 | + end |
| 114 | + |
| 115 | + it "exits with error when model does not exist" do |
| 116 | + allow(File).to receive(:exist?).with(model_path).and_return(false) |
| 117 | + |
| 118 | + expect { |
| 119 | + Ragnar::CLI.start(["umap", "apply"]) |
| 120 | + }.to raise_error(SystemExit) |
| 121 | + end |
| 122 | + |
| 123 | + it "handles apply errors gracefully" do |
| 124 | + allow(File).to receive(:exist?).with(model_path).and_return(true) |
| 125 | + allow(processor).to receive(:apply).and_raise("Database error") |
| 126 | + |
| 127 | + expect { |
| 128 | + Ragnar::CLI.start(["umap", "apply"]) |
| 129 | + }.to raise_error(SystemExit) |
| 130 | + end |
| 131 | + end |
| 132 | +end |
0 commit comments